use of org.apache.ofbiz.entity.GenericValue 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;
}
use of org.apache.ofbiz.entity.GenericValue 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 + "]";
}
use of org.apache.ofbiz.entity.GenericValue in project ofbiz-framework by apache.
the class ProductWorker method getRefurbishedProductIdSet.
public static Set<String> getRefurbishedProductIdSet(String productId, Delegator delegator) throws GenericEntityException {
Set<String> productIdSet = new HashSet<>();
// find associated refurb items, we want serial number for main item or any refurb items too
List<GenericValue> refubProductAssocs = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_REFURB").filterByDate().queryList();
for (GenericValue refubProductAssoc : refubProductAssocs) {
productIdSet.add(refubProductAssoc.getString("productIdTo"));
}
// see if this is a refurb productId to, and find product(s) it is a refurb of
List<GenericValue> refubProductToAssocs = EntityQuery.use(delegator).from("ProductAssoc").where("productIdTo", productId, "productAssocTypeId", "PRODUCT_REFURB").filterByDate().queryList();
for (GenericValue refubProductToAssoc : refubProductToAssocs) {
productIdSet.add(refubProductToAssoc.getString("productId"));
}
return productIdSet;
}
use of org.apache.ofbiz.entity.GenericValue 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;
}
use of org.apache.ofbiz.entity.GenericValue in project ofbiz-framework by apache.
the class ProductWorker method filterOutOfStockProducts.
// Method to filter-out out of stock products
public static List<GenericValue> filterOutOfStockProducts(List<GenericValue> productsToFilter, LocalDispatcher dispatcher, Delegator delegator) throws GeneralException {
List<GenericValue> productsInStock = new ArrayList<>();
if (UtilValidate.isNotEmpty(productsToFilter)) {
for (GenericValue genericRecord : productsToFilter) {
String productId = genericRecord.getString("productId");
GenericValue product = null;
product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache(true).queryOne();
Boolean isMarketingPackage = EntityTypeUtil.hasParentType(delegator, "ProductType", "productTypeId", product.getString("productTypeId"), "parentTypeId", "MARKETING_PKG");
if (UtilValidate.isNotEmpty(isMarketingPackage) && isMarketingPackage) {
Map<String, Object> resultOutput = new HashMap<>();
resultOutput = dispatcher.runSync("getMktgPackagesAvailable", UtilMisc.toMap("productId", productId));
Debug.logWarning("Error getting available marketing package.", module);
BigDecimal availableInventory = (BigDecimal) resultOutput.get("availableToPromiseTotal");
if (availableInventory.compareTo(BigDecimal.ZERO) > 0) {
productsInStock.add(genericRecord);
}
} else {
List<GenericValue> facilities = EntityQuery.use(delegator).from("ProductFacility").where("productId", productId).queryList();
BigDecimal availableInventory = BigDecimal.ZERO;
if (UtilValidate.isNotEmpty(facilities)) {
for (GenericValue facility : facilities) {
BigDecimal lastInventoryCount = facility.getBigDecimal("lastInventoryCount");
if (lastInventoryCount != null) {
availableInventory = lastInventoryCount.add(availableInventory);
}
}
if (availableInventory.compareTo(BigDecimal.ZERO) > 0) {
productsInStock.add(genericRecord);
}
}
}
}
}
return productsInStock;
}
Aggregations