Search in sources :

Example 96 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.

the class ProductSearchSession method listCountByFeatureForType.

/**
 * This method returns a list of productId counts grouped by productFeatureId's of input productFeatureTypeId,
 * the constraint being applied on current ProductSearchConstraint list in session.
 * @param productFeatureTypeId The productFeatureTypeId, productFeatureId's of which should be considered.
 * @param session Current session.
 * @param delegator The delegator object.
 * @return List of Maps containing productFeatureId, productFeatureTypeId, description, featureCount.
 */
public static List<Map<String, String>> listCountByFeatureForType(String productFeatureTypeId, HttpSession session, Delegator delegator) {
    String visitId = VisitHandler.getVisitId(session);
    ProductSearchContext productSearchContext = new ProductSearchContext(delegator, visitId);
    List<ProductSearchConstraint> productSearchConstraintList = ProductSearchOptions.getConstraintList(session);
    if (UtilValidate.isNotEmpty(productSearchConstraintList)) {
        productSearchContext.addProductSearchConstraints(productSearchConstraintList);
    }
    productSearchContext.finishKeywordConstraints();
    productSearchContext.finishCategoryAndFeatureConstraints();
    DynamicViewEntity dynamicViewEntity = productSearchContext.dynamicViewEntity;
    List<EntityCondition> entityConditionList = productSearchContext.entityConditionList;
    dynamicViewEntity.addMemberEntity("PFAC", "ProductFeatureAppl");
    dynamicViewEntity.addAlias("PFAC", "pfacProductFeatureId", "productFeatureId", null, null, Boolean.TRUE, null);
    dynamicViewEntity.addAlias("PFAC", "pfacFromDate", "fromDate", null, null, null, null);
    dynamicViewEntity.addAlias("PFAC", "pfacThruDate", "thruDate", null, null, null, null);
    dynamicViewEntity.addAlias("PFAC", "featureCount", "productId", null, null, null, "count-distinct");
    dynamicViewEntity.addViewLink("PROD", "PFAC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("productId"));
    entityConditionList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("pfacThruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("pfacThruDate", EntityOperator.GREATER_THAN, UtilDateTime.nowTimestamp())));
    entityConditionList.add(EntityCondition.makeCondition("pfacFromDate", EntityOperator.LESS_THAN, UtilDateTime.nowTimestamp()));
    dynamicViewEntity.addMemberEntity("PFC", "ProductFeature");
    dynamicViewEntity.addAlias("PFC", "pfcProductFeatureTypeId", "productFeatureTypeId", null, null, Boolean.TRUE, null);
    dynamicViewEntity.addAlias("PFC", "pfcDescription", "description", null, null, Boolean.TRUE, null);
    dynamicViewEntity.addViewLink("PFAC", "PFC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("productFeatureId"));
    entityConditionList.add(EntityCondition.makeCondition("pfcProductFeatureTypeId", EntityOperator.EQUALS, productFeatureTypeId));
    List<Map<String, String>> featureCountList = null;
    EntityQuery eq = EntityQuery.use(delegator).select(UtilMisc.toSet("pfacProductFeatureId", "featureCount", "pfcDescription", "pfcProductFeatureTypeId")).from(dynamicViewEntity).where(entityConditionList).orderBy(productSearchContext.orderByList).cursorScrollInsensitive();
    try (EntityListIterator eli = eq.queryIterator()) {
        featureCountList = new LinkedList<>();
        GenericValue searchResult = null;
        while ((searchResult = eli.next()) != null) {
            featureCountList.add(UtilMisc.<String, String>toMap("productFeatureId", (String) searchResult.get("pfacProductFeatureId"), "productFeatureTypeId", (String) searchResult.get("pfcProductFeatureTypeId"), "description", (String) searchResult.get("pfcDescription"), "featureCount", Long.toString((Long) searchResult.get("featureCount"))));
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, "Error in product search", module);
        return null;
    }
    return featureCountList;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) DynamicViewEntity(org.apache.ofbiz.entity.model.DynamicViewEntity) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ProductSearchContext(org.apache.ofbiz.product.product.ProductSearch.ProductSearchContext) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) HashMap(java.util.HashMap) Map(java.util.Map) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Example 97 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.

the class ProductSearchSession method getProductSearchResult.

@SuppressWarnings("unchecked")
public static Map<String, Object> getProductSearchResult(HttpServletRequest request, Delegator delegator, String prodCatalogId) {
    // ========== Create View Indexes
    int viewIndex = 0;
    int viewSize = 20;
    int highIndex = 0;
    int lowIndex = 0;
    int listSize = 0;
    String paging = "Y";
    int previousViewSize = 20;
    Map<String, Object> requestParams = UtilHttp.getCombinedMap(request);
    List<String> keywordTypeIds = new LinkedList<>();
    if (requestParams.get("keywordTypeId") instanceof String) {
        keywordTypeIds.add((String) requestParams.get("keywordTypeId"));
    } else if (requestParams.get("keywordTypeId") instanceof List) {
        keywordTypeIds = (List<String>) requestParams.get("keywordTypeId");
    }
    String statusId = (String) requestParams.get("statusId");
    HttpSession session = request.getSession();
    ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
    String addOnTopProdCategoryId = productSearchOptions.getTopProductCategoryId();
    Integer viewIndexInteger = productSearchOptions.getViewIndex();
    if (viewIndexInteger != null) {
        viewIndex = viewIndexInteger.intValue();
    }
    Integer viewSizeInteger = productSearchOptions.getViewSize();
    if (viewSizeInteger != null) {
        viewSize = viewSizeInteger.intValue();
    }
    Integer previousViewSizeInteger = productSearchOptions.getPreviousViewSize();
    if (previousViewSizeInteger != null) {
        previousViewSize = previousViewSizeInteger.intValue();
    }
    String pag = productSearchOptions.getPaging();
    paging = pag;
    lowIndex = viewIndex * viewSize;
    highIndex = (viewIndex + 1) * viewSize;
    // ========== Do the actual search
    List<String> productIds = new LinkedList<>();
    String visitId = VisitHandler.getVisitId(session);
    List<ProductSearchConstraint> productSearchConstraintList = ProductSearchOptions.getConstraintList(session);
    String noConditionFind = (String) requestParams.get("noConditionFind");
    if (UtilValidate.isEmpty(noConditionFind)) {
        noConditionFind = EntityUtilProperties.getPropertyValue("widget", "widget.defaultNoConditionFind", delegator);
    }
    // if noConditionFind to Y then find without conditions otherwise search according to constraints.
    if ("Y".equals(noConditionFind) || UtilValidate.isNotEmpty(productSearchConstraintList)) {
        // if the search options have changed since the last search, put at the beginning of the options history list
        checkSaveSearchOptionsHistory(session);
        int addOnTopTotalListSize = 0;
        int addOnTopListSize = 0;
        List<GenericValue> addOnTopProductCategoryMembers;
        if (UtilValidate.isNotEmpty(addOnTopProdCategoryId)) {
            // always include the members of the addOnTopProdCategoryId
            Timestamp now = UtilDateTime.nowTimestamp();
            List<EntityCondition> addOnTopProdCondList = new LinkedList<>();
            addOnTopProdCondList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN, now)));
            addOnTopProdCondList.add(EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN, now));
            addOnTopProdCondList.add(EntityCondition.makeCondition("productCategoryId", EntityOperator.EQUALS, addOnTopProdCategoryId));
            EntityQuery eq = EntityQuery.use(delegator).select(UtilMisc.toSet("productId", "sequenceNum")).from("ProductCategoryMember").where(addOnTopProdCondList).orderBy("sequenceNum").cursorScrollInsensitive().distinct().maxRows(highIndex);
            try (EntityListIterator pli = eq.queryIterator()) {
                addOnTopProductCategoryMembers = pli.getPartialList(lowIndex, viewSize);
                addOnTopListSize = addOnTopProductCategoryMembers.size();
                for (GenericValue alwaysAddProductCategoryMember : addOnTopProductCategoryMembers) {
                    productIds.add(alwaysAddProductCategoryMember.getString("productId"));
                }
                addOnTopTotalListSize = pli.getResultsSizeAfterPartialList();
                listSize = listSize + addOnTopTotalListSize;
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
            }
        }
        // setup resultOffset and maxResults, noting that resultOffset is 1 based, not zero based as these numbers
        int resultOffsetInt = lowIndex - addOnTopTotalListSize + 1;
        if (resultOffsetInt < 1) {
            resultOffsetInt = 1;
        }
        int maxResultsInt = viewSize - addOnTopListSize;
        Integer resultOffset = Integer.valueOf(resultOffsetInt);
        Integer maxResults = Integer.valueOf(maxResultsInt);
        ResultSortOrder resultSortOrder = ProductSearchOptions.getResultSortOrder(request);
        ProductSearchContext productSearchContext = new ProductSearchContext(delegator, visitId);
        if (UtilValidate.isNotEmpty(productSearchConstraintList)) {
            productSearchContext.addProductSearchConstraints(productSearchConstraintList);
        }
        productSearchContext.setResultSortOrder(resultSortOrder);
        productSearchContext.setResultOffset(resultOffset);
        productSearchContext.setMaxResults(maxResults);
        if (UtilValidate.isNotEmpty(keywordTypeIds)) {
            productSearchContext.keywordTypeIds = keywordTypeIds;
        } else {
            productSearchContext.keywordTypeIds = UtilMisc.toList("KWT_KEYWORD");
        }
        if (UtilValidate.isNotEmpty(statusId)) {
            productSearchContext.statusId = statusId;
        }
        List<String> foundProductIds = productSearchContext.doSearch();
        if (maxResultsInt > 0) {
            productIds.addAll(foundProductIds);
        }
        Integer totalResults = productSearchContext.getTotalResults();
        if (totalResults != null) {
            listSize = listSize + totalResults.intValue();
        }
    }
    if (listSize < highIndex) {
        highIndex = listSize;
    }
    // ========== Setup other display info
    List<String> searchConstraintStrings = searchGetConstraintStrings(false, session, delegator);
    String searchSortOrderString = searchGetSortOrderString(false, request);
    // ========== populate the result Map
    Map<String, Object> result = new HashMap<>();
    result.put("productIds", productIds);
    result.put("viewIndex", Integer.valueOf(viewIndex));
    result.put("viewSize", Integer.valueOf(viewSize));
    result.put("listSize", Integer.valueOf(listSize));
    result.put("lowIndex", Integer.valueOf(lowIndex));
    result.put("highIndex", Integer.valueOf(highIndex));
    result.put("paging", paging);
    result.put("previousViewSize", previousViewSize);
    result.put("searchConstraintStrings", searchConstraintStrings);
    result.put("searchSortOrderString", searchSortOrderString);
    result.put("noConditionFind", noConditionFind);
    return result;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ResultSortOrder(org.apache.ofbiz.product.product.ProductSearch.ResultSortOrder) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) Timestamp(java.sql.Timestamp) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint) FeatureConstraint(org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) LinkedList(java.util.LinkedList) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ProductSearchContext(org.apache.ofbiz.product.product.ProductSearch.ProductSearchContext) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Example 98 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.

