You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2007/09/11 23:32:13 UTC
svn commit: r574700 - in /ofbiz/trunk/applications/product:
src/org/ofbiz/product/promo/PromoServices.java
webapp/catalog/WEB-INF/controller.xml
webapp/catalog/promo/EditProductPromoCode.ftl
webapp/catalog/promo/FindProductPromoCode.ftl
Author: jaz
Date: Tue Sep 11 14:32:12 2007
New Revision: 574700
URL: http://svn.apache.org/viewvc?rev=574700&view=rev
Log:
added code to support importing promo codes and email addresses from a plain text file (one line for each code or email)
Modified:
ofbiz/trunk/applications/product/src/org/ofbiz/product/promo/PromoServices.java
ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml
ofbiz/trunk/applications/product/webapp/catalog/promo/EditProductPromoCode.ftl
ofbiz/trunk/applications/product/webapp/catalog/promo/FindProductPromoCode.ftl
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=574700&r1=574699&r2=574700&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 Sep 11 14:32:12 2007
@@ -18,13 +18,11 @@
*******************************************************************************/
package org.ofbiz.product.promo;
-import java.sql.Timestamp;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
+import javolution.util.FastList;
+import javolution.util.FastMap;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
+import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
@@ -33,11 +31,17 @@
import org.ofbiz.entity.condition.EntityConditionList;
import org.ofbiz.entity.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;
+import org.ofbiz.entity.util.ByteWrapper;
import org.ofbiz.entity.util.EntityListIterator;
-import org.ofbiz.service.DispatchContext;
-import org.ofbiz.service.GenericServiceException;
-import org.ofbiz.service.LocalDispatcher;
-import org.ofbiz.service.ServiceUtil;
+import org.ofbiz.service.*;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.sql.Timestamp;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
/**
* Promotions Services
@@ -103,6 +107,137 @@
return ServiceUtil.returnError(errMsg);
}
+ return ServiceUtil.returnSuccess();
+ }
+
+ public static Map importPromoCodesFromFile(DispatchContext dctx, Map context) {
+ LocalDispatcher dispatcher = dctx.getDispatcher();
+
+ // check the uploaded file
+ ByteWrapper wrapper = (ByteWrapper) context.get("uploadedFile");
+ if (wrapper == null) {
+ return ServiceUtil.returnError("Uploaded file not valid or corrupted");
+ }
+
+ // get the createProductPromoCode Model
+ ModelService promoModel;
+ try {
+ promoModel = dispatcher.getDispatchContext().getModelService("createProductPromoCode");
+ } catch (GenericServiceException e) {
+ Debug.logError(e, module);
+ return ServiceUtil.returnError(e.getMessage());
+ }
+
+ // make a temp context for invocations
+ Map invokeCtx = promoModel.makeValid(context, ModelService.IN_PARAM);
+
+ // read the bytes into a reader
+ BufferedReader reader = new BufferedReader(new StringReader(new String(wrapper.getBytes())));
+ List errors = FastList.newInstance();
+ int lines = 0;
+ String line;
+
+ // read the uploaded file and process each line
+ try {
+ while ((line = reader.readLine()) != null) {
+ // check to see if we should ignore this line
+ if (line.length() > 0 && !line.startsWith("#")) {
+ if (line.length() > 0 && line.length() <= 20) {
+ // valid promo code
+ Map inContext = FastMap.newInstance();
+ inContext.putAll(invokeCtx);
+ inContext.put("productPromoCodeId", line);
+ Map result = dispatcher.runSync("createProductPromoCode", inContext);
+ if (result != null && ServiceUtil.isError(result)) {
+ errors.add(line + ": " + ServiceUtil.getErrorMessage(result));
+ }
+ } else {
+ // not valid ignore and notify
+ errors.add(line + ": is not a valid promo code; must be between 1 and 20 characters");
+ }
+ ++lines;
+ }
+ }
+ } catch (IOException e) {
+ Debug.logError(e, module);
+ return ServiceUtil.returnError(e.getMessage());
+ } catch (GenericServiceException e) {
+ Debug.logError(e, module);
+ return ServiceUtil.returnError(e.getMessage());
+ } finally {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ Debug.logError(e, module);
+ }
+ }
+
+ // return errors or success
+ if (errors.size() > 0) {
+ return ServiceUtil.returnError(errors);
+ } else if (lines == 0) {
+ return ServiceUtil.returnError("Empty file; nothing to do");
+ }
+
+ return ServiceUtil.returnSuccess();
+ }
+
+ public static Map importPromoCodeEmailsFromFile(DispatchContext dctx, Map context) {
+ LocalDispatcher dispatcher = dctx.getDispatcher();
+
+ String productPromoCodeId = (String) context.get("productPromoCodeId");
+ ByteWrapper wrapper = (ByteWrapper) context.get("uploadedFile");
+ GenericValue userLogin = (GenericValue) context.get("userLogin");
+
+ if (wrapper == null) {
+ return ServiceUtil.returnError("Uploaded file not valid or corrupted");
+ }
+
+ // read the bytes into a reader
+ BufferedReader reader = new BufferedReader(new StringReader(new String(wrapper.getBytes())));
+ List errors = FastList.newInstance();
+ int lines = 0;
+ String line;
+
+ // read the uploaded file and process each line
+ try {
+ while ((line = reader.readLine()) != null) {
+ if (line.length() > 0 && !line.startsWith("#")) {
+ if (UtilValidate.isEmail(line)) {
+ // valid email address
+ Map result = dispatcher.runSync("createProductPromoCodeEmail", UtilMisc.toMap("productPromoCodeId",
+ productPromoCodeId, "emailAddress", line, "userLogin", userLogin));
+ if (result != null && ServiceUtil.isError(result)) {
+ errors.add(line + ": " + ServiceUtil.getErrorMessage(result));
+ }
+ } else {
+ // not valid ignore and notify
+ errors.add(line + ": is not a valid email address");
+ }
+ ++lines;
+ }
+ }
+ } catch (IOException e) {
+ Debug.logError(e, module);
+ return ServiceUtil.returnError(e.getMessage());
+ } catch (GenericServiceException e) {
+ Debug.logError(e, module);
+ return ServiceUtil.returnError(e.getMessage());
+ } finally {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ Debug.logError(e, module);
+ }
+ }
+
+ // return errors or success
+ if (errors.size() > 0) {
+ return ServiceUtil.returnError(errors);
+ } else if (lines == 0) {
+ return ServiceUtil.returnError("Empty file; nothing to do");
+ }
+
return ServiceUtil.returnSuccess();
}
}
Modified: ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml?rev=574700&r1=574699&r2=574700&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml (original)
+++ ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml Tue Sep 11 14:32:12 2007
@@ -1809,6 +1809,12 @@
<event type="service" path="" invoke="deleteProductPromoCodeEmail"/>
<response name="success" type="view" value="EditProductPromoCode"/>
<response name="error" type="view" value="EditProductPromoCode"/>
+ </request-map>
+ <request-map uri="createBulkProductPromoCodeEmail">
+ <security https="true" auth="true"/>
+ <event type="service" path="" invoke="createBulkProductPromoCodeEmail"/>
+ <response name="success" type="view" value="EditProductPromoCode"/>
+ <response name="error" type="view" value="EditProductPromoCode"/>
</request-map>
<request-map uri="createProductPromoCodeParty">
@@ -1826,6 +1832,12 @@
<request-map uri="createProductPromoCodeSet">
<security https="true" auth="true"/>
<event type="service" path="" invoke="createProductPromoCodeSet"/>
+ <response name="success" type="view" value="FindProductPromoCode"/>
+ <response name="error" type="view" value="FindProductPromoCode"/>
+ </request-map>
+ <request-map uri="createBulkProductPromoCode">
+ <security https="true" auth="true"/>
+ <event type="service" path="" invoke="createBulkProductPromoCode"/>
<response name="success" type="view" value="FindProductPromoCode"/>
<response name="error" type="view" value="FindProductPromoCode"/>
</request-map>
Modified: ofbiz/trunk/applications/product/webapp/catalog/promo/EditProductPromoCode.ftl
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/webapp/catalog/promo/EditProductPromoCode.ftl?rev=574700&r1=574699&r2=574700&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/webapp/catalog/promo/EditProductPromoCode.ftl (original)
+++ ofbiz/trunk/applications/product/webapp/catalog/promo/EditProductPromoCode.ftl Tue Sep 11 14:32:12 2007
@@ -24,23 +24,31 @@
</#if>
<h3>${uiLabelMap.ProductPromoCodeEmails}</h3>
<#list productPromoCodeEmails as productPromoCodeEmail>
- <div class="tabletext"><a href="<@o...@ofbizUrl>" class="buttontext">[X]</a> ${productPromoCodeEmail.emailAddress}</div>
+ <div class="tabletext"><a href="<@o...@ofbizUrl>" class="buttontext">[X]</a> ${productPromoCodeEmail.emailAddress}</div>
</#list>
<div class="tabletext">
<form method="post" action="<@o...@ofbizUrl>" style="margin: 0;">
<input type="hidden" name="productPromoCodeId" value="${productPromoCodeId?if_exists}"/>
+ <input type="hidden" name="productPromoId" value="${productPromoId}"/>
${uiLabelMap.ProductAddEmail} : <input type="text" size="40" name="emailAddress" class="inputBox">
<input type="submit" value="${uiLabelMap.CommonAdd}">
</form>
+ <form method="post" action="<@o...@ofbizUrl>" enctype="multipart/form-data" style="margin: 0;">
+ <input type="hidden" name="productPromoCodeId" value="${productPromoCodeId?if_exists}"/>
+ <input type="hidden" name="productPromoId" value="${productPromoId}"/>
+ <input type="file" size="40" name="uploadedFile" class="inputBox">
+ <input type="submit" value="${uiLabelMap.CommonUpload}">
+ </form>
</div>
<h3>${uiLabelMap.ProductPromoCodeParties}</h3>
<#list productPromoCodeParties as productPromoCodeParty>
- <div class="tabletext"><a href="<@o...@ofbizUrl>" class="buttontext">[X]</a> ${productPromoCodeParty.partyId}</div>
+ <div class="tabletext"><a href="<@o...@ofbizUrl>" class="buttontext">[X]</a> ${productPromoCodeParty.partyId}</div>
</#list>
<div class="tabletext">
<form method="post" action="<@o...@ofbizUrl>" style="margin: 0;">
<input type="hidden" name="productPromoCodeId" value="${productPromoCodeId?if_exists}"/>
+ <input type="hidden" name="productPromoId" value="${productPromoId}"/>
${uiLabelMap.ProductAddPartyId} : <input type="text" size="10" name="partyId" class="inputBox">
<input type="submit" value="${uiLabelMap.CommonAdd}">
</form>
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=574700&r1=574699&r2=574700&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/webapp/catalog/promo/FindProductPromoCode.ftl (original)
+++ ofbiz/trunk/applications/product/webapp/catalog/promo/FindProductPromoCode.ftl Tue Sep 11 14:32:12 2007
@@ -50,13 +50,31 @@
</#list>
</table>
<br/>
+
+ <h3>${uiLabelMap.ProductPromotionUploadSetOfPromotionCodes}:</h3>
+ <div class="tabletext">
+ <form method="post" action="<@o...@ofbizUrl>" enctype="multipart/form-data" style="margin: 0;">
+ <input type="hidden" name="productPromoId" value="${productPromoId}"/>
+ ${uiLabelMap.ProductPromoUserEntered}: <select name="userEntered" class="selectBox"><option>N</option><option>Y</option></select>
+ ${uiLabelMap.ProductPromotionReqEmailOrParty}: <select name="requireEmailOrParty" class="selectBox"><option>N</option><option>Y</option></select>
+ ${uiLabelMap.ProductPromotionUseLimits}:
+ ${uiLabelMap.ProductPromotionPerCode}<input type="text" size="5" name="useLimitPerCode" class="inputBox">
+ ${uiLabelMap.ProductPromotionPerCustomer}<input type="text" size="5" name="useLimitPerCustomer" class="inputBox">
+ <div>
+ <input type="file" size="40" name="uploadedFile" class="inputBox">
+ <input type="submit" value="${uiLabelMap.CommonUpload}">
+ </div>
+ </form>
+ </div>
+ <br/>
+
<h3>${uiLabelMap.ProductPromotionAddSetOfPromotionCodes}:</h3>
<div class="tabletext">
- <form method="post" action="<@o...@ofbizUrl>" style="margin: 0;">
- <input type="hidden" name="userEntered" value="N"/>
- <input type="hidden" name="requireEmailOrParty" value="N"/>
+ <form method="post" action="<@o...@ofbizUrl>" style="margin: 0;">
<input type="hidden" name="productPromoId" value="${productPromoId}"/>
${uiLabelMap.CommonQuantity}: <input type="text" size="5" name="quantity" class="inputBox">
+ ${uiLabelMap.ProductPromoUserEntered}: <select name="userEntered" class="selectBox"><option>N</option><option>Y</option></select>
+ ${uiLabelMap.ProductPromotionReqEmailOrParty}: <select name="requireEmailOrParty" class="selectBox"><option>N</option><option>Y</option></select>
${uiLabelMap.ProductPromotionUseLimits}:
${uiLabelMap.ProductPromotionPerCode}<input type="text" size="5" name="useLimitPerCode" class="inputBox">
${uiLabelMap.ProductPromotionPerCustomer}<input type="text" size="5" name="useLimitPerCustomer" class="inputBox">