Search in sources :

Example 11 with SearchResult

use of com.qcadoo.model.api.search.SearchResult in project mes by qcadoo.

the class ProductionCountingQuantityValidators method checkIfAnotherFinalProductExists.

private boolean checkIfAnotherFinalProductExists(final DataDefinition productionCountingQuantityDD, final Entity productionCountingQuantity) {
    Entity order = productionCountingQuantity.getBelongsToField(ProductionCountingQuantityFields.ORDER);
    SearchCriteriaBuilder searchCriteriaBuilder = productionCountingQuantityDD.find().add(SearchRestrictions.belongsTo(ProductionCountingQuantityFields.ORDER, order)).add(SearchRestrictions.eq(ProductionCountingQuantityFields.TYPE_OF_MATERIAL, ProductionCountingQuantityTypeOfMaterial.FINAL_PRODUCT.getStringValue()));
    if (productionCountingQuantity.getId() != null) {
        searchCriteriaBuilder.add(SearchRestrictions.ne("id", productionCountingQuantity.getId()));
    }
    SearchResult searchResult = searchCriteriaBuilder.list();
    return searchResult.getEntities().isEmpty();
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) SearchResult(com.qcadoo.model.api.search.SearchResult)

Example 12 with SearchResult

use of com.qcadoo.model.api.search.SearchResult 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 13 with SearchResult

use of com.qcadoo.model.api.search.SearchResult in project mes by qcadoo.

the class CommonReasonTypeModelHooksTest method stubFindResults.

private void stubFindResults(final boolean existsMatchingEntity) {
    SearchCriteriaBuilder scb = mock(SearchCriteriaBuilder.class);
    given(reasonDD.find()).willReturn(scb);
    given(scb.setMaxResults(anyInt())).willReturn(scb);
    given(scb.add(any(SearchCriterion.class))).willReturn(scb);
    given(scb.setProjection(any(SearchProjection.class))).willReturn(scb);
    SearchResult searchResult = mock(SearchResult.class);
    given(scb.list()).willReturn(searchResult);
    given(searchResult.getTotalNumberOfEntities()).willReturn(existsMatchingEntity ? 1 : 0);
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) SearchProjection(com.qcadoo.model.api.search.SearchProjection) SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) SearchResult(com.qcadoo.model.api.search.SearchResult)

Example 14 with SearchResult

use of com.qcadoo.model.api.search.SearchResult in project mes by qcadoo.

the class BatchBasicStateListenerService method checkIfOccursAsUsedBatch.

public void checkIfOccursAsUsedBatch(final StateChangeContext stateChangeContext) {
    final Entity batch = stateChangeContext.getOwner();
    final SearchResult occursAsUsedBatch = dataDefinitionService.get(AdvancedGenealogyConstants.PLUGIN_IDENTIFIER, AdvancedGenealogyConstants.MODEL_USED_BATCH_SIMPLE).find().add(SearchRestrictions.belongsTo(UsedBatchSimpleFields.BATCH, batch)).createAlias(AdvancedGenealogyConstants.MODEL_TRACKING_RECORD, AdvancedGenealogyConstants.MODEL_TRACKING_RECORD).add(SearchRestrictions.eq(AdvancedGenealogyConstants.MODEL_TRACKING_RECORD + '.' + TrackingRecordFields.STATE, TrackingRecordState.DRAFT.getStringValue())).list();
    for (Entity usedBatch : occursAsUsedBatch.getEntities()) {
        final Entity producedBatch = usedBatch.getBelongsToField(UsedBatchSimpleFields.TRACKING_RECORD).getBelongsToField(TrackingRecordFields.PRODUCED_BATCH);
        final String producedBatchNumber = producedBatch.getStringField(BatchFields.NUMBER);
        final Entity producedBatchProduct = producedBatch.getBelongsToField(BatchFields.PRODUCT);
        final String producedBatchProductName = producedBatchProduct.getStringField(ProductFields.NAME);
        final String producedBatchProductNumber = producedBatchProduct.getStringField(ProductFields.NUMBER);
        final String producedBatchProductNameNumber = " - " + producedBatchProductName + " (" + producedBatchProductNumber + ")";
        final Entity producedBatchSupplierEntity = producedBatch.getBelongsToField(BatchFields.SUPPLIER);
        String producedBatchSupplier = "";
        if (producedBatchSupplierEntity != null) {
            producedBatchSupplier = " - " + producedBatchSupplierEntity.getStringField("name");
        }
        final String message = producedBatchNumber + producedBatchProductNameNumber + producedBatchSupplier;
        stateChangeContext.addMessage(BLOCKED_ERR_MSG, StateMessageType.FAILURE, message);
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchResult(com.qcadoo.model.api.search.SearchResult)

Example 15 with SearchResult

use of com.qcadoo.model.api.search.SearchResult in project mes by qcadoo.

the class BatchBasicStateListenerService method checkIfOccursAsProducedBatch.

public void checkIfOccursAsProducedBatch(final StateChangeContext stateChangeContext) {
    final Entity batch = stateChangeContext.getOwner();
    final SearchResult occursAsProducedBatch = dataDefinitionService.get(AdvancedGenealogyConstants.PLUGIN_IDENTIFIER, AdvancedGenealogyConstants.MODEL_TRACKING_RECORD).find().add(SearchRestrictions.eq(TrackingRecordFields.STATE, TrackingRecordState.DRAFT.getStringValue())).add(SearchRestrictions.belongsTo(TrackingRecordFields.PRODUCED_BATCH, batch)).list();
    for (Entity trackingRecord : occursAsProducedBatch.getEntities()) {
        final String number = trackingRecord.getStringField(TrackingRecordFields.NUMBER);
        stateChangeContext.addMessage(BLOCKED_ERR_MSG, StateMessageType.FAILURE, number);
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchResult(com.qcadoo.model.api.search.SearchResult)

Aggregations

SearchResult (com.qcadoo.model.api.search.SearchResult)35 Entity (com.qcadoo.model.api.Entity)18 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)16 DataDefinition (com.qcadoo.model.api.DataDefinition)10 Test (org.junit.Test)10 SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)7 SearchProjection (com.qcadoo.model.api.search.SearchProjection)4 FieldEntityIdChangeListener (com.qcadoo.view.internal.FieldEntityIdChangeListener)4 AbstractStateTest (com.qcadoo.view.internal.states.AbstractStateTest)4 BigDecimal (java.math.BigDecimal)4 FieldDefinition (com.qcadoo.model.api.FieldDefinition)2 SearchQueryBuilder (com.qcadoo.model.api.search.SearchQueryBuilder)2 Date (java.util.Date)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ImmutableList (com.google.common.collect.ImmutableList)1 ProductWithQuantityAndCost (com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.domain.ProductWithQuantityAndCost)1 Monitorable (com.qcadoo.model.api.aop.Monitorable)1 ErrorMessage (com.qcadoo.model.api.validators.ErrorMessage)1 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)1 LocalDate (java.time.LocalDate)1