the class ProductSearchSession method getCountForListPriceRange.

/**
 * This method returns count of products within a given price range, the constraint being
 * applied on current ProductSearchConstraint list in session.
 * @param priceLow The low price.
 * @param priceHigh The high price.
 * @param session Current session.
 * @param delegator The delegator object.
 * @return The long value of count of products.
 */
public static long getCountForListPriceRange(BigDecimal priceLow, BigDecimal priceHigh, HttpSession session, Delegator delegator) {
    String visitId = VisitHandler.getVisitId(session);
    ProductSearchContext productSearchContext = new ProductSearchContext(delegator, visitId);
    List<ProductSearchConstraint> productSearchConstraintList = ProductSearchOptions.getConstraintList(session);
    if (UtilValidate.isNotEmpty(productSearchConstraintList)) {
        productSearchContext.addProductSearchConstraints(productSearchConstraintList);
    }
    productSearchContext.finishKeywordConstraints();
    productSearchContext.finishCategoryAndFeatureConstraints();
    DynamicViewEntity dynamicViewEntity = productSearchContext.dynamicViewEntity;
    List<EntityCondition> entityConditionList = productSearchContext.entityConditionList;
    List<String> fieldsToSelect = new LinkedList<>();
    dynamicViewEntity.addMemberEntity("PPC", "ProductPrice");
    dynamicViewEntity.addAlias("PPC", "ppcProductPriceTypeId", "productPriceTypeId", null, null, null, null);
    dynamicViewEntity.addAlias("PPC", "ppcFromDate", "fromDate", null, null, null, null);
    dynamicViewEntity.addAlias("PPC", "ppcThruDate", "thruDate", null, null, null, null);
    dynamicViewEntity.addAlias("PPC", "ppcPrice", "price", null, null, null, null);
    dynamicViewEntity.addAlias("PPC", "priceRangeCount", "productId", null, null, null, "count-distinct");
    dynamicViewEntity.addViewLink("PROD", "PPC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("productId"));
    fieldsToSelect.add("priceRangeCount");
    entityConditionList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("ppcThruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("ppcThruDate", EntityOperator.GREATER_THAN, UtilDateTime.nowTimestamp())));
    entityConditionList.add(EntityCondition.makeCondition("ppcFromDate", EntityOperator.LESS_THAN, UtilDateTime.nowTimestamp()));
    entityConditionList.add(EntityCondition.makeCondition("ppcPrice", EntityOperator.GREATER_THAN_EQUAL_TO, priceLow));
    entityConditionList.add(EntityCondition.makeCondition("ppcPrice", EntityOperator.LESS_THAN_EQUAL_TO, priceHigh));
    entityConditionList.add(EntityCondition.makeCondition("ppcProductPriceTypeId", EntityOperator.EQUALS, "LIST_PRICE"));
    Long priceRangeCount = Long.valueOf(0);
    EntityQuery eq = EntityQuery.use(delegator).select(UtilMisc.toSet(fieldsToSelect)).from(dynamicViewEntity).where(entityConditionList).orderBy(productSearchContext.orderByList).cursorScrollInsensitive();
    try (EntityListIterator eli = eq.queryIterator()) {
        GenericValue searchResult = null;
        while ((searchResult = eli.next()) != null) {
            priceRangeCount = searchResult.getLong("priceRangeCount");
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, "Error in product search", module);
    }
    return priceRangeCount;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) LinkedList(java.util.LinkedList) DynamicViewEntity(org.apache.ofbiz.entity.model.DynamicViewEntity) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ProductSearchContext(org.apache.ofbiz.product.product.ProductSearch.ProductSearchContext) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Example 99 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.

the class ProductWorker method getProductFeaturesByApplTypeId.

public static List<GenericValue> getProductFeaturesByApplTypeId(GenericValue product, String productFeatureApplTypeId) {
    if (product == null) {
        return null;
    }
    List<GenericValue> features = null;
    try {
        List<GenericValue> productAppls;
        List<EntityCondition> condList = UtilMisc.toList(EntityCondition.makeCondition("productId", product.getString("productId")), EntityUtil.getFilterByDateExpr());
        if (productFeatureApplTypeId != null) {
            condList.add(EntityCondition.makeCondition("productFeatureApplTypeId", productFeatureApplTypeId));
        }
        EntityCondition cond = EntityCondition.makeCondition(condList);
        productAppls = product.getDelegator().findList("ProductFeatureAppl", cond, null, null, null, false);
        features = EntityUtil.getRelated("ProductFeature", null, productAppls, false);
        features = EntityUtil.orderBy(features, UtilMisc.toList("description"));
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        features = new LinkedList<>();
    }
    return features;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList)

Example 100 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.

the class InventoryServices method getProductInventoryAndFacilitySummary.

public static Map<String, Object> getProductInventoryAndFacilitySummary(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Timestamp checkTime = (Timestamp) context.get("checkTime");
    String facilityId = (String) context.get("facilityId");
    String productId = (String) context.get("productId");
    BigDecimal minimumStock = (BigDecimal) context.get("minimumStock");
    String statusId = (String) context.get("statusId");
    Map<String, Object> result = new HashMap<>();
    Map<String, Object> resultOutput = new HashMap<>();
    Map<String, String> contextInput = UtilMisc.toMap("productId", productId, "facilityId", facilityId, "statusId", statusId);
    GenericValue product = null;
    try {
        product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    if (product != null) {
        if (EntityTypeUtil.hasParentType(delegator, "ProductType", "productTypeId", product.getString("productTypeId"), "parentTypeId", "MARKETING_PKG")) {
            try {
                resultOutput = dispatcher.runSync("getMktgPackagesAvailable", contextInput);
            } catch (GenericServiceException e) {
                Debug.logError(e, module);
            }
        } else {
            try {
                resultOutput = dispatcher.runSync("getInventoryAvailableByFacility", contextInput);
            } catch (GenericServiceException e) {
                Debug.logError(e, module);
            }
        }
        // filter for quantities
        minimumStock = minimumStock != null ? minimumStock : BigDecimal.ZERO;
        BigDecimal quantityOnHandTotal = BigDecimal.ZERO;
        if (resultOutput.get("quantityOnHandTotal") != null) {
            quantityOnHandTotal = (BigDecimal) resultOutput.get("quantityOnHandTotal");
        }
        BigDecimal offsetQOHQtyAvailable = quantityOnHandTotal.subtract(minimumStock);
        BigDecimal availableToPromiseTotal = BigDecimal.ZERO;
        if (resultOutput.get("availableToPromiseTotal") != null) {
            availableToPromiseTotal = (BigDecimal) resultOutput.get("availableToPromiseTotal");
        }
        BigDecimal offsetATPQtyAvailable = availableToPromiseTotal.subtract(minimumStock);
        BigDecimal quantityOnOrder = InventoryWorker.getOutstandingPurchasedQuantity(productId, delegator);
        result.put("totalQuantityOnHand", resultOutput.get("quantityOnHandTotal"));
        result.put("totalAvailableToPromise", resultOutput.get("availableToPromiseTotal"));
        result.put("quantityOnOrder", quantityOnOrder);
        result.put("quantityUomId", product.getString("quantityUomId"));
        result.put("offsetQOHQtyAvailable", offsetQOHQtyAvailable);
        result.put("offsetATPQtyAvailable", offsetATPQtyAvailable);
    }
    List<GenericValue> productPrices = null;
    try {
        productPrices = EntityQuery.use(delegator).from("ProductPrice").where("productId", productId).orderBy("-fromDate").cache(true).queryList();
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    // change this for product price
    if (productPrices != null) {
        for (GenericValue onePrice : productPrices) {
            if ("DEFAULT_PRICE".equals(onePrice.getString("productPriceTypeId"))) {
                // defaultPrice
                result.put("defaultPrice", onePrice.getBigDecimal("price"));
            } else if ("WHOLESALE_PRICE".equals(onePrice.getString("productPriceTypeId"))) {
                // 
                result.put("wholeSalePrice", onePrice.getBigDecimal("price"));
            } else if ("LIST_PRICE".equals(onePrice.getString("productPriceTypeId"))) {
                // listPrice
                result.put("listPrice", onePrice.getBigDecimal("price"));
            } else {
                result.put("defaultPrice", onePrice.getBigDecimal("price"));
                result.put("listPrice", onePrice.getBigDecimal("price"));
                result.put("wholeSalePrice", onePrice.getBigDecimal("price"));
            }
        }
    }
    DynamicViewEntity salesUsageViewEntity = new DynamicViewEntity();
    DynamicViewEntity productionUsageViewEntity = new DynamicViewEntity();
    if (!UtilValidate.isEmpty(checkTime)) {
        // Construct a dynamic view entity to search against for sales usage quantities
        salesUsageViewEntity.addMemberEntity("OI", "OrderItem");
        salesUsageViewEntity.addMemberEntity("OH", "OrderHeader");
        salesUsageViewEntity.addMemberEntity("ItIss", "ItemIssuance");
        salesUsageViewEntity.addMemberEntity("InvIt", "InventoryItem");
        salesUsageViewEntity.addViewLink("OI", "OH", Boolean.valueOf(false), ModelKeyMap.makeKeyMapList("orderId"));
        salesUsageViewEntity.addViewLink("OI", "ItIss", Boolean.valueOf(false), ModelKeyMap.makeKeyMapList("orderId", "orderId", "orderItemSeqId", "orderItemSeqId"));
        salesUsageViewEntity.addViewLink("ItIss", "InvIt", Boolean.valueOf(false), ModelKeyMap.makeKeyMapList("inventoryItemId"));
        salesUsageViewEntity.addAlias("OI", "productId");
        salesUsageViewEntity.addAlias("OH", "statusId");
        salesUsageViewEntity.addAlias("OH", "orderTypeId");
        salesUsageViewEntity.addAlias("OH", "orderDate");
        salesUsageViewEntity.addAlias("ItIss", "inventoryItemId");
        salesUsageViewEntity.addAlias("ItIss", "quantity");
        salesUsageViewEntity.addAlias("InvIt", "facilityId");
        // Construct a dynamic view entity to search against for production usage quantities
        productionUsageViewEntity.addMemberEntity("WEIA", "WorkEffortInventoryAssign");
        productionUsageViewEntity.addMemberEntity("WE", "WorkEffort");
        productionUsageViewEntity.addMemberEntity("II", "InventoryItem");
        productionUsageViewEntity.addViewLink("WEIA", "WE", Boolean.valueOf(false), ModelKeyMap.makeKeyMapList("workEffortId"));
        productionUsageViewEntity.addViewLink("WEIA", "II", Boolean.valueOf(false), ModelKeyMap.makeKeyMapList("inventoryItemId"));
        productionUsageViewEntity.addAlias("WEIA", "quantity");
        productionUsageViewEntity.addAlias("WE", "actualCompletionDate");
        productionUsageViewEntity.addAlias("WE", "workEffortTypeId");
        productionUsageViewEntity.addAlias("II", "facilityId");
        productionUsageViewEntity.addAlias("II", "productId");
        // Make a query against the sales usage view entity
        EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("facilityId", EntityOperator.EQUALS, facilityId), EntityCondition.makeCondition("productId", EntityOperator.EQUALS, productId), EntityCondition.makeCondition("statusId", EntityOperator.IN, UtilMisc.toList("ORDER_COMPLETED", "ORDER_APPROVED", "ORDER_HELD")), EntityCondition.makeCondition("orderTypeId", EntityOperator.EQUALS, "SALES_ORDER"), EntityCondition.makeCondition("orderDate", EntityOperator.GREATER_THAN_EQUAL_TO, checkTime)), EntityOperator.AND);
        try (EntityListIterator salesUsageIt = EntityQuery.use(delegator).from(salesUsageViewEntity).where(cond).queryIterator()) {
            // Sum the sales usage quantities found
            BigDecimal salesUsageQuantity = BigDecimal.ZERO;
            GenericValue salesUsageItem = null;
            while ((salesUsageItem = salesUsageIt.next()) != null) {
                if (salesUsageItem.get("quantity") != null) {
                    salesUsageQuantity = salesUsageQuantity.add(salesUsageItem.getBigDecimal("quantity"));
                }
            }
            // Make a query against the production usage view entity
            EntityCondition conditions = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("facilityId", EntityOperator.EQUALS, facilityId), EntityCondition.makeCondition("productId", EntityOperator.EQUALS, productId), EntityCondition.makeCondition("workEffortTypeId", EntityOperator.EQUALS, "PROD_ORDER_TASK"), EntityCondition.makeCondition("actualCompletionDate", EntityOperator.GREATER_THAN_EQUAL_TO, checkTime)), EntityOperator.AND);
            try (EntityListIterator productionUsageIt = EntityQuery.use(delegator).from(productionUsageViewEntity).where(conditions).queryIterator()) {
                // Sum the production usage quantities found
                BigDecimal productionUsageQuantity = BigDecimal.ZERO;
                GenericValue productionUsageItem = null;
                while ((productionUsageItem = productionUsageIt.next()) != null) {
                    if (productionUsageItem.get("quantity") != null) {
                        productionUsageQuantity = productionUsageQuantity.add(productionUsageItem.getBigDecimal("quantity"));
                    }
                }
                result.put("usageQuantity", salesUsageQuantity.add(productionUsageQuantity));
            } catch (GeneralException e) {
                Debug.logError(e, module);
                return ServiceUtil.returnError(e.getMessage());
            }
        } catch (GeneralException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
    }
    return result;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) HashMap(java.util.HashMap) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) DynamicViewEntity(org.apache.ofbiz.entity.model.DynamicViewEntity) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Aggregations

EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)118 GenericValue (org.apache.ofbiz.entity.GenericValue)96 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)78 Delegator (org.apache.ofbiz.entity.Delegator)60 LinkedList (java.util.LinkedList)58 Locale (java.util.Locale)43 Timestamp (java.sql.Timestamp)40 HashMap (java.util.HashMap)32 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)29 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)26 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)24 Map (java.util.Map)22 BigDecimal (java.math.BigDecimal)21 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)18 EntityQuery (org.apache.ofbiz.entity.util.EntityQuery)14 GeneralException (org.apache.ofbiz.base.util.GeneralException)12 ArrayList (java.util.ArrayList)11 DynamicViewEntity (org.apache.ofbiz.entity.model.DynamicViewEntity)9 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)8 HashSet (java.util.HashSet)7