001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: LoadDataAction.java,v $
031: * Revision 1.10 2005/10/11 18:52:03 colinmacleod
032: * Fixed some checkstyle and javadoc issues.
033: *
034: * Revision 1.9 2005/10/02 14:06:31 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.8 2005/04/09 18:04:14 colinmacleod
038: * Changed copyright text to GPL v2 explicitly.
039: *
040: * Revision 1.7 2005/01/19 12:18:45 colinmacleod
041: * Removed magic number from FieldWriterFactory constructor.
042: *
043: * Revision 1.6 2005/01/07 08:08:19 colinmacleod
044: * Moved up a version number.
045: * Changed copyright notices to 2005.
046: * Updated the documentation:
047: * - started working on multiproject:site docu.
048: * - changed the logo.
049: * Added checkstyle and fixed LOADS of style issues.
050: * Added separate thirdparty subproject.
051: * Added struts (in web), util and webgui (in webtheme) from ivata op.
052: *
053: * Revision 1.5 2004/12/29 15:11:12 colinmacleod
054: * Replaced hard-coded application attribute name with static variable.
055: *
056: * Revision 1.4 2004/12/23 21:28:16 colinmacleod
057: * Modifications to add ivata op compatibility.
058: *
059: * Revision 1.3 2004/11/12 15:10:41 colinmacleod
060: * Moved persistence classes from ivata op as a replacement for
061: ValueObjectLocator.
062: *
063: * Revision 1.2 2004/11/11 13:20:33 colinmacleod
064: * Added new data objects.
065: * Added logging.
066: *
067: * Revision 1.1.1.1 2004/05/16 20:40:07 colinmacleod
068: * Ready for 0.1 release
069: * -----------------------------------------------------------------------------
070: */
071: package com.ivata.mask.web.demo.struts;
072:
073: import java.math.BigDecimal;
074: import java.util.List;
075: import java.util.Vector;
076: import javax.servlet.ServletContext;
077: import javax.servlet.http.HttpServletRequest;
078: import javax.servlet.http.HttpServletResponse;
079: import javax.servlet.http.HttpSession;
080: import org.apache.log4j.Logger;
081: import org.apache.struts.action.Action;
082: import org.apache.struts.action.ActionForm;
083: import org.apache.struts.action.ActionForward;
084: import org.apache.struts.action.ActionMapping;
085: import com.ivata.mask.Mask;
086: import com.ivata.mask.MaskFactory;
087: import com.ivata.mask.persistence.PersistenceException;
088: import com.ivata.mask.web.demo.catalog.Catalog;
089: import com.ivata.mask.web.demo.customer.CustomerDO;
090: import com.ivata.mask.web.demo.order.OrderDO;
091: import com.ivata.mask.web.demo.order.item.OrderItemDO;
092: import com.ivata.mask.web.demo.product.ProductDO;
093: import com.ivata.mask.web.field.DefaultFieldWriterFactory;
094: import com.ivata.mask.web.field.FieldWriterFactory;
095: import com.ivata.mask.web.struts.ListForm;
096:
097: /**
098: * <p>
099: * If you have no data in your Http Session, you are redirected here to create
100: * some.
101: * </p>
102: *
103: * @since ivata masks 0.1 (2004-04-30)
104: * @author Colin MacLeod
105: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
106: * @version $Revision: 1.10 $
107: */
108: public class LoadDataAction extends Action {
109: /**
110: * <p>
111: * Specifies the path to the mask config file, relative to the webapp root.
112: * </p>
113: */
114: private static final String FILE_NAME = "/WEB-INF/ivataMasks.xml";
115: /**
116: * <p>
117: * This log provides tracing and debugging information.
118: * </p>
119: */
120: private static final Logger logger = Logger
121: .getLogger(LoadDataAction.class);
122: /**
123: * Number of hammers to start with.
124: */
125: private static final int QUANTITY_HAMMER_1 = 2;
126: /**
127: * Number of hammers to start with.
128: */
129: private static final int QUANTITY_HAMMER_2 = 3;
130: /**
131: * Number of hammers to start with.
132: */
133: private static final int QUANTITY_HAMMER_3 = 5;
134: /**
135: * Number of nails to start with.
136: */
137: private static final int QUANTITY_NAILS_1 = 55;
138: /**
139: * Number of nails to start with.
140: */
141: private static final int QUANTITY_NAILS_3 = 25;
142: /**
143: * Number of wood products to start with.
144: */
145: private static final int QUANTITY_WOOD_2 = 1;
146: /**
147: * Number of wood products to start with.
148: */
149: private static final int QUANTITY_WOOD_3 = 2;
150:
151: /**
152: * <p>
153: * Add a new customer.
154: * </p>
155: *
156: * @param emailAddress standard email address, such as
157: * <code>bob@nowhere.com</code>.
158: * @param firstName First name(s) of the customer.
159: * @param lastName Last name (surname) of the customer.
160: * @param postalAddress Full customer postal address, including carriage
161: * returns, region/state, country, etc.
162: * @return freshly created customer which has been added to the system.
163: */
164: private CustomerDO addCustomer(final String emailAddress,
165: final String firstName, final String lastName,
166: final String postalAddress) {
167: if (logger.isDebugEnabled()) {
168: logger.debug("addCustomer(String emailAddress = "
169: + emailAddress + ", String firstName = "
170: + firstName + ", String lastName = " + lastName
171: + ", String postalAddress = " + postalAddress
172: + ") - start");
173: }
174:
175: CustomerDO customer = new CustomerDO();
176: customer.setEmailAddress(emailAddress);
177: customer.setFirstName(firstName);
178: customer.setLastName(lastName);
179: customer.setPostalAddress(postalAddress);
180: Catalog catalog = Catalog.getInstance();
181: try {
182: catalog.add(null, customer);
183: } catch (PersistenceException e) {
184: logger.error("addCustomer(String, String, String, String)",
185: e);
186:
187: e.printStackTrace();
188: throw new RuntimeException(e);
189: }
190:
191: if (logger.isDebugEnabled()) {
192: logger.debug("addCustomer - end - return value = "
193: + customer);
194: }
195: return customer;
196: }
197:
198: /**
199: * <p>
200: * Add a new order.
201: * </p>
202: *
203: * @param customerParam customer who placed the order.
204: * @param itemsParam a <code>List</code> of {@link OrderItemDO} instances.
205: * @return new instance of order for the customer and items provided.
206: */
207: private OrderDO addOrder(final CustomerDO customerParam,
208: final List itemsParam) {
209: if (logger.isDebugEnabled()) {
210: logger.debug("addOrder(CustomerDO customerParam = "
211: + customerParam + ", List itemsParam = "
212: + itemsParam + ") - start");
213: }
214:
215: OrderDO order = new OrderDO();
216: order.setCustomer(customerParam);
217: order.setItems(itemsParam);
218: Catalog catalog = Catalog.getInstance();
219: try {
220: catalog.add(null, order);
221: } catch (PersistenceException e) {
222: logger.error("addOrder(CustomerDO, List)", e);
223:
224: e.printStackTrace();
225: throw new RuntimeException(e);
226: }
227:
228: if (logger.isDebugEnabled()) {
229: logger
230: .debug("addOrder(CustomerDO, List) - end - return value = "
231: + order);
232: }
233: return order;
234: }
235:
236: /**
237: * <p>
238: * Add a new product.
239: * </p>
240: *
241: * @param name Name of the product being added.
242: * @param price How much each item of this product will cost.
243: * @param description Just some silly text to describe what it <i>really</i>
244: * does.
245: * @return new product, freshly initialized.
246: */
247: private ProductDO addProduct(final String name,
248: final BigDecimal price, final String description) {
249: if (logger.isDebugEnabled()) {
250: logger.debug("addProduct(String name = " + name
251: + ", BigDecimal price = " + price
252: + ", String description = " + description
253: + ") - start");
254: }
255:
256: ProductDO product = new ProductDO();
257: product.setName(name);
258: product.setPrice(price);
259: product.setDescription(description);
260: Catalog catalog = Catalog.getInstance();
261: try {
262: catalog.add(null, product);
263: } catch (PersistenceException e) {
264: logger.error("addProduct(String, BigDecimal, String)", e);
265:
266: throw new RuntimeException(e);
267: }
268:
269: if (logger.isDebugEnabled()) {
270: logger
271: .debug("addProduct - end - return value = "
272: + product);
273: }
274: return product;
275: }
276:
277: /**
278: * <p>
279: * Create a new order item.
280: * </p>
281: *
282: * @param product the product which is being ordered.
283: * @param quantity number of this product which is being ordered.
284: * @return new instance of order item for the product and quantity.
285: */
286: private OrderItemDO createOrderItem(final ProductDO product,
287: final int quantity) {
288: if (logger.isDebugEnabled()) {
289: logger.debug("createOrderItem(ProductDO product = "
290: + product + ", int quantity = " + quantity
291: + ") - start");
292: }
293:
294: OrderItemDO item = new OrderItemDO();
295: item.setProduct(product);
296: item.setQuantity(quantity);
297: Catalog catalog = Catalog.getInstance();
298: try {
299: catalog.add(null, item);
300: } catch (PersistenceException e) {
301: logger.error("createOrderItem(ProductDO, int)", e);
302:
303: e.printStackTrace();
304: throw new RuntimeException(e);
305: }
306:
307: if (logger.isDebugEnabled()) {
308: logger.debug("createOrderItem - end - return value = "
309: + item);
310: }
311: return item;
312: }
313:
314: /**
315: * <p>
316: * Generic method called by the <strong>Struts </strong> interface. Always
317: * returns success. .
318: * </p>
319: *
320: * @param mapping
321: * The ActionMapping used to select this instance
322: * @param form
323: * The optional ActionForm bean for this request (if any)
324: * @param request
325: * The non-HTTP request we are processing
326: * @param response
327: * The non-HTTP response we are creating
328: * @exception Exception
329: * if the application business logic throws an exception
330: * @return this method returns a <code>"success"</code>
331: * <code>ActionForward</code>
332: * every time.
333: */
334: public ActionForward execute(final ActionMapping mapping,
335: final ActionForm form, final HttpServletRequest request,
336: final HttpServletResponse response) throws Exception {
337: if (logger.isDebugEnabled()) {
338: logger.debug("execute(ActionMapping mapping = " + mapping
339: + ", ActionForm form = " + form
340: + ", HttpServletRequest request = " + request
341: + ", HttpServletResponse response = " + response
342: + ") - start");
343: }
344:
345: // this is just a demo, so we use the HTTP session to contain all our
346: // objects. in real life, we'd use something like an IoC container
347: Catalog catalog = Catalog.getInstance();
348: HttpSession session = request.getSession();
349: catalog.reset();
350: // first create some products we can buy
351: // NOTE: I cheated here - these are hard-coded 'magic numbers' - this is
352: // a demo app, after all. :-)
353: ProductDO nails = addProduct("10 inch nails", new BigDecimal(
354: "1.99"), "pack of 50, 10 inch steel nails");
355: ProductDO hammer = addProduct("large hammer", new BigDecimal(
356: "4.99"), "huge hammer, good for hitting nails with");
357: ProductDO wood = addProduct("block of wood", new BigDecimal(
358: "2.99"),
359: "beautifully crafted piece of waste wood. ideal for practise.");
360: List products = catalog.findAll(null, ProductDO.class);
361: session.setAttribute("products", products);
362: // now create some test customers
363: CustomerDO johnny = addCustomer("john.tester@test.com",
364: "Johnny", "Tester",
365: "John H Tester\n23 Nowhere Avenue\nNotown\nOuter Nosuchland");
366: CustomerDO fred = addCustomer("fred.another@test.com", "Fred",
367: "Another",
368: "Freddie Another\n98 Madeupname Street\nDontexist\nNowayland");
369: // and create some test orders for those customers
370: List items = new Vector();
371: items.add(createOrderItem(nails, QUANTITY_NAILS_1));
372: items.add(createOrderItem(hammer, QUANTITY_HAMMER_1));
373: addOrder(johnny, items);
374: items = new Vector();
375: items.add(createOrderItem(hammer, QUANTITY_HAMMER_2));
376: items.add(createOrderItem(wood, QUANTITY_WOOD_2));
377: addOrder(johnny, items);
378: items = new Vector();
379: items.add(createOrderItem(nails, QUANTITY_NAILS_3));
380: items.add(createOrderItem(hammer, QUANTITY_HAMMER_3));
381: items.add(createOrderItem(wood, QUANTITY_WOOD_3));
382: addOrder(fred, items);
383: // read in the configuration for the masks
384: ServletContext context = servlet.getServletContext();
385: FieldWriterFactory fieldWriterFactory = new DefaultFieldWriterFactory(
386: catalog, "/find.action");
387: context.setAttribute(FieldWriterFactory.APPLICATION_ATTRIBUTE,
388: fieldWriterFactory);
389: MaskFactory maskFactory = catalog.getMaskFactory();
390: maskFactory.readConfiguration(context
391: .getResourceAsStream(FILE_NAME));
392: context.setAttribute("MaskFactory", maskFactory);
393: Mask mask = maskFactory.getMask(ProductDO.class, "list");
394: if (mask == null) {
395: logger.error("ERROR: mask is null");
396: throw new NullPointerException(
397: "ERROR in LoadDataAction: mask is " + "null");
398: }
399: ListForm listForm = new ListForm(products, mask,
400: ProductDO.class);
401: request.setAttribute(ListForm.REQUEST_ATTRIBUTE, listForm);
402: ActionForward returnActionForward = mapping
403: .findForward("success");
404: if (logger.isDebugEnabled()) {
405: logger.debug("execute - end - return value = "
406: + returnActionForward);
407: }
408: return returnActionForward;
409: }
410: }
|