KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > addressbook > struts > GroupListAction


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: GroupListAction.java,v $
31  * Revision 1.4 2005/04/29 02:48:14 colinmacleod
32  * Data bugfixes.
33  * Changed primary key back to Integer.
34  *
35  * Revision 1.3 2005/04/10 20:09:38 colinmacleod
36  * Added new themes.
37  * Changed id type to String.
38  * Changed i tag to em and b tag to strong.
39  * Improved PicoContainerFactory with NanoContainer scripts.
40  *
41  * Revision 1.2 2005/04/09 17:19:08 colinmacleod
42  * Changed copyright text to GPL v2 explicitly.
43  *
44  * Revision 1.1.1.1 2005/03/10 17:50:31 colinmacleod
45  * Restructured ivata op around Hibernate/PicoContainer.
46  * Renamed ivata groupware.
47  *
48  * Revision 1.4 2004/12/31 18:27:42 colinmacleod
49  * Added MaskFactory to constructor of MaskAction.
50  *
51  * Revision 1.3 2004/12/23 21:01:25 colinmacleod
52  * Updated Struts to v1.2.4.
53  * Changed base classes to use ivata masks.
54  *
55  * Revision 1.2 2004/11/12 18:19:13 colinmacleod
56  * Change action and form classes to extend MaskAction, MaskForm respectively.
57  *
58  * Revision 1.1 2004/11/12 15:40:25 colinmacleod
59  * First version in CVS.
60  * -----------------------------------------------------------------------------
61  */

62 package com.ivata.groupware.business.addressbook.struts;
63
64 import java.lang.reflect.InvocationTargetException JavaDoc;
65 import java.util.HashMap JavaDoc;
66 import java.util.Iterator JavaDoc;
67 import java.util.List JavaDoc;
68 import java.util.Map JavaDoc;
69
70 import javax.servlet.http.HttpServletRequest JavaDoc;
71 import javax.servlet.http.HttpServletResponse JavaDoc;
72 import javax.servlet.http.HttpSession JavaDoc;
73
74 import org.apache.commons.beanutils.PropertyUtils;
75 import org.apache.struts.action.ActionErrors;
76 import org.apache.struts.action.ActionForm;
77 import org.apache.struts.action.ActionMapping;
78 import org.apache.struts.taglib.TagUtils;
79
80 import com.ivata.groupware.admin.security.server.SecuritySession;
81 import com.ivata.groupware.business.addressbook.AddressBook;
82 import com.ivata.groupware.business.addressbook.person.group.GroupConstants;
83 import com.ivata.groupware.business.addressbook.person.group.GroupDO;
84 import com.ivata.mask.MaskFactory;
85 import com.ivata.mask.util.StringHandling;
86 import com.ivata.mask.util.SystemException;
87 import com.ivata.mask.web.struts.MaskAction;
88 import com.ivata.mask.web.struts.MaskAuthenticator;
89
90 /**
91  * <p>
92  * This action tells the JSP page <code>groupList.jsp</code> what to display,
93  * and how to display it.
94  * </p>
95  *
96  * @since ivata groupware 0.9 (2004-11-03)
97  * @author Colin MacLeod
98  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
99  * @version $Revision: 1.4 $
100  */

