use of com.qcadoo.model.api.search.SearchCriterion in project mes by qcadoo.
the class ProductValidatorsTest method shouldCheckEanUniquenessReturnTrueIfThereIsNoExistingProductsWithGivenId.
@Test
public final void shouldCheckEanUniquenessReturnTrueIfThereIsNoExistingProductsWithGivenId() {
// given
String oldVal = "123456";
String newVal = "654321";
ArgumentCaptor<SearchCriterion> criterionCaptor = ArgumentCaptor.forClass(SearchCriterion.class);
stubSearchCriteriaWith(null);
// when
boolean isValid = productValidators.checkEanUniqueness(dataDefinition, fieldDefinition, entity, oldVal, newVal);
// then
assertTrue(isValid);
verify(entity, never()).addError(any(FieldDefinition.class), anyString());
verify(scb).add(criterionCaptor.capture());
assertEquals(SearchRestrictions.eq(ProductFields.EAN, newVal), criterionCaptor.getValue());
}
use of com.qcadoo.model.api.search.SearchCriterion 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.SearchCriterion in project mes by qcadoo.
the class OrderedProductReservationHooks method locationUnique.
private boolean locationUnique(final Entity orderedProductReservation) {
Entity location = orderedProductReservation.getBelongsToField(OrderedProductReservationFields.LOCATION);
Entity orderedProduct = orderedProductReservation.getBelongsToField(OrderedProductReservationFields.ORDERED_PRODUCT);
if (Objects.nonNull(location)) {
SearchCriterion criterion;
SearchCriterion criterionLocation = SearchRestrictions.belongsTo(OrderedProductReservationFields.LOCATION, location);
SearchCriterion criterionOrderedProduct = SearchRestrictions.belongsTo(OrderedProductReservationFields.ORDERED_PRODUCT, orderedProduct);
Long orderedProductReservationId = orderedProductReservation.getId();
if (Objects.isNull(orderedProductReservationId)) {
criterion = SearchRestrictions.and(criterionLocation, criterionOrderedProduct);
} else {
SearchCriterion criterionId = SearchRestrictions.idNe(orderedProductReservationId);
criterion = SearchRestrictions.and(criterionLocation, criterionOrderedProduct, criterionId);
}
boolean locationUnique = orderedProductReservation.getDataDefinition().count(criterion) == 0;
if (!locationUnique) {
FieldDefinition locationField = orderedProductReservation.getDataDefinition().getField(OrderedProductReservationFields.LOCATION);
orderedProductReservation.addError(locationField, "deliveries.deliveredProductReservation.error.locationNotUnique");
}
return locationUnique;
}
return true;
}
use of com.qcadoo.model.api.search.SearchCriterion in project mes by qcadoo.
the class ReservationService method recalculateReservationsForDelivery.
public void recalculateReservationsForDelivery(final Long deliveryId) {
SearchCriterion criterion = SearchRestrictions.eq(DeliveredProductFields.DELIVERY + ".id", deliveryId);
DataDefinition deliveredProductDD = getDeliveredProductDD();
List<Entity> deliveredProducts = deliveredProductDD.find().add(criterion).list().getEntities();
deletePreviousReservations(deliveredProducts);
for (Entity deliveredProduct : deliveredProducts) {
if (!deliveredProduct.getBooleanField(DeliveredProductFields.IS_WASTE)) {
deliveredProduct = createDefaultReservationsForDeliveredProduct(deliveredProduct);
deliveredProductDD.save(deliveredProduct);
}
}
}
use of com.qcadoo.model.api.search.SearchCriterion in project mes by qcadoo.
the class OrderedProductHooksTest method init.
@Before
public void init() {
MockitoAnnotations.initMocks(this);
orderedProductHooks = new OrderedProductHooks();
PowerMockito.mockStatic(SearchRestrictions.class);
when(orderedProduct.getBelongsToField(DELIVERY)).thenReturn(delivery);
when(orderedProduct.getBelongsToField(PRODUCT)).thenReturn(product);
when(orderedProductDD.find()).thenReturn(searchCriteriaBuilder);
Long id = 1L;
when(orderedProduct.getId()).thenReturn(id);
SearchCriterion criterion1 = SearchRestrictions.belongsTo(DELIVERY, delivery);
SearchCriterion criterion2 = SearchRestrictions.belongsTo(PRODUCT, product);
SearchCriterion criterion3 = SearchRestrictions.ne("id", id);
when(searchCriteriaBuilder.add(criterion1)).thenReturn(searchCriteriaBuilder);
when(searchCriteriaBuilder.add(criterion2)).thenReturn(searchCriteriaBuilder);
when(searchCriteriaBuilder.add(criterion3)).thenReturn(searchCriteriaBuilder);
when(searchCriteriaBuilder.setMaxResults(1)).thenReturn(searchCriteriaBuilder);
PowerMockito.mockStatic(PluginUtils.class);
when(PluginUtils.isEnabled("supplyNegotiations")).thenReturn(false);
}
Aggregations