1 24 package org.ofbiz.product.promo; 25 26 import java.sql.Timestamp ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.ofbiz.base.util.Debug; 32 import org.ofbiz.base.util.UtilDateTime; 33 import org.ofbiz.base.util.UtilValidate; 34 import org.ofbiz.entity.GenericDelegator; 35 import org.ofbiz.entity.GenericEntityException; 36 import org.ofbiz.entity.GenericValue; 37 import org.ofbiz.entity.condition.EntityCondition; 38 import org.ofbiz.entity.condition.EntityConditionList; 39 import org.ofbiz.entity.condition.EntityExpr; 40 import org.ofbiz.entity.condition.EntityOperator; 41 import org.ofbiz.entity.util.EntityListIterator; 42 import org.ofbiz.service.DispatchContext; 43 import org.ofbiz.service.GenericServiceException; 44 import org.ofbiz.service.LocalDispatcher; 45 import org.ofbiz.service.ServiceUtil; 46 47 54 public class PromoServices { 55 56 public final static String module = PromoServices.class.getName(); 57 58 public static Map createProductPromoCodeSet(DispatchContext dctx, Map context) { 59 LocalDispatcher dispatcher = dctx.getDispatcher(); 61 Long quantity = (Long ) context.get("quantity"); 62 67 StringBuffer bankOfNumbers = new StringBuffer (); 68 for (long i = 0; i < quantity.longValue(); i++) { 69 Map createProductPromoCodeMap = null; 70 try { 71 createProductPromoCodeMap = dispatcher.runSync("createProductPromoCode", dctx.makeValidContext("createProductPromoCode", "IN", context)); 72 } catch (GenericServiceException err) { 73 return ServiceUtil.returnError("Could not create a bank of promo codes", null, null, createProductPromoCodeMap); 74 } 75 if (ServiceUtil.isError(createProductPromoCodeMap)) { 76 return ServiceUtil.returnError("Could not create a bank of promo codes", null, null, createProductPromoCodeMap); 78 } 79 bankOfNumbers.append((String ) createProductPromoCodeMap.get("productPromoCodeId")); 80 bankOfNumbers.append("<br/>"); 81 } 82 83 return ServiceUtil.returnSuccess(bankOfNumbers.toString()); 84 } 85 86 public static Map purgeOldStoreAutoPromos(DispatchContext dctx, Map context) { 87 GenericDelegator delegator = dctx.getDelegator(); 88 String productStoreId = (String ) context.get("productStoreId"); 89 Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); 90 91 List condList = new LinkedList (); 92 if (UtilValidate.isEmpty(productStoreId)) { 93 condList.add(new EntityExpr("productStoreId", EntityOperator.EQUALS, productStoreId)); 94 } 95 condList.add(new EntityExpr("userEntered", EntityOperator.EQUALS, "Y")); 96 condList.add(new EntityExpr("thruDate", EntityOperator.NOT_EQUAL, null)); 97 condList.add(new EntityExpr("thruDate", EntityOperator.LESS_THAN, nowTimestamp)); 98 EntityCondition cond = new EntityConditionList(condList, EntityOperator.AND); 99 100 try { 101 EntityListIterator eli = delegator.findListIteratorByCondition("ProductStorePromoAndAppl", cond, null, null); 102 GenericValue productStorePromoAndAppl = null; 103 while ((productStorePromoAndAppl = (GenericValue) eli.next()) != null) { 104 GenericValue productStorePromo = delegator.makeValue("ProductStorePromo", null); 105 productStorePromo.setAllFields(productStorePromoAndAppl, true, null, null); 106 productStorePromo.remove(); 107 } 108 eli.close(); 109 } catch (GenericEntityException e) { 110 String errMsg = "Error removing expired ProductStorePromo records: " + e.toString(); 111 Debug.logError(e, errMsg, module); 112 return ServiceUtil.returnError(errMsg); 113 } 114 115 return ServiceUtil.returnSuccess(); 116 } 117 } 118 | Popular Tags |