101
102 public class GroupListAction extends MaskAction {
103     /**
104      * <p>
105      * Address book implementation, used to retrieve address books/user groups.
106      * </p>
107      */

108     private AddressBook addressBook;
109
110     /**
111      * <p>
112      * Constructor. This is called by <code>PicoContainer</code>.
113      * </p>
114      *
115      * @param addressBook Address book implementation, used to retrieve address
116      * books/user groups.
117      * @param maskFactory This factory is needed to access the masks and groups
118      * of masks.
119      * @param authenticator used to confirm whether or not the
120      * user should be allowed to continue, in the <code>execute</code> method.
121      */

122     public GroupListAction(final AddressBook addressBook,
123             final MaskFactory maskFactory,
124             MaskAuthenticator authenticator) {
125         super(maskFactory, authenticator);
126         this.addressBook = addressBook;
127     }
128
129     /**
130      * <p>Clear all bean properties to their default state.The difference
131      * between this and <code>reset</code> is that all properties are changed,
132      * regardless of current request state.</p>
133      *
134      * @param mapping The mapping used to select this instance
135      * @param request The servlet request we are processing
136      * @see com.ivata.mask.web.struts.DialogForm#clear()
137      */

138     protected void clear() {
139     }
140
141     /**
142      * <p>
143      * This method does all the hard work in preparing data for
144      * <code>frameIndex.jsp</code>.
145      * </p>
146      *
147      * @param mapping The ActionMapping used to select this instance.
148      * @param log valid logging object to write messages to.
149      * @param errors valid errors object to append errors to. If there are
150      * any errors, the action will return to the input.
151      * @param form optional ActionForm bean for this request (if any)
152      * @param request non-HTTP request we are processing
153      * @param response The non-HTTP response we are creating
154      * @param session returned from the <code>request</code> parameter.
155      * @exception SystemException if there is any problem which
156      * prevents processing. It will result in the webapp being forwarded
157      * to
158      * the standard error page.
159      * @return this method returns the string used to identify the correct
160      * <em>Struts</em> <code>ActionForward</code> which should follow this
161      * page, or <code>null</code> if it should return to the input.
162
163      * @see com.ivata.mask.web.struts.MaskAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionMessages, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.http.HttpSession)
164      */

165     public String JavaDoc execute(final ActionMapping mapping,
166             final ActionErrors errors,
167             final ActionForm form,
168             final HttpServletRequest JavaDoc request,
169             final HttpServletResponse JavaDoc response,
170             final HttpSession JavaDoc session)
171             throws SystemException {
172         String JavaDoc mode = request.getParameter("mode");
173         if (StringHandling.isNullOrEmpty(mode)) {
174             throw new SystemException("No mode specified in GroupListAction");
175         }
176         Integer JavaDoc selectedId = StringHandling.integerValue(request
177                 .getParameter("selectedId"));
178
179         // this page can handle 2 different types of content:
180
// address books and user groups
181
String JavaDoc contentFrameName, menuFrameName;
182         List JavaDoc groups;
183         SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
184         String JavaDoc inputMask, forward, menuFrameURI, resourceFieldPath, deleteKey;
185         Boolean JavaDoc empty = null;
186         if ("addressBook".equals(mode)) {
187             contentFrameName = "ivataAddressBook";
188             deleteKey = "addressBook.alert.delete";
189             groups = addressBook.findAddressBooks(securitySession, true);
190             inputMask = "imAddressBookInputMaskAction";
191             forward ="addressBookGroupList";
192             menuFrameURI = "/addressBook/groupList.action?mode=addressBook";
193             resourceFieldPath = "addressBook";
194         } else if ("userGroup".equals(mode)) {
195             contentFrameName = "ivataUserGroup";
196             deleteKey = "userGroup.alert.delete";
197             groups = addressBook.findUserGroups(securitySession, false);
198             inputMask = "imUserGroupInputMaskAction";
199             forward ="addressBookGroupList";
200             menuFrameURI = "/addressBook/groupList.action?mode=userGroup";
201             resourceFieldPath = "userGroup";
202         } else if ("group".equals(mode)) {
203             contentFrameName = "ivataGroup";
204             deleteKey = "group.alert.delete";
205             groups = addressBook.findAddressBooks(securitySession, true);
206             inputMask = "imGroupInputMaskAction";
207             forward ="addressBookGroupTree";
208             resourceFieldPath = "group";
209             menuFrameURI = "/addressBook/groupTree.action"
210                 + "?inputMaskInvoked=true&mode=group";
211
212             // indicates the requests did came from the group tree page
213
boolean idIsAddressBook =
214                 "true".equals(request.getParameter("idIsAddressBook"));
215
216             Integer JavaDoc selectedIdString = null;
217             // if there is no request parameter, default back to the form
218
// value
219
if (selectedId == null) {
220                 try {
221                     selectedId = (Integer JavaDoc)PropertyUtils.getProperty(form,
222                             "selectedId");
223                     // if we got the id from the form, that's an address
224
// book, AlRighty!
225
if (selectedId != null) {
226                         idIsAddressBook = true;
227                     }
228                 } catch (IllegalAccessException JavaDoc e) {
229                     throw new SystemException(e);
230                 } catch (InvocationTargetException JavaDoc e) {
231                     throw new SystemException(e);
232                 } catch (NoSuchMethodException JavaDoc e) {
233                     throw new SystemException(e);
234                 }
235             }
236             if (selectedId == null) {
237                 // default to the system-wide address book
238
selectedIdString = GroupConstants.ADDRESS_BOOK_DEFAULT;
239                 idIsAddressBook = true;
240             } else {
241                 selectedIdString = selectedId;
242             }
243             if (!idIsAddressBook) {
244                 // note in this case the selected id comes from the input
245
// mask, which refers to the group which
246
// is appearing in the right pane - we need to translate
247
// that into the address book
248
GroupDO group = addressBook.findGroupByPrimaryKey(
249                         securitySession,
250                         selectedIdString);
251                 assert (group != null);
252                 selectedIdString = group.getAddressBook().getId();
253             }
254             // see if the tree is empty or not
255
List JavaDoc children = addressBook.findGroupsByParent(securitySession,
256                     selectedIdString);
257             empty = new Boolean JavaDoc(children.size() <= 0);
258             selectedId = selectedIdString;
259         } else {
260             throw new SystemException("ERROR in GroupFrameAction: unidentified mode '"
261                     + mode
262                     + "'");
263         }
264
265         // we just want a map - the ids to the group names
266
Map JavaDoc groupMap = new HashMap JavaDoc();
267         Iterator JavaDoc iterator = groups.iterator();
268         while (iterator.hasNext()) {
269             GroupDO group = (GroupDO) iterator.next();
270             // ids are converted to strings ... easier to compare
271
groupMap.put(group.getId().toString(), group.getName());
272         }
273
274         try {
275             TagUtils tagUtils = TagUtils.getInstance();
276
277             PropertyUtils.setProperty(form, "contentFrameName", contentFrameName);
278             PropertyUtils.setProperty(form, "deleteKey", deleteKey);
279             PropertyUtils.setProperty(form, "groupMap", groupMap);
280             PropertyUtils.setProperty(form, "inputMask", inputMask);
281             PropertyUtils.setProperty(form, "menuFrameURI", menuFrameURI);
282             PropertyUtils.setProperty(form, "resourceFieldPath", resourceFieldPath);
283             PropertyUtils.setProperty(form, "selectedId", selectedId);
284
285             // only the tree form has empty...
286
if (empty != null) {
287                 PropertyUtils.setProperty(form, "empty", empty);
288             }
289         } catch (IllegalAccessException JavaDoc e) {
290             throw new SystemException(e);
291         } catch (InvocationTargetException JavaDoc e) {
292             throw new SystemException(e);
293         } catch (NoSuchMethodException JavaDoc e) {
294             throw new SystemException(e);
295         }
296
297         return forward;
298     }
299 }
300
Popular Tags