Search in sources :

Example 81 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.

the class DeliveredProductReservationHooks method locationUnique.

private boolean locationUnique(final Entity deliveredProductReservation) {
    Entity location = deliveredProductReservation.getBelongsToField(DeliveredProductReservationFields.LOCATION);
    Entity deliveredProduct = deliveredProductReservation.getBelongsToField(DeliveredProductReservationFields.DELIVERED_PRODUCT);
    if (Objects.nonNull(location)) {
        SearchCriterion criterion;
        SearchCriterion criterionLocation = SearchRestrictions.belongsTo(DeliveredProductReservationFields.LOCATION, location);
        SearchCriterion criterionDeliveredProduct = SearchRestrictions.belongsTo(DeliveredProductReservationFields.DELIVERED_PRODUCT, deliveredProduct);
        Long deliveredProductReservationId = deliveredProductReservation.getId();
        if (Objects.isNull(deliveredProductReservationId)) {
            criterion = SearchRestrictions.and(criterionLocation, criterionDeliveredProduct);
        } else {
            SearchCriterion criterionId = SearchRestrictions.idNe(deliveredProductReservationId);
            criterion = SearchRestrictions.and(criterionLocation, criterionDeliveredProduct, criterionId);
        }
        boolean locationUnique = deliveredProductReservation.getDataDefinition().count(criterion) == 0;
        if (!locationUnique) {
            FieldDefinition locationField = deliveredProductReservation.getDataDefinition().getField(DeliveredProductReservationFields.LOCATION);
            deliveredProductReservation.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 82 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.

the class OrderedProductReservationHooks method locationUniqueToDelivery.

private boolean locationUniqueToDelivery(final Entity orderedProductReservation) {
    Entity orderedProduct = orderedProductReservation.getBelongsToField(OrderedProductReservationFields.ORDERED_PRODUCT);
    Entity delivery = orderedProduct.getBelongsToField(OrderedProductFields.DELIVERY);
    Entity deliveryLocation = delivery.getBelongsToField(DeliveryFields.LOCATION);
    Entity reservationLocation = orderedProductReservation.getBelongsToField(OrderedProductReservationFields.LOCATION);
    boolean locationOtherThenDelivery = Objects.isNull(deliveryLocation) || !deliveryLocation.getId().equals(reservationLocation.getId());
    if (!locationOtherThenDelivery) {
        FieldDefinition locationField = orderedProductReservation.getDataDefinition().getField(OrderedProductReservationFields.LOCATION);
        orderedProductReservation.addError(locationField, "deliveries.deliveredProductReservation.error.locationNotUniqueToDelivery");
    }
    return locationOtherThenDelivery;
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition)

Example 83 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.

the class OrderedProductReservationHooks method sumIsNotExceeded.

private boolean sumIsNotExceeded(final Entity orderedProductReservation) {
    Entity orderedProduct = orderedProductReservation.getBelongsToField(OrderedProductReservationFields.ORDERED_PRODUCT);
    BigDecimal productOrderedQuantity = orderedProduct.getDecimalField(OrderedProductReservationFields.ORDERED_QUANTITY);
    BigDecimal reservationOrderedQuantity = orderedProductReservation.getDecimalField(OrderedProductReservationFields.ORDERED_QUANTITY);
    SearchCriteriaBuilder searchCriteriaBuilder = orderedProductReservation.getDataDefinition().find();
    SearchProjection sumOfQuantityProjection = SearchProjections.alias(SearchProjections.sum(OrderedProductReservationFields.ORDERED_QUANTITY), L_SUM_OF_QUANTITY);
    searchCriteriaBuilder.setProjection(SearchProjections.list().add(sumOfQuantityProjection).add(SearchProjections.rowCount()));
    SearchCriterion criterion;
    SearchCriterion criterionOrderedProduct = SearchRestrictions.belongsTo(OrderedProductReservationFields.ORDERED_PRODUCT, orderedProduct);
    Long orderedProductReservationId = orderedProductReservation.getId();
    if (Objects.isNull(orderedProductReservationId)) {
        criterion = criterionOrderedProduct;
    } else {
        SearchCriterion criterionId = SearchRestrictions.idNe(orderedProductReservationId);
        criterion = SearchRestrictions.and(criterionOrderedProduct, 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);
    boolean sumIsNotExceeded = productOrderedQuantity.compareTo(reservationOrderedQuantity.add(sumOfQuantity)) >= 0;
    if (!sumIsNotExceeded) {
        FieldDefinition orderedQuantityField = orderedProductReservation.getDataDefinition().getField(OrderedProductReservationFields.ORDERED_QUANTITY);
        orderedProductReservation.addError(orderedQuantityField, "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 84 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.

the class MasterOrderValidators method addUnsupportedOrdersError.

private void addUnsupportedOrdersError(final Entity targetEntity, final String errorTargetFieldName, final String errorMessageKey, final Collection<String> unsupportedOrderNumbers) {
    FieldDefinition errorTargetFieldDef = targetEntity.getDataDefinition().getField(errorTargetFieldName);
    targetEntity.addError(errorTargetFieldDef, errorMessageKey, StringUtils.join(unsupportedOrderNumbers, ", "));
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition)

Example 85 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.

the class StateChangeViewClientValidationUtilTest method shouldAddValidationErrorToFormIfFieldIsEmpty.

@Test
public final void shouldAddValidationErrorToFormIfFieldIsEmpty() {
    // given
    DataDefinition dataDefinition = mockDataDefinition(Lists.<FieldDefinition>newArrayList());
    given(entity.getDataDefinition()).willReturn(dataDefinition);
    given(formComponent.getEntity()).willReturn(entity);
    Entity message = mockValidationErrorMsg("", false, TRANSLATION_KEY);
    given(messagesHolder.getAllMessages()).willReturn(Lists.newArrayList(message));
    // when
    validationUtil.addValidationErrorMessages(formComponent, entity, messagesHolder);
    // then
    verify(entity, Mockito.never()).addError(Mockito.any(FieldDefinition.class), Mockito.eq(TRANSLATION_KEY));
    verify(formComponent).addTranslatedMessage(Mockito.any(String.class), Mockito.eq(convertViewMessageType(VALIDATION_ERROR)), Mockito.any(Boolean.class));
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) DataDefinition(com.qcadoo.model.api.DataDefinition) Test(org.junit.Test)

Aggregations

FieldDefinition (com.qcadoo.model.api.FieldDefinition)142 Test (org.junit.Test)92 DataDefinition (com.qcadoo.model.api.DataDefinition)49 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)48 Entity (com.qcadoo.model.api.Entity)32 BelongsToType (com.qcadoo.model.api.types.BelongsToType)19 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)15 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)13 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)12 TextInputComponentPattern (com.qcadoo.view.internal.components.TextInputComponentPattern)11 HasManyType (com.qcadoo.model.api.types.HasManyType)9 StringType (com.qcadoo.model.internal.types.StringType)9 JSONObject (org.json.JSONObject)8 Map (java.util.Map)7 Matchers.anyString (org.mockito.Matchers.anyString)7 SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)6 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)6 WindowComponentPattern (com.qcadoo.view.internal.components.window.WindowComponentPattern)6 Before (org.junit.Before)6 EntityList (com.qcadoo.model.api.EntityList)5