001: /*
002: * $Id: PromoServices.java,v 1.2 2004/01/15 17:45:38 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
007: * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
009: *
010: * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
011: *
012: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
013: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
014: * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
015: */
016: package org.ofbiz.product.promo;
017:
018: import java.sql.Timestamp;
019: import java.util.LinkedList;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.base.util.UtilDateTime;
025: import org.ofbiz.base.util.UtilValidate;
026: import org.ofbiz.entity.GenericDelegator;
027: import org.ofbiz.entity.GenericEntityException;
028: import org.ofbiz.entity.GenericValue;
029: import org.ofbiz.entity.condition.EntityCondition;
030: import org.ofbiz.entity.condition.EntityConditionList;
031: import org.ofbiz.entity.condition.EntityExpr;
032: import org.ofbiz.entity.condition.EntityOperator;
033: import org.ofbiz.entity.util.EntityListIterator;
034: import org.ofbiz.service.DispatchContext;
035: import org.ofbiz.service.GenericServiceException;
036: import org.ofbiz.service.LocalDispatcher;
037: import org.ofbiz.service.ServiceUtil;
038:
039: /**
040: * Inventory Services
041: *
042: * @author Nathan De Graw
043: * @version $Revision: 1.2 $
044: * @since 3.0
045: */
046: public class PromoServices {
047:
048: public final static String module = PromoServices.class.getName();
049:
050: public static Map createProductPromoCodeSet(DispatchContext dctx,
051: Map context) {
052: //GenericDelegator delegator = dctx.getDelegator();
053: LocalDispatcher dispatcher = dctx.getDispatcher();
054: Long quantity = (Long) context.get("quantity");
055: //Long useLimitPerCode = (Long) context.get("useLimitPerCode");
056: //Long useLimitPerCustomer = (Long) context.get("useLimitPerCustomer");
057: //GenericValue promoItem = null;
058: //GenericValue newItem = null;
059:
060: StringBuffer bankOfNumbers = new StringBuffer();
061: for (long i = 0; i < quantity.longValue(); i++) {
062: Map createProductPromoCodeMap = null;
063: try {
064: createProductPromoCodeMap = dispatcher.runSync(
065: "createProductPromoCode", dctx
066: .makeValidContext(
067: "createProductPromoCode", "IN",
068: context));
069: } catch (GenericServiceException err) {
070: return ServiceUtil.returnError(
071: "Could not create a bank of promo codes", null,
072: null, createProductPromoCodeMap);
073: }
074: if (ServiceUtil.isError(createProductPromoCodeMap)) {
075: // what to do here? try again?
076: return ServiceUtil.returnError(
077: "Could not create a bank of promo codes", null,
078: null, createProductPromoCodeMap);
079: }
080: bankOfNumbers.append((String) createProductPromoCodeMap
081: .get("productPromoCodeId"));
082: bankOfNumbers.append("<br>");
083: }
084:
085: return ServiceUtil.returnSuccess(bankOfNumbers.toString());
086: }
087:
088: public static Map purgeOldStoreAutoPromos(DispatchContext dctx,
089: Map context) {
090: GenericDelegator delegator = dctx.getDelegator();
091: String productStoreId = (String) context.get("productStoreId");
092: Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
093:
094: List condList = new LinkedList();
095: if (UtilValidate.isEmpty(productStoreId)) {
096: condList.add(new EntityExpr("productStoreId",
097: EntityOperator.EQUALS, productStoreId));
098: }
099: condList.add(new EntityExpr("userEntered",
100: EntityOperator.EQUALS, "Y"));
101: condList.add(new EntityExpr("thruDate",
102: EntityOperator.NOT_EQUAL, null));
103: condList.add(new EntityExpr("thruDate",
104: EntityOperator.LESS_THAN, nowTimestamp));
105: EntityCondition cond = new EntityConditionList(condList,
106: EntityOperator.AND);
107:
108: try {
109: EntityListIterator eli = delegator
110: .findListIteratorByCondition(
111: "ProductStorePromoAndAppl", cond, null,
112: null);
113: GenericValue productStorePromoAndAppl = null;
114: while ((productStorePromoAndAppl = (GenericValue) eli
115: .next()) != null) {
116: GenericValue productStorePromo = delegator.makeValue(
117: "ProductStorePromo", null);
118: productStorePromo.setAllFields(
119: productStorePromoAndAppl, true, null, null);
120: productStorePromo.remove();
121: }
122: eli.close();
123: } catch (GenericEntityException e) {
124: String errMsg = "Error removing expired ProductStorePromo records: "
125: + e.toString();
126: Debug.logError(e, errMsg, module);
127: return ServiceUtil.returnError(errMsg);
128: }
129:
130: return ServiceUtil.returnSuccess();
131: }
132: }
|