Search in sources :

Example 6 with DynamicViewEntity

use of org.apache.ofbiz.entity.model.DynamicViewEntity 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 7 with DynamicViewEntity

use of org.apache.ofbiz.entity.model.DynamicViewEntity 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 8 with DynamicViewEntity

use of org.apache.ofbiz.entity.model.DynamicViewEntity 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)

Example 9 with DynamicViewEntity

use of org.apache.ofbiz.entity.model.DynamicViewEntity in project ofbiz-framework by apache.

the class ProductUtilServices method removeDuplicateOpenEndedCategoryMembers.

public static Map<String, Object> removeDuplicateOpenEndedCategoryMembers(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    DynamicViewEntity dve = new DynamicViewEntity();
    dve.addMemberEntity("PCM", "ProductCategoryMember");
    dve.addAlias("PCM", "productId", null, null, null, Boolean.TRUE, null);
    dve.addAlias("PCM", "productCategoryId", null, null, null, Boolean.TRUE, null);
    dve.addAlias("PCM", "fromDate", null, null, null, null, null);
    dve.addAlias("PCM", "thruDate", null, null, null, null, null);
    dve.addAlias("PCM", "productIdCount", "productId", null, null, null, "count");
    EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN, nowTimestamp), EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null)), EntityOperator.AND);
    EntityCondition havingCond = EntityCondition.makeCondition("productIdCount", EntityOperator.GREATER_THAN, Long.valueOf(1));
    try (EntityListIterator eli = EntityQuery.use(delegator).select("productId", "productCategoryId", "productIdCount").from(dve).where(condition).having(havingCond).queryIterator()) {
        GenericValue pcm = null;
        int numSoFar = 0;
        while ((pcm = eli.next()) != null) {
            List<GenericValue> productCategoryMemberList = EntityQuery.use(delegator).from("ProductCategoryMember").where("productId", pcm.get("productId"), "productCategoryId", pcm.get("productCategoryId")).queryList();
            if (productCategoryMemberList.size() > 1) {
                // remove all except the first...
                productCategoryMemberList.remove(0);
                for (GenericValue productCategoryMember : productCategoryMemberList) {
                    productCategoryMember.remove();
                }
                numSoFar++;
                if (numSoFar % 500 == 0) {
                    Debug.logInfo("Removed category members for " + numSoFar + " products with duplicate category members.", module);
                }
            }
        }
        Debug.logInfo("Completed - Removed category members for " + numSoFar + " products with duplicate category members.", module);
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_removeDuplicateOpenEndedCategoryMembers", messageMap, locale);
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(errMsg);
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) DynamicViewEntity(org.apache.ofbiz.entity.model.DynamicViewEntity) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) Timestamp(java.sql.Timestamp) HashMap(java.util.HashMap) Map(java.util.Map) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap)

Aggregations

GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)9 GenericValue (org.apache.ofbiz.entity.GenericValue)9 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)9 DynamicViewEntity (org.apache.ofbiz.entity.model.DynamicViewEntity)9 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)8 Delegator (org.apache.ofbiz.entity.Delegator)6 Locale (java.util.Locale)5 EntityQuery (org.apache.ofbiz.entity.util.EntityQuery)5 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)4 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)4 Timestamp (java.sql.Timestamp)3 Map (java.util.Map)3 ProductSearchConstraint (org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)3 ProductSearchContext (org.apache.ofbiz.product.product.ProductSearch.ProductSearchContext)3 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)3 GeneralException (org.apache.ofbiz.base.util.GeneralException)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1