use of com.qcadoo.model.api.exception.EntityRuntimeException in project mes by qcadoo.
the class AnomalyProductionTrackingDetailsListeners method createAnomaly.
private void createAnomaly(final Entity entity) {
Entity anomaly = dataDefinitionService.get(ProductionCountingConstants.PLUGIN_IDENTIFIER, ProductionCountingConstants.MODEL_ANOMALY).create();
Entity productionTracking = entity.getBelongsToField("trackingOperationProductInComponent").getBelongsToField(TrackingOperationProductInComponentFields.PRODUCTION_TRACKING);
Entity product = entity.getBelongsToField("trackingOperationProductInComponent").getBelongsToField(TrackingOperationProductInComponentFields.PRODUCT);
anomaly.setField(AnomalyFields.NUMBER, numberGeneratorService.generateNumber(ProductionCountingConstants.PLUGIN_IDENTIFIER, ProductionCountingConstants.MODEL_ANOMALY));
anomaly.setField(AnomalyFields.PRODUCTION_TRACKING, productionTracking);
anomaly.setField(AnomalyFields.PRODUCT, product);
anomaly.setField(AnomalyFields.USED_QUANTITY, entity.getDecimalField("usedQuantity"));
anomaly.setField(AnomalyFields.STATE, "01draft");
anomaly.setField(AnomalyFields.ISSUED, false);
Entity order = productionTracking.getBelongsToField(ProductionTrackingFields.ORDER);
if (Objects.isNull(order.getBelongsToField("root"))) {
anomaly.setField(AnomalyFields.MASTER_PRODUCT, order.getBelongsToField(OrderFields.PRODUCT));
} else {
anomaly.setField(AnomalyFields.MASTER_PRODUCT, order.getBelongsToField("root").getBelongsToField(OrderFields.PRODUCT));
}
List<Entity> reasons = Lists.newArrayList();
List<Entity> anomalyContainers = entity.getHasManyField("anomalyReasons");
anomalyContainers.forEach(ac -> {
reasons.add(ac.getBelongsToField("anomalyReason"));
});
anomaly.setField(AnomalyFields.ANAOMALY_REASONS, reasons);
anomaly.setField(AnomalyFields.LOCATION, findLocation(productionTracking, product));
anomaly = anomaly.getDataDefinition().save(anomaly);
if (!anomaly.isValid()) {
throw new EntityRuntimeException(anomaly);
}
}
Aggregations