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.party.webapp.controller.action;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import net.sf.oness.common.model.util.PaginatedList;
22  import net.sf.oness.common.webapp.controller.action.ActionUtils;
23  import net.sf.oness.common.webapp.controller.action.AutoDispatchAction;
24  import net.sf.oness.party.model.facade.PartyFacadeDelegate;
25  import net.sf.oness.party.model.party.bo.Organization;
26  import net.sf.oness.party.model.party.bo.Party;
27  import net.sf.oness.party.model.party.bo.Person;
28  
29  import org.apache.commons.beanutils.BeanUtils;
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  import org.apache.struts.action.DynaActionForm;
34  
35  /***
36   * Party actions
37   * 
38   * @author Carlos Sanchez
39   * @version $Revision: 1.20 $
40   */
41  public class PartyAction extends AutoDispatchAction {
42  
43      /***
44       * Create a party.
45       * 
46       * @param mapping
47       * @param actionForm
48       * @param request
49       * @param response
50       * @return ActionForward success
51       * @throws Exception
52       */
53      public ActionForward create(ActionMapping mapping, ActionForm actionForm,
54              HttpServletRequest request, HttpServletResponse response)
55              throws Exception {
56  
57          Party party = getParty(actionForm);
58  
59          party = getFacade().createParty(party);
60          request.setAttribute("party", party);
61  
62          /* Return ActionForward. */
63          return mapping.findForward("success");
64      }
65  
66      /***
67       * Update a party.
68       * 
69       * @param mapping
70       * @param actionForm
71       * @param request
72       * @param response
73       * @return ActionForward success
74       * @throws Exception
75       */
76      public ActionForward update(ActionMapping mapping, ActionForm actionForm,
77              HttpServletRequest request, HttpServletResponse response)
78              throws Exception {
79  
80          Party party = getParty(actionForm);
81          request.setAttribute("party", getFacade().updateParty(party));
82          return mapping.findForward("success");
83      }
84  
85      /***
86       * Find a party.
87       * 
88       * @param mapping
89       * @param actionForm
90       * @param request
91       * @param response
92       * @return ActionForward success
93       * @throws Exception
94       */
95      public ActionForward find(ActionMapping mapping, ActionForm actionForm,
96              HttpServletRequest request, HttpServletResponse response)
97              throws Exception {
98  
99          Party party = getParty(actionForm);
100 
101         int firstElement = ActionUtils.getFirstElement(request);
102         int maxElements = ActionUtils.getMaxElements(request);
103 
104         PaginatedList results = getFacade().findParty(party, firstElement,
105                 maxElements);
106 
107         ActionUtils.setIteratorPageParameters(results, request);
108 
109         /* Return ActionForward. */
110         return mapping.findForward("success");
111     }
112 
113     /***
114      * Show a party.
115      * 
116      * @param mapping
117      * @param actionForm
118      * @param request
119      * @param response
120      * @return ActionForward success
121      * @throws Exception
122      */
123     public ActionForward show(ActionMapping mapping, ActionForm actionForm,
124             HttpServletRequest request, HttpServletResponse response)
125             throws Exception {
126 
127         Party party = getFacade().findPartyWithDetails(getId(actionForm));
128         request.setAttribute("party", party);
129 
130         return mapping.findForward("success");
131     }
132 
133     /***
134      * Edit a party.
135      * 
136      * @param mapping
137      * @param actionForm
138      * @param request
139      * @param response
140      * @return ActionForward success
141      * @throws Exception
142      */
143     public ActionForward edit(ActionMapping mapping, ActionForm actionForm,
144             HttpServletRequest request, HttpServletResponse response)
145             throws Exception {
146 
147         Party party = getFacade().findParty(getId(actionForm));
148         BeanUtils.copyProperties(actionForm, party);
149         return mapping.findForward("success");
150     }
151 
152     /***
153      * Delete a party.
154      * 
155      * @param mapping
156      * @param actionForm
157      * @param request
158      * @param response
159      * @return ActionForward success
160      * @throws Exception
161      */
162     public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
163             HttpServletRequest request, HttpServletResponse response)
164             throws Exception {
165 
166         getFacade().deleteParty(getId(actionForm));
167         return mapping.findForward("success");
168     }
169 
170     private PartyFacadeDelegate getFacade() {
171         return (PartyFacadeDelegate) getBean("partyFacadeDelegate");
172     }
173 
174     private Long getId(ActionForm actionForm) throws Exception {
175         DynaActionForm form = (DynaActionForm) actionForm;
176         return new Long((String) form.get("id"));
177     }
178 
179     private Party getParty(ActionForm actionForm) throws Exception {
180         DynaActionForm form = (DynaActionForm) actionForm;
181         Party party = null;
182         String type = (String) form.get("type");
183         if (type.equals("PER")) {
184             party = new Person();
185         } else if (type.equals("ORG")) {
186             party = new Organization();
187         } else {
188             party = new Party();
189         }
190 
191         BeanUtils.copyProperties(party, form);
192         if (form.get("id").equals(""))
193             party.setId(null);
194         return party;
195     }
196 
197 }