View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.inventory.webapp.controller.action;
17  
18  import java.util.Collection;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import net.sf.oness.common.model.util.PaginatedList;
24  import net.sf.oness.common.webapp.controller.action.ActionUtils;
25  import net.sf.oness.common.webapp.controller.action.AutoDispatchAction;
26  import net.sf.oness.common.webapp.controller.util.ConvertUtils;
27  import net.sf.oness.common.webapp.controller.util.NonEmptyStringConverter;
28  import net.sf.oness.inventory.model.facade.InventoryFacadeDelegate;
29  import net.sf.oness.inventory.model.product.bo.Model;
30  
31  import org.apache.commons.beanutils.BeanUtils;
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  import org.apache.struts.action.DynaActionForm;
36  
37  /***
38   * Model actions
39   * 
40   * @author Carlos Sanchez
41   * @version $Revision: 1.8 $
42   */
43  public class ModelAction extends AutoDispatchAction {
44  
45      /***
46       * Create a model.
47       * 
48       * @param mapping
49       * @param actionForm
50       * @param request
51       * @param response
52       * @return ActionForward success
53       * @throws Exception
54       */
55      public ActionForward create(ActionMapping mapping, ActionForm actionForm,
56              HttpServletRequest request, HttpServletResponse response)
57              throws Exception {
58  
59          Model model = new Model();
60          BeanUtils.copyProperties(model, actionForm);
61          model.setId(null);
62          model = getFacade().createModel(model, getSizeGroupId(actionForm),
63                  getColorIds(actionForm));
64          showModel(request, model);
65  
66          /* Return ActionForward. */
67          return mapping.findForward("success");
68      }
69  
70      /***
71       * Update a model.
72       * 
73       * @param mapping
74       * @param actionForm
75       * @param request
76       * @param response
77       * @return ActionForward success
78       * @throws Exception
79       */
80      public ActionForward update(ActionMapping mapping, ActionForm actionForm,
81              HttpServletRequest request, HttpServletResponse response)
82              throws Exception {
83  
84          Model model = new Model();
85          BeanUtils.copyProperties(model, actionForm);
86          showModel(request, getFacade().updateModel(model));
87          return mapping.findForward("success");
88      }
89  
90      /***
91       * Find a model.
92       * 
93       * @param mapping
94       * @param actionForm
95       * @param request
96       * @param response
97       * @return ActionForward success
98       * @throws Exception
99       */
100     public ActionForward find(ActionMapping mapping, ActionForm actionForm,
101             HttpServletRequest request, HttpServletResponse response)
102             throws Exception {
103 
104         Model model = new Model();
105         NonEmptyStringConverter.register();
106         BeanUtils.copyProperties(model, actionForm);
107         NonEmptyStringConverter.deregister();
108         int firstElement = ActionUtils.getFirstElement(request);
109         int maxElements = ActionUtils.getMaxElements(request);
110 
111         PaginatedList results = getFacade().findModel(model, firstElement,
112                 maxElements);
113 
114         ActionUtils.setIteratorPageParameters(results, request);
115 
116         /* Return ActionForward. */
117         return mapping.findForward("success");
118     }
119 
120     /***
121      * Show a model.
122      * 
123      * @param mapping
124      * @param actionForm
125      * @param request
126      * @param response
127      * @return ActionForward success
128      * @throws Exception
129      */
130     public ActionForward show(ActionMapping mapping, ActionForm actionForm,
131             HttpServletRequest request, HttpServletResponse response)
132             throws Exception {
133 
134         Model model = getFacade().findModelWithDetails(getId(actionForm));
135         showModel(request, model);
136 
137         return mapping.findForward("success");
138     }
139 
140     /***
141      * Edit a model.
142      * 
143      * @param mapping
144      * @param actionForm
145      * @param request
146      * @param response
147      * @return ActionForward success
148      * @throws Exception
149      */
150     public ActionForward edit(ActionMapping mapping, ActionForm actionForm,
151             HttpServletRequest request, HttpServletResponse response)
152             throws Exception {
153 
154         Model model = getFacade().findModel(getId(actionForm));
155         BeanUtils.copyProperties(actionForm, model);
156         return mapping.findForward("success");
157     }
158 
159     /***
160      * Delete a model.
161      * 
162      * @param mapping
163      * @param actionForm
164      * @param request
165      * @param response
166      * @return ActionForward success
167      * @throws Exception
168      */
169     public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
170             HttpServletRequest request, HttpServletResponse response)
171             throws Exception {
172 
173         getFacade().deleteModel(getId(actionForm));
174         return mapping.findForward("success");
175     }
176 
177     private InventoryFacadeDelegate getFacade() {
178         return (InventoryFacadeDelegate) getBean("inventoryFacadeDelegate");
179     }
180 
181     private Long getId(ActionForm actionForm) throws Exception {
182         DynaActionForm form = (DynaActionForm) actionForm;
183         return new Long((String) form.get("id"));
184     }
185 
186     private Long getSizeGroupId(ActionForm actionForm) {
187         return new Long((String) getDynaProperty(actionForm, "sizeGroupId"));
188     }
189 
190     private Collection getColorIds(ActionForm actionForm) {
191         return ConvertUtils.toLong((String[]) getDynaProperty(actionForm,
192                 "colorIds"));
193     }
194 
195     private void showModel(HttpServletRequest request, Model model) {
196         request.setAttribute("model", model);
197         request.setAttribute("productsSize", Integer.toString(model
198                 .getProducts().size()));
199     }
200 
201 }