use of com.qcadoo.mes.states.StateChangeContext in project mes by qcadoo.
the class OrderCreationService method createOrder.
public OrderCreationResponse createOrder(OrderCreationRequest orderCreationRequest) {
Entity parameter = parameterService.getParameter();
boolean createOperationalTasks = !orderCreationRequest.getTechnologyOperations().isEmpty();
if (Objects.isNull(orderCreationRequest.getTechnologyId())) {
if (!isParameterSet(parameter, createOperationalTasks)) {
return new OrderCreationResponse(translationService.translate("basic.dashboard.orderDefinitionWizard.createOrder.parameterNotSet", LocaleContextHolder.getLocale()));
}
}
Entity product = getProduct(orderCreationRequest.getProductId());
Either<String, Entity> isTechnology = getOrCreateTechnology(orderCreationRequest);
if (isTechnology.isLeft()) {
return new OrderCreationResponse(isTechnology.getLeft());
}
Entity technology = isTechnology.getRight();
Entity productionLine = getProductionLine(orderCreationRequest.getProductionLineId(), technology);
OrderCreationResponse response = new OrderCreationResponse(OrderCreationResponse.StatusCode.OK);
Entity order = dataDefinitionService.get(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER).create();
order.setField(OrderFields.NUMBER, numberGeneratorService.generateNumber(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER, 6));
order.setField(OrderFields.NAME, orderService.makeDefaultName(product, technology, LocaleContextHolder.getLocale()));
order.setField(OrderFields.PRODUCT, product);
order.setField(OrderFields.TECHNOLOGY_PROTOTYPE, technology);
order.setField(OrderFields.PRODUCTION_LINE, productionLine);
order.setField(OrderFields.DIVISION, orderService.getDivision(technology));
order.setField(OrderFields.DATE_FROM, orderCreationRequest.getStartDate());
order.setField(OrderFields.DATE_TO, orderCreationRequest.getFinishDate());
order.setField(OrderFields.EXTERNAL_SYNCHRONIZED, true);
order.setField(IS_SUBCONTRACTED, false);
order.setField(OrderFields.STATE, OrderStateStringValues.PENDING);
order.setField(OrderFields.PLANNED_QUANTITY, orderCreationRequest.getQuantity());
order.setField(OrderFields.DESCRIPTION, buildDescription(parameter, orderCreationRequest.getDescription(), technology, product));
order.setField(IGNORE_MISSING_COMPONENTS, parameter.getBooleanField(IGNORE_MISSING_COMPONENTS));
order.setField("typeOfProductionRecording", orderCreationRequest.getTypeOfProductionRecording());
order = order.getDataDefinition().save(order);
if (order.isValid()) {
if (!order.getGlobalMessages().isEmpty()) {
Optional<GlobalMessage> message = order.getGlobalMessages().stream().filter(gm -> gm.getMessage().equals("orders.order.message.plannedQuantityChanged")).findFirst();
message.ifPresent(m -> {
response.setAdditionalInformation(translationService.translate(m.getMessage(), LocaleContextHolder.getLocale(), m.getVars()[0], m.getVars()[1]));
});
}
final StateChangeContext orderStateChangeContext = stateChangeContextBuilder.build(orderStateChangeAspect.getChangeEntityDescriber(), order, OrderState.ACCEPTED.getStringValue());
orderStateChangeAspect.changeState(orderStateChangeContext);
order = order.getDataDefinition().get(order.getId());
if (!order.getStringField(OrderFields.STATE).equals(OrderStateStringValues.ACCEPTED)) {
return new OrderCreationResponse(translationService.translate("basic.dashboard.orderDefinitionWizard.createOrder.acceptError", LocaleContextHolder.getLocale(), order.getStringField(OrderFields.NUMBER)));
}
} else {
return new OrderCreationResponse(translationService.translate("basic.dashboard.orderDefinitionWizard.createOrder.validationError", LocaleContextHolder.getLocale()));
}
if (createOperationalTasks) {
createOperationalTasks(order, orderCreationRequest);
modifyProductionCountingQuantityForEach(order, orderCreationRequest.getTechnologyOperations());
} else {
modifyProductionCountingQuantity(order, orderCreationRequest.getMaterials());
}
response.setMessage(translationService.translate("orders.orderCreationService.created", LocaleContextHolder.getLocale(), order.getStringField(OrderFields.NUMBER)));
if (createOperationalTasks) {
response.setOperationalTasks(dashboardKanbanDataProvider.getOperationalTasksPendingForOrder(order.getId()));
} else {
response.setOrder(dashboardKanbanDataProvider.getOrder(order.getId()));
}
return response;
}
use of com.qcadoo.mes.states.StateChangeContext in project mes by qcadoo.
the class OrderCreationService method createTechnologyForEachOperation.
private Either<String, Entity> createTechnologyForEachOperation(OrderCreationRequest orderCreationRequest) {
Entity product = getProduct(orderCreationRequest.getProductId());
Entity parameter = parameterService.getParameter();
Entity dashboardComponentsLocation = parameter.getBelongsToField(OrderCreationService.L_DASHBOARD_COMPONENTS_LOCATION);
Entity dashboardProductsInputLocation = parameter.getBelongsToField(OrderCreationService.L_DASHBOARD_PRODUCTS_INPUT_LOCATION);
orderCreationRequest.getTechnologyOperations().sort(Comparator.comparing(TechnologyOperationDto::getNode));
String range = parameter.getStringField(L_RANGE);
Entity technology = dataDefinitionService.get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_TECHNOLOGY).create();
technology.setField(TechnologyFields.NUMBER, technologyNameAndNumberGenerator.generateNumber(product));
technology.setField(TechnologyFields.NAME, technologyNameAndNumberGenerator.generateName(product));
technology.setField(TechnologyFields.PRODUCT, product);
technology.setField(TechnologyFields.EXTERNAL_SYNCHRONIZED, true);
technology.setField(L_RANGE, range);
technology.setField("componentsLocation", dashboardComponentsLocation);
technology.setField("productsInputLocation", dashboardProductsInputLocation);
technology.setField("typeOfProductionRecording", orderCreationRequest.getTypeOfProductionRecording());
technology = technology.getDataDefinition().save(technology);
if (!technology.isValid()) {
return Either.left(translationService.translate("basic.dashboard.orderDefinitionWizard.createTechnology.validationError", LocaleContextHolder.getLocale()));
}
Entity parent = null;
for (TechnologyOperationDto technologyOperation : orderCreationRequest.getTechnologyOperations()) {
Entity operation = dataDefinitionService.get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_OPERATION).get(technologyOperation.getOperationId());
Entity toc = dataDefinitionService.get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_TECHNOLOGY_OPERATION_COMPONENT).create();
toc.setField(TechnologyOperationComponentFields.OPERATION, operation);
toc.setField(TechnologyOperationComponentFields.ENTITY_TYPE, L_OPERATION);
for (String fieldName : FIELDS_OPERATION) {
toc.setField(fieldName, operation.getField(fieldName));
}
if (operation.getField(NEXT_OPERATION_AFTER_PRODUCED_TYPE) == null) {
toc.setField(NEXT_OPERATION_AFTER_PRODUCED_TYPE, L_ALL);
}
if (operation.getField(PRODUCTION_IN_ONE_CYCLE) == null) {
toc.setField(PRODUCTION_IN_ONE_CYCLE, "1");
}
if (operation.getField(NEXT_OPERATION_AFTER_PRODUCED_QUANTITY) == null) {
toc.setField(NEXT_OPERATION_AFTER_PRODUCED_QUANTITY, "0");
}
if (Objects.isNull(parent)) {
Entity topoc = dataDefinitionService.get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_OPERATION_PRODUCT_OUT_COMPONENT).create();
topoc.setField(OperationProductOutComponentFields.QUANTITY, BigDecimal.ONE);
topoc.setField(OperationProductOutComponentFields.PRODUCT, product);
toc.setField(TechnologyOperationComponentFields.OPERATION_PRODUCT_OUT_COMPONENTS, Lists.newArrayList(topoc));
}
List<Entity> topics = Lists.newArrayList();
for (MaterialDto material : technologyOperation.getMaterials()) {
Entity inProduct = getProduct(material.getProductId());
Entity topic = dataDefinitionService.get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_OPERATION_PRODUCT_IN_COMPONENT).create();
topic.setField(OperationProductInComponentFields.PRODUCT, inProduct);
topic.setField(OperationProductInComponentFields.QUANTITY, material.getQuantityPerUnit());
topics.add(topic);
}
toc.setField(TechnologyOperationComponentFields.OPERATION_PRODUCT_IN_COMPONENTS, topics);
if (Objects.nonNull(parent)) {
toc.setField(TechnologyOperationComponentFields.PARENT, parent.getId());
}
toc.setField(TechnologyOperationComponentFields.TECHNOLOGY, technology.getId());
toc = toc.getDataDefinition().save(toc);
if (Objects.nonNull(technologyOperation.getWorkstationId())) {
List<Entity> workstations = Lists.newArrayList(toc.getHasManyField(TechnologyOperationComponentFields.WORKSTATIONS));
Entity workstation = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_WORKSTATION).get(technologyOperation.getWorkstationId());
workstations.add(workstation);
toc.setField(TechnologyOperationComponentFields.WORKSTATIONS, workstations);
}
toc = toc.getDataDefinition().save(toc);
technologyOperation.setId(toc.getId());
if (toc.getHasManyField(TechnologyOperationComponentFields.OPERATION_PRODUCT_OUT_COMPONENTS).isEmpty()) {
Entity topoc = dataDefinitionService.get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_OPERATION_PRODUCT_OUT_COMPONENT).create();
topoc.setField(OperationProductOutComponentFields.QUANTITY, BigDecimal.ONE);
topoc.setField(OperationProductOutComponentFields.PRODUCT, getOrCreateProduct(operation));
topoc.setField(OperationProductOutComponentFields.OPERATION_COMPONENT, toc);
topoc.getDataDefinition().save(topoc);
}
parent = toc;
}
if (technology.isValid()) {
final StateChangeContext technologyStateChangeContext = stateChangeContextBuilder.build(technologyStateChangeAspect.getChangeEntityDescriber(), technology, TechnologyState.ACCEPTED.getStringValue());
technologyStateChangeAspect.changeState(technologyStateChangeContext);
technology = technology.getDataDefinition().get(technology.getId());
if (!technology.getStringField(TechnologyFields.STATE).equals(TechnologyStateStringValues.ACCEPTED)) {
return Either.left(translationService.translate("basic.dashboard.orderDefinitionWizard.createTechnology.acceptError", LocaleContextHolder.getLocale()));
}
technology.setField(TechnologyFields.MASTER, Boolean.TRUE);
technology.getDataDefinition().save(technology);
} else {
return Either.left(translationService.translate("basic.dashboard.orderDefinitionWizard.createTechnology.validationError", LocaleContextHolder.getLocale()));
}
return Either.right(technology);
}
use of com.qcadoo.mes.states.StateChangeContext in project mes by qcadoo.
the class OrderStateReasonViewListeners method cancelStateChange.
public void cancelStateChange(final ViewDefinitionState view, final ComponentState form, final String[] args) {
final Entity stateChangeEntity = ((FormComponent) form).getEntity();
stateChangeEntity.setField(REASON_REQUIRED, false);
final StateChangeContext stateContext = stateChangeContextBuilder.build(orderStateChangeService.getChangeEntityDescriber(), stateChangeEntity);
stateContext.setStatus(StateChangeStatus.CANCELED);
stateContext.save();
orderStateChangeViewClient.showMessages(new ViewContextHolder(view, form), stateContext);
}
use of com.qcadoo.mes.states.StateChangeContext in project mes by qcadoo.
the class AssignmentToShiftStatesHelper method changeState.
public void changeState(final Entity assignmentToShift, final AssignmentToShiftState state) {
StateChangeContext context = stateChangeContextBuilder.build(assignmentToShiftStateChangeDescriber, assignmentToShift, state.getStringValue());
assignmentToShiftStateChangeAspect.changeState(context);
}
use of com.qcadoo.mes.states.StateChangeContext in project mes by qcadoo.
the class OrderStatesListenerServicePFTD method checkMaterialAvailability.
public void checkMaterialAvailability(StateChangeContext stateChangeContext) {
Entity order = stateChangeContext.getOwner();
StateEnum targetState = stateChangeContext.getStateEnumValue(stateChangeContext.getDescriber().getTargetStateFieldName());
Entity technology = order.getBelongsToField(OrderFields.TECHNOLOGY);
String momentOfValidation = parameterService.getParameter().getStringField(ParameterFieldsPFTD.MOMENT_OF_VALIDATION);
String message = "productFlowThruDivision.order.state.accept.error.missingComponents";
String messageShort = "productFlowThruDivision.order.state.accept.error.missingComponentsShort";
if (MomentOfValidation.ORDER_STARTING.getStrValue().equals(momentOfValidation)) {
message = "productFlowThruDivision.order.state.inProgress.error.missingComponents";
messageShort = "productFlowThruDivision.order.state.inProgress.error.missingComponentsShort";
}
if (order.getField(OrderFieldsPFTD.IGNORE_MISSING_COMPONENTS) != null && !order.getBooleanField(OrderFieldsPFTD.IGNORE_MISSING_COMPONENTS) && technology != null && (OrderState.ACCEPTED == targetState && MomentOfValidation.ORDER_ACCEPTANCE.getStrValue().equals(momentOfValidation) || OrderState.IN_PROGRESS == targetState && MomentOfValidation.ORDER_STARTING.getStrValue().equals(momentOfValidation))) {
List<Entity> entries = orderMaterialAvailability.generateMaterialAvailabilityForOrder(order);
List<Entity> notAvailableProducts = entries.stream().filter(en -> !AvailabilityOfMaterialAvailability.FULL.getStrValue().equals(en.getStringField(MaterialAvailabilityFields.AVAILABILITY))).map(entity -> entity.getBelongsToField(MaterialAvailabilityFields.PRODUCT)).collect(Collectors.toList());
if (!notAvailableProducts.isEmpty()) {
String missingProductNames = StringUtils.join(notAvailableProducts.stream().map(product -> product.getStringField(ProductFields.NAME)).collect(Collectors.toList()), ", ");
if (missingProductNames.length() < 255) {
stateChangeContext.addMessage(message, StateMessageType.FAILURE, false, missingProductNames);
} else {
stateChangeContext.addMessage(messageShort, StateMessageType.FAILURE, false);
}
}
}
}
Aggregations