Search in sources :

Example 56 with GenericEntityException

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

the class ProductStoreWorker method getAvailableStoreShippingMethods.

public static List<GenericValue> getAvailableStoreShippingMethods(Delegator delegator, String productStoreId, GenericValue shippingAddress, List<BigDecimal> itemSizes, Map<String, BigDecimal> featureIdMap, BigDecimal weight, BigDecimal orderTotal) {
    if (featureIdMap == null) {
        featureIdMap = new HashMap<String, BigDecimal>();
    }
    List<GenericValue> shippingMethods = null;
    try {
        shippingMethods = EntityQuery.use(delegator).from("ProductStoreShipmentMethView").where("productStoreId", productStoreId).orderBy("sequenceNumber").cache(true).queryList();
    } catch (GenericEntityException e) {
        Debug.logError(e, "Unable to get ProductStore shipping methods", module);
        return null;
    }
    // clone the list for concurrent modification
    List<GenericValue> returnShippingMethods = UtilMisc.makeListWritable(shippingMethods);
    if (shippingMethods != null) {
        for (GenericValue method : shippingMethods) {
            // test min/max weight first
            BigDecimal minWeight = method.getBigDecimal("minWeight");
            BigDecimal maxWeight = method.getBigDecimal("maxWeight");
            if (minWeight != null && minWeight.compareTo(BigDecimal.ZERO) > 0 && minWeight.compareTo(weight) > 0) {
                returnShippingMethods.remove(method);
                continue;
            }
            if (maxWeight != null && maxWeight.compareTo(BigDecimal.ZERO) > 0 && maxWeight.compareTo(weight) < 0) {
                returnShippingMethods.remove(method);
                continue;
            }
            // test order total
            BigDecimal minTotal = method.getBigDecimal("minTotal");
            BigDecimal maxTotal = method.getBigDecimal("maxTotal");
            if (minTotal != null && minTotal.compareTo(BigDecimal.ZERO) > 0 && minTotal.compareTo(orderTotal) > 0) {
                returnShippingMethods.remove(method);
                continue;
            }
            if (maxTotal != null && maxTotal.compareTo(BigDecimal.ZERO) > 0 && maxTotal.compareTo(orderTotal) < 0) {
                returnShippingMethods.remove(method);
                continue;
            }
            // test product sizes
            BigDecimal minSize = method.getBigDecimal("minSize");
            BigDecimal maxSize = method.getBigDecimal("maxSize");
            if (minSize != null && minSize.compareTo(BigDecimal.ZERO) > 0) {
                boolean allMatch = false;
                if (itemSizes != null) {
                    allMatch = true;
                    for (BigDecimal size : itemSizes) {
                        if (size.compareTo(minSize) < 0) {
                            allMatch = false;
                        }
                    }
                }
                if (!allMatch) {
                    returnShippingMethods.remove(method);
                    continue;
                }
            }
            if (maxSize != null && maxSize.compareTo(BigDecimal.ZERO) > 0) {
                boolean allMatch = false;
                if (itemSizes != null) {
                    allMatch = true;
                    for (BigDecimal size : itemSizes) {
                        if (size.compareTo(maxSize) > 0) {
                            allMatch = false;
                        }
                    }
                }
                if (!allMatch) {
                    returnShippingMethods.remove(method);
                    continue;
                }
            }
            // check USPS address
            String allowUspsAddr = method.getString("allowUspsAddr");
            String requireUspsAddr = method.getString("requireUspsAddr");
            boolean isUspsAddress = ContactMechWorker.isUspsAddress(shippingAddress);
            if ("N".equals(allowUspsAddr) && isUspsAddress) {
                returnShippingMethods.remove(method);
                continue;
            }
            if ("Y".equals(requireUspsAddr) && !isUspsAddress) {
                returnShippingMethods.remove(method);
                continue;
            }
            // check company address
            String companyPartyId = method.getString("companyPartyId");
            String allowCompanyAddr = method.getString("allowCompanyAddr");
            String requireCompanyAddr = method.getString("requireCompanyAddr");
            boolean isCompanyAddress = ContactMechWorker.isCompanyAddress(shippingAddress, companyPartyId);
            if ("N".equals(allowCompanyAddr) && isCompanyAddress) {
                returnShippingMethods.remove(method);
                continue;
            }
            if ("Y".equals(requireCompanyAddr) && !isCompanyAddress) {
                returnShippingMethods.remove(method);
                continue;
            }
            // check the items excluded from shipping
            String includeFreeShipping = method.getString("includeNoChargeItems");
            if (includeFreeShipping != null && "N".equalsIgnoreCase(includeFreeShipping)) {
                if (UtilValidate.isEmpty(itemSizes) && orderTotal.compareTo(BigDecimal.ZERO) == 0) {
                    returnShippingMethods.remove(method);
                    continue;
                }
            }
            // check the geos
            String includeGeoId = method.getString("includeGeoId");
            String excludeGeoId = method.getString("excludeGeoId");
            if (UtilValidate.isNotEmpty(includeGeoId) || UtilValidate.isNotEmpty(excludeGeoId)) {
                if (shippingAddress == null) {
                    returnShippingMethods.remove(method);
                    continue;
                }
            }
            if (UtilValidate.isNotEmpty(includeGeoId)) {
                List<GenericValue> includeGeoGroup = GeoWorker.expandGeoGroup(includeGeoId, delegator);
                if (!GeoWorker.containsGeo(includeGeoGroup, shippingAddress.getString("countryGeoId"), delegator) && !GeoWorker.containsGeo(includeGeoGroup, shippingAddress.getString("stateProvinceGeoId"), delegator) && !GeoWorker.containsGeo(includeGeoGroup, shippingAddress.getString("postalCodeGeoId"), delegator)) {
                    // not in required included geos
                    returnShippingMethods.remove(method);
                    continue;
                }
            }
            if (UtilValidate.isNotEmpty(excludeGeoId)) {
                List<GenericValue> excludeGeoGroup = GeoWorker.expandGeoGroup(excludeGeoId, delegator);
                if (GeoWorker.containsGeo(excludeGeoGroup, shippingAddress.getString("countryGeoId"), delegator) || GeoWorker.containsGeo(excludeGeoGroup, shippingAddress.getString("stateProvinceGeoId"), delegator) || GeoWorker.containsGeo(excludeGeoGroup, shippingAddress.getString("postalCodeGeoId"), delegator)) {
                    // in excluded geos
                    returnShippingMethods.remove(method);
                    continue;
                }
            }
            // check the features
            String includeFeatures = method.getString("includeFeatureGroup");
            String excludeFeatures = method.getString("excludeFeatureGroup");
            if (UtilValidate.isNotEmpty(includeFeatures)) {
                List<GenericValue> includedFeatures = null;
                try {
                    includedFeatures = EntityQuery.use(delegator).from("ProductFeatureGroupAppl").where("productFeatureGroupId", includeFeatures).cache(true).queryList();
                } catch (GenericEntityException e) {
                    Debug.logError(e, "Unable to lookup ProductFeatureGroupAppl records for group : " + includeFeatures, module);
                }
                if (includedFeatures != null) {
                    boolean foundOne = false;
                    for (GenericValue appl : includedFeatures) {
                        if (featureIdMap.containsKey(appl.getString("productFeatureId"))) {
                            foundOne = true;
                            break;
                        }
                    }
                    if (!foundOne) {
                        returnShippingMethods.remove(method);
                        continue;
                    }
                }
            }
            if (UtilValidate.isNotEmpty(excludeFeatures)) {
                List<GenericValue> excludedFeatures = null;
                try {
                    excludedFeatures = EntityQuery.use(delegator).from("ProductFeatureGroupAppl").where("productFeatureGroupId", excludeFeatures).cache(true).queryList();
                } catch (GenericEntityException e) {
                    Debug.logError(e, "Unable to lookup ProductFeatureGroupAppl records for group : " + excludeFeatures, module);
                }
                if (excludedFeatures != null) {
                    for (GenericValue appl : excludedFeatures) {
                        if (featureIdMap.containsKey(appl.getString("productFeatureId"))) {
                            returnShippingMethods.remove(method);
                            continue;
                        }
                    }
                }
            }
        }
    }
    return returnShippingMethods;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal)

