Search in sources :

Example 6 with SearchCriterion

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());
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition) SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 7 with SearchCriterion

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;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) SearchProjection(com.qcadoo.model.api.search.SearchProjection) FieldDefinition(com.qcadoo.model.api.FieldDefinition) SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) SearchResult(com.qcadoo.model.api.search.SearchResult) BigDecimal(java.math.BigDecimal)

Example 8 with SearchCriterion

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;
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) SearchCriterion(com.qcadoo.model.api.search.SearchCriterion)

Example 9 with SearchCriterion

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);
        }
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) DataDefinition(com.qcadoo.model.api.DataDefinition)

Example 10 with SearchCriterion

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);
}
Also used : SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) Before(org.junit.Before)

Aggregations

SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)41 Entity (com.qcadoo.model.api.Entity)16 Test (org.junit.Test)12 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)7 FieldDefinition (com.qcadoo.model.api.FieldDefinition)6 DataDefinition (com.qcadoo.model.api.DataDefinition)4 SearchProjection (com.qcadoo.model.api.search.SearchProjection)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 SearchResult (com.qcadoo.model.api.search.SearchResult)3 BigDecimal (java.math.BigDecimal)2 Date (java.util.Date)2 Before (org.junit.Before)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ImmutableList (com.google.common.collect.ImmutableList)1 BatchNumberUniqueness (com.qcadoo.mes.advancedGenealogy.constants.BatchNumberUniqueness)1 AssignmentToShiftCriteria (com.qcadoo.mes.assignmentToShift.dataProviders.AssignmentToShiftCriteria)1 SearchDisjunction (com.qcadoo.model.api.search.SearchDisjunction)1 SearchOrder (com.qcadoo.model.api.search.SearchOrder)1 ParseException (java.text.ParseException)1 List (java.util.List)1