1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 package com.ivata.mask.web.demo.struts;
66 import java.math.BigDecimal;
67 import java.util.List;
68 import java.util.Vector;
69 import javax.servlet.ServletContext;
70 import javax.servlet.http.HttpServletRequest;
71 import javax.servlet.http.HttpServletResponse;
72 import javax.servlet.http.HttpSession;
73 import org.apache.log4j.Logger;
74 import org.apache.struts.action.Action;
75 import org.apache.struts.action.ActionForm;
76 import org.apache.struts.action.ActionForward;
77 import org.apache.struts.action.ActionMapping;
78 import com.ivata.mask.Mask;
79 import com.ivata.mask.MaskFactory;
80 import com.ivata.mask.persistence.PersistenceException;
81 import com.ivata.mask.web.demo.catalog.Catalog;
82 import com.ivata.mask.web.demo.customer.CustomerDO;
83 import com.ivata.mask.web.demo.order.OrderDO;
84 import com.ivata.mask.web.demo.order.item.OrderItemDO;
85 import com.ivata.mask.web.demo.product.ProductDO;
86 import com.ivata.mask.web.field.DefaultFieldWriterFactory;
87 import com.ivata.mask.web.field.FieldWriterFactory;
88 import com.ivata.mask.web.struts.ListForm;
89 /***
90 * <p>
91 * If you have no data in your Http Session, you are redirected here to create
92 * some.
93 * </p>
94 *
95 * @since ivata masks 0.1 (2004-04-30)
96 * @author Colin MacLeod
97 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
98 * @version $Revision: 1.8 $
99 */
100 public class LoadDataAction extends Action {
101 /***
102 * <p>
103 * Specifies the path to the mask config file, relative to the webapp root.
104 * </p>
105 */
106 private static final String FILE_NAME = "/WEB-INF/ivataMasks.xml";
107 /***
108 * <p>
109 * This log provides tracing and debugging information.
110 * </p>
111 */
112 private Logger log = Logger.getLogger(LoadDataAction.class);
113 /***
114 * <p>
115 * Add a new customer.
116 * </p>
117 *
118 * @param emailAddress standard email address, such as
119 * <code>bob@nowhere.com</code>.
120 * @param firstName First name(s) of the customer.
121 * @param lastName Last name (surname) of the customer.
122 * @param postalAddress Full customer postal address, including carriage
123 * returns, region/state, country, etc.
124 * @return freshly created customer which has been added to the system.
125 */
126 private CustomerDO addCustomer(final String emailAddress,
127 final String firstName,
128 final String lastName,
129 final String postalAddress) {
130 CustomerDO customer = new CustomerDO();
131 customer.setEmailAddress(emailAddress);
132 customer.setFirstName(firstName);
133 customer.setLastName(lastName);
134 customer.setPostalAddress(postalAddress);
135 Catalog catalog = Catalog.getInstance();
136 try {
137 catalog.add(null, customer);
138 } catch (PersistenceException e) {
139 e.printStackTrace();
140 throw new RuntimeException(e);
141 }
142 return customer;
143 }
144 /***
145 * <p>
146 * Add a new order.
147 * </p>
148 *
149 * @param customerParam customer who placed the order.
150 * @param itemsParam a <code>List</code> of {@link OrderItemDO} instances.
151 * @return new instance of order for the customer and items provided.
152 */
153 private OrderDO addOrder(final CustomerDO customerParam,
154 final List itemsParam) {
155 OrderDO order = new OrderDO();
156 order.setCustomer(customerParam);
157 order.setItems(itemsParam);
158 Catalog catalog = Catalog.getInstance();
159 try {
160 catalog.add(null, order);
161 } catch (PersistenceException e) {
162 e.printStackTrace();
163 throw new RuntimeException(e);
164 }
165 return order;
166 }
167 /***
168 * <p>
169 * Add a new product.
170 * </p>
171 *
172 * @param name Name of the product being added.
173 * @param price How much each item of this product will cost.
174 * @param description Just some silly text to describe what it <i>really</i>
175 * does.
176 * @return new product, freshly initialized.
177 */
178 private ProductDO addProduct(final String name,
179 final BigDecimal price,
180 final String description) {
181 ProductDO product = new ProductDO();
182 product.setName(name);
183 product.setPrice(price);
184 product.setDescription(description);
185 Catalog catalog = Catalog.getInstance();
186 try {
187 catalog.add(null, product);
188 } catch (PersistenceException e) {
189 throw new RuntimeException(e);
190 }
191 return product;
192 }
193 /***
194 * <p>
195 * Create a new order item.
196 * </p>
197 *
198 * @param product the product which is being ordered.
199 * @param quantity number of this product which is being ordered.
200 * @return new instance of order item for the product and quantity.
201 */
202 private OrderItemDO createOrderItem(final ProductDO product,
203 final int quantity) {
204 OrderItemDO item = new OrderItemDO();
205 item.setProduct(product);
206 item.setQuantity(quantity);
207 Catalog catalog = Catalog.getInstance();
208 try {
209 catalog.add(null, item);
210 } catch (PersistenceException e) {
211 e.printStackTrace();
212 throw new RuntimeException(e);
213 }
214 return item;
215 }
216 /***
217 * <p>
218 * Generic method called by the <strong>Struts </strong> interface. Always
219 * returns success. .
220 * </p>
221 *
222 * @param mapping
223 * The ActionMapping used to select this instance
224 * @param form
225 * The optional ActionForm bean for this request (if any)
226 * @param request
227 * The non-HTTP request we are processing
228 * @param response
229 * The non-HTTP response we are creating
230 * @exception Exception
231 * if the application business logic throws an exception
232 * @return this method returns a <code>"success"</code>
233 * <code>ActionForward</code>
234 * every time.
235 */
236 public ActionForward execute(final ActionMapping mapping,
237 final ActionForm form,
238 final HttpServletRequest request,
239 final HttpServletResponse response)
240 throws Exception {
241
242
243 Catalog catalog = Catalog.getInstance();
244 HttpSession session = request.getSession();
245 catalog.reset();
246
247
248
249 ProductDO nails = addProduct("10 inch nails", new BigDecimal("1.99"),
250 "pack of 50, 10 inch steel nails");
251 ProductDO hammer = addProduct("large hammer", new BigDecimal("4.99"),
252 "huge hammer, good for hitting nails with");
253 ProductDO wood = addProduct("block of wood", new BigDecimal("2.99"),
254 "beautifully crafted piece of waste wood. ideal for practise.");
255 List products = catalog.findAll(null, ProductDO.class);
256 session.setAttribute("products", products);
257
258 CustomerDO johnny = addCustomer("john.tester@test.com", "Johnny",
259 "Tester",
260 "John H Tester\n23 Nowhere Avenue\nNotown\nOuter Nosuchland");
261 CustomerDO fred = addCustomer("fred.another@test.com", "Fred",
262 "Another",
263 "Freddie Another\n98 Madeupname Street\nDontexist\nNowayland");
264
265 List items = new Vector();
266 items.add(createOrderItem(nails, 55));
267 items.add(createOrderItem(hammer, 2));
268 addOrder(johnny, items);
269 items = new Vector();
270 items.add(createOrderItem(hammer, 2));
271 items.add(createOrderItem(wood, 1));
272 addOrder(johnny, items);
273 items = new Vector();
274 items.add(createOrderItem(nails, 25));
275 items.add(createOrderItem(hammer, 1));
276 items.add(createOrderItem(wood, 5));
277 addOrder(fred, items);
278
279 ServletContext context = servlet.getServletContext();
280 FieldWriterFactory fieldWriterFactory = new DefaultFieldWriterFactory(
281 catalog, "/find.action");
282 context.setAttribute(FieldWriterFactory.APPLICATION_ATTRIBUTE,
283 fieldWriterFactory);
284 MaskFactory maskFactory = catalog.getMaskFactory();
285 maskFactory.readConfiguration(context.getResourceAsStream(FILE_NAME));
286 context.setAttribute("MaskFactory", maskFactory);
287 Mask mask = maskFactory.getMask(ProductDO.class, "list");
288 if (mask == null) {
289 log.error("ERROR: mask is null");
290 throw new NullPointerException("ERROR in LoadDataAction: mask is "
291 + "null");
292 }
293 ListForm listForm = new ListForm(products, mask, ProductDO.class);
294 request.setAttribute(ListForm.REQUEST_ATTRIBUTE, listForm);
295 return mapping.findForward("success");
296 }
297 }