public void startOrderProcess (String json) {
OrderRecord orderRecord = deserialize (json);
if (orderRecord == null) return;
Integer maxDelayTimer = appSettings. load (OrderProcessingSettings.class).getMaxDelayTimer ();
int timerNew = random. nextInt (1, maxDelayTimer);
int timerVerified = random. nextInt (1, maxDelayTimer);
String timerNewDuration = Iso8601Converter. convertSecondsToDuration (timerNew);
String timerVerifiedDuration = Iso8601Converter. convertSecondsToDuration (timerVerified);
String businessKey = generateBusinessKey ();
Item item = findItemByName (orderRecord.item ());
Map<String, Object> map = new HashMap<>();
map.put ("orderNumber", businessKey);
map.put ("customer", orderRecord. customer ());
map.put ("address", orderRecord. address ());
map.put ("quantity", orderRecord. quantity ());
map.put ("item", item);
map.put ("timerNew", timerNewDuration);
map.put ("timerVerified", timerVerifiedDuration);
systemAuthenticator.begin ("admin");
try {
runtimeService.startProcessInstanceByMessage (START_MESSAGE_NAME,
businessKey,
map);
uiEventPublisher.publishEvent (new IncomingOrderEvent (this, json));
} catch (Exception e) {
throw new RuntimeException (e);
} finally {
systemAuthenticator.end ();
}
}
private String generateBusinessKey () {
Integer number = numeratorService. getNext ("order");
String formattedNumber = String. format ("%05d", number);
return "ORD-" + formattedNumber;
}
public OrderRecord deserialize (String json) {
ObjectMapper objectMapper = new ObjectMapper ();
try {
return objectMapper. readValue (json, OrderRecord. class);
} catch (JsonProcessingException e) {
log.error ("Bad message: {}", json);
return null;
}
}
private Item findItemByName (String name) {
systemAuthenticator.begin ("admin");
try {
return dataManager. load (Item.class)
.query ("select e from ord_Item e where e.name = :name")
.parameter ("name", name)
.one ();
} catch (Exception ignored) {
} finally {
systemAuthenticator.end ();
}
return null;
}