Search in sources :

Example 16 with SearchResult

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

the class BatchModelValidatorsTest method stubSearchResultsAndReturnCriteriaCaptor.

private ArgumentCaptor<SearchCriterion> stubSearchResultsAndReturnCriteriaCaptor(final Collection<Entity> entities) {
    given(batchDD.find()).willReturn(searchCriteriaBuilder);
    SearchResult searchResult = mock(SearchResult.class);
    given(searchCriteriaBuilder.list()).willReturn(searchResult);
    given(searchCriteriaBuilder.setMaxResults(anyInt())).willReturn(searchCriteriaBuilder);
    // stub .list() results
    given(searchResult.getTotalNumberOfEntities()).willReturn(entities.size());
    given(searchResult.getEntities()).willAnswer(new Answer<List<Entity>>() {

        @Override
        public List<Entity> answer(final InvocationOnMock ignored) throws Throwable {
            return Lists.newArrayList(entities);
        }
    });
    // stub unique result
    given(searchCriteriaBuilder.uniqueResult()).willAnswer(new Answer<Entity>() {

        @Override
        public Entity answer(final InvocationOnMock ignored) throws Throwable {
            return Iterables.getFirst(entities, null);
        }
    });
    // build argumentCaptor
    ArgumentCaptor<SearchCriterion> criterionCaptor = ArgumentCaptor.forClass(SearchCriterion.class);
    given(searchCriteriaBuilder.add(criterionCaptor.capture())).willReturn(searchCriteriaBuilder);
    return criterionCaptor;
}
Also used : Entity(com.qcadoo.model.api.Entity) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) SearchResult(com.qcadoo.model.api.search.SearchResult) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 17 with SearchResult

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

the class AddressHooks method checkIfOnlyOneMainAddressExists.

private boolean checkIfOnlyOneMainAddressExists(final DataDefinition addressDD, final Entity address, final String addressType) {
    Long addressId = address.getId();
    Entity company = address.getBelongsToField(AddressFields.COMPANY);
    SearchCriteriaBuilder searchCriteriaBuilder = addressDD.find().add(SearchRestrictions.belongsTo(AddressFields.COMPANY, company)).add(SearchRestrictions.eq(AddressFields.ADDRESS_TYPE, addressType));
    if (addressId != null) {
        searchCriteriaBuilder.add(SearchRestrictions.ne("id", addressId));
    }
    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 18 with SearchResult

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

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

the class OrderStatesListenerServicePFTD method tryAcceptInboundDocumentsFor.

@Transactional()
private Either<String, Void> tryAcceptInboundDocumentsFor(final Entity order) {
    updateDocumentQuantities(order);
    DataDefinition documentDD = dataDefinitionService.get(MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_DOCUMENT);
    SearchResult searchResult = documentDD.find().add(SearchRestrictions.belongsTo(DocumentFieldsPFTD.ORDER, order)).add(SearchRestrictions.eq(DocumentFields.TYPE, DocumentType.INTERNAL_INBOUND.getStringValue())).list();
    String priceBasedOn = parameterService.getParameter().getStringField(ParameterFieldsPC.PRICE_BASED_ON);
    boolean isNominalProductCost = priceBasedOn != null && priceBasedOn.equals(PriceBasedOn.NOMINAL_PRODUCT_COST.getStringValue());
    Optional<BigDecimal> price = Optional.empty();
    if (!isNominalProductCost) {
        price = Optional.ofNullable(order.getDecimalField("productPricePerUnit"));
    }
    for (Entity document : searchResult.getEntities()) {
        if (!isNominalProductCost) {
            fillInDocumentPositionsPrice(document, order.getBelongsToField(OrderFields.PRODUCT), price);
        }
        if (DocumentState.of(document) == DocumentState.ACCEPTED) {
            LOG.warn("Document {} already accepted.", document.getId());
            continue;
        }
        Entity acceptedDocument = acceptInboundDocument(document);
        if (!acceptedDocument.isValid()) {
            documentStateChangeService.buildFailureStateChange(acceptedDocument.getId());
            for (ErrorMessage error : acceptedDocument.getGlobalErrors()) {
                order.addGlobalError(error.getMessage(), error.getVars());
            }
            order.addGlobalError(L_ACCEPT_INBOUND_DOCUMENT_ERROR);
            return Either.left(L_ACCEPT_INBOUND_DOCUMENT_ERROR);
        }
    }
    Either<String, Void> documentsForNotUsedMaterials = createDocumentsForNotUsedMaterials(order);
    if (documentsForNotUsedMaterials.isLeft()) {
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }
    return documentsForNotUsedMaterials;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchResult(com.qcadoo.model.api.search.SearchResult) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) DataDefinition(com.qcadoo.model.api.DataDefinition) BigDecimal(java.math.BigDecimal) Transactional(org.springframework.transaction.annotation.Transactional)

Example 20 with SearchResult

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

the class WarehouseIssueGenerator method getOrderDtos.

private List<Entity> getOrderDtos(final int daysBeforeOrderStart) {
    String query = "SELECT order FROM #orders_order order  " + "WHERE order.state IN (:states) AND startDate BETWEEN :startDate AND :endDate AND order.warehouseIssues IS EMPTY";
    SearchQueryBuilder searchQueryBuilder = getOrderDtoDD().find(query);
    Date startDate = Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    Date endDate = Date.from(LocalDate.now().plusDays(daysBeforeOrderStart + 1).atStartOfDay().minusSeconds(1).atZone(ZoneId.systemDefault()).toInstant());
    searchQueryBuilder.setParameter("startDate", startDate);
    searchQueryBuilder.setParameter("endDate", endDate);
    searchQueryBuilder.setParameterList("states", Arrays.asList(OrderState.ACCEPTED.getStringValue(), OrderState.IN_PROGRESS.getStringValue()));
    SearchResult orders = searchQueryBuilder.list();
    return orders.getEntities();
}
Also used : SearchQueryBuilder(com.qcadoo.model.api.search.SearchQueryBuilder) SearchResult(com.qcadoo.model.api.search.SearchResult) Date(java.util.Date) LocalDate(java.time.LocalDate)

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