Search in sources :

Example 66 with SearchCriteriaBuilder

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

the class ResourceManagementServiceImpl method getQuantitiesInWarehouse.

public Multimap<Long, BigDecimal> getQuantitiesInWarehouse(final Entity warehouse, final Multimap<Entity, Entity> productsAndPositions) {
    Multimap<Long, BigDecimal> result = ArrayListMultimap.create();
    for (Map.Entry<Entity, Entity> productAndPosition : productsAndPositions.entries()) {
        Entity additionalCode = productAndPosition.getValue().getBelongsToField(PositionFields.ADDITIONAL_CODE);
        BigDecimal conversion = productAndPosition.getValue().getDecimalField(PositionFields.CONVERSION);
        Entity batch = productAndPosition.getValue().getBelongsToField(PositionFields.BATCH);
        List<Entity> resources = Lists.newArrayList();
        if (additionalCode != null) {
            SearchCriteriaBuilder scb = getSearchCriteriaForResourceForProductAndWarehouse(productAndPosition.getKey(), warehouse);
            if (!StringUtils.isEmpty(productAndPosition.getKey().getStringField(ProductFields.ADDITIONAL_UNIT))) {
                scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, conversion));
            } else {
                scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
            }
            if (Objects.nonNull(batch)) {
                scb.add(SearchRestrictions.belongsTo(ResourceFields.BATCH, batch));
            }
            resources = scb.add(SearchRestrictions.belongsTo(ResourceFields.ADDITIONAL_CODE, additionalCode)).list().getEntities();
            scb = getSearchCriteriaForResourceForProductAndWarehouse(productAndPosition.getKey(), warehouse);
            if (!StringUtils.isEmpty(productAndPosition.getKey().getStringField(ProductFields.ADDITIONAL_UNIT))) {
                scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, conversion));
            } else {
                scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
            }
            if (Objects.nonNull(batch)) {
                scb.add(SearchRestrictions.belongsTo(ResourceFields.BATCH, batch));
            }
            resources.addAll(scb.add(SearchRestrictions.or(SearchRestrictions.isNull(ResourceFields.ADDITIONAL_CODE), SearchRestrictions.ne("additionalCode.id", additionalCode.getId()))).list().getEntities());
        }
        if (resources.isEmpty()) {
            SearchCriteriaBuilder scb = getSearchCriteriaForResourceForProductAndWarehouse(productAndPosition.getKey(), warehouse);
            if (Objects.nonNull(batch)) {
                scb.add(SearchRestrictions.belongsTo(ResourceFields.BATCH, batch));
            }
            if (!StringUtils.isEmpty(productAndPosition.getKey().getStringField(ProductFields.ADDITIONAL_UNIT))) {
                scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, conversion));
            } else {
                scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
            }
            resources = scb.list().getEntities();
        }
        if (result.containsKey(productAndPosition.getKey().getId())) {
            BigDecimal currentQuantity = result.get(productAndPosition.getKey().getId()).stream().reduce(BigDecimal.ZERO, BigDecimal::add);
            result.put(productAndPosition.getKey().getId(), (resources.stream().map(res -> res.getDecimalField(ResourceFields.AVAILABLE_QUANTITY)).reduce(BigDecimal.ZERO, BigDecimal::add)).add(currentQuantity));
        } else {
            result.put(productAndPosition.getKey().getId(), resources.stream().map(res -> res.getDecimalField(ResourceFields.AVAILABLE_QUANTITY)).reduce(BigDecimal.ZERO, BigDecimal::add));
        }
    }
    return result;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

Example 67 with SearchCriteriaBuilder

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

the class LocationValidators method noReservationExists.

public boolean noReservationExists(Entity entity) {
    if (entity.getId() == null) {
        return true;
    }
    SearchCriteriaBuilder scb = dataDefinitionService.get(MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_RESERVATION).find();
    scb.add(SearchRestrictions.isNotNull(ReservationFields.POSITION));
    scb.add(SearchRestrictions.belongsTo(ReservationFields.LOCATION, entity));
    scb.setProjection(alias(rowCount(), "cnt"));
    scb.addOrder(asc("cnt"));
    Entity countProjection = scb.setMaxResults(1).uniqueResult();
    return ((Long) countProjection.getField("cnt")).compareTo(0L) == 0;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder)

Example 68 with SearchCriteriaBuilder

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

the class LocationValidators method noDraftDocumentExists.

