Search in sources :

Example 71 with FieldDefinition

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

the class OperationModelValidators method appendErrorToModelField.

private void appendErrorToModelField(final Entity entity, final String fieldName, final String messageKey) {
    FieldDefinition productInFieldDef = entity.getDataDefinition().getField(fieldName);
    entity.addError(productInFieldDef, messageKey);
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition)

Example 72 with FieldDefinition

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

the class RegexFailedValidationLoggingAspect method afterReturningCallExecution.

@AfterReturning(pointcut = "callExecution(target, entity, oldValue, newValue)", returning = "result")
public void afterReturningCallExecution(final Object target, final Entity entity, final Object oldValue, final Object newValue, final boolean result) {
    if (!result && logger.isInfoEnabled()) {
        try {
            List<String> messageStatements = Lists.newArrayList();
            FieldDefinition fieldDefinition = (FieldDefinition) FieldUtils.readField(target, "fieldDefinition", true);
            DataDefinition dataDefinition = fieldDefinition.getDataDefinition();
            messageStatements.add("INPUT REJECTED FOR ENTITY: " + dataDefinition.getPluginIdentifier() + "." + dataDefinition.getName());
            messageStatements.add("AND FIELD: " + fieldDefinition.getName());
            messageStatements.add("OLD VALUE: " + Optional.ofNullable(oldValue).map(Object::toString).orElse(StringUtils.EMPTY));
            messageStatements.add("NEW VALUE: " + Optional.ofNullable(newValue).map(Object::toString).orElse(StringUtils.EMPTY));
            messageStatements.add("AUTHENTICATION:" + SecurityContextHolder.getContext().getAuthentication());
            logger.info(String.join(" ", messageStatements));
        } catch (IllegalAccessException iae) {
        // ignore
        }
    }
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition) DataDefinition(com.qcadoo.model.api.DataDefinition) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Example 73 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition 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 74 with FieldDefinition

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

the class ProductValidatorsTest method shouldCheckEanUniquenessJustReturnTrueIfNewEanIsNull.

@Test
public final void shouldCheckEanUniquenessJustReturnTrueIfNewEanIsNull() {
    // given
    String oldVal = "123456";
    String newVal = null;
    // when
    boolean isValid = productValidators.checkEanUniqueness(dataDefinition, fieldDefinition, entity, oldVal, newVal);
    // then
    assertTrue(isValid);
    verify(entity, never()).addError(any(FieldDefinition.class), anyString());
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 75 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition 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)

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