KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > navigation > struts > AddFavoriteAction


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: AddFavoriteAction.java,v $
31  * Revision 1.4 2005/04/28 18:47:09 colinmacleod
32  * Fixed XHMTL, styles and resin compatibility.
33  * Added support for URL rewriting.
34  *
35  * Revision 1.3 2005/04/10 20:32:01 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:10 colinmacleod
42  * Changed copyright text to GPL v2 explicitly.
43  *
44  * Revision 1.1.1.1 2005/03/10 17:50:36 colinmacleod
45  * Restructured ivata op around Hibernate/PicoContainer.
46  * Renamed ivata groupware.
47  *
48  * Revision 1.9 2004/12/31 18:27:43 colinmacleod
49  * Added MaskFactory to constructor of MaskAction.
50  *
51  * Revision 1.8 2004/12/23 21:01:26 colinmacleod
52  * Updated Struts to v1.2.4.
53  * Changed base classes to use ivata masks.
54  *
55  * Revision 1.7 2004/11/12 18:19:14 colinmacleod
56  * Change action and form classes to extend MaskAction, MaskForm respectively.
57  *
58  * Revision 1.6 2004/11/12 15:57:07 colinmacleod
59  * Removed dependencies on SSLEXT.
60  * Moved Persistence classes to ivata masks.
61  *
62  * Revision 1.5 2004/11/03 15:37:33 colinmacleod
63  * Changed todo comments to all caps.
64  *
65  * Revision 1.4 2004/07/13 19:41:17 colinmacleod
66  * Moved project to POJOs from EJBs.
67  * Applied PicoContainer to services layer (replacing session EJBs).
68  * Applied Hibernate to persistence layer (replacing entity EJBs).
69  *
70  * Revision 1.3 2004/03/21 21:16:19 colinmacleod
71  * Shortened name to ivata op.
72  *
73  * Revision 1.2 2004/02/01 22:00:34 colinmacleod
74  * Added full names to author tags
75  *
76  * Revision 1.1.1.1 2004/01/27 20:57:59 colinmacleod
77  * Moved ivata openportal to SourceForge..
78  *
79  * Revision 1.3 2003/10/17 12:36:13 jano
80  * fixing problems with building
81  * converting intranet -> portal
82  * Eclipse building
83  *
84  * Revision 1.2 2003/10/15 13:49:57 colin
85  * fixing fo Xdoclet
86  *
87  * Revision 1.2 2003/06/06 09:18:08 peter
88  * fixed for URL non-URL rewriting
89  *
90  * Revision 1.1 2003/03/04 14:22:37 colin
91  * first version in cvs
92  * -----------------------------------------------------------------------------
93  */

94 package com.ivata.groupware.navigation.struts;
95
96 import java.lang.reflect.InvocationTargetException JavaDoc;
97
98 import javax.servlet.http.HttpServletRequest JavaDoc;
99 import javax.servlet.http.HttpServletResponse JavaDoc;
100 import javax.servlet.http.HttpSession JavaDoc;
101
102 import org.apache.commons.beanutils.PropertyUtils;
103 import org.apache.log4j.Logger;
104 import org.apache.struts.action.ActionErrors;
105 import org.apache.struts.action.ActionForm;
106 import org.apache.struts.action.ActionMapping;
107
108 import com.ivata.groupware.admin.security.server.SecuritySession;
109 import com.ivata.groupware.admin.setting.Settings;
110 import com.ivata.groupware.navigation.menu.MenuConstants;
111 import com.ivata.groupware.navigation.menu.MenuDO;
112 import com.ivata.groupware.navigation.menu.item.MenuItemDO;
113 import com.ivata.mask.MaskFactory;
114 import com.ivata.mask.persistence.PersistenceManager;
115 import com.ivata.mask.persistence.PersistenceSession;
116 import com.ivata.mask.util.StringHandling;
117 import com.ivata.mask.util.SystemException;
118 import com.ivata.mask.web.RewriteHandling;
119 import com.ivata.mask.web.struts.MaskAction;
120 import com.ivata.mask.web.struts.MaskAuthenticator;
121
122
123 /**
124  * <p>This action is called when the user clicks on the
125  * 'add to favorites' heart in the title frame.</p>
126  *
127  * @since 2003-03-04
128  * @author Colin MacLeod
129  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
130  * @version $Revision: 1.4 $
131  */

