KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > product > VariantEvents


1 /*
2  * $Id: VariantEvents.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-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.product;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.ofbiz.base.util.Debug;
30 import org.ofbiz.base.util.UtilDateTime;
31 import org.ofbiz.base.util.UtilMisc;
32 import org.ofbiz.base.util.UtilValidate;
33 import org.ofbiz.base.util.UtilProperties;
34 import org.ofbiz.base.util.UtilHttp;
35 import org.ofbiz.entity.GenericDelegator;
36 import org.ofbiz.entity.GenericEntityException;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.entity.transaction.GenericTransactionException;
39 import org.ofbiz.entity.transaction.TransactionUtil;
40 import org.ofbiz.security.Security;
41
42 import java.util.Map JavaDoc;
43
44 /**
45  * Product Variant Related Events
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @version $Rev: 5462 $
49  * @since 2.0
50  */

51 public class VariantEvents {
52
53     public static final String JavaDoc module = VariantEvents.class.getName();
54     public static final String JavaDoc resource = "ProductUiLabels";
55
56     /** Creates variant products from a virtual product and a combination of selectable features
57      *@param request The HTTPRequest object for the current request
58      *@param response The HTTPResponse object for the current request
59      *@return String specifying the exit status of this event
60      */

61     public static String JavaDoc quickAddChosenVariant(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
62         String JavaDoc errMsg = "";
63         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
64         Security security = (Security) request.getAttribute("security");
65
66         String JavaDoc productId = request.getParameter("productId");
67         String JavaDoc variantProductId = request.getParameter("variantProductId");
68         String JavaDoc featureTypeSizeStr = request.getParameter("featureTypeSize");
69
70         if (UtilValidate.isEmpty(productId)) {
71             errMsg = UtilProperties.getMessage(resource,"variantevents.productId_required_but_missing", UtilHttp.getLocale(request));
72             request.setAttribute("_ERROR_MESSAGE_", errMsg);
73             return "error";
74         }
75
76         if (UtilValidate.isEmpty(variantProductId)) {
77             errMsg = UtilProperties.getMessage(resource,"variantevents.variantProductId_required_but_missing_enter_an_id", UtilHttp.getLocale(request));
78             request.setAttribute("_ERROR_MESSAGE_", errMsg);
79             return "error";
80         }
81
82         int featureTypeSize = 0;
83
84         try {
85             featureTypeSize = Integer.parseInt(featureTypeSizeStr);
86         } catch (NumberFormatException JavaDoc e) {
87             Map JavaDoc messageMap = UtilMisc.toMap("featureTypeSizeStr", featureTypeSizeStr);
88             errMsg = UtilProperties.getMessage(resource,"variantevents.featureTypeSize_not_number", messageMap, UtilHttp.getLocale(request));
89             request.setAttribute("_ERROR_MESSAGE_", errMsg);
90             return "error";
91         }
92
93         try {
94             boolean beganTransacton = TransactionUtil.begin();
95
96             try {
97                 // read the product, duplicate it with the given id
98
GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId));
99                 if (product == null) {
100                     Map JavaDoc messageMap = UtilMisc.toMap("productId", productId);
101                     errMsg = UtilProperties.getMessage(resource,"variantevents.product_not_found_with_ID", messageMap, UtilHttp.getLocale(request));
102                     TransactionUtil.rollback(beganTransacton, errMsg, null);
103                     request.setAttribute("_ERROR_MESSAGE_", errMsg);
104                     return "error";
105                 }
106
107                 // check if product exists
108
GenericValue variantProduct = delegator.findByPrimaryKey("Product",UtilMisc.toMap("productId", variantProductId));
109                 if (variantProduct == null) {
110                     //if product does not exist
111
variantProduct = GenericValue.create(product);
112                     variantProduct.set("productId", variantProductId);
113                     variantProduct.set("isVirtual", "N");
114                     variantProduct.set("isVariant", "Y");
115                     variantProduct.set("primaryProductCategoryId", null);
116                     //create new
117
variantProduct.create();
118                 } else {
119                     //if product does exist
120
variantProduct.set("isVirtual", "N");
121                     variantProduct.set("isVariant", "Y");
122                     variantProduct.set("primaryProductCategoryId", null);
123                     //update entry
124
variantProduct.store();
125                 }
126
127                 // add an association from productId to variantProductId of the PRODUCT_VARIANT
128
GenericValue productAssoc = delegator.makeValue("ProductAssoc",
129                         UtilMisc.toMap("productId", productId, "productIdTo", variantProductId,
130                             "productAssocTypeId", "PRODUCT_VARIANT", "fromDate", UtilDateTime.nowTimestamp()));
131                 productAssoc.create();
132
133                 // add the selected standard features to the new product given the productFeatureIds
134
for (int i = 0; i < featureTypeSize; i++) {
135                     String JavaDoc productFeatureId = request.getParameter("feature_" + i);
136                     if (productFeatureId == null) {
137                         Map JavaDoc messageMap = UtilMisc.toMap("i", Integer.toString(i));
138                         errMsg = UtilProperties.getMessage(resource,"variantevents.productFeatureId_for_feature_type_number_not_found", messageMap, UtilHttp.getLocale(request));
139                         TransactionUtil.rollback(beganTransacton, errMsg, null);
140                         request.setAttribute("_ERROR_MESSAGE_", errMsg);
141                         return "error";
142                     }
143
144                     GenericValue productFeature = delegator.findByPrimaryKey("ProductFeature", UtilMisc.toMap("productFeatureId", productFeatureId));
145
146                     GenericValue productFeatureAppl = delegator.makeValue("ProductFeatureAppl",
147                             UtilMisc.toMap("productId", variantProductId, "productFeatureId", productFeatureId,
148                                 "productFeatureApplTypeId", "STANDARD_FEATURE", "fromDate", UtilDateTime.nowTimestamp()));
149
150                     // set the default seq num if it's there...
151
if (productFeature != null) {
152                         productFeatureAppl.set("sequenceNum", productFeature.get("defaultSequenceNum"));
153                     }
154
155                     productFeatureAppl.create();
156                 }
157
158                 TransactionUtil.commit(beganTransacton);
159             } catch (GenericEntityException e) {
160                 Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.toString());
161                 errMsg = UtilProperties.getMessage(resource,"variantevents.entity_error_quick_add_variant_data", messageMap, UtilHttp.getLocale(request));
162                 TransactionUtil.rollback(beganTransacton, errMsg, null);
163                 Debug.logError(e, "Entity error creating quick add variant data", module);
164                 request.setAttribute("_ERROR_MESSAGE_", errMsg);
165                 return "error";
166             }
167         } catch (GenericTransactionException e) {
168             Debug.logError(e, "Transaction error creating quick add variant data", module);
169             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.toString());
170             errMsg = UtilProperties.getMessage(resource,"variantevents.transaction_error_quick_add_variant_data", messageMap, UtilHttp.getLocale(request));
171             request.setAttribute("_ERROR_MESSAGE_", errMsg);
172             return "error";
173         }
174
175         Map JavaDoc messageMap = UtilMisc.toMap("variantProductId", variantProductId);
176         String JavaDoc sucMsg = UtilProperties.getMessage(resource,"variantevents.successfully_created_variant_product_with_id", messageMap, UtilHttp.getLocale(request));
177         request.setAttribute("_EVENT_MESSAGE_", sucMsg);
178         return "success";
179     }
180 }
181
Popular Tags