View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: LoadDataAction.java,v $
31   * Revision 1.8  2005/04/09 18:04:14  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.7  2005/01/19 12:18:45  colinmacleod
35   * Removed magic number from FieldWriterFactory constructor.
36   *
37   * Revision 1.6  2005/01/07 08:08:19  colinmacleod
38   * Moved up a version number.
39   * Changed copyright notices to 2005.
40   * Updated the documentation:
41   *   - started working on multiproject:site docu.
42   *   - changed the logo.
43   * Added checkstyle and fixed LOADS of style issues.
44   * Added separate thirdparty subproject.
45   * Added struts (in web), util and webgui (in webtheme) from ivata op.
46   *
47   * Revision 1.5  2004/12/29 15:11:12  colinmacleod
48   * Replaced hard-coded application attribute name with static variable.
49   *
50   * Revision 1.4  2004/12/23 21:28:16  colinmacleod
51   * Modifications to add ivata op compatibility.
52   *
53   * Revision 1.3  2004/11/12 15:10:41  colinmacleod
54   * Moved persistence classes from ivata op as a replacement for
55   ValueObjectLocator.
56   *
57   * Revision 1.2  2004/11/11 13:20:33  colinmacleod
58   * Added new data objects.
59   * Added logging.
60   *
61   * Revision 1.1.1.1  2004/05/16 20:40:07  colinmacleod
62   * Ready for 0.1 release
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         // this is just a demo, so we use the HTTP session to contain all our
242         // objects. in real life, we'd use something like an IoC container
243         Catalog catalog = Catalog.getInstance();
244         HttpSession session = request.getSession();
245         catalog.reset();
246         // first create some products we can buy
247         // NOTE: I cheated here - these are hard-coded 'magic numbers' - this is
248         // a demo app, after all. :-)
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         // now create some test customers
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         // and create some test orders for those customers
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         // read in the configuration for the masks
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 }