KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > library > struts > CommentAction


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: CommentAction.java,v $
31  * Revision 1.4 2005/04/29 02:48:16 colinmacleod
32  * Data bugfixes.
33  * Changed primary key back to Integer.
34  *
35  * Revision 1.3 2005/04/10 20:31:58 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:46 colinmacleod
42  * Changed copyright text to GPL v2 explicitly.
43  *
44  * Revision 1.1.1.1 2005/03/10 17:52:02 colinmacleod
45  * Restructured ivata op around Hibernate/PicoContainer.
46  * Renamed ivata groupware.
47  *
48  * Revision 1.10 2004/12/31 18:27:43 colinmacleod
49  * Added MaskFactory to constructor of MaskAction.
50  *
51  * Revision 1.9 2004/12/23 21:01:28 colinmacleod
52  * Updated Struts to v1.2.4.
53  * Changed base classes to use ivata masks.
54  *
55  * Revision 1.8 2004/11/12 18:19:15 colinmacleod
56  * Change action and form classes to extend MaskAction, MaskForm respectively.
57  *
58  * Revision 1.7 2004/11/12 15:57:16 colinmacleod
59  * Removed dependencies on SSLEXT.
60  * Moved Persistence classes to ivata masks.
61  *
62  * Revision 1.6 2004/11/03 15:31:51 colinmacleod
63  * Change method interfaces to remove log.
64  *
65  * Revision 1.5 2004/07/13 19:47:29 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.4 2004/03/21 21:16:29 colinmacleod
71  * Shortened name to ivata op.
72  *
73  * Revision 1.3 2004/03/21 20:29:56 colinmacleod
74  * Changed session variable called mailSession to securityServerSession.
75  *
76  * Revision 1.2 2004/02/01 22:07:31 colinmacleod
77  * Added full names to author tags
78  *
79  * Revision 1.1.1.1 2004/01/27 20:58:40 colinmacleod
80  * Moved ivata openportal to SourceForge..
81  *
82  * Revision 1.6 2004/01/12 14:01:03 jano
83  * fixing bugs
84  *
85  * Revision 1.5 2003/12/16 15:08:46 jano
86  * fixing library functionality
87  *
88  * Revision 1.4 2003/12/12 13:23:36 jano
89  * fixing library functionality
90  *
91  * Revision 1.3 2003/10/28 13:16:14 jano
92  * commiting library,
93  * still fixing compile and building openGroupware project
94  *
95  * Revision 1.2 2003/10/15 14:16:53 colin
96  * fixing for XDoclet
97  *
98  * Revision 1.9 2003/07/21 14:16:48 jano
99  * we have new field - list
100  * somethime we are commenting without itemForm in session
101  * Revision 1.8 2003/06/30 07:12:29 peter
102  * fixed flushing of commentTrees - all users want to see the news
103  *
104  * Revision 1.7 2003/06/30 05:23:07 peter
105  * userName at the end of cache key, to match other cache namings
106  *
107  * Revision 1.6 2003/06/30 04:53:10 peter
108  * comment tree is cached for each user separately now
109  *
110  * Revision 1.5 2003/06/09 12:06:13 peter
111  * Implemented comment tree cache flushing on new/amended comment
112  *
113  * Revision 1.4 2003/03/04 17:26:32 colin
114  * fixed bug in defaulting of subject (if parent is null)
115  *
116  * Revision 1.3 2003/02/28 13:44:12 colin
117  * made comments work with Struts & popups
118  *
119  * Revision 1.2 2003/02/28 07:30:22 colin
120  * implemented editing/displaying of faqs & notes
121  *
122  * Revision 1.1 2003/02/24 19:09:24 colin
123  * moved to business
124  * -----------------------------------------------------------------------------
125  */

126 package com.ivata.groupware.business.library.struts;
127
128 import javax.servlet.http.HttpServletRequest JavaDoc;
129 import javax.servlet.http.HttpServletResponse JavaDoc;
130 import javax.servlet.http.HttpSession JavaDoc;
131
132 import org.apache.struts.action.ActionErrors;
133 import org.apache.struts.action.ActionForm;
134 import org.apache.struts.action.ActionMapping;
135
136 import com.ivata.groupware.admin.security.server.SecuritySession;
137 import com.ivata.groupware.admin.setting.Settings;
138 import com.ivata.groupware.admin.setting.SettingsDataTypeException;
139 import com.ivata.groupware.business.library.Library;
140 import com.ivata.groupware.business.library.comment.CommentDO;
141 import com.ivata.groupware.business.library.item.LibraryItemDO;
142 import com.ivata.mask.MaskFactory;
143 import com.ivata.mask.util.StringHandling;
144 import com.ivata.mask.util.SystemException;
145 import com.ivata.mask.web.struts.MaskAction;
146 import com.ivata.mask.web.struts.MaskAuthenticator;
147
148
149 /**
150  * <p>Invoked when the user edits, displays or enters a new library
151  * comment.</p>
152  *
153  * @since 2003-02-18
154  * @author Colin MacLeod
155  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
156  * @version $Revision: 1.4 $
157  *
158  */

