📄 productconfigwrapper.java
字号:
/* * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, 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. */package org.ofbiz.product.config;import java.io.Serializable;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.HashMap;import java.util.Locale;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.service.LocalDispatcher;/** * Product Config Wrapper: gets product config to display * * @author <a href="mailto:tiz@sastau.it">Jacopo Cappellato</a> * */public class ProductConfigWrapper implements Serializable { public static final String module = ProductConfigWrapper.class.getName(); protected GenericValue product = null; // the aggregated product protected double basePrice = 0.0; protected List questions = null; // ProductConfigs /** Creates a new instance of ProductConfigWrapper */ public ProductConfigWrapper() { } public ProductConfigWrapper(GenericDelegator delegator, LocalDispatcher dispatcher, String productId, String productStoreId, String catalogId, String webSiteId, String currencyUomId, Locale locale, GenericValue autoUserLogin) throws Exception { init(delegator, dispatcher, productId, productStoreId, catalogId, webSiteId, currencyUomId, locale, autoUserLogin); } public ProductConfigWrapper(ProductConfigWrapper pcw) { product = GenericValue.create(pcw.product); basePrice = pcw.basePrice; questions = new ArrayList(); for (int i = 0; i < pcw.questions.size(); i++) { questions.add(new ConfigItem((ConfigItem)pcw.questions.get(i))); } } private void init(GenericDelegator delegator, LocalDispatcher dispatcher, String productId, String productStoreId, String catalogId, String webSiteId, String currencyUomId, Locale locale, GenericValue autoUserLogin) throws Exception { product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); if (product == null || !product.getString("productTypeId").equals("AGGREGATED")) { throw new ProductConfigWrapperException("Product " + productId + " is not an AGGREGATED product."); } // get the base price Map priceContext = UtilMisc.toMap("product", product, "prodCatalogId", catalogId, "webSiteId", webSiteId, "productStoreId", productStoreId, "currencyUomId", currencyUomId, "autoUserLogin", autoUserLogin); Map priceMap = dispatcher.runSync("calculateProductPrice", priceContext); Double price = (Double)priceMap.get("price"); if (price != null) { basePrice = price.doubleValue(); } questions = new ArrayList(); List questionsValues = new ArrayList(); if (product.getString("productTypeId") != null && product.getString("productTypeId").equals("AGGREGATED")) { questionsValues = delegator.findByAnd("ProductConfig", UtilMisc.toMap("productId", productId), UtilMisc.toList("sequenceNum")); questionsValues = EntityUtil.filterByDate(questionsValues); Iterator questionsValuesIt = questionsValues.iterator(); HashMap itemIds = new HashMap(); while (questionsValuesIt.hasNext()) { ConfigItem oneQuestion = new ConfigItem((GenericValue)questionsValuesIt.next()); oneQuestion.setContent(locale, "text/html"); // TODO: mime-type shouldn't be hardcoded if (itemIds.containsKey(oneQuestion.getConfigItem().getString("configItemId"))) { oneQuestion.setFirst(false); } else { itemIds.put(oneQuestion.getConfigItem().getString("configItemId"), null); } questions.add(oneQuestion); List configOptions = delegator.findByAnd("ProductConfigOption", UtilMisc.toMap("configItemId", oneQuestion.getConfigItemAssoc().getString("configItemId")), UtilMisc.toList("sequenceNum")); Iterator configOptionsIt = configOptions.iterator(); while (configOptionsIt.hasNext()) { ConfigOption option = new ConfigOption(delegator, dispatcher, (GenericValue)configOptionsIt.next(), catalogId, webSiteId, currencyUomId, autoUserLogin); oneQuestion.addOption(option); } } } } public void resetConfig() { for (int i = 0; i < questions.size(); i++) { ConfigItem ci = (ConfigItem)questions.get(i); if (!ci.isStandard()) { List options = ci.getOptions(); for (int j = 0; j < options.size(); j++) { ConfigOption co = (ConfigOption)options.get(j); co.setSelected(false); } } } } public void setDefaultConfig() { resetConfig(); for (int i = 0; i < questions.size(); i++) { ConfigItem ci = (ConfigItem)questions.get(i); if (ci.isMandatory()) { if (ci.getOptions().size() > 0) { ConfigOption co = (ConfigOption)ci.getOptions().get(0); co.setSelected(true); } } } } public boolean equals(Object obj) { if (obj == null || !(obj instanceof ProductConfigWrapper)) { return false; } ProductConfigWrapper cw = (ProductConfigWrapper)obj; if (!product.getString("productId").equals(cw.getProduct().getString("productId"))) { return false; } List cwq = cw.getQuestions(); if (questions.size() != cwq.size()) { return false; } for (int i = 0; i < questions.size(); i++) { ConfigItem ci = (ConfigItem)questions.get(i); if (!ci.equals(cwq.get(i))) { return false; } } return true; } public String toString() { return "" + questions; } public List getQuestions() { return questions; } public GenericValue getProduct() { return product; } public void setSelected(int question, int option) throws Exception { ConfigItem ci = (ConfigItem)questions.get(question); List avalOptions = ci.getOptions(); if (ci.isSingleChoice()) { for (int j = 0; j < avalOptions.size(); j++) { ConfigOption oneOption = (ConfigOption)avalOptions.get(j); oneOption.setSelected(false); } } ConfigOption theOption = null; if (option >= 0 && option < avalOptions.size()) { theOption = (ConfigOption)avalOptions.get(option); } if (theOption != null) { theOption.setSelected(true); } } public List getSelectedOptions() { List selectedOptions = new ArrayList(); for (int i = 0; i < questions.size(); i++) { ConfigItem ci = (ConfigItem)questions.get(i); if (ci.isStandard()) { selectedOptions.addAll(ci.getOptions()); } else { Iterator availOptions = ci.getOptions().iterator(); while (availOptions.hasNext()) { ConfigOption oneOption = (ConfigOption)availOptions.next(); if (oneOption.isSelected()) { selectedOptions.add(oneOption); } } } } return selectedOptions; } public double getTotalPrice() { double totalPrice = basePrice; List options = getSelectedOptions(); for (int i = 0; i < options.size(); i++) { ConfigOption oneOption = (ConfigOption)options.get(i); totalPrice += oneOption.getPrice(); } return totalPrice; } public boolean isCompleted() { boolean completed = true; for (int i = 0; i < questions.size(); i++) { ConfigItem ci = (ConfigItem)questions.get(i); if (!ci.isStandard() && ci.isMandatory()) { Iterator availOptions = ci.getOptions().iterator(); while (availOptions.hasNext()) { ConfigOption oneOption = (ConfigOption)availOptions.next(); if (oneOption.isSelected()) { completed = true; break; } else { completed = false; } } if (!completed) { break; } } } return completed; } public class ConfigItem implements java.io.Serializable { GenericValue configItem = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -