KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > promo > PromoServices


1 /*
2  * $Id: PromoServices.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.product.promo;
25
26 import java.sql.Timestamp JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
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 /**
48  * Promotions Services
49  *
50  * @author Nathan De Graw
51  * @version $Rev: 5462 $
52  * @since 3.0
53  */

54 public class PromoServices {
55
56     public final static String JavaDoc module = PromoServices.class.getName();
57
58     public static Map JavaDoc createProductPromoCodeSet(DispatchContext dctx, Map JavaDoc context) {
59         //GenericDelegator delegator = dctx.getDelegator();
60
LocalDispatcher dispatcher = dctx.getDispatcher();
61         Long JavaDoc quantity = (Long JavaDoc) context.get("quantity");
62         //Long useLimitPerCode = (Long) context.get("useLimitPerCode");
63
//Long useLimitPerCustomer = (Long) context.get("useLimitPerCustomer");
64
//GenericValue promoItem = null;
65
//GenericValue newItem = null;
66

67         StringBuffer JavaDoc bankOfNumbers = new StringBuffer JavaDoc();
68         for (long i = 0; i < quantity.longValue(); i++) {
69             Map JavaDoc 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                 // what to do here? try again?
77
return ServiceUtil.returnError("Could not create a bank of promo codes", null, null, createProductPromoCodeMap);
78             }
79             bankOfNumbers.append((String JavaDoc) createProductPromoCodeMap.get("productPromoCodeId"));
80             bankOfNumbers.append("<br/>");
81         }
82
83         return ServiceUtil.returnSuccess(bankOfNumbers.toString());
84     }
85
86     public static Map JavaDoc purgeOldStoreAutoPromos(DispatchContext dctx, Map JavaDoc context) {
87         GenericDelegator delegator = dctx.getDelegator();
88         String JavaDoc productStoreId = (String JavaDoc) context.get("productStoreId");
89         Timestamp JavaDoc nowTimestamp = UtilDateTime.nowTimestamp();
90         
91         List JavaDoc condList = new LinkedList JavaDoc();
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 JavaDoc 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