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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations