Search in sources :

Example 41 with Delegator

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

the class ProductServices method quickAddVariant.

public static Map<String, Object> quickAddVariant(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    Map<String, Object> result = new HashMap<>();
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    String productId = (String) context.get("productId");
    String variantProductId = (String) context.get("productVariantId");
    String productFeatureIds = (String) context.get("productFeatureIds");
    Long prodAssocSeqNum = (Long) context.get("sequenceNum");
    try {
        // read the product, duplicate it with the given id
        GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
        if (product == null) {
            Map<String, String> messageMap = UtilMisc.toMap("productId", productId);
            errMsg = UtilProperties.getMessage(resourceError, "productservices.product_not_found_with_ID", messageMap, locale);
            result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
            result.put(ModelService.ERROR_MESSAGE, errMsg);
            return result;
        }
        // check if product exists
        GenericValue variantProduct = EntityQuery.use(delegator).from("Product").where("productId", variantProductId).queryOne();
        boolean variantProductExists = (variantProduct != null);
        if (variantProduct == null) {
            // if product does not exist
            variantProduct = GenericValue.create(product);
            variantProduct.set("productId", variantProductId);
            variantProduct.set("isVirtual", "N");
            variantProduct.set("isVariant", "Y");
            variantProduct.set("primaryProductCategoryId", null);
            // create new
            variantProduct.create();
        } else {
            // if product does exist
            variantProduct.set("isVirtual", "N");
            variantProduct.set("isVariant", "Y");
            variantProduct.set("primaryProductCategoryId", null);
            // update entry
            variantProduct.store();
        }
        if (variantProductExists) {
            // Since the variant product is already a variant, first of all we remove the old features
            // and the associations of type PRODUCT_VARIANT: a given product can be a variant of only one product.
            delegator.removeByAnd("ProductAssoc", UtilMisc.toMap("productIdTo", variantProductId, "productAssocTypeId", "PRODUCT_VARIANT"));
            delegator.removeByAnd("ProductFeatureAppl", UtilMisc.toMap("productId", variantProductId, "productFeatureApplTypeId", "STANDARD_FEATURE"));
        }
        // add an association from productId to variantProductId of the PRODUCT_VARIANT
        Map<String, Object> productAssocMap = UtilMisc.toMap("productId", productId, "productIdTo", variantProductId, "productAssocTypeId", "PRODUCT_VARIANT", "fromDate", UtilDateTime.nowTimestamp());
        if (prodAssocSeqNum != null) {
            productAssocMap.put("sequenceNum", prodAssocSeqNum);
        }
        GenericValue productAssoc = delegator.makeValue("ProductAssoc", productAssocMap);
        productAssoc.create();
        // add the selected standard features to the new product given the productFeatureIds
        java.util.StringTokenizer st = new java.util.StringTokenizer(productFeatureIds, "|");
        while (st.hasMoreTokens()) {
            String productFeatureId = st.nextToken();
            GenericValue productFeature = EntityQuery.use(delegator).from("ProductFeature").where("productFeatureId", productFeatureId).queryOne();
            GenericValue productFeatureAppl = delegator.makeValue("ProductFeatureAppl", UtilMisc.toMap("productId", variantProductId, "productFeatureId", productFeatureId, "productFeatureApplTypeId", "STANDARD_FEATURE", "fromDate", UtilDateTime.nowTimestamp()));
            // set the default seq num if it's there...
            if (productFeature != null) {
                productFeatureAppl.set("sequenceNum", productFeature.get("defaultSequenceNum"));
            }
            productFeatureAppl.create();
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, "Entity error creating quick add variant data", module);
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productservices.entity_error_quick_add_variant_data", messageMap, locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    result.put("productVariantId", variantProductId);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 42 with Delegator

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

the class ProductServices method prodFindAssociatedByType.

/**
 * Finds associated products by product ID and association ID.
 */
public static Map<String, Object> prodFindAssociatedByType(DispatchContext dctx, Map<String, ? extends Object> context) {
    // String type -- Type of association (ie PRODUCT_UPGRADE, PRODUCT_COMPLEMENT, PRODUCT_VARIANT)
    Delegator delegator = dctx.getDelegator();
    Map<String, Object> result = new HashMap<>();
    String productId = (String) context.get("productId");
    String productIdTo = (String) context.get("productIdTo");
    String type = (String) context.get("type");
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    Boolean cvaBool = (Boolean) context.get("checkViewAllow");
    boolean checkViewAllow = (cvaBool == null ? false : cvaBool);
    String prodCatalogId = (String) context.get("prodCatalogId");
    Boolean bidirectional = (Boolean) context.get("bidirectional");
    bidirectional = bidirectional == null ? Boolean.FALSE : bidirectional;
    Boolean sortDescending = (Boolean) context.get("sortDescending");
    sortDescending = sortDescending == null ? Boolean.FALSE : sortDescending;
    if (productId == null && productIdTo == null) {
        errMsg = UtilProperties.getMessage(resourceError, "productservices.both_productId_and_productIdTo_cannot_be_null", locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    if (productId != null && productIdTo != null) {
        errMsg = UtilProperties.getMessage(resourceError, "productservices.both_productId_and_productIdTo_cannot_be_defined", locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    productId = productId == null ? productIdTo : productId;
    GenericValue product = null;
    try {
        product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.getMessage());
        errMsg = UtilProperties.getMessage(resourceError, "productservices.problems_reading_product_entity", messageMap, locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    if (product == null) {
        errMsg = UtilProperties.getMessage(resourceError, "productservices.problems_getting_product_entity", locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    try {
        List<GenericValue> productAssocs = null;
        List<String> orderBy = new LinkedList<>();
        if (sortDescending) {
            orderBy.add("sequenceNum DESC");
        } else {
            orderBy.add("sequenceNum");
        }
        if (bidirectional) {
            EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productId", productId), EntityCondition.makeCondition("productIdTo", productId)), EntityJoinOperator.OR);
            productAssocs = EntityQuery.use(delegator).from("ProductAssoc").where(EntityCondition.makeCondition(cond, EntityCondition.makeCondition("productAssocTypeId", type))).orderBy(orderBy).cache(true).queryList();
        } else {
            if (productIdTo == null) {
                productAssocs = product.getRelated("MainProductAssoc", UtilMisc.toMap("productAssocTypeId", type), orderBy, true);
            } else {
                productAssocs = product.getRelated("AssocProductAssoc", UtilMisc.toMap("productAssocTypeId", type), orderBy, true);
            }
        }
        // filter the list by date
        productAssocs = EntityUtil.filterByDate(productAssocs);
        // first check to see if there is a view allow category and if these products are in it...
        if (checkViewAllow && prodCatalogId != null && UtilValidate.isNotEmpty(productAssocs)) {
            String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, prodCatalogId);
            if (viewProductCategoryId != null) {
                if (productIdTo == null) {
                    productAssocs = CategoryWorker.filterProductsInCategory(delegator, productAssocs, viewProductCategoryId, "productIdTo");
                } else {
                    productAssocs = CategoryWorker.filterProductsInCategory(delegator, productAssocs, viewProductCategoryId, "productId");
                }
            }
        }
        result.put("assocProducts", productAssocs);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.getMessage());
        errMsg = UtilProperties.getMessage(resourceError, "productservices.problems_product_association_relation_error", messageMap, locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 43 with Delegator

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

the class ProductServices method prodGetFeatures.

/**
 * Gets the product features of a product.
 */
public static Map<String, Object> prodGetFeatures(DispatchContext dctx, Map<String, ? extends Object> context) {
    // String type          -- Type of feature (STANDARD_FEATURE, SELECTABLE_FEATURE)
    // String distinct      -- Distinct feature (SIZE, COLOR)
    Delegator delegator = dctx.getDelegator();
    Map<String, Object> result = new HashMap<>();
    String productId = (String) context.get("productId");
    String distinct = (String) context.get("distinct");
    String type = (String) context.get("type");
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    List<GenericValue> features = null;
    try {
        Map<String, String> fields = UtilMisc.toMap("productId", productId);
        if (distinct != null) {
            fields.put("productFeatureTypeId", distinct);
        }
        if (type != null) {
            fields.put("productFeatureApplTypeId", type);
        }
        features = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where(fields).orderBy("sequenceNum", "productFeatureTypeId").cache(true).queryList();
        result.put("productFeatures", features);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productservices.problem_reading_product_feature_entity", messageMap, locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
    }
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 44 with Delegator

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

the class ProductWorker method isAllowedToAddress.

private static boolean isAllowedToAddress(GenericValue product, GenericValue postalAddress, String productGeoPrefix) {
    if (product != null && postalAddress != null) {
        Delegator delegator = product.getDelegator();
        List<GenericValue> productGeos = null;
        try {
            productGeos = product.getRelated("ProductGeo", null, null, false);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        List<GenericValue> excludeGeos = EntityUtil.filterByAnd(productGeos, UtilMisc.toMap("productGeoEnumId", productGeoPrefix + "EXCLUDE"));
        List<GenericValue> includeGeos = EntityUtil.filterByAnd(productGeos, UtilMisc.toMap("productGeoEnumId", productGeoPrefix + "INCLUDE"));
        if (UtilValidate.isEmpty(excludeGeos) && UtilValidate.isEmpty(includeGeos)) {
            // If no GEOs are configured the default is TRUE
            return true;
        }
        // exclusion
        for (GenericValue productGeo : excludeGeos) {
            List<GenericValue> excludeGeoGroup = GeoWorker.expandGeoGroup(productGeo.getString("geoId"), delegator);
            if (GeoWorker.containsGeo(excludeGeoGroup, postalAddress.getString("countryGeoId"), delegator) || GeoWorker.containsGeo(excludeGeoGroup, postalAddress.getString("stateProvinceGeoId"), delegator) || GeoWorker.containsGeo(excludeGeoGroup, postalAddress.getString("postalCodeGeoId"), delegator)) {
                return false;
            }
        }
        if (UtilValidate.isEmpty(includeGeos)) {
            // If no GEOs are configured the default is TRUE
            return true;
        }
        // inclusion
        for (GenericValue productGeo : includeGeos) {
            List<GenericValue> includeGeoGroup = GeoWorker.expandGeoGroup(productGeo.getString("geoId"), delegator);
            if (GeoWorker.containsGeo(includeGeoGroup, postalAddress.getString("countryGeoId"), delegator) || GeoWorker.containsGeo(includeGeoGroup, postalAddress.getString("stateProvinceGeoId"), delegator) || GeoWorker.containsGeo(includeGeoGroup, postalAddress.getString("postalCodeGeoId"), delegator)) {
                return true;
            }
        }
    } else {
        throw new IllegalArgumentException("product and postalAddress cannot be null.");
    }
    return false;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 45 with Delegator

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

the class ProductWorker method getSelectableProductFeaturesByTypesAndSeq.

/**
 * @param product
 * @return list featureType and related featuresIds, description and feature price for this product ordered by type and sequence
 */
public static List<List<Map<String, String>>> getSelectableProductFeaturesByTypesAndSeq(GenericValue product) {
    if (product == null) {
        return null;
    }
    List<List<Map<String, String>>> featureTypeFeatures = new LinkedList<>();
    try {
        Delegator delegator = product.getDelegator();
        List<GenericValue> featuresSorted = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", product.getString("productId"), "productFeatureApplTypeId", "SELECTABLE_FEATURE").orderBy("productFeatureTypeId", "sequenceNum").cache(true).queryList();
        String oldType = null;
        List<Map<String, String>> featureList = new LinkedList<>();
        for (GenericValue productFeatureAppl : featuresSorted) {
            if (oldType == null || !oldType.equals(productFeatureAppl.getString("productFeatureTypeId"))) {
                // use first entry for type and description
                if (oldType != null) {
                    featureTypeFeatures.add(featureList);
                    featureList = new LinkedList<>();
                }
                GenericValue productFeatureType = EntityQuery.use(delegator).from("ProductFeatureType").where("productFeatureTypeId", productFeatureAppl.getString("productFeatureTypeId")).queryOne();
                featureList.add(UtilMisc.<String, String>toMap("productFeatureTypeId", productFeatureAppl.getString("productFeatureTypeId"), "description", productFeatureType.getString("description")));
                oldType = productFeatureAppl.getString("productFeatureTypeId");
            }
            // fill other entries with featureId, description and default price and currency
            Map<String, String> featureData = UtilMisc.toMap("productFeatureId", productFeatureAppl.getString("productFeatureId"));
            if (UtilValidate.isNotEmpty(productFeatureAppl.get("description"))) {
                featureData.put("description", productFeatureAppl.getString("description"));
            } else {
                featureData.put("description", productFeatureAppl.getString("productFeatureId"));
            }
            List<GenericValue> productFeaturePrices = EntityQuery.use(delegator).from("ProductFeaturePrice").where("productFeatureId", productFeatureAppl.getString("productFeatureId"), "productPriceTypeId", "DEFAULT_PRICE").filterByDate().queryList();
            if (UtilValidate.isNotEmpty(productFeaturePrices)) {
                GenericValue productFeaturePrice = productFeaturePrices.get(0);
                if (UtilValidate.isNotEmpty(productFeaturePrice.get("price"))) {
                    featureData.put("price", productFeaturePrice.getBigDecimal("price").toString());
                    featureData.put("currencyUomId", productFeaturePrice.getString("currencyUomId"));
                }
            }
            featureList.add(featureData);
        }
        if (oldType != null) {
            // last map
            featureTypeFeatures.add(featureList);
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    return featureTypeFeatures;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Aggregations

Delegator (org.apache.ofbiz.entity.Delegator)869 GenericValue (org.apache.ofbiz.entity.GenericValue)721 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)611 Locale (java.util.Locale)485 HashMap (java.util.HashMap)328 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)324 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)278 BigDecimal (java.math.BigDecimal)205 LinkedList (java.util.LinkedList)166 Timestamp (java.sql.Timestamp)163 GeneralException (org.apache.ofbiz.base.util.GeneralException)130 IOException (java.io.IOException)117 Map (java.util.Map)113 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)61 Security (org.apache.ofbiz.security.Security)60 HttpSession (javax.servlet.http.HttpSession)59 Properties (java.util.Properties)37 UtilProperties (org.apache.ofbiz.base.util.UtilProperties)37 EntityUtilProperties (org.apache.ofbiz.entity.util.EntityUtilProperties)35 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)33