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;
}
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;
}
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;
}
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, ", "));
}
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));
}
Aggregations