Example 57 with GenericEntityException

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

the class ProductStoreWorker method getSurveys.

public static List<GenericValue> getSurveys(Delegator delegator, String productStoreId, String groupName, String productId, String surveyApplTypeId, String parentProductId) {
    List<GenericValue> surveys = new LinkedList<GenericValue>();
    List<GenericValue> storeSurveys = null;
    try {
        storeSurveys = EntityQuery.use(delegator).from("ProductStoreSurveyAppl").where("productStoreId", productStoreId, "surveyApplTypeId", surveyApplTypeId).orderBy("sequenceNum").cache(true).queryList();
    } catch (GenericEntityException e) {
        Debug.logError(e, "Unable to get ProductStoreSurveyAppl for store : " + productStoreId, module);
        return surveys;
    }
    // limit by date
    storeSurveys = EntityUtil.filterByDate(storeSurveys);
    // limit based on group name
    if (UtilValidate.isNotEmpty(groupName)) {
        storeSurveys = EntityUtil.filterByAnd(storeSurveys, UtilMisc.toMap("groupName", groupName));
    }
    Debug.logInfo("getSurvey for product " + productId, module);
    // limit by product
    if (UtilValidate.isNotEmpty(productId) && UtilValidate.isNotEmpty(storeSurveys)) {
        for (GenericValue surveyAppl : storeSurveys) {
            GenericValue product = null;
            String virtualProductId = null;
            // if the item is a variant, get its virtual productId
            try {
                product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
                if ((product != null) && ("Y".equals(product.get("isVariant")))) {
                    if (parentProductId != null) {
                        virtualProductId = parentProductId;
                    } else {
                        virtualProductId = ProductWorker.getVariantVirtualId(product);
                    }
                    Debug.logInfo("getSurvey for virtual product " + virtualProductId, module);
                }
            } catch (GenericEntityException e) {
                Debug.logError(e, "Problem finding product from productId " + productId, module);
            }
            // use survey if productId or virtualProductId of the variant product is in the ProductStoreSurveyAppl
            if (surveyAppl.get("productId") != null) {
                if (surveyAppl.get("productId").equals(productId)) {
                    surveys.add(surveyAppl);
                } else if ((virtualProductId != null) && (surveyAppl.getString("productId").equals(virtualProductId))) {
                    surveys.add(surveyAppl);
                }
            } else if (surveyAppl.get("productCategoryId") != null) {
                List<GenericValue> categoryMembers = null;
                try {
                    categoryMembers = EntityQuery.use(delegator).from("ProductCategoryMember").where("productCategoryId", surveyAppl.get("productCategoryId")).cache(true).queryList();
                } catch (GenericEntityException e) {
                    Debug.logError(e, "Unable to get ProductCategoryMember records for survey application : " + surveyAppl, module);
                }
                if (categoryMembers != null) {
                    for (GenericValue member : categoryMembers) {
                        if (productId != null && productId.equals(member.getString("productId"))) {
                            surveys.add(surveyAppl);
                            break;
                        } else if ((virtualProductId != null) && (virtualProductId.equals(member.getString("productId")))) {
                            // similarly, check if virtual productId is in category
                            surveys.add(surveyAppl);
                            break;
                        }
                    }
                }
            }
        }
    } else if (storeSurveys != null) {
        surveys.addAll(storeSurveys);
    }
    return surveys;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 58 with GenericEntityException

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

the class SupplierProductServices method convertFeaturesForSupplier.

/*
     * Parameters: partyId of a supplier and productFeatures, a Collection (usually List) of product features
     * Service will convert each feature in the Collection, changing their idCode and description based on the
     * SupplierProduct entity for that supplier party and feature, and return it as convertedProductFeatures
     */
public static Map<String, Object> convertFeaturesForSupplier(DispatchContext dctx, Map<String, ? extends Object> context) {
    Map<String, Object> results;
    String partyId = (String) context.get("partyId");
    Collection<GenericValue> features = UtilGenerics.checkList(context.get("productFeatures"));
    try {
        if (partyId != null && UtilValidate.isNotEmpty(features)) {
            // substitue description and idCode
            for (GenericValue nextFeature : features) {
                List<GenericValue> supplierFeatures = EntityUtil.filterByAnd(nextFeature.getRelated("SupplierProductFeature", null, null, false), UtilMisc.toMap("partyId", partyId));
                GenericValue supplierFeature = null;
                if ((supplierFeatures != null) && (supplierFeatures.size() > 0)) {
                    supplierFeature = supplierFeatures.get(0);
                    if (supplierFeature.get("description") != null) {
                        nextFeature.put("description", supplierFeature.get("description"));
                    }
                    if (supplierFeature.get("idCode") != null) {
                        nextFeature.put("idCode", supplierFeature.get("idCode"));
                    }
                // TODO: later, do some kind of uom/quantity conoversion with the UomConversion entity
                }
            }
        }
        results = ServiceUtil.returnSuccess();
        results.put("convertedProductFeatures", features);
    } catch (GenericEntityException ex) {
        Debug.logError(ex, ex.getMessage(), module);
        return ServiceUtil.returnError(ex.getMessage());
    }
    return results;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 59 with GenericEntityException

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

the class SupplierProductServices method getSuppliersForProduct.

/*
     * Parameters: productId, partyId, currencyUomId, quantity
     * Result: a List of SupplierProduct entities for productId,
     *         filtered by date and optionally by partyId, ordered with lowest price first
     */
public static Map<String, Object> getSuppliersForProduct(DispatchContext dctx, Map<String, ? extends Object> context) {
    Map<String, Object> results;
    Delegator delegator = dctx.getDelegator();
    GenericValue product = null;
    String productId = (String) context.get("productId");
    String partyId = (String) context.get("partyId");
    String currencyUomId = (String) context.get("currencyUomId");
    BigDecimal quantity = (BigDecimal) context.get("quantity");
    String canDropShip = (String) context.get("canDropShip");
    try {
        product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
        if (product == null) {
            results = ServiceUtil.returnSuccess();
            results.put("supplierProducts", null);
            return results;
        }
        List<GenericValue> supplierProducts = product.getRelated("SupplierProduct", null, null, true);
        // if there were no related SupplierProduct entities and the item is a variant, then get the SupplierProducts of the virtual parent product
        if (supplierProducts.size() == 0 && product.getString("isVariant") != null && "Y".equals(product.getString("isVariant"))) {
            String virtualProductId = ProductWorker.getVariantVirtualId(product);
            GenericValue virtualProduct = EntityQuery.use(delegator).from("Product").where("productId", virtualProductId).cache().queryOne();
            if (virtualProduct != null) {
                supplierProducts = virtualProduct.getRelated("SupplierProduct", null, null, true);
            }
        }
        // filter the list by date
        supplierProducts = EntityUtil.filterByDate(supplierProducts, UtilDateTime.nowTimestamp(), "availableFromDate", "availableThruDate", true);
        // filter the list down by the partyId if one is provided
        if (partyId != null) {
            supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("partyId", partyId));
        }
        // filter the list down by the currencyUomId if one is provided
        if (currencyUomId != null) {
            supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("currencyUomId", currencyUomId));
        }
        // filter the list down by the minimumOrderQuantity if one is provided
        if (quantity != null) {
            // minimumOrderQuantity
            supplierProducts = EntityUtil.filterByCondition(supplierProducts, EntityCondition.makeCondition("minimumOrderQuantity", EntityOperator.LESS_THAN_EQUAL_TO, quantity));
        }
        // filter the list down by the canDropShip if one is provided
        if (canDropShip != null) {
            supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("canDropShip", canDropShip));
        }
        // sort resulting list of SupplierProduct entities by price in ASCENDING order
        supplierProducts = EntityUtil.orderBy(supplierProducts, UtilMisc.toList("lastPrice ASC"));
        results = ServiceUtil.returnSuccess();
        results.put("supplierProducts", supplierProducts);
    } catch (GenericEntityException ex) {
        Debug.logError(ex, ex.getMessage(), module);
        return ServiceUtil.returnError(ex.getMessage());
    }
    return results;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal)

Example 60 with GenericEntityException

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

the class CertificateServices method importIssuerCertificate.

public static Map<String, Object> importIssuerCertificate(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String certString = (String) context.get("certString");
    String componentName = (String) context.get("componentName");
    String keystoreName = (String) context.get("keystoreName");
    String alias = (String) context.get("alias");
    String importIssuer = (String) context.get("importIssuer");
    // load the keystore
    KeyStore ks;
    try {
        ks = KeyStoreUtil.getComponentKeyStore(componentName, keystoreName);
    } catch (Exception e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    // read the certificate
    X509Certificate cert;
    try {
        cert = (X509Certificate) KeyStoreUtil.pemToCert(certString);
    } catch (CertificateException e) {
        return ServiceUtil.returnError(e.getMessage());
    } catch (IOException e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    // store the cert
    try {
        ks.setCertificateEntry(alias, cert);
    } catch (Exception e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    // save the keystore
    try {
        KeyStoreUtil.storeComponentKeyStore(componentName, keystoreName, ks);
    } catch (Exception e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    // set the issuer provision
    Map<String, String> x500Map = KeyStoreUtil.getCertX500Map(cert);
    if (importIssuer != null && "Y".equalsIgnoreCase(importIssuer)) {
        GenericValue provision = delegator.makeValue("X509IssuerProvision");
        provision.set("commonName", x500Map.get("CN"));
        provision.set("organizationalUnit", x500Map.get("OU"));
        provision.set("organizationName", x500Map.get("O"));
        provision.set("cityLocality", x500Map.get("L"));
        provision.set("stateProvince", x500Map.get("ST"));
        provision.set("country", x500Map.get("C"));
        provision.set("serialNumber", cert.getSerialNumber().toString(16));
        try {
            delegator.createSetNextSeqId(provision);
        } catch (GenericEntityException e) {
            return ServiceUtil.returnError(e.getMessage());
        }
    }
    return ServiceUtil.returnSuccess();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) X509Certificate(java.security.cert.X509Certificate)

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