1 4 package com.openedit.modules.cart; 5 6 import java.io.FileNotFoundException ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.dom4j.DocumentException; 14 import org.dom4j.Element; 15 import org.openedit.money.Fraction; 16 import org.openedit.money.Money; 17 18 import com.openedit.ModuleManager; 19 import com.openedit.OpenEditException; 20 import com.openedit.WebPageRequest; 21 import com.openedit.config.XMLConfiguration; 22 import com.openedit.page.Page; 23 import com.openedit.page.manage.PageManager; 24 import com.openedit.store.CreditCardType; 25 import com.openedit.store.DefaultProductPathFinder; 26 import com.openedit.store.HandlingCharge; 27 import com.openedit.store.ProductPathFinder; 28 import com.openedit.store.SegmentedProductPathFinder; 29 import com.openedit.store.Store; 30 import com.openedit.store.StoreException; 31 import com.openedit.store.shipping.BaseShippingMethod; 32 import com.openedit.store.shipping.PriceBasedShippingMethod; 33 import com.openedit.store.shipping.WeightBasedShippingMethod; 34 import com.openedit.util.XmlUtil; 35 36 42 public class StoreDataReader 43 { 44 private static final Log log = LogFactory.getLog(StoreDataReader.class); 45 46 protected ModuleManager fieldModuleManager; 47 protected PageManager fieldPageManager; 48 protected Map fieldStores; 49 protected String fieldBeanName = "store"; 50 51 public boolean hasChanged(Store inStore) throws OpenEditException 52 { 53 Page config = getPageManager().getPage(inStore.getStoreHome() + "/configuration/store.xml"); 54 if (config.getLastModified().getTime() == inStore.getLastModified() ) 55 { 56 return false; 57 } 58 else 59 { 60 return true; 61 } 62 } 63 public Store loadStore(WebPageRequest inReq) throws StoreException 64 { 65 Store store = getStore(inReq.getContentPage()); 66 inReq.putSessionValue("store", store); 67 inReq.putPageValue("catalogid", store.getCatalogId()); 68 inReq.putPageValue("store", store); 69 return store; 70 } 71 public Store getStore(Page inPage) throws StoreException 72 { 73 String name = inPage.get("catalogid"); 74 if( name == null) 75 { 76 name = "store"; 77 log.error("Should define catalogid property. Defaulting to /store/"); 78 } 79 return getStore( name); 80 } 81 82 public Store getStore(String inCatalogId) throws StoreException 83 { 84 try 85 { 86 Store store = getStoreForCatalog(inCatalogId); 87 if( hasChanged(store) ) 88 { 89 store.clear(); 90 configureStore(store, inCatalogId); 91 } 92 return store; 93 } 94 catch (Exception ex) 95 { 96 throw new StoreException(ex); 97 } 98 } 99 106 protected void configureStore(Store inStore, String inCatalogId) throws StoreException, FileNotFoundException , DocumentException, Exception 107 { 108 log.info("Configuring " + inCatalogId); 109 Page config = getPageManager().getPage("/" + inCatalogId + "/configuration/store.xml"); 110 inStore.setLastModified( config.getLastModified().getTime() ); 111 inStore.getProductArchive().clearProducts(); 112 113 Element rootElement = new XmlUtil().getXml(config.getReader(),config.getCharacterEncoding()); 114 115 inStore.setConfiguration(new XMLConfiguration(rootElement)); 116 String hostName = rootElement.elementTextTrim("hostName"); 118 if ( hostName == null) 119 { 120 hostName = rootElement.elementTextTrim("hostname"); 122 } 123 inStore.setHostName(hostName); 124 inStore.setName(rootElement.elementTextTrim("name")); 125 Element coup = rootElement.element("coupons"); 126 inStore.setCouponsAccepted(coup != null && "true".equals( coup.attributeValue("enabled"))); 127 128 Element ponum = rootElement.element("ponumber"); 129 inStore.setCouponsAccepted(coup != null && "true".equals( coup.attributeValue("enabled"))); 130 131 inStore.setSmtpServer(rootElement.elementTextTrim("smtp-server")); 132 for ( Iterator it = rootElement.elementIterator("to-address"); it.hasNext();) 133 { 134 Element toAddressElement = (Element)it.next(); 135 inStore.addToAddress( toAddressElement.getTextTrim()); 136 } 137 for ( Iterator it = rootElement.elementIterator("notify-address"); it.hasNext();) 138 { 139 Element toAddressElement = (Element)it.next(); 140 inStore.addNotifyAddress( toAddressElement.getTextTrim()); 141 } 142 inStore.setFromAddress(rootElement.elementTextTrim("from-address")); 143 inStore.setEmailLayout(rootElement.elementTextTrim("email-layout")); 144 inStore.setOrderLayout(rootElement.elementTextTrim("order-layout")); 145 inStore.setUsesPoNumbers(Boolean.parseBoolean(rootElement.elementTextTrim("uses-po-numbers"))); 146 inStore.setDisplayTermsConditions(Boolean.parseBoolean(rootElement.elementTextTrim("display-terms-conditions"))); 147 inStore.setUsesBillMeLater(Boolean.parseBoolean(rootElement.elementTextTrim("uses-bill-me-later"))); 148 inStore.setAllowDuplicateAccounts(Boolean.parseBoolean(rootElement.elementTextTrim("allow-duplicate-accounts"))); 149 inStore.setAllowSpecialRequest(Boolean.parseBoolean(rootElement.elementTextTrim("allow-special-request"))); 150 inStore.setAutoCapture(Boolean.parseBoolean(rootElement.elementTextTrim("auto-capture-credit-cards"))); 151 152 configureCreditCards(inStore, rootElement); 153 configureTax(inStore, rootElement); 154 156 configureShipping(inStore, rootElement); 157 configureProductPathFinder(inStore, rootElement); 158 } 159 160 164 private void configureShipping(Store inStore, Element rootElement) throws StoreException 165 { 166 Element assignShippingMethodElem = rootElement.element("assign-shipping-method"); 169 if ( assignShippingMethodElem != null ) 170 { 171 inStore.setAssignShippingMethod( Boolean.valueOf(assignShippingMethodElem.getTextTrim()).booleanValue() ); 172 } 173 else 174 { 175 inStore.setAssignShippingMethod( false ); 176 } 177 178 inStore.getAllShippingMethods().clear(); 179 180 Iterator methods = rootElement.elementIterator("price-shipping-method"); 181 if ( !methods.hasNext() ) 182 { 183 methods = rootElement.elementIterator("shipping-method"); 185 } 186 for (; methods.hasNext();) 187 { 188 Element method = (Element) methods.next(); 189 PriceBasedShippingMethod shippingmethod = (PriceBasedShippingMethod)getModuleManager().getBean("priceBasedShipping"); 190 configureShippingMethod(shippingmethod, method); 191 192 appendHandlingCharge(shippingmethod, method); 193 194 inStore.getAllShippingMethods().add(shippingmethod); 195 } 196 197 for (Iterator iter = rootElement.elementIterator("weight-shipping-method"); iter.hasNext();) 198 { 199 Element element = (Element) iter.next(); 200 WeightBasedShippingMethod shippingmethod = (WeightBasedShippingMethod)getModuleManager().getBean("weightBasedShipping"); 201 configureShippingMethod(shippingmethod, element); 202 203 appendHandlingCharge(shippingmethod, element); 204 205 inStore.getAllShippingMethods().add(shippingmethod); 206 } 207 } 208 209 private void configureShippingMethod(BaseShippingMethod shippingmethod, Element method) 210 { 211 shippingmethod.setDescription(method.attributeValue("description")); 212 shippingmethod.setId(method.attributeValue("id")); 213 String costStr = method.attributeValue("costs"); 214 if (costStr != null) 215 { 216 Money cost = new Money(costStr); 217 shippingmethod.setCost(cost); 218 } 219 String percentageCostStr = method.attributeValue("percentageCosts"); 220 if ( percentageCostStr != null ) 221 { 222 shippingmethod.setPercentageCost(Double.parseDouble(percentageCostStr)); 223 } 224 String lowerThresholdStr = method.attributeValue("lowerThreshold"); 225 if (lowerThresholdStr != null) 226 { 227 shippingmethod.setLowerThreshold(new Money(lowerThresholdStr)); 228 } 229 String upperThresholdStr = method.attributeValue("upperThreshold"); 230 if (upperThresholdStr != null) 231 { 232 shippingmethod.setUpperThreshold(new Money(upperThresholdStr)); 233 } 234 String hidden = method.attributeValue("hidden"); 235 shippingmethod.setHidden(Boolean.parseBoolean(hidden)); 236 } 237 238 private void appendHandlingCharge(BaseShippingMethod shippingmethod, Element method) 239 { 240 for ( Iterator handlingCharges = method.elementIterator("handling-charge"); 241 handlingCharges.hasNext(); ) 242 { 243 Element handlingChargeElem = (Element) handlingCharges.next(); 244 HandlingCharge handlingCharge = new HandlingCharge(); 245 handlingCharge.setLevel(handlingChargeElem.attributeValue("level")); 246 String cost = handlingChargeElem.attributeValue("costs"); 247 if ( cost != null) 248 { 249 handlingCharge.setCost(new Money(cost)); 250 } 251 else 252 { 253 handlingCharge.setCost(Money.ZERO); 254 } 255 String additionalCosts = handlingChargeElem.attributeValue("additionalCosts"); 256 if ( additionalCosts != null ) 257 { 258 handlingCharge.setAdditionalCosts( additionalCosts.equalsIgnoreCase( "true" ) ); 259 } 260 shippingmethod.addHandlingCharge(handlingCharge); 261 } 262 } 263 264 274 275 278 private void configureTax(Store inStore, Element inElement) 279 { 280 for (Iterator iter = inElement.elementIterator("tax"); iter.hasNext();) 281 { 282 Element element = (Element) iter.next(); 283 String state = element.attributeValue("statecode"); 284 Fraction rate = new Fraction(element.attributeValue("rate")); 285 inStore.putTaxRate(state, rate); 286 } 287 } 288 289 290 293 private void configureCreditCards(Store inStore, Element inElement) 294 { 295 for (Iterator iter = inElement.elementIterator("credit-card-type"); iter.hasNext();) 296 { 297 Element element = (Element) iter.next(); 298 299 CreditCardType type = new CreditCardType(element.attributeValue("name")); 300 type.setId(element.attributeValue("id")); 301 302 inStore.addCreditCardType(type); 303 } 304 } 305 306 314 private void configureProductPathFinder( Store inStore, Element inRootElement ) 315 throws StoreException 316 { 317 ProductPathFinder pathFinder = null; 318 319 Element element = inRootElement.element( "segmented-product-paths" ); 320 if ( element != null ) 321 { 322 SegmentedProductPathFinder segPathFinder = 323 new SegmentedProductPathFinder(); 324 325 Element segmentLengthElem = element.element( "segment-length" ); 326 if ( segmentLengthElem != null ) 327 { 328 segPathFinder.setSegmentLength( 329 Integer.parseInt( segmentLengthElem.getTextTrim() ) ); 330 } 331 332 Element maxSegmentsElem = element.element( "reverse" ); 333 if ( maxSegmentsElem != null ) 334 { 335 segPathFinder.setReverse(Boolean.parseBoolean( maxSegmentsElem.getTextTrim() ) ); 336 } 337 338 pathFinder = segPathFinder; 339 } 340 341 if ( pathFinder == null ) 342 { 343 pathFinder = new DefaultProductPathFinder(); 344 } 345 346 inStore.setProductPathFinder( pathFinder ); 347 } 348 349 350 public PageManager getPageManager() 351 { 352 return fieldPageManager; 353 } 354 355 public void setPageManager(PageManager inPageManager) 356 { 357 fieldPageManager = inPageManager; 358 } 359 360 361 public ModuleManager getModuleManager() 362 { 363 return fieldModuleManager; 364 } 365 366 public void setModuleManager(ModuleManager inModuleManager) 367 { 368 fieldModuleManager = inModuleManager; 369 } 370 public Map getStores() 371 { 372 if (fieldStores == null) 373 { 374 fieldStores = new HashMap (); 375 } 376 return fieldStores; 377 } 378 public Store getStoreForCatalog( String inCatalogId) throws Exception 379 { 380 Store store = (Store)getStores().get( inCatalogId ); 381 if( store == null) 382 { 383 store = (Store)getModuleManager().getBean(getBeanName()); store.setCatalogId(inCatalogId); 385 configureStore(store, inCatalogId); 386 387 getStores().put( inCatalogId, store); 388 } 389 return store; 390 391 } 392 public String getBeanName() 393 { 394 return fieldBeanName; 395 } 396 public void setBeanName(String inBeanName) 397 { 398 fieldBeanName = inBeanName; 399 } 400 } | Popular Tags |