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 java.util.Arrays;
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.party.model.contact.bo.ContactInfo;
27  import net.sf.oness.party.model.contact.bo.Country;
28  import net.sf.oness.party.model.facade.PartyFacadeDelegate;
29  import net.sf.oness.party.model.party.bo.Party;
30  import net.sf.oness.party.webapp.controller.util.CountryToStringConverter;
31  import net.sf.oness.party.webapp.controller.util.StringToCountryConverter;
32  
33  import org.apache.commons.beanutils.BeanUtils;
34  import org.apache.commons.beanutils.ConvertUtils;
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  import org.apache.struts.action.DynaActionForm;
39  
40  /***
41   * Party actions
42   * 
43   * @author Carlos Sanchez
44   * @version $Revision: 1.7 $
45   */
46  public class ContactInfoAction extends AutoDispatchAction {
47  
48      static {
49          /* needed in edit */
50          ConvertUtils.register(new CountryToStringConverter(), String.class);
51          /* needed in getContactInfo */
52          ConvertUtils.register(new StringToCountryConverter(), Country.class);
53      }
54  
55      /***
56       * Create a contact info.
57       * 
58       * @param mapping
59       * @param actionForm
60       * @param request
61       * @param response
62       * @return ActionForward success
63       * @throws Exception
64       */
65      public ActionForward create(ActionMapping mapping, ActionForm actionForm,
66              HttpServletRequest request, HttpServletResponse response)
67              throws Exception {
68  
69          ContactInfo contactInfo = getContactInfo(actionForm);
70          contactInfo = getFacade().createContactInfo(contactInfo);
71          request.setAttribute("contactInfo", contactInfo);
72  
73          /* Return ActionForward. */
74          return mapping.findForward("success");
75      }
76  
77      /***
78       * Update a contact info.
79       * 
80       * @param mapping
81       * @param actionForm
82       * @param request
83       * @param response
84       * @return ActionForward success
85       * @throws Exception
86       */
87      public ActionForward update(ActionMapping mapping, ActionForm actionForm,
88              HttpServletRequest request, HttpServletResponse response)
89              throws Exception {
90  
91          ContactInfo contactInfo = getContactInfo(actionForm);
92          request.setAttribute("contactInfo", getFacade().updateContactInfo(
93                  contactInfo));
94          return mapping.findForward("success");
95      }
96  
97      /***
98       * Find a contact info.
99       * 
100      * @param mapping
101      * @param actionForm
102      * @param request
103      * @param response
104      * @return ActionForward success
105      * @throws Exception
106      */
107     public ActionForward find(ActionMapping mapping, ActionForm actionForm,
108             HttpServletRequest request, HttpServletResponse response)
109             throws Exception {
110 
111         ContactInfo contactInfo = getContactInfo(actionForm);
112 
113         int firstElement = ActionUtils.getFirstElement(request);
114         int maxElements = ActionUtils.getMaxElements(request);
115 
116         PaginatedList results = getFacade().findContactInfo(contactInfo,
117                 firstElement, maxElements);
118 
119         ActionUtils.setIteratorPageParameters(results, request);
120 
121         /* Return ActionForward. */
122         return mapping.findForward("success");
123     }
124 
125     /***
126      * Show a contact info.
127      * 
128      * @param mapping
129      * @param actionForm
130      * @param request
131      * @param response
132      * @return ActionForward success
133      * @throws Exception
134      */
135     public ActionForward show(ActionMapping mapping, ActionForm actionForm,
136             HttpServletRequest request, HttpServletResponse response)
137             throws Exception {
138 
139         request.setAttribute("contactInfo", getFacade().findContactInfo(
140                 getId(actionForm)));
141         return mapping.findForward("success");
142     }
143 
144     /***
145      * Edit a contact info.
146      * 
147      * @param mapping
148      * @param actionForm
149      * @param request
150      * @param response
151      * @return ActionForward success
152      * @throws Exception
153      */
154     public ActionForward edit(ActionMapping mapping, ActionForm actionForm,
155             HttpServletRequest request, HttpServletResponse response)
156             throws Exception {
157 
158         ContactInfo contactInfo = getFacade()
159                 .findContactInfo(getId(actionForm));
160         BeanUtils.copyProperties(actionForm, contactInfo);
161         DynaActionForm form = (DynaActionForm) actionForm;
162         form.set("class", contactInfo.getClass().getName());
163         form.set("partyId", contactInfo.getParty().getId().toString());
164         return mapping.findForward("success");
165     }
166 
167     /***
168      * Delete a contact info.
169      * 
170      * @param mapping
171      * @param actionForm
172      * @param request
173      * @param response
174      * @return ActionForward success
175      * @throws Exception
176      */
177     public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
178             HttpServletRequest request, HttpServletResponse response)
179             throws Exception {
180 
181         getFacade().deleteContactInfo(getId(actionForm));
182         return mapping.findForward("success");
183     }
184 
185     private PartyFacadeDelegate getFacade() {
186         return (PartyFacadeDelegate) getBean("partyFacadeDelegate");
187     }
188 
189     private Long getId(ActionForm actionForm) throws Exception {
190         DynaActionForm form = (DynaActionForm) actionForm;
191         return new Long((String) form.get("id"));
192     }
193 
194     private ContactInfo getContactInfo(ActionForm actionForm) throws Exception {
195         DynaActionForm form = (DynaActionForm) actionForm;
196         Class theClass = Class.forName((String) form.get("class"));
197         ContactInfo contactInfo = (ContactInfo) theClass.newInstance();
198 
199         String[] purposes = (String[]) form.get("purposes");
200         form.set("purposes", null);
201         BeanUtils.copyProperties(contactInfo, form);
202         if (purposes != null)
203             contactInfo.setPurposes(Arrays.asList(purposes));
204 
205         if (form.get("id").equals(""))
206             contactInfo.setId(null);
207 
208         if (!form.get("partyId").equals("")) {
209             Party party = new Party();
210             party.setId(new Long((String) form.get("partyId")));
211             contactInfo.setParty(party);
212         }
213 
214         return contactInfo;
215     }
216 
217 }