use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class ProductServices method prodFindAssociatedByType.
/**
* Finds associated products by product ID and association ID.
*/
public static Map<String, Object> prodFindAssociatedByType(DispatchContext dctx, Map<String, ? extends Object> context) {
// String type -- Type of association (ie PRODUCT_UPGRADE, PRODUCT_COMPLEMENT, PRODUCT_VARIANT)
Delegator delegator = dctx.getDelegator();
Map<String, Object> result = new HashMap<>();
String productId = (String) context.get("productId");
String productIdTo = (String) context.get("productIdTo");
String type = (String) context.get("type");
Locale locale = (Locale) context.get("locale");
String errMsg = null;
Boolean cvaBool = (Boolean) context.get("checkViewAllow");
boolean checkViewAllow = (cvaBool == null ? false : cvaBool);
String prodCatalogId = (String) context.get("prodCatalogId");
Boolean bidirectional = (Boolean) context.get("bidirectional");
bidirectional = bidirectional == null ? Boolean.FALSE : bidirectional;
Boolean sortDescending = (Boolean) context.get("sortDescending");
sortDescending = sortDescending == null ? Boolean.FALSE : sortDescending;
if (productId == null && productIdTo == null) {
errMsg = UtilProperties.getMessage(resourceError, "productservices.both_productId_and_productIdTo_cannot_be_null", locale);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, errMsg);
return result;
}
if (productId != null && productIdTo != null) {
errMsg = UtilProperties.getMessage(resourceError, "productservices.both_productId_and_productIdTo_cannot_be_defined", locale);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, errMsg);
return result;
}
productId = productId == null ? productIdTo : productId;
GenericValue product = null;
try {
product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.getMessage());
errMsg = UtilProperties.getMessage(resourceError, "productservices.problems_reading_product_entity", messageMap, locale);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, errMsg);
return result;
}
if (product == null) {
errMsg = UtilProperties.getMessage(resourceError, "productservices.problems_getting_product_entity", locale);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, errMsg);
return result;
}
try {
List<GenericValue> productAssocs = null;
List<String> orderBy = new LinkedList<>();
if (sortDescending) {
orderBy.add("sequenceNum DESC");
} else {
orderBy.add("sequenceNum");
}
if (bidirectional) {
EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productId", productId), EntityCondition.makeCondition("productIdTo", productId)), EntityJoinOperator.OR);
productAssocs = EntityQuery.use(delegator).from("ProductAssoc").where(EntityCondition.makeCondition(cond, EntityCondition.makeCondition("productAssocTypeId", type))).orderBy(orderBy).cache(true).queryList();
} else {
if (productIdTo == null) {
productAssocs = product.getRelated("MainProductAssoc", UtilMisc.toMap("productAssocTypeId", type), orderBy, true);
} else {
productAssocs = product.getRelated("AssocProductAssoc", UtilMisc.toMap("productAssocTypeId", type), orderBy, true);
}
}
// filter the list by date
productAssocs = EntityUtil.filterByDate(productAssocs);
// first check to see if there is a view allow category and if these products are in it...
if (checkViewAllow && prodCatalogId != null && UtilValidate.isNotEmpty(productAssocs)) {
String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, prodCatalogId);
if (viewProductCategoryId != null) {
if (productIdTo == null) {
productAssocs = CategoryWorker.filterProductsInCategory(delegator, productAssocs, viewProductCategoryId, "productIdTo");
} else {
productAssocs = CategoryWorker.filterProductsInCategory(delegator, productAssocs, viewProductCategoryId, "productId");
}
}
}
result.put("assocProducts", productAssocs);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.getMessage());
errMsg = UtilProperties.getMessage(resourceError, "productservices.problems_product_association_relation_error", messageMap, locale);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, errMsg);
return result;
}
return result;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class PromoServices method purgeOldStoreAutoPromos.
public static Map<String, Object> purgeOldStoreAutoPromos(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
String productStoreId = (String) context.get("productStoreId");
Locale locale = (Locale) context.get("locale");
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
List<EntityCondition> condList = new LinkedList<>();
if (UtilValidate.isEmpty(productStoreId)) {
condList.add(EntityCondition.makeCondition("productStoreId", EntityOperator.EQUALS, productStoreId));
}
condList.add(EntityCondition.makeCondition("userEntered", EntityOperator.EQUALS, "Y"));
condList.add(EntityCondition.makeCondition("thruDate", EntityOperator.NOT_EQUAL, null));
condList.add(EntityCondition.makeCondition("thruDate", EntityOperator.LESS_THAN, nowTimestamp));
try (EntityListIterator eli = EntityQuery.use(delegator).from("ProductStorePromoAndAppl").where(condList).queryIterator()) {
GenericValue productStorePromoAndAppl = null;
while ((productStorePromoAndAppl = eli.next()) != null) {
GenericValue productStorePromo = delegator.makeValue("ProductStorePromoAppl");
productStorePromo.setAllFields(productStorePromoAndAppl, true, null, null);
productStorePromo.remove();
}
} catch (GenericEntityException e) {
Debug.logError(e, "Error removing expired ProductStorePromo records: " + e.toString(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPromoCodeCannotBeRemoved", UtilMisc.toMap("errorString", e.toString()), locale));
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class SubscriptionServices method runServiceOnSubscriptionExpiry.
public static Map<String, Object> runServiceOnSubscriptionExpiry(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
Map<String, Object> result = new HashMap<>();
Map<String, Object> expiryMap = new HashMap<>();
String gracePeriodOnExpiry = null;
String gracePeriodOnExpiryUomId = null;
String subscriptionId = null;
Timestamp expirationCompletedDate = null;
try {
EntityCondition cond1 = EntityCondition.makeCondition("automaticExtend", EntityOperator.EQUALS, "N");
EntityCondition cond2 = EntityCondition.makeCondition("automaticExtend", EntityOperator.EQUALS, null);
EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(cond1, cond2), EntityOperator.OR);
List<GenericValue> subscriptionList = null;
subscriptionList = EntityQuery.use(delegator).from("Subscription").where(cond).queryList();
if (subscriptionList != null) {
for (GenericValue subscription : subscriptionList) {
expirationCompletedDate = subscription.getTimestamp("expirationCompletedDate");
if (expirationCompletedDate == null) {
Calendar currentDate = Calendar.getInstance();
currentDate.setTime(UtilDateTime.nowTimestamp());
// check if the thruDate + grace period (if provided) is earlier than today's date
Calendar endDateSubscription = Calendar.getInstance();
int field = Calendar.MONTH;
String subscriptionResourceId = subscription.getString("subscriptionResourceId");
GenericValue subscriptionResource = null;
subscriptionResource = EntityQuery.use(delegator).from("SubscriptionResource").where("subscriptionResourceId", subscriptionResourceId).queryOne();
subscriptionId = subscription.getString("subscriptionId");
gracePeriodOnExpiry = subscription.getString("gracePeriodOnExpiry");
gracePeriodOnExpiryUomId = subscription.getString("gracePeriodOnExpiryUomId");
String serviceNameOnExpiry = subscriptionResource.getString("serviceNameOnExpiry");
endDateSubscription.setTime(subscription.getTimestamp("thruDate"));
if (gracePeriodOnExpiry != null && gracePeriodOnExpiryUomId != null) {
if ("TF_day".equals(gracePeriodOnExpiryUomId)) {
field = Calendar.DAY_OF_YEAR;
} else if ("TF_wk".equals(gracePeriodOnExpiryUomId)) {
field = Calendar.WEEK_OF_YEAR;
} else if ("TF_mon".equals(gracePeriodOnExpiryUomId)) {
field = Calendar.MONTH;
} else if ("TF_yr".equals(gracePeriodOnExpiryUomId)) {
field = Calendar.YEAR;
} else {
Debug.logWarning("Don't know anything about gracePeriodOnExpiryUomId [" + gracePeriodOnExpiryUomId + "], defaulting to month", module);
}
endDateSubscription.add(field, Integer.parseInt(gracePeriodOnExpiry));
}
if ((currentDate.after(endDateSubscription) || currentDate.equals(endDateSubscription)) && serviceNameOnExpiry != null) {
if (userLogin != null) {
expiryMap.put("userLogin", userLogin);
}
if (subscriptionId != null) {
expiryMap.put("subscriptionId", subscriptionId);
}
result = dispatcher.runSync(serviceNameOnExpiry, expiryMap);
if (ServiceUtil.isSuccess(result)) {
subscription.set("expirationCompletedDate", UtilDateTime.nowTimestamp());
delegator.store(subscription);
Debug.logInfo("Subscription expired successfully for subscription ID:" + subscriptionId, module);
} else if (ServiceUtil.isError(result)) {
result = null;
Debug.logError("Error expiring subscription while processing with subscriptionId: " + subscriptionId, module);
}
if (result != null && subscriptionId != null) {
Debug.logInfo("Service mentioned in serviceNameOnExpiry called with result: " + ServiceUtil.makeSuccessMessage(result, "", "", "", ""), module);
} else if (result == null && subscriptionId != null) {
Debug.logError("Subscription couldn't be expired for subscriptionId: " + subscriptionId, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductSubscriptionCouldntBeExpired", UtilMisc.toMap("subscriptionId", subscriptionId), locale));
}
}
}
}
}
} catch (GenericServiceException e) {
Debug.logError("Error while calling service specified in serviceNameOnExpiry", module);
return ServiceUtil.returnError(e.toString());
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
return result;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class ProductUtilServices method removeCategoryMembersOfDiscProducts.
/**
* for all disc products, remove from category memberships
*/
public static Map<String, Object> removeCategoryMembersOfDiscProducts(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
Locale locale = (Locale) context.get("locale");
String errMsg = null;
EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp)), EntityOperator.AND);
try (EntityListIterator eli = EntityQuery.use(delegator).from("Product").where(condition).queryIterator()) {
GenericValue product = null;
int numSoFar = 0;
while ((product = eli.next()) != null) {
String productId = product.getString("productId");
List<GenericValue> productCategoryMemberList = EntityQuery.use(delegator).from("ProductCategoryMember").where("productId", productId).queryList();
if (productCategoryMemberList.size() > 0) {
for (GenericValue productCategoryMember : productCategoryMemberList) {
// coded this way rather than a removeByAnd so it can be easily changed...
productCategoryMember.remove();
}
numSoFar++;
if (numSoFar % 500 == 0) {
Debug.logInfo("Removed category members for " + numSoFar + " sales discontinued products.", module);
}
}
}
Debug.logInfo("Completed - Removed category members for " + numSoFar + " sales discontinued products.", module);
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_removeCategoryMembersOfDiscProducts", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class ProductUtilServices method makeStandAloneFromSingleVariantVirtuals.
public static Map<String, Object> makeStandAloneFromSingleVariantVirtuals(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
Locale locale = (Locale) context.get("locale");
String errMsg = null;
Debug.logInfo("Starting makeStandAloneFromSingleVariantVirtuals", module);
DynamicViewEntity dve = new DynamicViewEntity();
dve.addMemberEntity("PVIRT", "Product");
dve.addMemberEntity("PVA", "ProductAssoc");
dve.addViewLink("PVIRT", "PVA", Boolean.FALSE, UtilMisc.toList(new ModelKeyMap("productId", "productId")));
dve.addAlias("PVIRT", "productId", null, null, null, Boolean.TRUE, null);
dve.addAlias("PVIRT", "salesDiscontinuationDate", null, null, null, null, null);
dve.addAlias("PVA", "productAssocTypeId", null, null, null, null, null);
dve.addAlias("PVA", "fromDate", null, null, null, null, null);
dve.addAlias("PVA", "thruDate", null, null, null, null, null);
dve.addAlias("PVA", "productIdToCount", "productIdTo", null, null, null, "count-distinct");
EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productAssocTypeId", EntityOperator.EQUALS, "PRODUCT_VARIANT"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN, nowTimestamp))), EntityOperator.AND);
EntityCondition havingCond = EntityCondition.makeCondition("productIdToCount", EntityOperator.EQUALS, Long.valueOf(1));
EntityQuery eq = EntityQuery.use(delegator).select("productId", "productIdToCount").from(dve).where(condition).having(havingCond);
try (EntityListIterator eliOne = eq.queryIterator()) {
List<GenericValue> valueList = eliOne.getCompleteList();
Debug.logInfo("Found " + valueList.size() + " virtual products with one variant to turn into a stand alone product.", module);
int numWithOneOnly = 0;
for (GenericValue value : valueList) {
// has only one variant period, is it valid? should already be discontinued if not
String productId = value.getString("productId");
List<GenericValue> paList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
// verify the query; tested on a bunch, looks good
if (paList.size() != 1) {
Debug.logInfo("Virtual product with ID " + productId + " should have 1 assoc, has " + paList.size(), module);
} else {
// for all virtuals with one variant move all info from virtual to variant and remove virtual, make variant as not a variant
dispatcher.runSync("mergeVirtualWithSingleVariant", UtilMisc.<String, Object>toMap("productId", productId, "removeOld", Boolean.TRUE, "userLogin", userLogin));
numWithOneOnly++;
if (numWithOneOnly % 100 == 0) {
Debug.logInfo("Made " + numWithOneOnly + " virtual products with only one valid variant stand-alone products.", module);
}
}
}
EntityCondition conditionWithDates = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productAssocTypeId", EntityOperator.EQUALS, "PRODUCT_VARIANT"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN, nowTimestamp)), EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp), EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp))), EntityOperator.AND);
eq = EntityQuery.use(delegator).select("productId", "productIdToCount").from(dve).where(conditionWithDates).having(havingCond);
try (EntityListIterator eliMulti = eq.queryIterator()) {
List<GenericValue> valueMultiList = eliMulti.getCompleteList();
Debug.logInfo("Found " + valueMultiList.size() + " virtual products with one VALID variant to pull the variant from to make a stand alone product.", module);
int numWithOneValid = 0;
for (GenericValue value : valueMultiList) {
// has only one valid variant
String productId = value.getString("productId");
List<GenericValue> paList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
// verify the query; tested on a bunch, looks good
if (paList.size() != 1) {
Debug.logInfo("Virtual product with ID " + productId + " should have 1 assoc, has " + paList.size(), module);
} else {
// for all virtuals with one valid variant move info from virtual to variant, put variant in categories from virtual, remove virtual from all categories but leave "family" otherwise intact, mark variant as not a variant
dispatcher.runSync("mergeVirtualWithSingleVariant", UtilMisc.<String, Object>toMap("productId", productId, "removeOld", Boolean.FALSE, "userLogin", userLogin));
numWithOneValid++;
if (numWithOneValid % 100 == 0) {
Debug.logInfo("Made " + numWithOneValid + " virtual products with one valid variant stand-alone products.", module);
}
}
}
Debug.logInfo("Found virtual products with one valid variant: " + numWithOneValid + ", with one variant only: " + numWithOneOnly, module);
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
} catch (GenericEntityException | GenericServiceException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
Aggregations