KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > config > ProductConfigWrapper


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

22
23 package org.ofbiz.product.config;
24
25 import java.io.Serializable JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Locale JavaDoc;
33
34 import org.ofbiz.base.util.UtilMisc;
35 import org.ofbiz.base.util.UtilValidate;
36 import org.ofbiz.entity.GenericDelegator;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.entity.util.EntityUtil;
39 import org.ofbiz.service.LocalDispatcher;
40
41
42 /**
43  * Product Config Wrapper: gets product config to display
44  *
45  * @author <a HREF="mailto:tiz@sastau.it">Jacopo Cappellato</a>
46  *
47  */

48
49 public class ProductConfigWrapper implements Serializable JavaDoc {
50     
51     public static final String JavaDoc module = ProductConfigWrapper.class.getName();
52     
53     protected GenericValue product = null; // the aggregated product
54
protected double basePrice = 0.0;
55     protected List JavaDoc questions = null; // ProductConfigs
56

57     /** Creates a new instance of ProductConfigWrapper */
58     public ProductConfigWrapper() {
59     }
60     
61     public ProductConfigWrapper(GenericDelegator delegator, LocalDispatcher dispatcher, String JavaDoc productId, String JavaDoc productStoreId, String JavaDoc catalogId, String JavaDoc webSiteId, String JavaDoc currencyUomId, Locale JavaDoc locale, GenericValue autoUserLogin) throws Exception JavaDoc {
62         init(delegator, dispatcher, productId, productStoreId, catalogId, webSiteId, currencyUomId, locale, autoUserLogin);
63     }
64
65     public ProductConfigWrapper(ProductConfigWrapper pcw) {
66         product = GenericValue.create(pcw.product);
67         basePrice = pcw.basePrice;
68         questions = new ArrayList JavaDoc();
69         for (int i = 0; i < pcw.questions.size(); i++) {
70             questions.add(new ConfigItem((ConfigItem)pcw.questions.get(i)));
71         }
72     }
73
74     private void init(GenericDelegator delegator, LocalDispatcher dispatcher, String JavaDoc productId, String JavaDoc productStoreId, String JavaDoc catalogId, String JavaDoc webSiteId, String JavaDoc currencyUomId, Locale JavaDoc locale, GenericValue autoUserLogin) throws Exception JavaDoc {
75         product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId));
76         if (product == null || !product.getString("productTypeId").equals("AGGREGATED")) {
77             throw new ProductConfigWrapperException("Product " + productId + " is not an AGGREGATED product.");
78         }
79         // get the base price
80
Map JavaDoc priceContext = UtilMisc.toMap("product", product, "prodCatalogId", catalogId, "webSiteId", webSiteId, "productStoreId", productStoreId,
81                                       "currencyUomId", currencyUomId, "autoUserLogin", autoUserLogin);
82         Map JavaDoc priceMap = dispatcher.runSync("calculateProductPrice", priceContext);
83         Double JavaDoc price = (Double JavaDoc)priceMap.get("price");
84         if (price != null) {
85             basePrice = price.doubleValue();
86         }
87         questions = new ArrayList JavaDoc();
88         List JavaDoc questionsValues = new ArrayList JavaDoc();
89         if (product.getString("productTypeId") != null && product.getString("productTypeId").equals("AGGREGATED")) {
90             questionsValues = delegator.findByAnd("ProductConfig", UtilMisc.toMap("productId", productId), UtilMisc.toList("sequenceNum"));
91             questionsValues = EntityUtil.filterByDate(questionsValues);
92             Iterator JavaDoc questionsValuesIt = questionsValues.iterator();
93             HashMap JavaDoc itemIds = new HashMap JavaDoc();
94             while (questionsValuesIt.hasNext()) {
95                 ConfigItem oneQuestion = new ConfigItem((GenericValue)questionsValuesIt.next());
96                 oneQuestion.setContent(locale, "text/html"); // TODO: mime-type shouldn't be hardcoded
97
if (itemIds.containsKey(oneQuestion.getConfigItem().getString("configItemId"))) {
98                     oneQuestion.setFirst(false);
99                 } else {
100                     itemIds.put(oneQuestion.getConfigItem().getString("configItemId"), null);
101                 }
102                 questions.add(oneQuestion);
103                 List JavaDoc configOptions = delegator.findByAnd("ProductConfigOption", UtilMisc.toMap("configItemId", oneQuestion.getConfigItemAssoc().getString("configItemId")), UtilMisc.toList("sequenceNum"));
104                 Iterator JavaDoc configOptionsIt = configOptions.iterator();
105                 while (configOptionsIt.hasNext()) {
106                     ConfigOption option = new ConfigOption(delegator, dispatcher, (GenericValue)configOptionsIt.next(), catalogId, webSiteId, currencyUomId, autoUserLogin);
107                     oneQuestion.addOption(option);
108                 }
109             }
110         }
111     }
112     
113     public void resetConfig() {
114         for (int i = 0; i < questions.size(); i++) {
115             ConfigItem ci = (ConfigItem)questions.get(i);
116             if (!ci.isStandard()) {
117                 List JavaDoc options = ci.getOptions();
118                 for (int j = 0; j < options.size(); j++) {
119                     ConfigOption co = (ConfigOption)options.get(j);
120                     co.setSelected(false);
121                 }
122             }
123         }
124     }
125     
126     public void setDefaultConfig() {
127         resetConfig();
128         for (int i = 0; i < questions.size(); i++) {
129             ConfigItem ci = (ConfigItem)questions.get(i);
130             if (ci.isMandatory()) {
131                 if (ci.getOptions().size() > 0) {
132                     ConfigOption co = (ConfigOption)ci.getOptions().get(0);
133                     co.setSelected(true);
134                 }
135             }
136         }
137     }
138     
139     public boolean equals(Object JavaDoc obj) {
140         if (obj == null || !(obj instanceof ProductConfigWrapper)) {
141             return false;
142         }
143         ProductConfigWrapper cw = (ProductConfigWrapper)obj;
144         if (!product.getString("productId").equals(cw.getProduct().getString("productId"))) {
145             return false;
146         }
147         List JavaDoc cwq = cw.getQuestions();
148         if (questions.size() != cwq.size()) {
149             return false;
150         }
151         for (int i = 0; i < questions.size(); i++) {
152             ConfigItem ci = (ConfigItem)questions.get(i);
153             if (!ci.equals(cwq.get(i))) {
154                 return false;
155             }
156         }
157         return true;
158     }
159
160     public String JavaDoc toString() {
161         return "" + questions;
162     }
163
164     public List JavaDoc getQuestions() {
165         return questions;
166     }
167     
168     public GenericValue getProduct() {
169         return product;
170     }
171     
172     public void setSelected(int question, int option) throws Exception JavaDoc {
173         ConfigItem ci = (ConfigItem)questions.get(question);
174         List JavaDoc avalOptions = ci.getOptions();
175         if (ci.isSingleChoice()) {
176             for (int j = 0; j < avalOptions.size(); j++) {
177                 ConfigOption oneOption = (ConfigOption)avalOptions.get(j);
178                 oneOption.setSelected(false);
179             }
180         }
181         ConfigOption theOption = null;
182         if (option >= 0 && option < avalOptions.size()) {
183             theOption = (ConfigOption)avalOptions.get(option);
184         }
185         if (theOption != null) {
186             theOption.setSelected(true);
187         }
188     }
189     
190     public List JavaDoc getSelectedOptions() {
191         List JavaDoc selectedOptions = new ArrayList JavaDoc();
192         for (int i = 0; i < questions.size(); i++) {
193             ConfigItem ci = (ConfigItem)questions.get(i);
194             if (ci.isStandard()) {
195                 selectedOptions.addAll(ci.getOptions());
196             } else {
197                 Iterator JavaDoc availOptions = ci.getOptions().iterator();
198                 while (availOptions.hasNext()) {
199                     ConfigOption oneOption = (ConfigOption)availOptions.next();
200                     if (oneOption.isSelected()) {
201                         selectedOptions.add(oneOption);
202                     }
203                 }
204             }
205         }
206         return selectedOptions;
207     }
208     
209     public double getTotalPrice() {
210         double totalPrice = basePrice;
211         List JavaDoc options = getSelectedOptions();
212         for (int i = 0; i < options.size(); i++) {
213             ConfigOption oneOption = (ConfigOption)options.get(i);
214             totalPrice += oneOption.getPrice();
215         }
216         return totalPrice;
217     }
218     
219     public boolean isCompleted() {
220         boolean completed = true;
221         for (int i = 0; i < questions.size(); i++) {
222             ConfigItem ci = (ConfigItem)questions.get(i);
223             if (!ci.isStandard() && ci.isMandatory()) {
224                 Iterator JavaDoc availOptions = ci.getOptions().iterator();
225                 while (availOptions.hasNext()) {
226                     ConfigOption oneOption = (ConfigOption)availOptions.next();
227                     if (oneOption.isSelected()) {
228                         completed = true;
229                         break;
230                     } else {
231                         completed = false;
232                     }
233                 }
234                 if (!completed) {
235                     break;
236                 }
237             }
238         }
239         return completed;
240     }
241     
242     public class ConfigItem implements java.io.Serializable JavaDoc {
243         GenericValue configItem = null;
244         GenericValue configItemAssoc = null;
245         ProductConfigItemContentWrapper content = null;
246         List JavaDoc options = null;
247         boolean first = true;
248         
249         public ConfigItem(GenericValue questionAssoc) throws Exception JavaDoc {
250             configItemAssoc = questionAssoc;
251             configItem = configItemAssoc.getRelatedOne("ConfigItemProductConfigItem");
252             options = new ArrayList JavaDoc();
253         }
254         
255         public ConfigItem(ConfigItem ci) {
256             configItem = GenericValue.create(ci.configItem);
257             configItemAssoc = GenericValue.create(ci.configItemAssoc);
258             options = new ArrayList JavaDoc();
259             for (int i = 0; i < ci.options.size(); i++) {
260                 options.add(new ConfigOption((ConfigOption)ci.options.get(i)));
261             }
262             first = ci.first;
263             content = ci.content; // FIXME: this should be cloned
264
}
265
266         public void setContent(Locale JavaDoc locale, String JavaDoc mimeTypeId) {
267             content = new ProductConfigItemContentWrapper(configItem, locale, mimeTypeId);
268         }
269         
270         public ProductConfigItemContentWrapper getContent() {
271             return content;
272         }
273         
274         public GenericValue getConfigItem() {
275             return configItem;
276         }
277         
278         public GenericValue getConfigItemAssoc() {
279             return configItemAssoc;
280         }
281
282         public boolean isStandard() {
283             return configItemAssoc.getString("configTypeId").equals("STANDARD");
284         }
285         
286         public boolean isSingleChoice() {
287             return configItem.getString("configItemTypeId").equals("SINGLE");
288         }
289         
290         public boolean isMandatory() {
291             return configItemAssoc.getString("isMandatory") != null && configItemAssoc.getString("isMandatory").equals("Y");
292         }
293         
294         public boolean isFirst() {
295             return first;
296         }
297         
298         public void setFirst(boolean newValue) {
299             first = newValue;
300         }
301         
302         public void addOption(ConfigOption option) {
303             options.add(option);
304         }
305         
306         public List JavaDoc getOptions() {
307             return options;
308         }
309         
310         public String JavaDoc getQuestion() {
311             String JavaDoc question = "";
312             if (UtilValidate.isNotEmpty(configItemAssoc.getString("description"))) {
313                 question = configItemAssoc.getString("description");
314             } else {
315                 if (content != null) {
316                     question = content.get("DESCRIPTION");
317                 } else {
318                     question = (configItem.getString("description") != null? configItem.getString("description"): "");
319                 }
320             }
321             return question;
322         }
323
324         public String JavaDoc getDescription() {
325             String JavaDoc description = "";
326             if (UtilValidate.isNotEmpty(configItemAssoc.getString("longDescription"))) {
327                 description = configItemAssoc.getString("longDescription");
328             } else {
329                 if (content != null) {
330                     description = content.get("LONG_DESCRIPTION");
331                 } else {
332                     description = (configItem.getString("longDescription") != null? configItem.getString("longDescription"): "");
333                 }
334             }
335             return description;
336         }
337
338         public boolean isSelected() {
339             if (isStandard()) return true;
340             Iterator JavaDoc availOptions = getOptions().iterator();
341             while (availOptions.hasNext()) {
342                 ConfigOption oneOption = (ConfigOption)availOptions.next();
343                 if (oneOption.isSelected()) {
344                     return true;
345                 }
346             }
347             return false;
348         }
349         
350         public ConfigOption getSelected() {
351             Iterator JavaDoc availOptions = getOptions().iterator();
352             while (availOptions.hasNext()) {
353                 ConfigOption oneOption = (ConfigOption)availOptions.next();
354                 if (oneOption.isSelected()) {
355                     return oneOption;
356                 }
357             }
358             return null;
359         }
360         
361         public boolean equals(Object JavaDoc obj) {
362             if (obj == null || !(obj instanceof ConfigItem)) {
363                 return false;
364             }
365             ConfigItem ci = (ConfigItem)obj;
366             if (!configItem.getString("configItemId").equals(ci.getConfigItem().getString("configItemId"))) {
367                 return false;
368             }
369             List JavaDoc opts = ci.getOptions();
370             if (options.size() != opts.size()) {
371                 return false;
372             }
373             for (int i = 0; i < options.size(); i++) {
374                 ConfigOption co = (ConfigOption)options.get(i);
375                 if (!co.equals(opts.get(i))) {
376                     return false;
377                 }
378             }
379             return true;
380         }
381
382         public String JavaDoc toString() {
383             return configItem.getString("configItemId");
384         }
385     }
386     
387     public class ConfigOption implements java.io.Serializable JavaDoc {
388         double optionPrice = 0;
389         Date JavaDoc availabilityDate = null;
390         List JavaDoc componentList = null; // lists of ProductConfigProduct
391
GenericValue configOption = null;
392         boolean selected = false;
393         boolean available = true;
394         
395         public ConfigOption(GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue option, String JavaDoc catalogId, String JavaDoc webSiteId, String JavaDoc currencyUomId, GenericValue autoUserLogin) throws Exception JavaDoc {
396             configOption = option;
397             componentList = option.getRelated("ConfigOptionProductConfigProduct");
398             Iterator JavaDoc componentsIt = componentList.iterator();
399             while (componentsIt.hasNext()) {
400                 double price = 0;
401                 GenericValue oneComponent = (GenericValue)componentsIt.next();
402                 // Get the component's price
403
Map JavaDoc fieldMap = UtilMisc.toMap("product", oneComponent.getRelatedOne("ProductProduct"), "prodCatalogId", catalogId, "webSiteId", webSiteId,
404                         "currencyUomId", currencyUomId, "productPricePurposeId", "COMPONENT_PRICE", "autoUserLogin", autoUserLogin);
405                 Map JavaDoc priceMap = dispatcher.runSync("calculateProductPrice", fieldMap);
406                 Double JavaDoc componentPrice = (Double JavaDoc) priceMap.get("price");
407                 double mult = 1;
408                 if (oneComponent.getDouble("quantity") != null) {
409                     mult = oneComponent.getDouble("quantity").doubleValue();
410                 }
411                 if (mult == 0) {
412                     mult = 1;
413                 }
414                 if (componentPrice != null) {
415                     price = componentPrice.doubleValue();
416                 } else {
417                     fieldMap.put("productPricePurposeId", "PURCHASE");
418                     Map JavaDoc purchasePriceResultMap = dispatcher.runSync("calculateProductPrice", fieldMap);
419                     Double JavaDoc purchasePrice = (Double JavaDoc) purchasePriceResultMap.get("price");
420                     if (purchasePrice != null) {
421                         price = purchasePrice.doubleValue();
422                     }
423                 }
424                 optionPrice += (price * mult);
425                 // TODO: get the component's availability date
426
}
427         }
428         
429         public ConfigOption(ConfigOption co) {
430             configOption = GenericValue.create(co.configOption);
431             componentList = new ArrayList JavaDoc();
432             for (int i = 0; i < co.componentList.size(); i++) {
433                 componentList.add(GenericValue.create((GenericValue)co.componentList.get(i)));
434             }
435             optionPrice = co.optionPrice;
436             available = co.available;
437             selected = co.selected;
438         }
439
440         public String JavaDoc getDescription() {
441             return (configOption.getString("description") != null? configOption.getString("description"): "no description");
442         }
443         
444         public double getPrice() {
445             return optionPrice;
446         }
447         
448         public boolean isSelected() {
449             return selected;
450         }
451         
452         public void setSelected(boolean newValue) {
453             selected = newValue;
454         }
455         
456         public boolean isAvailable() {
457             return available;
458         }
459         
460         public void setAvailable(boolean newValue) {
461             available = newValue;
462         }
463
464         public List JavaDoc getComponents() {
465             return componentList;
466         }
467         
468         public boolean equals(Object JavaDoc obj) {
469             if (obj == null || !(obj instanceof ConfigOption)) {
470                 return false;
471             }
472             ConfigOption co = (ConfigOption)obj;
473             // TODO: we should compare also the GenericValues
474

475             return isSelected() == co.isSelected();
476         }
477         
478         public String JavaDoc toString() {
479             return configOption.getString("configItemId") + "/" + configOption.getString("configOptionId") + (isSelected()? "*": "");
480         }
481
482     }
483     
484 }
485
Popular Tags