1 package be.dvw.administration.mvc.actions;
2
3 import be.dvw.administration.Constants;
4 import be.dvw.administration.model.Score;
5 import be.dvw.administration.model.Instrument;
6 import be.dvw.administration.services.scores.ScoreService;
7 import be.dvw.administration.services.scores.ScoreServiceCastorImpl;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionForward;
12 import org.apache.struts.action.ActionMapping;
13 import org.apache.struts.action.DynaActionForm;
14 import org.apache.struts.actions.MappingDispatchAction;
15
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18 import java.util.List;
19
20
21 public final class ScoreAction extends MappingDispatchAction {
22
23
24 /***
25 * Logger for the action class
26 */
27 private static Log LOG = LogFactory.getLog(ScoreAction.class);
28 /***
29 * Score 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 ScoreService scoreService = new ScoreServiceCastorImpl();
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 score.
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
73 if (request.getParameter("id") == null || "".equals( request.getParameter("id" )))
74 {
75 request.setAttribute("error","error-noselection");
76 List list = scoreService.getAll();
77 request.setAttribute("list", list);
78 return mapping.findForward("error-noselection");
79 }
80
81
82 Score p;
83 p = scoreService.load(Integer.parseInt(request.getParameter("id")));
84 DynaActionForm dynForm = (DynaActionForm) form;
85
86 dynForm.set("id", Integer.toString( p.getId()) ) ;
87 dynForm.set("name", p.getName() );
88 dynForm.set("composer", p.getComposer() );
89 dynForm.set("arranger", p.getArranger() );
90 dynForm.set("price", Float.toString( p.getPrice() ) );
91 dynForm.set("boxnumber", Integer.toString( p.getBoxNumber() ) ) ;
92 dynForm.set("description", p.getDescription());
93
94
95 request.getSession().setAttribute(Constants.ACTION, "edit");
96
97 return mapping.findForward("success");
98 }
99 catch (Exception e)
100 {
101 LOG.error("Edit form failed to load ", e);
102 return mapping.findForward("error-partituur");
103 }
104
105 }
106
107
108 /***
109 * Saves the score passed by the creation / update form. This action is used both when creating a new
110 * @param mapping
111 * @param form
112 * @param request
113 * @param response
114 * @return
115 * @throws Exception
116 */
117 public ActionForward save(ActionMapping mapping,
118 ActionForm form,
119 HttpServletRequest request,
120 HttpServletResponse response)
121 throws Exception {
122
123 Score p = new Score();
124 boolean newScore = true;
125
126 DynaActionForm dynForm = (DynaActionForm) form;
127
128 if ( dynForm.get("id") != null &&
129 !"".equals( dynForm.get("id") ) &&
130 !"0".equals( (String) dynForm.get("id") ) )
131 {
132 p.setId( Integer.parseInt((String) dynForm.get( "id")) );
133 newScore = false;
134 }
135
136 p.setName( (String)dynForm.get("name") );
137 p.setComposer( (String)dynForm.get( "composer") );
138 p.setArranger( (String)dynForm.get( "arranger") );
139 p.setBoxNumber( Integer.parseInt( (String) dynForm.get( "boxnumber") ) ) ;
140 p.setPrice( Float.parseFloat ( (String) dynForm.get( "price") ) );
141 p.setDescription( (String)dynForm.get( "description") );
142
143 p = scoreService.save(p, newScore);
144
145 request.setAttribute( Constants.SCORE_CURRENT, p);
146 return mapping.findForward("success");
147 }
148
149 public ActionForward delete(ActionMapping mapping,
150 ActionForm form,
151 HttpServletRequest request,
152 HttpServletResponse response)
153 throws Exception {
154
155 scoreService.delete( Integer.parseInt(request.getParameter("id")) );
156 request.getSession().setAttribute(Constants.ACTION, "delete");
157 return mapping.findForward("success");
158
159 }
160
161
162 /***
163 * Loads the inventory in the request and forwards to the list page.
164 * Adds the inventory action so that the list page is aware of its function.
165 * @param mapping
166 * @param form
167 * @param request
168 * @param response
169 * @return
170 * @throws Exception
171 */
172 public ActionForward inventory(ActionMapping mapping,
173 ActionForm form,
174 HttpServletRequest request,
175 HttpServletResponse response)
176 throws Exception {
177
178 List list = scoreService.getAll();
179 request.setAttribute("list", list);
180 request.getSession().setAttribute(Constants.NAVIGATION_CONTEXT, "inventory");
181 request.getSession().setAttribute(Constants.ACTION, "inventory");
182 return mapping.findForward("success");
183 }
184
185
186 /***
187 * Loads the list corresponding to the search results in the request and forwards to the list page.
188 * Adds the inventory action so that the list page is aware of its function.
189 * @param mapping
190 * @param form
191 * @param request
192 * @param response
193 * @return
194 * @throws Exception
195 */
196 public ActionForward search (ActionMapping mapping,
197 ActionForm form,
198 HttpServletRequest request,
199 HttpServletResponse response)
200 throws Exception
201 {
202
203 List list = scoreService.search("");
204 request.setAttribute("list", list);
205 return mapping.findForward("success");
206 }
207
208
209 /***
210 * Forwards to the search form for scores.
211 * @param mapping
212 * @param form
213 * @param request
214 * @param response
215 * @return
216 * @throws Exception
217 */
218 public ActionForward searchForm (ActionMapping mapping,
219 ActionForm form,
220 HttpServletRequest request,
221 HttpServletResponse response)
222 throws Exception
223 {
224 request.getSession().setAttribute(Constants.NAVIGATION_CONTEXT, "search");
225 request.getSession().setAttribute(Constants.ACTION, "search");
226 return mapping.findForward("success");
227 }
228
229 public ActionForward details (ActionMapping mapping,
230 ActionForm form,
231 HttpServletRequest request,
232 HttpServletResponse response)
233 throws Exception
234 {
235 try
236 {
237
238 if (request.getParameter("id") == null || "".equals( request.getParameter("id" )))
239 {
240 request.setAttribute("error","error-noselection");
241 List list = scoreService.getAll();
242 request.setAttribute("list", list);
243 return mapping.findForward("error-noselection");
244 }
245
246 Score i = scoreService.load(Integer.parseInt( request.getParameter("id") ));
247 request.setAttribute( Constants.SCORE_CURRENT, i);
248 request.getSession().setAttribute(Constants.ACTION, "details");
249 return mapping.findForward("success");
250 }
251 catch (Exception e)
252 {
253 LOG.error("Score failed to load ", e);
254 return mapping.findForward("error-instrument");
255 }
256 }
257
258 }