Author: sascharodekamp
Date: Tue Oct 25 11:19:35 2011 New Revision: 1188607 URL: http://svn.apache.org/viewvc?rev=1188607&view=rev Log: Optimize Promo code generation. Promo Codes which llok like: GT4eI1a are bad to enter for a customer, furthermore there are some literals which could esaylie mixed up (ie "0" and "O"). So the generation should be a little bit smarter. So we add three different options to generate a PromoCode: 1.) "clever" only capital letters ans numeric values (no O,0,I,1 ...) 2.) "normal" all aplha numeric charachters 3.) "sequenz" a sequenz number (default like before) Modified: ofbiz/trunk/applications/product/config/ProductUiLabels.xml ofbiz/trunk/applications/product/servicedef/services_pricepromo.xml ofbiz/trunk/applications/product/src/org/ofbiz/product/promo/PromoServices.java ofbiz/trunk/applications/product/webapp/catalog/promo/FindProductPromoCode.ftl Modified: ofbiz/trunk/applications/product/config/ProductUiLabels.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/config/ProductUiLabels.xml?rev=1188607&r1=1188606&r2=1188607&view=diff ============================================================================== --- ofbiz/trunk/applications/product/config/ProductUiLabels.xml (original) +++ ofbiz/trunk/applications/product/config/ProductUiLabels.xml Tue Oct 25 11:19:35 2011 @@ -19816,6 +19816,10 @@ <value xml:lang="en">: is not a valid promo code; must be between 1 and 20 characters</value> <value xml:lang="it">: non è un codice promozione valido; deve essere fra 1 e 20 caratteri</value> </property> + <property key="ProductPromoCodeLength"> + <value xml:lang="de">Aktionscodelänge</value> + <value xml:lang="en">product promo code length</value> + </property> <property key="ProductPromoCodeParties"> <value xml:lang="de">Aktionscode Akteure</value> <value xml:lang="en">Promo Code Parties</value> @@ -19909,6 +19913,26 @@ <value xml:lang="zh">ç¨æ·è¾å ¥</value> <value xml:lang="zh_TW">ç¨æ¶è¼¸å ¥</value> </property> + <property key="ProductPromoLayoutSmart"> + <value xml:lang="de">clever</value> + <value xml:lang="en">smart</value> + </property> + <property key="ProductPromoLayoutNormal"> + <value xml:lang="de">normal</value> + <value xml:lang="en">normal</value> + </property> + <property key="ProductPromoLayoutSeqNum"> + <value xml:lang="de">Sequenz</value> + <value xml:lang="en">sequence</value> + </property> + <property key="ProductPromoCodeLayout"> + <value xml:lang="de">Aussehen vom Aktionscode</value> + <value xml:lang="en">Promo code layout</value> + </property> + <property key="ProductPromoCodeLayoutTooltip"> + <value xml:lang="de">"clever": Vermeidet ähnliche Zeichen (0 und O, ...) | "normal": Alphanummerisch | "sequenz": Laufnummer</value> + <value xml:lang="en">"smart": Avoids equal Chars (0 and O, ...) | "normal": alphanumeric | "sequence": Sequence Number</value> + </property> <property key="ProductPromos"> <value xml:lang="de">Aktionen</value> <value xml:lang="en">Promos</value> Modified: ofbiz/trunk/applications/product/servicedef/services_pricepromo.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/services_pricepromo.xml?rev=1188607&r1=1188606&r2=1188607&view=diff ============================================================================== --- ofbiz/trunk/applications/product/servicedef/services_pricepromo.xml (original) +++ ofbiz/trunk/applications/product/servicedef/services_pricepromo.xml Tue Oct 25 11:19:35 2011 @@ -270,6 +270,8 @@ under the License. <exclude field-name="lastModifiedByUserLogin"/> </auto-attributes> <attribute name="quantity" type="Long" mode="IN" optional="false"/> + <attribute name="codeLength" type="Integer" mode="IN" optional="true" default-value="8"/> + <attribute name="promoCodeLayout" type="String" mode="IN" optional="true" default-value="sequence"/> </service> <service name="createProductPromoCond" default-entity-name="ProductPromoCond" engine="simple" Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/promo/PromoServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/promo/PromoServices.java?rev=1188607&r1=1188606&r2=1188607&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/promo/PromoServices.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/promo/PromoServices.java Tue Oct 25 11:19:35 2011 @@ -31,6 +31,7 @@ import java.util.Map; import javolution.util.FastList; import javolution.util.FastMap; +import org.apache.commons.lang.RandomStringUtils; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilMisc; @@ -55,33 +56,68 @@ public class PromoServices { public final static String module = PromoServices.class.getName(); public static final String resource = "ProductUiLabels"; - + protected final static char[] smartChars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', + 'Z', '2', '3', '4', '5', '6', '7', '8', '9' }; + public static Map<String, Object> createProductPromoCodeSet(DispatchContext dctx, Map<String, ? extends Object> context) { - //Delegator delegator = dctx.getDelegator(); + Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Long quantity = (Long) context.get("quantity"); - Locale locale = (Locale) context.get("locale"); - //Long useLimitPerCode = (Long) context.get("useLimitPerCode"); - //Long useLimitPerCustomer = (Long) context.get("useLimitPerCustomer"); - //GenericValue promoItem = null; - //GenericValue newItem = null; + int codeLength = (Integer) context.get("codeLength"); + String promoCodeLayout = (String) context.get("promoCodeLayout"); + // For PromoCodes we give the option not to use chars that are easy to mix up like 0<>O, 1<>I, ... + boolean useSmartLayout = false; + boolean useNormalLayout = false; + if ("smart".equals(promoCodeLayout)) { + useSmartLayout = true; + } else if ("normal".equals(promoCodeLayout)) { + useNormalLayout = true; + } + + String newPromoCodeId = ""; StringBuilder bankOfNumbers = new StringBuilder(); - for (long i = 0; i < quantity.longValue(); i++) { + bankOfNumbers.append("Following PromoCodes have been created: "); + for (long i = 0; i < quantity; i++) { Map<String, Object> createProductPromoCodeMap = null; + boolean foundUniqueNewCode = false; + long count = 0; + + while (!foundUniqueNewCode) { + if (useSmartLayout) { + newPromoCodeId = RandomStringUtils.random(codeLength, smartChars); + } else if (useNormalLayout) { + newPromoCodeId = RandomStringUtils.randomAlphanumeric(codeLength); + } + GenericValue existingPromoCode = null; + try { + existingPromoCode = delegator.findByPrimaryKeyCache("ProductPromoCode", "productPromoCodeId", newPromoCodeId); + } + catch (GenericEntityException e) { + Debug.logWarning("Could not find ProductPromoCode for just generated ID: " + newPromoCodeId, module); + } + if (existingPromoCode == null) { + foundUniqueNewCode = true; + } + + count++; + if (count > 999999) { + return ServiceUtil.returnError("Unable to locate unique PromoCode! Length [" + codeLength + "]"); + } + } try { - createProductPromoCodeMap = dispatcher.runSync("createProductPromoCode", dctx.makeValidContext("createProductPromoCode", "IN", context)); + Map<String, Object> newContext = dctx.makeValidContext("createProductPromoCode", "IN", context); + newContext.put("productPromoCodeId", newPromoCodeId); + createProductPromoCodeMap = dispatcher.runSync("createProductPromoCode", newContext); } catch (GenericServiceException err) { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "ProductPromoCodeCannotBeCreated", locale), null, null, createProductPromoCodeMap); + return ServiceUtil.returnError("Could not create a bank of promo codes", null, null, createProductPromoCodeMap); } if (ServiceUtil.isError(createProductPromoCodeMap)) { // what to do here? try again? - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "ProductPromoCodeCannotBeCreated", locale), null, null, createProductPromoCodeMap); + return ServiceUtil.returnError("Could not create a bank of promo codes", null, null, createProductPromoCodeMap); } bankOfNumbers.append((String) createProductPromoCodeMap.get("productPromoCodeId")); - bankOfNumbers.append("<br/>"); + bankOfNumbers.append(","); } return ServiceUtil.returnSuccess(bankOfNumbers.toString()); Modified: ofbiz/trunk/applications/product/webapp/catalog/promo/FindProductPromoCode.ftl URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/webapp/catalog/promo/FindProductPromoCode.ftl?rev=1188607&r1=1188606&r2=1188607&view=diff ============================================================================== --- ofbiz/trunk/applications/product/webapp/catalog/promo/FindProductPromoCode.ftl (original) +++ ofbiz/trunk/applications/product/webapp/catalog/promo/FindProductPromoCode.ftl Tue Oct 25 11:19:35 2011 @@ -53,6 +53,15 @@ under the License. <form method="post" action="<@ofbizUrl>createProductPromoCodeSet</@ofbizUrl>"> <input type="hidden" name="productPromoId" value="${productPromoId}"/> <span class="label">${uiLabelMap.CommonQuantity}:</span><input type="text" size="5" name="quantity" /> + <span class="label">${uiLabelMap.ProductPromoCodeLength}:</span><input type="text" size="12" name="codeLength" /> + <span class="label">${uiLabelMap.ProductPromoCodeLayout}:</span> + <select name="promoCodeLayout"> + <option value="smart">${uiLabelMap.ProductPromoLayoutSmart}</option> + <option value="normal">${uiLabelMap.ProductPromoLayoutNormal}</option> + <option value="sequence">${uiLabelMap.ProductPromoLayoutSeqNum}</option> + </select> + <span class="tooltip">${uiLabelMap.ProductPromoCodeLayoutTooltip}</span> + <br /> <span class="label">${uiLabelMap.ProductPromoUserEntered}:</span> <select name="userEntered"> <option value="Y">${uiLabelMap.CommonY}</option> |
Free forum by Nabble | Edit this page |