KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > cart > BaseXmlArchive


1 package com.openedit.modules.cart;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.dom4j.Element;
8 import org.openedit.money.Money;
9
10 import com.openedit.config.Configuration;
11 import com.openedit.store.BaseArchive;
12 import com.openedit.store.Option;
13 import com.openedit.store.Price;
14 import com.openedit.store.PriceSupport;
15 import com.openedit.store.PriceTier;
16
17 public class BaseXmlArchive extends BaseArchive
18 {
19     protected void saveOptions(Collection JavaDoc inOptions, Element productelm)
20     {
21         deleteElements(productelm, "option");
22         for (Iterator JavaDoc iter = inOptions.iterator(); iter.hasNext();)
23         {
24             Option option = (Option) iter.next();
25             Element newOption = productelm.addElement("option");
26             newOption.addAttribute("id", option.getId());
27             newOption.addAttribute("name", option.getName());
28             if (option.isRequired())
29             {
30                 newOption.addAttribute("required", "true");
31             }
32             if (option.getPriceSupport() != null)
33             {
34                 addPrice(newOption, option.getPriceSupport());
35             }
36             if( option.getValue() != null)
37             {
38                 newOption.setText(option.getValue());
39             }
40         }
41     }
42
43     protected Option createOption(Configuration inOptionConfig)
44     {
45         Option option = new Option();
46         option.setName(inOptionConfig.getAttribute("name"));
47         option.setId(inOptionConfig.getAttribute("id"));
48         option.setPriceSupport(createPriceSupport(inOptionConfig));
49         String JavaDoc requiredStr = inOptionConfig.getAttribute("required");
50         if (requiredStr != null && "true".equalsIgnoreCase(requiredStr))
51         {
52             option.setRequired(true);
53         }
54         option.setValue(inOptionConfig.getValue());
55         return option;
56     }
57
58     
59     /**
60      * @param elm
61      */

62     protected void deleteElements(Element elm, String JavaDoc inName)
63     {
64         //remove old pricing
65
for (Iterator JavaDoc iter = elm.elements(inName).iterator(); iter.hasNext();)
66         {
67             Element element = (Element) iter.next();
68             elm.remove(element);
69         }
70     }
71     
72     /**
73      * @param ielement
74      * @param price
75      */

76     protected void addPrice(Element inElement, PriceSupport inPriceSupport)
77     {
78         for (Iterator JavaDoc iter = inPriceSupport.getTiers().iterator(); iter.hasNext();)
79         {
80             PriceTier priceTier = (PriceTier) iter.next();
81
82             Element tierElement = inElement.addElement("price");
83             Price price = priceTier.getPrice();
84             if (price.getSalePrice() != null)
85             {
86                 Money money = price.getSalePrice();
87                 if (money != null)
88                 {
89                     Element salePriceElement = tierElement.addElement("sale");
90                     salePriceElement.setText(money.toShortString());
91                 }
92                 money = price.getRetailPrice();
93                 if (money != null)
94                 {
95                     Element retailPriceElement = tierElement.addElement("retail");
96                     retailPriceElement.setText(money.toShortString());
97                 }
98             }
99             else
100             {
101                 if (price.getRetailPrice() != null)
102                 {
103                     tierElement.setText(price.getRetailPrice().toShortString());
104                 }
105                 else
106                 {
107                     tierElement.setText("0.00");
108                 }
109             }
110             if ( price.getRegion() != null)
111             {
112                 tierElement.addAttribute("region", price.getRegion() );
113             }
114
115             tierElement.addAttribute("quantity", String.valueOf(priceTier.getThresholdQuantity()));
116         }
117     }
118     
119     protected PriceSupport createPriceSupport(Configuration inConfig)
120     {
121         List JavaDoc prices = inConfig.getChildren("price");
122         if (prices == null || prices.size() == 0)
123         {
124             return null;
125         }
126         PriceSupport priceSupport = null;
127         for (Iterator JavaDoc iter = prices.iterator(); iter.hasNext();)
128         {
129             Configuration priceConfig = (Configuration) iter.next();
130             int quantity = Integer.valueOf(priceConfig.getAttribute("quantity")).intValue();
131
132             Price price = new Price();
133             String JavaDoc inRegion = priceConfig.getAttribute("region"); //Not used anyplace
134
if ( inRegion != null)
135             {
136                 price.setRegion(inRegion);
137             }
138
139             Configuration salePrice = priceConfig.getChild("sale");
140             Configuration retailPrice = priceConfig.getChild("retail");
141             if (retailPrice != null)
142             {
143                 price.setRetailPrice(new Money(retailPrice.getValue()));
144             }
145             if (salePrice != null)
146             {
147                 price.setSalePrice(new Money(salePrice.getValue()));
148             }
149             if (retailPrice == null && salePrice == null && priceConfig.getValue() != null)
150             {
151                 price.setRetailPrice(new Money(priceConfig.getValue()));
152             }
153             if( priceSupport == null)
154             {
155                 priceSupport = new PriceSupport();
156             }
157             priceSupport.addTierPrice(quantity, price);
158         }
159         return priceSupport;
160     }
161
162
163
164 }
165
Popular Tags