View Javadoc

1   package be.dvw.administration.mvc.actions;
2   
3   import be.dvw.administration.Constants;
4   import be.dvw.administration.model.Instrument;
5   import be.dvw.administration.services.instruments.InstrumentService;
6   import be.dvw.administration.services.instruments.InstrumentServiceCastorImpl;
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   import org.apache.struts.action.ActionForm;
10  import org.apache.struts.action.ActionForward;
11  import org.apache.struts.action.ActionMapping;
12  import org.apache.struts.action.DynaActionForm;
13  import org.apache.struts.actions.MappingDispatchAction;
14  
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  import java.util.List;
18  import java.util.Calendar;
19  
20  
21  public final class InstrumentAction extends MappingDispatchAction {
22  
23  
24      /***
25       * Logger for the action class
26       */
27      private static Log LOG = LogFactory.getLog(InstrumentAction.class);
28      /***
29       * Instrument Service used by the Action. Currently, this is the castor implementation, but this is to
30       * be replaced by the Hibernate implementation.
31       */
32      private	InstrumentService instrumentService = new InstrumentServiceCastorImpl();
33  
34      /***
35       * Redirects to the edit form putting the create action in session, so that
36       * the form can be shown as a creation form.
37       * @param mapping
38       * @param form
39       * @param request
40       * @param response
41       * @return
42       * @throws Exception
43       */
44      public ActionForward create(ActionMapping mapping,
45                                  ActionForm form,
46                                  HttpServletRequest request,
47                                  HttpServletResponse response)
48              throws Exception {
49          request.getSession().setAttribute(Constants.NAVIGATION_CONTEXT, "create");
50          request.getSession().setAttribute(Constants.ACTION, "create");
51          return mapping.findForward("success");
52      }
53  
54      /***
55       * Populates the edit form and redirects to it after setting the edit action
56       * in session, so that the form can be shown as an update form.
57       * @param mapping
58       * @param form
59       * @param request must contain a parameter "id" containing the id of the instrument.
60       * @param response
61       * @return
62       * @throws Exception
63       */
64      public ActionForward edit(ActionMapping mapping,
65                                ActionForm form,
66                                HttpServletRequest request,
67                                HttpServletResponse response)
68              throws Exception {
69  
70          try
71          {
72              // If no ID is passed, show the entire inventory + an error message
73              if (request.getParameter("id") == null || "".equals( request.getParameter("id" )))
74              {
75                  request.setAttribute("error","error-noselection");
76                  List list = instrumentService.getAll();
77                  request.setAttribute("list", list);
78                  return mapping.findForward("error-noselection");
79              }
80  
81              // if an ID is passed, find the corresponding instrument and populate the form
82              Instrument i;
83              i = instrumentService.load(Integer.parseInt(request.getParameter("id")));
84              DynaActionForm dynForm = (DynaActionForm) form;
85  
86              dynForm.set("id", Integer.toString( i.getId()) ) ;
87              dynForm.set("brand",  i.getBrand() );
88              dynForm.set("type",  i.getType() );
89              dynForm.set("serialNumber", i.getSerialNumber());
90              dynForm.set("instrumentGroup", i.getInstrumentGroup() );
91              dynForm.set("purchaseDate",  i.getPurchaseDate().toString() );
92              dynForm.set("purchasePrice",  Float.toString(i.getPurchasePrice())  ) ;
93              dynForm.set("purchasedFrom",  i.getPurchasedFrom());
94              dynForm.set("purchasedNew",  Boolean.toString(i.isPurchasedNew()) );
95              dynForm.set("description",  i.getDescription() );
96  
97              // put edit in the action parameter of the session
98              request.getSession().setAttribute(Constants.ACTION, "edit");
99              // forward to the success mapping
100             return mapping.findForward("success");
101         }
102         catch (Exception e)
103         {
104             LOG.error("Edit form failed to load ", e);
105             return mapping.findForward("error-partituur");
106         }
107 
108     }
109 
110 
111     /***
112      * Saves the instrument passed by the creation / update form. This action is used both when creating a new
113      * @param mapping
114      * @param form
115      * @param request
116      * @param response
117      * @return
118      * @throws Exception
119      */
120     public ActionForward save(ActionMapping mapping,
121                               ActionForm form,
122                               HttpServletRequest request,
123                               HttpServletResponse response)
124             throws Exception {
125 
126         Instrument i = new Instrument();
127         boolean newInstrument = true;
128 
129         DynaActionForm dynForm = (DynaActionForm) form;
130 
131         if ( dynForm.get("id") != null &&
132                 !"".equals( dynForm.get("id") ) &&
133                 !"0".equals( (String) dynForm.get("id") ) )
134         {
135             i.setId( Integer.parseInt((String) dynForm.get( "id")) );
136             newInstrument = false;
137         }
138 
139         i.setBrand( (String)dynForm.get("brand") );
140         i.setType( (String)dynForm.get( "type") );
141         i.setSerialNumber( (String)dynForm.get( "serialNumber") );
142         i.setInstrumentGroup( (String)dynForm.get( "instrumentGroup") );
143         i.setPurchaseDate( (String) dynForm.get("purchaseDate") ); ;
144         i.setPurchasePrice(  Float.parseFloat ( (String) dynForm.get( "purchasePrice") ) );
145         i.setPurchasedFrom( (String)dynForm.get( "purchasedFrom") );
146         i.setPurchasedNew( new Boolean( (String) dynForm.get( "purchasedNew")).booleanValue()  );
147         i.setDescription( (String) dynForm.get( "description") );
148 
149         i = instrumentService.save(i, newInstrument);
150 
151         request.setAttribute( Constants.INSTRUMENT_CURRENT, i);
152         return mapping.findForward("success");
153     }
154 
155     public ActionForward delete(ActionMapping mapping,
156                                 ActionForm form,
157                                 HttpServletRequest request,
158                                 HttpServletResponse response)
159             throws Exception {
160 
161         instrumentService.delete( Integer.parseInt(request.getParameter("id")) );
162         request.getSession().setAttribute(Constants.ACTION, "delete");
163         return mapping.findForward("success");
164 
165     }
166 
167 
168     /***
169      * Loads the inventory in the request and forwards to the list page.
170      * Adds the inventory action so that the list page is aware of its function.
171      * @param mapping
172      * @param form
173      * @param request
174      * @param response
175      * @return
176      * @throws Exception
177      */
178     public ActionForward inventory(ActionMapping mapping,
179                                    ActionForm form,
180                                    HttpServletRequest request,
181                                    HttpServletResponse response)
182             throws Exception {
183 
184         List list = instrumentService.getAll();
185         request.setAttribute("list", list);
186         request.getSession().setAttribute(Constants.NAVIGATION_CONTEXT, "inventory");
187         request.getSession().setAttribute(Constants.ACTION, "inventory");
188         return mapping.findForward("success");
189     }
190 
191 
192     /***
193      * Loads the list corresponding to the search results in the request and forwards to the list page.
194      * Adds the inventory action so that the list page is aware of its function.
195      * @param mapping
196      * @param form
197      * @param request
198      * @param response
199      * @return
200      * @throws Exception
201      */
202     public ActionForward search (ActionMapping mapping,
203                                  ActionForm form,
204                                  HttpServletRequest request,
205                                  HttpServletResponse response)
206             throws Exception
207     {
208         //@todo define the correct search string
209         List list = instrumentService.search("");
210         request.setAttribute("list", list);
211         return mapping.findForward("success");
212     }
213 
214 
215     /***
216      * Forwards to the search form for instruments.
217      * @param mapping
218      * @param form
219      * @param request
220      * @param response
221      * @return
222      * @throws Exception
223      */
224     public ActionForward searchForm (ActionMapping mapping,
225                                      ActionForm form,
226                                      HttpServletRequest request,
227                                      HttpServletResponse response)
228             throws Exception
229     {
230         request.getSession().setAttribute(Constants.NAVIGATION_CONTEXT, "search");
231         request.getSession().setAttribute(Constants.ACTION, "search");
232         return mapping.findForward("success");
233     }
234 
235     public ActionForward details (ActionMapping mapping,
236                                   ActionForm form,
237                                   HttpServletRequest request,
238                                   HttpServletResponse response)
239             throws Exception
240     {
241         try
242         {
243             // If no ID is passed, show the entire inventory + an error message
244             if (request.getParameter("id") == null || "".equals( request.getParameter("id" )))
245             {
246                 request.setAttribute("error","error-noselection");
247                 List list = instrumentService.getAll();
248                 request.setAttribute("list", list);
249                 return mapping.findForward("error-noselection");
250             }
251 
252             Instrument i = instrumentService.load(Integer.parseInt( request.getParameter("id") ));
253             request.setAttribute( Constants.INSTRUMENT_CURRENT, i);
254             request.getSession().setAttribute(Constants.ACTION, "details");
255             return mapping.findForward("success");
256         }
257         catch (Exception e)
258         {
259             LOG.error("Instrument failed to load ", e);
260             return mapping.findForward("error-instrument");
261         }
262     }
263 
264 }