159 public class CommentAction extends MaskAction {
160     private Library library;
161     private Settings settings;
162     /**
163      * TODO
164      * @param library
165      * @param settings
166      * @param maskFactory This factory is needed to access the masks and groups
167      * of masks.
168      * @param authenticator used to confirm whether or not the
169      * user should be allowed to continue, in the <code>execute</code> method.
170      */

171     public CommentAction(Library library, Settings settings,
172             MaskFactory maskFactory, MaskAuthenticator authenticator) {
173         super(maskFactory, authenticator);
174         this.library = library;
175         this.settings = settings;
176     }
177
178     /**
179      * <p>Called when the clear button is pressed, or after an ok or
180      * delete button, where the session should be restored to its default
181      * state.</p>
182      *
183      * @param mapping The ActionMapping used to select this instance.
184      * @param log valid logging object to write messages to.
185      * @param errors valid errors object to append errors to. If there are
186      * any errors, the action will return to the input.
187      * @param form optional ActionForm bean for this request (if any)
188      * @param request non-HTTP request we are processing
189      * @param response The non-HTTP response we are creating
190      * @param session returned from the <code>request</code> parameter.
191      * @param userName valid, non-null user name from session.
192      * @param settings valid, non-null settings from session.
193      * @exception SystemException if there is any problem which
194      * prevents processing. It will result in the webapp being forwarded
195      * to
196      * the standard error page.
197      */

198     public void clear(final ActionMapping mapping,
199             final ActionErrors errors,
200             final ActionForm form,
201             final HttpServletRequest JavaDoc request,
202             final HttpServletResponse JavaDoc response,
203             final HttpSession JavaDoc session) throws SystemException {
204         // clear the comment form
205
session.removeAttribute("libraryCommentForm");
206     }
207
208     /**
209      * <p>Overridden to TODO:.</p>
210      *
211      * @param mapping current action mapping from <em>Struts</em> config.
212      * @param log valid logging object to write messages to.
213      * @param errors valid errors object to append errors to. If there are
214      * any errors, the action will return to the input.
215      * @param form optional ActionForm bean for this request (if any)
216      * @param request non-HTTP request we are processing
217      * @param response The non-HTTP response we are creating
218      * @param session returned from the <code>request</code> parameter.
219      * @param userName current user name from session. .
220      * @param settings valid, non-null settings from session.
221      * @exception SystemException if there is any problem which
222      * prevents processing. It will result in the webapp being forwarded
223      * to
224      * the standard error page.
225      * @return this method returns the string used to identify the correct
226      * <em>Struts</em> <code>ActionForward</code> which should follow this
227      * page, or <code>null</code> if it should return to the input.
228      *
229      *
230      */

231     public String JavaDoc execute(final ActionMapping mapping,
232             final ActionErrors errors,
233             final ActionForm form,
234             final HttpServletRequest JavaDoc request,
235             final HttpServletResponse JavaDoc response,
236             final HttpSession JavaDoc session) throws SystemException {
237         CommentForm commentForm = (CommentForm) form;
238
239         ItemForm itemForm = (ItemForm) session.getAttribute("libraryItemForm");
240
241         if (StringHandling.isNullOrEmpty(commentForm.getList())) {
242             commentForm.setList("false");
243         }
244
245         // if there is no item form, just get out
246
// but only if we are not in list of openComments
247
if (itemForm == null && commentForm.getList().equals("false")) {
248             return "utilClosePopUp";
249         }
250
251         LibraryItemDO item = null;
252
253         if (commentForm.getList().equals("false")) {
254             item = itemForm.getItem();
255         }
256
257         Integer JavaDoc requestParentId = StringHandling.integerValue(
258                 request.getParameter("parentId"));
259         Integer JavaDoc requestId = StringHandling.integerValue(
260                 request.getParameter("id"));
261         CommentDO comment = commentForm.getComment();
262         CommentDO parent = null;
263         if (comment != null) {
264             parent = comment.getParent();
265         }
266         SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
267         comment.setCreatedBy(securitySession.getUser());
268
269         // reply to an existing comment
270
if (requestParentId != null) {
271             commentForm.setComment(comment = new CommentDO());
272             comment.setParent(parent = library.findCommentByPrimaryKey(
273                         securitySession, requestParentId));
274
275
276             // edit an existing comment
277
} else if (requestId != null) {
278             commentForm.setComment(comment = library.findCommentByPrimaryKey(
279                     securitySession, requestId));
280         }
281
282         if (comment.getItem() == null) {
283             if (item != null) {
284                 comment.setItem(item);
285             } else {
286                 comment.setItem(parent.getItem());
287             }
288         }
289
290         if (comment.getCreatedBy() == null) {
291             comment.setCreatedBy(securitySession.getUser());
292         }
293
294         commentForm.setBundle("library");
295         commentForm.setDeleteKey("submitComment.alert.delete");
296
297         // set default values here
298
if (StringHandling.isNullOrEmpty(comment.getSubject())) {
299             try {
300                 comment.setSubject(settings.getStringSetting(securitySession,
301                         "emailSubjectReplyPrefix",
302                         securitySession.getUser()) +
303                     (((parent == null) ||
304                     StringHandling.isNullOrEmpty(parent.getSubject()))
305                     ? ((item != null) ? item.getTitle() : comment.getItem().getTitle())
306                     : parent.getSubject()));
307             } catch (SettingsDataTypeException e) {
308                 throw new SystemException(e);
309             }
310         }
311         return null;
312     }
313
314     /**
315      * <p>This method is called if the ok or apply buttons are
316      * pressed.</p>
317      *
318      * @param mapping current action mapping from <em>Struts</em> config.
319      * @param log valid logging object to write messages to.
320      * @param errors valid errors object to append errors to. If there are
321      * any errors, the action will return to the input.
322      * @param form optional ActionForm bean for this request (if any)
323      * @param request non-HTTP request we are processing
324      * @param response The non-HTTP response we are creating
325      * @param session returned from the <code>request</code> parameter.
326      * @param userName valid, non-null user name from session.
327      * @param settings valid, non-null settings from session.
328      * @param ok <code>true</code> if the ok button was pressed, otherwise
329      * <code>false</code> if the apply button was pressed.
330      * @exception SystemException if there is any problem which
331      * prevents processing. It will result in the webapp being forwarded
332      * to
333      * the standard error page.
334      * @return this method returns the string used to identify the correct
335      * <em>Struts</em> <code>ActionForward</code> which should follow this
336      * page, or <code>null</code> if it should return to the input.
337      *
338      */

339     public String JavaDoc onConfirm(final ActionMapping mapping,
340             final ActionErrors errors,
341             final ActionForm form,
342             final HttpServletRequest JavaDoc request,
343             final HttpServletResponse JavaDoc response,
344             final HttpSession JavaDoc session,
345             final String JavaDoc defaultForward) throws SystemException {
346
347         // new comment ?
348
CommentDO comment = ((CommentForm) form).getComment();
349         CommentDO parent = comment.getParent();
350         SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
351         StringBuffer JavaDoc openerPage = new StringBuffer JavaDoc();
352
353         if (((CommentForm) form).getList().equals("true")) {
354             openerPage.append("/library/openComments.jsp");
355         } else {
356             openerPage.append("/library/display.action?id=");
357             openerPage.append(comment.getItem().getId());
358         }
359
360         if (comment.getId() == null) {
361             comment.setCreatedBy(securitySession.getUser());
362
363             Integer JavaDoc parentId = (parent == null) ? null : parent.getId();
364
365             comment = library.addComment(securitySession, comment);
366         } else {
367             // amend an existing comment
368
library.amendComment(securitySession, comment);
369         }
370
371         // flush the comment tree for this item from jsp cache
372
//flushCache("itemCommentTree_" + comment.getItemId().toString(),PageContext.APPLICATION_SCOPE, request);
373

374         // if it was 'ok' we won't know the id till now
375
if (!((CommentForm) form).getList().equals("true")) {
376             openerPage.append("#comment");
377             openerPage.append(comment.getId());
378         }
379
380         request.setAttribute("openerPage", openerPage.toString());
381
382         return defaultForward;
383     }
384
385     /**
386      * <p>This method is called if the delete (confirm, not warn) button
387      * is pressed.</p>
388      * @param mapping current action mapping from <em>Struts</em> config.
389      * @param errors valid errors object to append errors to. If there are
390      * any errors, the action will return to the input.
391      * @param form optional ActionForm bean for this request (if any)
392      * @param request non-HTTP request we are processing
393      * @param response The non-HTTP response we are creating
394      * @param session returned from the <code>request</code> parameter.
395      * @param log valid logging object to write messages to.
396      * @param userName valid, non-null user name from session.
397      * @param settings valid, non-null settings from session.
398      *
399      * @exception SystemException if there is any problem which
400      * prevents processing. It will result in the webapp being forwarded
401      * to
402      * the standard error page.
403      * @return this method returns the string used to identify the correct
404      * <em>Struts</em> <code>ActionForward</code> which should follow this
405      * page, or <code>null</code> if it should return to the input.
406      *
407      */

408     public String JavaDoc onDelete(final ActionMapping mapping,
409             final ActionErrors errors,
410             final ActionForm form,
411             final HttpServletRequest JavaDoc request,
412             final HttpServletResponse JavaDoc response,
413             final HttpSession JavaDoc session, final String JavaDoc defaultForward) throws SystemException {
414         return null;
415     }
416 }
417
Popular Tags