public boolean noDraftDocumentExists(Entity entity) {
    if (entity.getId() == null) {
        return true;
    }
    SearchCriteriaBuilder scb = dataDefinitionService.get(MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_DOCUMENT).find();
    scb.add(SearchRestrictions.eq(DocumentFields.STATE, DocumentState.DRAFT.getStringValue()));
    scb.add(SearchRestrictions.in(DocumentFields.TYPE, DOCUMENT_TYPES));
    scb.add(SearchRestrictions.belongsTo(DocumentFields.LOCATION_FROM, entity));
    scb.setProjection(alias(rowCount(), "cnt"));
    scb.addOrder(asc("cnt"));
    Entity countProjection = scb.setMaxResults(1).uniqueResult();
    return ((Long) countProjection.getField("cnt")).compareTo(0L) == 0;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder)

Example 69 with SearchCriteriaBuilder

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

the class MaterialRequirementCoverageForOrderServiceImpl method getUsedQuantityForOtherFromProductionTrackings.

private BigDecimal getUsedQuantityForOtherFromProductionTrackings(final Entity order, final Entity technologyOperationComponent, final Entity product, final String typeOfProductionRecording) {
    BigDecimal usedQuantity = BigDecimal.ZERO;
    SearchCriteriaBuilder searchCriteriaBuilder = dataDefinitionService.get(ProductionCountingConstants.PLUGIN_IDENTIFIER, ProductionCountingConstants.MODEL_PRODUCTION_TRACKING).find().add(SearchRestrictions.belongsTo(ProductionTrackingFields.ORDER, order));
    if (TypeOfProductionRecording.FOR_EACH.getStringValue().equals(typeOfProductionRecording)) {
        searchCriteriaBuilder.add(SearchRestrictions.belongsTo(ProductionTrackingFields.TECHNOLOGY_OPERATION_COMPONENT, technologyOperationComponent));
    }
    List<Entity> productionTrackings = searchCriteriaBuilder.list().getEntities();
    for (Entity productionTracking : productionTrackings) {
        Entity trackingOperationProductInComponent = productionTracking.getHasManyField(ProductionTrackingFields.TRACKING_OPERATION_PRODUCT_IN_COMPONENTS).find().add(SearchRestrictions.belongsTo(TrackingOperationProductInComponentFields.PRODUCT, product)).setMaxResults(1).uniqueResult();
        if (trackingOperationProductInComponent != null) {
            BigDecimal quantity = trackingOperationProductInComponent.getDecimalField(TrackingOperationProductInComponentFields.USED_QUANTITY);
            if (quantity != null) {
                usedQuantity = usedQuantity.add(quantity, numberService.getMathContext());
            }
        }
    }
    return usedQuantity;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) BigDecimal(java.math.BigDecimal)

Example 70 with SearchCriteriaBuilder

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

the class MaterialRequirementCoverageForOrderServiceImpl method getCoverageProductsForOrder.

private List<Entity> getCoverageProductsForOrder(final Entity order) {
    SearchCriteriaBuilder scb = dataDefinitionService.get(OrderSuppliesConstants.PLUGIN_IDENTIFIER, OrderSuppliesConstants.MODEL_COVERAGE_PRODUCT).find();
    scb.add(SearchRestrictions.belongsTo(CoverageProductFields.ORDER, OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER, order.getId()));
    return scb.list().getEntities();
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder)

Aggregations

SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)176 Entity (com.qcadoo.model.api.Entity)82 DataDefinition (com.qcadoo.model.api.DataDefinition)26 Autowired (org.springframework.beans.factory.annotation.Autowired)19 Service (org.springframework.stereotype.Service)19 DataDefinitionService (com.qcadoo.model.api.DataDefinitionService)18 EntityList (com.qcadoo.model.api.EntityList)17 SearchRestrictions (com.qcadoo.model.api.search.SearchRestrictions)17 Collectors (java.util.stream.Collectors)17 SearchResult (com.qcadoo.model.api.search.SearchResult)16 SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)14 Objects (java.util.Objects)14 FilterValueHolder (com.qcadoo.view.api.components.lookup.FilterValueHolder)11 BigDecimal (java.math.BigDecimal)11 Set (java.util.Set)11 UserFieldsMF (com.qcadoo.mes.materialFlow.constants.UserFieldsMF)10 UserLocationFields (com.qcadoo.mes.materialFlow.constants.UserLocationFields)10 SecurityService (com.qcadoo.security.api.SecurityService)10 QcadooSecurityConstants (com.qcadoo.security.constants.QcadooSecurityConstants)10 CustomRestriction (com.qcadoo.model.api.search.CustomRestriction)8