use of com.qcadoo.mes.technologies.dto.OperationProductComponentMessage in project mes by qcadoo.
the class ProductQuantitiesServiceImpl method preloadOperationProductComponentQuantity.
@Override
public void preloadOperationProductComponentQuantity(final List<Entity> operationProductComponents, final OperationProductComponentWithQuantityContainer operationProductComponentWithQuantityContainer) {
for (Entity operationProductComponent : operationProductComponents) {
BigDecimal neededQuantity = operationProductComponent.getDecimalField(L_QUANTITY);
if (operationProductComponent.getDataDefinition().getName().equals(TechnologiesConstants.MODEL_OPERATION_PRODUCT_IN_COMPONENT) && operationProductComponent.getBooleanField(OperationProductInComponentFields.DIFFERENT_PRODUCTS_IN_DIFFERENT_SIZES) && !operationProductComponentWithQuantityContainer.getSizeGroups().isEmpty()) {
for (Entity sizeGroup : operationProductComponentWithQuantityContainer.getSizeGroups()) {
List<Entity> productsByGroup = operationProductComponent.getHasManyField(OperationProductInComponentFields.PRODUCT_BY_SIZE_GROUPS).stream().filter(pG -> pG.getBelongsToField(ProductBySizeGroupFields.SIZE_GROUP).getId().equals(sizeGroup.getId())).collect(Collectors.toList());
for (Entity productByGroup : productsByGroup) {
operationProductComponentWithQuantityContainer.put(operationProductComponent, productByGroup.getBelongsToField(ProductBySizeGroupFields.PRODUCT), productByGroup.getDecimalField(ProductBySizeGroupFields.QUANTITY));
}
}
} else if (Objects.nonNull(operationProductComponentWithQuantityContainer.getOrderedProduct()) && operationProductComponent.getDataDefinition().getName().equals(TechnologiesConstants.MODEL_OPERATION_PRODUCT_IN_COMPONENT) && Objects.nonNull(getOperationProductProduct(operationProductComponent)) && ProductFamilyElementType.PRODUCTS_FAMILY.getStringValue().equals(getOperationProductProduct(operationProductComponent).getField(ProductFields.ENTITY_TYPE)) && Objects.nonNull(operationProductComponent.getBelongsToField(OperationProductInComponentFields.ATTRIBUTE))) {
Entity attribute = operationProductComponent.getBelongsToField(OperationProductInComponentFields.ATTRIBUTE);
List<Entity> attributeValues = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.PRODUCT_ATTRIBUTE_VALUE).find().add(SearchRestrictions.belongsTo(ProductAttributeValueFields.PRODUCT, operationProductComponentWithQuantityContainer.getOrderedProduct())).add(SearchRestrictions.belongsTo(ProductAttributeValueFields.ATTRIBUTE, attribute)).list().getEntities();
Entity opicProduct = getOperationProductProduct(operationProductComponent);
if (!attributeValues.isEmpty()) {
Collection<Long> attributeValuesId = attributeValues.stream().map(av -> av.getBelongsToField(ProductAttributeValueFields.ATTRIBUTE_VALUE).getId()).collect(Collectors.toList());
List<Entity> allProductAttributeValues = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.PRODUCT_ATTRIBUTE_VALUE).find().createAlias(ProductAttributeValueFields.PRODUCT, "prod", JoinType.LEFT).createAlias("prod.parent", "parentProduct", JoinType.LEFT).add(SearchRestrictions.eq("parentProduct.id", opicProduct.getId())).list().getEntities();
Map<Long, List<Entity>> attributeValuesByProduct = allProductAttributeValues.stream().collect(groupingBy(e -> e.getBelongsToField(ProductAttributeValueFields.PRODUCT).getId()));
Long specificProductId = null;
boolean moreThanOneSpecificProduct = false;
for (Entry<Long, List<Entity>> entry : attributeValuesByProduct.entrySet()) {
Long productId = entry.getKey();
List<Entity> attrValues = entry.getValue();
Collection<Long> allProductAttributeValuesId = attrValues.stream().filter(av -> Objects.nonNull(av.getBelongsToField(ProductAttributeValueFields.ATTRIBUTE_VALUE))).map(av -> av.getBelongsToField(ProductAttributeValueFields.ATTRIBUTE_VALUE).getId()).collect(Collectors.toList());
boolean hasNotCalculatedElements = attrValues.stream().anyMatch(av -> Objects.isNull(av.getBelongsToField(ProductAttributeValueFields.ATTRIBUTE_VALUE)));
boolean isSame = attributeValuesId.stream().allMatch(a -> allProductAttributeValuesId.stream().anyMatch(b -> b.equals(a)));
if (isSame && !hasNotCalculatedElements && attributeValuesId.size() == allProductAttributeValuesId.size()) {
if (Objects.nonNull(specificProductId)) {
moreThanOneSpecificProduct = true;
break;
}
specificProductId = productId;
}
}
if (Objects.isNull(specificProductId)) {
Entity toc = operationProductComponent.getBelongsToField(OperationProductInComponentFields.OPERATION_COMPONENT);
Entity operation = toc.getBelongsToField(TechnologyOperationComponentFields.OPERATION);
OperationProductComponentMessage message = new OperationProductComponentMessage();
message.setMessage("basicProductionCounting.productionCountingQuantity.error.couldNotFindSpecificProductWithMatchingAttributeValue");
message.setOperationNumber(operation.getStringField(OperationFields.NUMBER));
message.setProductNumber(opicProduct.getStringField(ProductFields.NUMBER));
message.setNodeNumber(toc.getStringField(TechnologyOperationComponentFields.NODE_NUMBER));
message.setAttributeNumber(attribute.getStringField(AttributeFields.NUMBER));
message.setAttributeValue(attributeValues.stream().map(a -> a.getStringField(ProductAttributeValueFields.VALUE)).collect(Collectors.joining(", ")));
operationProductComponentWithQuantityContainer.addMessage(message);
} else if (moreThanOneSpecificProduct) {
Entity toc = operationProductComponent.getBelongsToField(OperationProductInComponentFields.OPERATION_COMPONENT);
Entity operation = toc.getBelongsToField(TechnologyOperationComponentFields.OPERATION);
OperationProductComponentMessage message = new OperationProductComponentMessage();
message.setMessage("basicProductionCounting.productionCountingQuantity.error.toManyProductsWithMatchingAttributeValue");
message.setOperationNumber(operation.getStringField(OperationFields.NUMBER));
message.setProductNumber(opicProduct.getStringField(ProductFields.NUMBER));
message.setNodeNumber(toc.getStringField(TechnologyOperationComponentFields.NODE_NUMBER));
message.setAttributeNumber(attribute.getStringField(AttributeFields.NUMBER));
message.setAttributeValue(attributeValues.stream().map(a -> a.getStringField(ProductAttributeValueFields.VALUE)).collect(Collectors.joining(", ")));
operationProductComponentWithQuantityContainer.addMessage(message);
} else {
Entity product = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_PRODUCT).get(specificProductId);
operationProductComponentWithQuantityContainer.put(operationProductComponent, product, operationProductComponent.getDecimalField(ProductBySizeGroupFields.QUANTITY));
}
} else {
Entity toc = operationProductComponent.getBelongsToField(OperationProductInComponentFields.OPERATION_COMPONENT);
Entity operation = toc.getBelongsToField(TechnologyOperationComponentFields.OPERATION);
OperationProductComponentMessage message = new OperationProductComponentMessage();
message.setMessage("basicProductionCounting.productionCountingQuantity.error.orderedProductDoesNotHaveAttribute");
message.setOperationNumber(operation.getStringField(OperationFields.NUMBER));
message.setProductNumber(opicProduct.getStringField(ProductFields.NUMBER));
message.setNodeNumber(toc.getStringField(TechnologyOperationComponentFields.NODE_NUMBER));
message.setAttributeNumber(attribute.getStringField(AttributeFields.NUMBER));
operationProductComponentWithQuantityContainer.addMessage(message);
}
} else {
operationProductComponentWithQuantityContainer.put(operationProductComponent, neededQuantity);
}
}
}
Aggregations