1 22 23 package org.ofbiz.product.config; 24 25 import java.io.Serializable ; 26 import java.util.ArrayList ; 27 import java.util.Date ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.util.Locale ; 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 48 49 public class ProductConfigWrapper implements Serializable { 50 51 public static final String module = ProductConfigWrapper.class.getName(); 52 53 protected GenericValue product = null; protected double basePrice = 0.0; 55 protected List questions = null; 57 58 public ProductConfigWrapper() { 59 } 60 61 public ProductConfigWrapper(GenericDelegator delegator, LocalDispatcher dispatcher, String productId, String productStoreId, String catalogId, String webSiteId, String currencyUomId, Locale locale, GenericValue autoUserLogin) throws Exception { 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 (); 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 productId, String productStoreId, String catalogId, String webSiteId, String currencyUomId, Locale locale, GenericValue autoUserLogin) throws Exception { 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 Map priceContext = UtilMisc.toMap("product", product, "prodCatalogId", catalogId, "webSiteId", webSiteId, "productStoreId", productStoreId, 81 "currencyUomId", currencyUomId, "autoUserLogin", autoUserLogin); 82 Map priceMap = dispatcher.runSync("calculateProductPrice", priceContext); 83 Double price = (Double )priceMap.get("price"); 84 if (price != null) { 85 basePrice = price.doubleValue(); 86 } 87 questions = new ArrayList (); 88 List questionsValues = new ArrayList (); 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 questionsValuesIt = questionsValues.iterator(); 93 HashMap itemIds = new HashMap (); 94 while (questionsValuesIt.hasNext()) { 95 ConfigItem oneQuestion = new ConfigItem((GenericValue)questionsValuesIt.next()); 96 oneQuestion.setContent(locale, "text/html"); 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 configOptions = delegator.findByAnd("ProductConfigOption", UtilMisc.toMap("configItemId", oneQuestion.getConfigItemAssoc().getString("configItemId")), UtilMisc.toList("sequenceNum")); 104 Iterator 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 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 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 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 toString() { 161 return "" + questions; 162 } 163 164 public List 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 { 173 ConfigItem ci = (ConfigItem)questions.get(question); 174 List 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 getSelectedOptions() { 191 List selectedOptions = new ArrayList (); 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 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 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 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 { 243 GenericValue configItem = null; 244 GenericValue configItemAssoc = null; 245 ProductConfigItemContentWrapper content = null; 246 List options = null; 247 boolean first = true; 248 249 public ConfigItem(GenericValue questionAssoc) throws Exception { 250 configItemAssoc = questionAssoc; 251 configItem = configItemAssoc.getRelatedOne("ConfigItemProductConfigItem"); 252 options = new ArrayList (); 253 } 254 255 public ConfigItem(ConfigItem ci) { 256 configItem = GenericValue.create(ci.configItem); 257 configItemAssoc = GenericValue.create(ci.configItemAssoc); 258 options = new ArrayList (); 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; } 265 266 public void setContent(Locale locale, String 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 getOptions() { 307 return options; 308 } 309 310 public String getQuestion() { 311 String 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 getDescription() { 325 String 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 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 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 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 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 toString() { 383 return configItem.getString("configItemId"); 384 } 385 } 386 387 public class ConfigOption implements java.io.Serializable { 388 double optionPrice = 0; 389 Date availabilityDate = null; 390 List componentList = null; GenericValue configOption = null; 392 boolean selected = false; 393 boolean available = true; 394 395 public ConfigOption(GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue option, String catalogId, String webSiteId, String currencyUomId, GenericValue autoUserLogin) throws Exception { 396 configOption = option; 397 componentList = option.getRelated("ConfigOptionProductConfigProduct"); 398 Iterator componentsIt = componentList.iterator(); 399 while (componentsIt.hasNext()) { 400 double price = 0; 401 GenericValue oneComponent = (GenericValue)componentsIt.next(); 402 Map fieldMap = UtilMisc.toMap("product", oneComponent.getRelatedOne("ProductProduct"), "prodCatalogId", catalogId, "webSiteId", webSiteId, 404 "currencyUomId", currencyUomId, "productPricePurposeId", "COMPONENT_PRICE", "autoUserLogin", autoUserLogin); 405 Map priceMap = dispatcher.runSync("calculateProductPrice", fieldMap); 406 Double componentPrice = (Double ) 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 purchasePriceResultMap = dispatcher.runSync("calculateProductPrice", fieldMap); 419 Double purchasePrice = (Double ) purchasePriceResultMap.get("price"); 420 if (purchasePrice != null) { 421 price = purchasePrice.doubleValue(); 422 } 423 } 424 optionPrice += (price * mult); 425 } 427 } 428 429 public ConfigOption(ConfigOption co) { 430 configOption = GenericValue.create(co.configOption); 431 componentList = new ArrayList (); 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 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 getComponents() { 465 return componentList; 466 } 467 468 public boolean equals(Object obj) { 469 if (obj == null || !(obj instanceof ConfigOption)) { 470 return false; 471 } 472 ConfigOption co = (ConfigOption)obj; 473 475 return isSelected() == co.isSelected(); 476 } 477 478 public String toString() { 479 return configOption.getString("configItemId") + "/" + configOption.getString("configOptionId") + (isSelected()? "*": ""); 480 } 481 482 } 483 484 } 485 | Popular Tags |