Search in sources :

Example 56 with GenericValue

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

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

the class ProductWorker method isProductInventoryAvailableByFacility.

/**
 * Invokes the getInventoryAvailableByFacility service, returns true if specified quantity is available for all the selected parts, else false.
 * Also, set the available flag for all the product configuration's options.
 */
public static boolean isProductInventoryAvailableByFacility(ProductConfigWrapper productConfig, String inventoryFacilityId, BigDecimal quantity, LocalDispatcher dispatcher) {
    boolean available = true;
    List<ConfigOption> options = productConfig.getSelectedOptions();
    for (ConfigOption ci : options) {
        List<GenericValue> products = ci.getComponents();
        for (GenericValue product : products) {
            String productId = product.getString("productId");
            BigDecimal cmpQuantity = product.getBigDecimal("quantity");
            BigDecimal neededQty = BigDecimal.ZERO;
            if (cmpQuantity != null) {
                neededQty = quantity.multiply(cmpQuantity);
            }
            if (!isProductInventoryAvailableByFacility(productId, inventoryFacilityId, neededQty, dispatcher)) {
                ci.setAvailable(false);
            }
        }
        if (!ci.isAvailable()) {
            available = false;
        }
    }
    return available;
}
Also used : ConfigOption(org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption) GenericValue(org.apache.ofbiz.entity.GenericValue) BigDecimal(java.math.BigDecimal)

Example 58 with GenericValue

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

Example 59 with GenericValue

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

the class ProductWorker method getAverageProductRating.

public static BigDecimal getAverageProductRating(GenericValue product, List<GenericValue> reviews, String productStoreId) {
    if (product == null) {
        Debug.logWarning("Invalid product entity passed; unable to obtain valid product rating", module);
        return BigDecimal.ZERO;
    }
    BigDecimal productRating = BigDecimal.ZERO;
    BigDecimal productEntityRating = product.getBigDecimal("productRating");
    String entityFieldType = product.getString("ratingTypeEnum");
    // null check
    if (productEntityRating == null) {
        productEntityRating = BigDecimal.ZERO;
    }
    if (entityFieldType == null) {
        entityFieldType = "";
    }
    if ("PRDR_FLAT".equals(entityFieldType)) {
        productRating = productEntityRating;
    } else {
        // get the product rating from the ProductReview entity; limit by product store if ID is passed
        Map<String, String> reviewByAnd = UtilMisc.toMap("statusId", "PRR_APPROVED");
        if (productStoreId != null) {
            reviewByAnd.put("productStoreId", productStoreId);
        }
        // lookup the reviews if we didn't pass them in
        if (reviews == null) {
            try {
                reviews = product.getRelated("ProductReview", reviewByAnd, UtilMisc.toList("-postedDateTime"), true);
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
            }
        }
        // tally the average
        BigDecimal ratingTally = BigDecimal.ZERO;
        BigDecimal numRatings = BigDecimal.ZERO;
        if (reviews != null) {
            for (GenericValue productReview : reviews) {
                BigDecimal rating = productReview.getBigDecimal("productRating");
                if (rating != null) {
                    ratingTally = ratingTally.add(rating);
                    numRatings = numRatings.add(BigDecimal.ONE);
                }
            }
        }
        if (ratingTally.compareTo(BigDecimal.ZERO) > 0 && numRatings.compareTo(BigDecimal.ZERO) > 0) {
            productRating = ratingTally.divide(numRatings, generalRounding);
        }
        if ("PRDR_MIN".equals(entityFieldType)) {
            // check for min
            if (productEntityRating.compareTo(productRating) > 0) {
                productRating = productEntityRating;
            }
        } else if ("PRDR_MAX".equals(entityFieldType)) {
            // check for max
            if (productRating.compareTo(productEntityRating) > 0) {
                productRating = productEntityRating;
            }
        }
    }
    return productRating;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal)

Example 60 with GenericValue

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

the class ProductWorker method isPhysical.

public static boolean isPhysical(GenericValue product) {
    boolean isPhysical = false;
    if (product != null) {
        GenericValue productType = null;
        try {
            productType = product.getRelatedOne("ProductType", true);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
        }
        String isPhysicalValue = (productType != null ? productType.getString("isPhysical") : null);
        isPhysical = isPhysicalValue != null && "Y".equalsIgnoreCase(isPhysicalValue);
    }
    return isPhysical;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Aggregations

GenericValue (org.apache.ofbiz.entity.GenericValue)1422 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)871 Delegator (org.apache.ofbiz.entity.Delegator)721 Locale (java.util.Locale)505 HashMap (java.util.HashMap)463 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)370 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)356 BigDecimal (java.math.BigDecimal)338 LinkedList (java.util.LinkedList)312 Timestamp (java.sql.Timestamp)202 GeneralException (org.apache.ofbiz.base.util.GeneralException)168 Map (java.util.Map)155 IOException (java.io.IOException)116 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)97 HttpSession (javax.servlet.http.HttpSession)89 ArrayList (java.util.ArrayList)69 Security (org.apache.ofbiz.security.Security)69 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)59 List (java.util.List)56 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)52