use of com.qcadoo.model.api.search.SearchResult in project mes by qcadoo.
the class ProductionCountingQuantityValidators method checkIfAnotherFinalProductExists.
private boolean checkIfAnotherFinalProductExists(final DataDefinition productionCountingQuantityDD, final Entity productionCountingQuantity) {
Entity order = productionCountingQuantity.getBelongsToField(ProductionCountingQuantityFields.ORDER);
SearchCriteriaBuilder searchCriteriaBuilder = productionCountingQuantityDD.find().add(SearchRestrictions.belongsTo(ProductionCountingQuantityFields.ORDER, order)).add(SearchRestrictions.eq(ProductionCountingQuantityFields.TYPE_OF_MATERIAL, ProductionCountingQuantityTypeOfMaterial.FINAL_PRODUCT.getStringValue()));
if (productionCountingQuantity.getId() != null) {
searchCriteriaBuilder.add(SearchRestrictions.ne("id", productionCountingQuantity.getId()));
}
SearchResult searchResult = searchCriteriaBuilder.list();
return searchResult.getEntities().isEmpty();
}
use of com.qcadoo.model.api.search.SearchResult in project mes by qcadoo.
the class DeliveredProductReservationHooks method sumIsNotExceeded.
private boolean sumIsNotExceeded(final Entity deliveredProductReservation) {
Entity deliveredProduct = deliveredProductReservation.getBelongsToField(DeliveredProductReservationFields.DELIVERED_PRODUCT);
BigDecimal productDeliveredQuantity = deliveredProduct.getDecimalField(DeliveredProductFields.DELIVERED_QUANTITY);
if (Objects.isNull(productDeliveredQuantity)) {
return true;
}
BigDecimal reservationDeliveredQuantity = deliveredProductReservation.getDecimalField(DeliveredProductReservationFields.DELIVERED_QUANTITY);
SearchCriteriaBuilder searchCriteriaBuilder = deliveredProductReservation.getDataDefinition().find();
SearchProjection sumOfQuantityProjection = SearchProjections.alias(SearchProjections.sum(DeliveredProductReservationFields.DELIVERED_QUANTITY), L_SUM_OF_QUANTITY);
searchCriteriaBuilder.setProjection(SearchProjections.list().add(sumOfQuantityProjection).add(SearchProjections.rowCount()));
SearchCriterion criterion;
SearchCriterion criterionDeliveredProduct = SearchRestrictions.belongsTo(DeliveredProductReservationFields.DELIVERED_PRODUCT, deliveredProduct);
Long deliveredProductReservationId = deliveredProductReservation.getId();
if (Objects.isNull(deliveredProductReservationId)) {
criterion = criterionDeliveredProduct;
} else {
SearchCriterion criterionId = SearchRestrictions.idNe(deliveredProductReservationId);
criterion = SearchRestrictions.and(criterionDeliveredProduct, criterionId);
}
searchCriteriaBuilder.add(criterion);
searchCriteriaBuilder.addOrder(SearchOrders.asc(L_SUM_OF_QUANTITY));
SearchResult resList = searchCriteriaBuilder.setMaxResults(1).list();
BigDecimal sumOfQuantity = (resList.getTotalNumberOfEntities() == 0) ? BigDecimal.ZERO : resList.getEntities().get(0).getDecimalField(L_SUM_OF_QUANTITY);
sumOfQuantity = BigDecimalUtils.convertNullToZero(sumOfQuantity);
BigDecimal damagedQuantity = deliveredProduct.getDecimalField(DeliveredProductFields.DAMAGED_QUANTITY);
damagedQuantity = BigDecimalUtils.convertNullToZero(damagedQuantity);
productDeliveredQuantity = productDeliveredQuantity.subtract(damagedQuantity);
boolean sumIsNotExceeded = productDeliveredQuantity.compareTo(reservationDeliveredQuantity.add(sumOfQuantity)) >= 0;
if (!sumIsNotExceeded) {
FieldDefinition deliveredQuantityField = deliveredProductReservation.getDataDefinition().getField(DeliveredProductReservationFields.DELIVERED_QUANTITY);
deliveredProductReservation.addError(deliveredQuantityField, "deliveries.deliveredProductReservation.error.sumIsExceeded");
}
return sumIsNotExceeded;
}
use of com.qcadoo.model.api.search.SearchResult in project mes by qcadoo.
the class CommonReasonTypeModelHooksTest method stubFindResults.
private void stubFindResults(final boolean existsMatchingEntity) {
SearchCriteriaBuilder scb = mock(SearchCriteriaBuilder.class);
given(reasonDD.find()).willReturn(scb);
given(scb.setMaxResults(anyInt())).willReturn(scb);
given(scb.add(any(SearchCriterion.class))).willReturn(scb);
given(scb.setProjection(any(SearchProjection.class))).willReturn(scb);
SearchResult searchResult = mock(SearchResult.class);
given(scb.list()).willReturn(searchResult);
given(searchResult.getTotalNumberOfEntities()).willReturn(existsMatchingEntity ? 1 : 0);
}
use of com.qcadoo.model.api.search.SearchResult in project mes by qcadoo.
the class BatchBasicStateListenerService method checkIfOccursAsUsedBatch.
public void checkIfOccursAsUsedBatch(final StateChangeContext stateChangeContext) {
final Entity batch = stateChangeContext.getOwner();
final SearchResult occursAsUsedBatch = dataDefinitionService.get(AdvancedGenealogyConstants.PLUGIN_IDENTIFIER, AdvancedGenealogyConstants.MODEL_USED_BATCH_SIMPLE).find().add(SearchRestrictions.belongsTo(UsedBatchSimpleFields.BATCH, batch)).createAlias(AdvancedGenealogyConstants.MODEL_TRACKING_RECORD, AdvancedGenealogyConstants.MODEL_TRACKING_RECORD).add(SearchRestrictions.eq(AdvancedGenealogyConstants.MODEL_TRACKING_RECORD + '.' + TrackingRecordFields.STATE, TrackingRecordState.DRAFT.getStringValue())).list();
for (Entity usedBatch : occursAsUsedBatch.getEntities()) {
final Entity producedBatch = usedBatch.getBelongsToField(UsedBatchSimpleFields.TRACKING_RECORD).getBelongsToField(TrackingRecordFields.PRODUCED_BATCH);
final String producedBatchNumber = producedBatch.getStringField(BatchFields.NUMBER);
final Entity producedBatchProduct = producedBatch.getBelongsToField(BatchFields.PRODUCT);
final String producedBatchProductName = producedBatchProduct.getStringField(ProductFields.NAME);
final String producedBatchProductNumber = producedBatchProduct.getStringField(ProductFields.NUMBER);
final String producedBatchProductNameNumber = " - " + producedBatchProductName + " (" + producedBatchProductNumber + ")";
final Entity producedBatchSupplierEntity = producedBatch.getBelongsToField(BatchFields.SUPPLIER);
String producedBatchSupplier = "";
if (producedBatchSupplierEntity != null) {
producedBatchSupplier = " - " + producedBatchSupplierEntity.getStringField("name");
}
final String message = producedBatchNumber + producedBatchProductNameNumber + producedBatchSupplier;
stateChangeContext.addMessage(BLOCKED_ERR_MSG, StateMessageType.FAILURE, message);
}
}
use of com.qcadoo.model.api.search.SearchResult in project mes by qcadoo.
the class BatchBasicStateListenerService method checkIfOccursAsProducedBatch.
public void checkIfOccursAsProducedBatch(final StateChangeContext stateChangeContext) {
final Entity batch = stateChangeContext.getOwner();
final SearchResult occursAsProducedBatch = dataDefinitionService.get(AdvancedGenealogyConstants.PLUGIN_IDENTIFIER, AdvancedGenealogyConstants.MODEL_TRACKING_RECORD).find().add(SearchRestrictions.eq(TrackingRecordFields.STATE, TrackingRecordState.DRAFT.getStringValue())).add(SearchRestrictions.belongsTo(TrackingRecordFields.PRODUCED_BATCH, batch)).list();
for (Entity trackingRecord : occursAsProducedBatch.getEntities()) {
final String number = trackingRecord.getStringField(TrackingRecordFields.NUMBER);
stateChangeContext.addMessage(BLOCKED_ERR_MSG, StateMessageType.FAILURE, number);
}
}
Aggregations