Search in sources :

Example 36 with GenericEntityException

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

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

the class ProductWorker method getGwpAlternativeOptionName.

/**
 *  Get the name to show to the customer for GWP alternative options.
 *  If the alternative is a variant, find the distinguishing features and show those instead of the name; if it is not a variant then show the PRODUCT_NAME content.
 */
public static String getGwpAlternativeOptionName(LocalDispatcher dispatcher, Delegator delegator, String alternativeOptionProductId, Locale locale) {
    try {
        GenericValue alternativeOptionProduct = EntityQuery.use(delegator).from("Product").where("productId", alternativeOptionProductId).cache().queryOne();
        if (alternativeOptionProduct != null) {
            if ("Y".equals(alternativeOptionProduct.getString("isVariant"))) {
                Set<GenericValue> distFeatures = getVariantDistinguishingFeatures(alternativeOptionProduct);
                if (UtilValidate.isNotEmpty(distFeatures)) {
                    StringBuilder nameBuf = new StringBuilder();
                    for (GenericValue productFeature : distFeatures) {
                        if (nameBuf.length() > 0) {
                            nameBuf.append(", ");
                        }
                        GenericValue productFeatureType = productFeature.getRelatedOne("ProductFeatureType", true);
                        if (productFeatureType != null) {
                            nameBuf.append(productFeatureType.get("description", locale));
                            nameBuf.append(":");
                        }
                        nameBuf.append(productFeature.get("description", locale));
                    }
                    return nameBuf.toString();
                }
            }
            // got to here, default to PRODUCT_NAME
            String alternativeProductName = ProductContentWrapper.getProductContentAsText(alternativeOptionProduct, "PRODUCT_NAME", locale, dispatcher, "html");
            return alternativeProductName;
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    // finally fall back to the ID in square braces
    return "[" + alternativeOptionProductId + "]";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 38 with GenericEntityException

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

the class ProductWorker method getParentProduct.

// get parent product
public static GenericValue getParentProduct(String productId, Delegator delegator) {
    GenericValue _parentProduct = null;
    if (productId == null) {
        Debug.logWarning("Bad product id", module);
    }
    try {
        List<GenericValue> virtualProductAssocs = EntityQuery.use(delegator).from("ProductAssoc").where("productIdTo", productId, "productAssocTypeId", "PRODUCT_VARIANT").orderBy("-fromDate").cache(true).filterByDate().queryList();
        if (UtilValidate.isEmpty(virtualProductAssocs)) {
            // okay, not a variant, try a UNIQUE_ITEM
            virtualProductAssocs = EntityQuery.use(delegator).from("ProductAssoc").where("productIdTo", productId, "productAssocTypeId", "UNIQUE_ITEM").orderBy("-fromDate").cache(true).filterByDate().queryList();
        }
        if (UtilValidate.isNotEmpty(virtualProductAssocs)) {
            // found one, set this first as the parent product
            GenericValue productAssoc = EntityUtil.getFirst(virtualProductAssocs);
            _parentProduct = productAssoc.getRelatedOne("MainProduct", true);
        }
    } catch (GenericEntityException e) {
        throw new RuntimeException("Entity Engine error getting Parent Product (" + e.getMessage() + ")");
    }
    return _parentProduct;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 39 with GenericEntityException

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

Example 40 with GenericEntityException

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

the class ProductWorker method getOptionalProductFeatures.

public static Map<String, List<GenericValue>> getOptionalProductFeatures(Delegator delegator, String productId) {
    Map<String, List<GenericValue>> featureMap = new LinkedHashMap<>();
    List<GenericValue> productFeatureAppls = null;
    try {
        productFeatureAppls = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", productId, "productFeatureApplTypeId", "OPTIONAL_FEATURE").orderBy("productFeatureTypeId", "sequenceNum").queryList();
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    if (productFeatureAppls != null) {
        for (GenericValue appl : productFeatureAppls) {
            String featureType = appl.getString("productFeatureTypeId");
            List<GenericValue> features = featureMap.get(featureType);
            if (features == null) {
                features = new LinkedList<>();
            }
            features.add(appl);
            featureMap.put(featureType, features);
        }
    }
    return featureMap;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)913 GenericValue (org.apache.ofbiz.entity.GenericValue)847 Delegator (org.apache.ofbiz.entity.Delegator)599 Locale (java.util.Locale)384 HashMap (java.util.HashMap)336 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)270 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)259 LinkedList (java.util.LinkedList)231 BigDecimal (java.math.BigDecimal)213 Timestamp (java.sql.Timestamp)171 Map (java.util.Map)109 GeneralException (org.apache.ofbiz.base.util.GeneralException)95 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)78 IOException (java.io.IOException)75 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)57 Security (org.apache.ofbiz.security.Security)54 ArrayList (java.util.ArrayList)48 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)47 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)39 LinkedHashMap (java.util.LinkedHashMap)37