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