132 public class AddFavoriteAction extends MaskAction {
133     /**
134      * Refer to {@link Logger}.
135      */

136     private static Logger log = Logger.getLogger(AddFavoriteAction.class);
137     /**
138      * <p>
139      * Used to retrieve favorites menu and add new items to it.
140      * </p>
141      */

142     private PersistenceManager persistenceManager;
143
144     /**
145      * <p>
146      * Settings implementation.
147      * </p>
148      */

149     private Settings settings;
150
151     /**
152      * Construct the action.
153      *
154      * @param persisitenceManager used to retrieve favorites menu, and add new
155      * items to it.
156      * @param settings settings implementation.
157      * @param maskFactory This factory is needed to access the masks and groups
158      * of masks.
159      * @param authenticator used to confirm whether or not the
160      * user should be allowed to continue, in the <code>execute</code> method.
161      */

162     public AddFavoriteAction(final PersistenceManager persistenceManagerParam,
163             final Settings settings,
164             final MaskFactory maskFactory,
165             final MaskAuthenticator authenticator) {
166         super(maskFactory, authenticator);
167         this.persistenceManager = persistenceManagerParam;
168         this.settings = settings;
169     }
170
171     /**
172      * <p>Add the title and URL in request paramters to the favorites
173      * and forward to the left frame refreshed.</p>
174      *
175      * @param mapping The ActionMapping used to select this instance.
176      * @param log valid logging object to write messages to.
177      * @param errors valid errors object to append errors to. If there are
178      * any errors, the action will return to the input.
179      * @param form optional ActionForm bean for this request (if any)
180      * @param request non-HTTP request we are processing
181      * @param response The non-HTTP response we are creating
182      * @param session returned from the <code>request</code> parameter.
183      * @exception SystemException if there is any problem which
184      * prevents processing. It will result in the webapp being forwarded
185      * to
186      * the standard error page.
187      * @return this method returns the string used to identify the correct
188      * <em>Struts</em> <code>ActionForward</code> which should follow this
189      * page, or <code>null</code> if it should return to the input.
190      */

191     public String JavaDoc execute(final ActionMapping mapping,
192             final ActionErrors errors,
193             final ActionForm form,
194             final HttpServletRequest JavaDoc request,
195             final HttpServletResponse JavaDoc response,
196             final HttpSession JavaDoc session) throws SystemException {
197         if (log.isDebugEnabled()) {
198             log.debug("In AddFavoriteAction.execute.");
199         }
200         // TODO: might want to make favorite id a setting somewhere
201
// - for now, it is hard-coded to 0 :-)
202

203         // store the favorite, only store the path without the contextPath and
204
// strip the jsessionid
205
String JavaDoc favorite, uRL;
206         try {
207             favorite = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "favorite");;
208             uRL = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "uRL");;
209             if (log.isDebugEnabled()) {
210                 log.debug("Favorite title is '"
211                         + favorite
212                         + "', URL is '"
213                         + uRL
214                         + "'");
215             }
216         } catch (NoSuchMethodException JavaDoc e) {
217             throw new SystemException(e);
218         } catch (InvocationTargetException JavaDoc e) {
219             throw new SystemException(e);
220         } catch (IllegalAccessException JavaDoc e) {
221             throw new SystemException(e);
222         }
223
224         // do nothing when URL or title empty
225
if (StringHandling.isNullOrEmpty(favorite)
226                 || StringHandling.isNullOrEmpty(uRL) ) {
227             log.warn("Either favorite title ("
228                     + favorite
229                     + ") or URL ("
230                     + uRL
231                     + ") is null --> returning null and not adding favorite");
232             return null;
233         }
234         SecuritySession securitySession =
235             (SecuritySession) session.getAttribute("securitySession");
236
237         // remove the context path - we want relative links
238
uRL = uRL.substring(uRL.lastIndexOf(
239                 RewriteHandling.getContextPath(request)));
240
241
242         // remove the " - sitename" from the title
243
StringBuffer JavaDoc standardEnding = new StringBuffer JavaDoc("- ");
244         standardEnding.append(settings.getStringSetting(securitySession,
245                 "siteTitle", securitySession.getUser()));
246         if (favorite.endsWith(standardEnding.toString())) {
247             favorite = favorite.substring(0, favorite.length()
248                     - standardEnding.length()).trim();
249         }
250
251         // URL rewriting case, the jsessionid part has to be stripped out
252
if (uRL.indexOf("jsessionid")!=-1) {
253             int jsessionStart = uRL.indexOf(";");
254             int jsessionEnd = uRL.lastIndexOf("?");
255             if (jsessionStart < jsessionEnd) {
256                 uRL = uRL.substring(0, jsessionStart) +
257                                               uRL.substring(jsessionEnd);
258             } else {
259                 uRL = uRL.substring(0,jsessionStart);
260             }
261         }
262         // get the favorites menu
263
PersistenceSession persistenceSession = persistenceManager
264             .openSession(securitySession);
265         try {
266             MenuDO favoritesMenu = (MenuDO)persistenceManager.findByPrimaryKey(
267                     persistenceSession,
268                     MenuDO.class,
269                     MenuConstants.ID_FAVORITES
270                     );
271
272             MenuItemDO menuItem = new MenuItemDO();
273
274             menuItem.setMenu(favoritesMenu);
275             menuItem.setUser(securitySession.getUser());
276             menuItem.setImage(null);
277             menuItem.setText(favorite);
278             menuItem.setURL(uRL);
279             // use the inverse count to ensure the new item always appears at
280
// the top!
281
menuItem.setPriority(new Integer JavaDoc(0
282                     - favoritesMenu.getItems().size()));
283             persistenceManager.add(persistenceSession, menuItem);
284         } finally {
285             persistenceSession.close();
286         }
287
288         // this should always go to left.jsp
289
return null;
290     }
291 }
292
Popular Tags