KickJava   Java API By Example, From Geeks To Geeks.

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


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: DisplayAction.java,v $
31  * Revision 1.8 2005/04/30 13:04:13 colinmacleod
32  * Fixes reverting id type from String to Integer.
33  *
34  * Revision 1.7 2005/04/29 02:48:16 colinmacleod
35  * Data bugfixes.
36  * Changed primary key back to Integer.
37  *
38  * Revision 1.6 2005/04/28 18:47:07 colinmacleod
39  * Fixed XHMTL, styles and resin compatibility.
40  * Added support for URL rewriting.
41  *
42  * Revision 1.5 2005/04/26 15:07:07 colinmacleod
43  * Consolidated print JSP into display pages (eliminating need for separate print pages).
44  * Renamed Faq to FAQ.
45  *
46  * Revision 1.4 2005/04/22 09:53:33 colinmacleod
47  * Removed table surrounding the page
48  * links.
49  *
50  * Revision 1.3 2005/04/10 20:31:58 colinmacleod
51  * Added new themes.
52  * Changed id type to String.
53  * Changed i tag to em and b tag to strong.
54  * Improved PicoContainerFactory with NanoContainer scripts.
55  *
56  * Revision 1.2 2005/04/09 17:19:46 colinmacleod
57  * Changed copyright text to GPL v2 explicitly.
58  *
59  * Revision 1.1.1.1 2005/03/10 17:52:01 colinmacleod
60  * Restructured ivata op around Hibernate/PicoContainer.
61  * Renamed ivata groupware.
62  *
63  * Revision 1.10 2004/12/31 18:27:44 colinmacleod
64  * Added MaskFactory to constructor of MaskAction.
65  *
66  * Revision 1.9 2004/12/23 21:01:29 colinmacleod
67  * Updated Struts to v1.2.4.
68  * Changed base classes to use ivata masks.
69  *
70  * Revision 1.8 2004/11/12 18:19:15 colinmacleod
71  * Change action and form classes to extend MaskAction, MaskForm respectively.
72  *
73  * Revision 1.7 2004/11/12 15:57:16 colinmacleod
74  * Removed dependencies on SSLEXT.
75  * Moved Persistence classes to ivata masks.
76  *
77  * Revision 1.6 2004/11/03 15:31:51 colinmacleod
78  * Change method interfaces to remove log.
79  *
80  * Revision 1.5 2004/08/01 11:45:19 colinmacleod
81  * Restructured search engine into separate subproject.
82  *
83  * Revision 1.4 2004/07/13 19:47:29 colinmacleod
84  * Moved project to POJOs from EJBs.
85  * Applied PicoContainer to services layer (replacing session EJBs).
86  * Applied Hibernate to persistence layer (replacing entity EJBs).
87  *
88  * Revision 1.3 2004/03/21 21:16:29 colinmacleod
89  * Shortened name to ivata op.
90  *
91  * Revision 1.2 2004/02/01 22:07:31 colinmacleod
92  * Added full names to author tags
93  *
94  * Revision 1.1.1.1 2004/01/27 20:58:41 colinmacleod
95  * Moved ivata openportal to SourceForge..
96  *
97  * Revision 1.5 2004/01/20 15:12:13 jano
98  * fixing problems with new sslext
99  *
100  * Revision 1.4 2004/01/19 21:13:14 colin
101  * Removed minutes for ivata groupware v0.9
102  *
103  * Revision 1.3 2003/10/28 13:16:14 jano
104  * commiting library,
105  * still fixing compile and building openGroupware project
106  *
107  * Revision 1.2 2003/10/15 14:16:53 colin
108  * fixing for XDoclet
109  *
110  * Revision 1.14 2003/09/05 07:19:42 peter
111  * changes in drive methods
112  * Revision 1.13 2003/08/20 08:35:08 peter
113  * directory issues fixed
114  *
115  * Revision 1.12 2003/08/18 11:38:55 peter
116  * findByPath replaced by findByParentIdName
117  *
118  * Revision 1.11 2003/07/08 13:24:25 jano
119  * we have library items in CVS
120  *
121  * Revision 1.10 2003/06/30 13:07:26 jano
122  * we want new link in LinksOfpage
123  *
124  * Revision 1.9 2003/06/20 13:22:15 jano
125  * we want deleteFile button in list of attached files
126  *
127  * Revision 1.8 2003/06/03 05:08:00 peter
128  * changes due to posibility to change filelist from display mode
129  *
130  * Revision 1.7 2003/05/20 08:29:40 jano
131  * maintaing attaching files to libray item
132  *
133  * Revision 1.6 2003/05/07 14:01:59 jano
134  * we want findFilesByPath
135  *
136  * Revision 1.5 2003/03/27 15:40:46 jano
137  * read pageInt from form befor seting to lastPage or 0
138  *
139  * Revision 1.4 2003/03/04 16:01:52 colin
140  * fixed page for print.jsp
141  *
142  * Revision 1.3 2003/02/28 10:52:22 colin
143  * when edit is pressed, now forwards to SubmitAction (was submit.jsp)
144  *
145  * Revision 1.2 2003/02/28 07:30:22 colin
146  * implemented editing/displaying of faqs & notes
147  *
148  * Revision 1.1 2003/02/24 19:09:24 colin
149  * moved to business
150  * -----------------------------------------------------------------------------
151  */

152 package com.ivata.groupware.business.library.struts;
153
154 import java.net.MalformedURLException JavaDoc;
155 import java.util.HashMap JavaDoc;
156 import java.util.Iterator JavaDoc;
157 import java.util.Locale JavaDoc;
158
159 import javax.servlet.http.HttpServletRequest JavaDoc;
160 import javax.servlet.http.HttpServletResponse JavaDoc;
161 import javax.servlet.http.HttpSession JavaDoc;
162 import javax.servlet.jsp.JspFactory JavaDoc;
163 import javax.servlet.jsp.PageContext JavaDoc;
164
165 import org.apache.struts.Globals;
166 import org.apache.struts.action.ActionErrors;
167 import org.apache.struts.action.ActionForm;
168 import org.apache.struts.action.ActionMapping;
169 import org.apache.struts.taglib.TagUtils;
170 import org.apache.struts.util.MessageResources;
171
172 import com.ivata.groupware.admin.security.server.SecuritySession;
173 import com.ivata.groupware.admin.setting.Settings;
174 import com.ivata.groupware.business.library.Library;
175 import com.ivata.groupware.business.library.faq.category.FAQCategoryDO;
176 import com.ivata.groupware.business.library.item.LibraryItemConstants;
177 import com.ivata.groupware.business.library.item.LibraryItemDO;
178 import com.ivata.mask.MaskFactory;
179 import com.ivata.mask.util.StringHandling;
180 import com.ivata.mask.util.SystemException;
181 import com.ivata.mask.web.RewriteHandling;
182 import com.ivata.mask.web.struts.MaskAction;
183 import com.ivata.mask.web.struts.MaskAuthenticator;
184
185
186 /**
187  * <p><code>Action</code> invoked whenever a library item is
188  * displayed.</p>
189  *
190  * @since 2003-02-18
191  * @author Colin MacLeod
192  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
193  * @version $Revision: 1.8 $
194  */

195 public class DisplayAction extends MaskAction {
196     private Library library;
197     private Settings settings;
198
199     /**
200      * TODO
201      * @param library
202      * @param settings
203      * @param maskFactory This factory is needed to access the masks and groups
204      * of masks.
205      * @param authenticator used to confirm whether or not the
206      * user should be allowed to continue, in the <code>execute</code> method.
207      */

208     public DisplayAction(Library library, Settings settings,
209             MaskFactory maskFactory, MaskAuthenticator authenticator) {
210         super(maskFactory, authenticator);
211         this.library = library;
212         this.settings = settings;
213     }
214
215     /**
216      * <p>Set information to display the item type correcly.</p>
217      *
218      * @param itemForm the fom which should be set up fo the given
219      * type.
220      */

221     public void chooseItemType(final ItemForm itemForm) {
222         LibraryItemDO item = itemForm.getItem();
223
224         // find out which page we're on (starting with 0)
225
itemForm.setDeleteKey("display.alert.delete");
226         Integer JavaDoc itemType = item.getType();
227         assert (itemType != null);
228
229         if (itemType.equals(LibraryItemConstants.ITEM_MEETING)) {
230             itemForm.setThemeName("meeting");
231             itemForm.setSummaryThemeName("meetingSummary");
232             itemForm.setDeleteKey("display.alert.delete.isMeeting");
233             itemForm.setDisplayIncludePage("/library/displayMeeting.jsp");
234         } else if (itemType.equals(LibraryItemConstants.ITEM_NOTE)) {
235             itemForm.setThemeName("note");
236             itemForm.setSummaryThemeName("note");
237             itemForm.setDisplayIncludePage(null);
238             itemForm.setDisplayIncludePage(null);
239         } else if (itemType.equals(LibraryItemConstants.ITEM_FAQ)) {
240             itemForm.setDisplayIncludePage("/library/displayFAQ.jsp");
241             itemForm.setThemeName("fAQ");
242             itemForm.setSummaryThemeName("fAQ");
243             itemForm.setDisplayIncludePage("/library/displayFAQ.jsp");
244         } else {
245             itemForm.setDisplayIncludePage("/library/displayMeeting.jsp");
246             itemForm.setThemeName("document");
247             itemForm.setSummaryThemeName("documentSummary");
248             itemForm.setDisplayIncludePage("/library/displayDocument.jsp");
249         }
250     }
251     /**
252      * <p>Overridden to proved flow for <code>onEdit</code> and
253      * <code>onPreview</code>.</p>
254      *
255      * @param mapping current action mapping from <em>Struts</em> config.
256      * @param log valid logging object to write messages to.
257      * @param errors valid errors object to append errors to. If there are
258      * any errors, the action will return to the input.
259      * @param form optional ActionForm bean for this request (if any)
260      * @param request non-HTTP request we are processing
261      * @param response The non-HTTP response we are creating
262      * @param session returned from the <code>request</code> parameter.
263      * @param userName current user name from session. .
264      * @param settings valid, non-null settings from session.
265      * @exception SystemException if there is any problem which
266      * prevents processing. It will result in the webapp being forwarded
267      * to
268      * the standard error page.
269      * @return this method returns the string used to identify the correct
270      * <em>Struts</em> <code>ActionForward</code> which should follow this
271      * page, or <code>null</code> if it should return to the input.
272      *
273      *
274      */

275     public String JavaDoc execute(final ActionMapping mapping,
276             final ActionErrors errors,
277             final ActionForm form,
278             final HttpServletRequest JavaDoc request,
279             final HttpServletResponse JavaDoc response,
280             final HttpSession JavaDoc session) throws SystemException {
281         ItemForm itemForm = (ItemForm) form;
282         LibraryItemDO item = itemForm.getItem();
283         Integer JavaDoc requestId = StringHandling.integerValue(request.getParameter(
284                     "id"));
285         Integer JavaDoc requestMeetingId = StringHandling.integerValue(request.getParameter(
286                     "meetingId"));
287         MessageResources resources = getResources(request);
288         MessageResources libraryResources = getResources(request, "library");
289         Locale JavaDoc locale = (Locale JavaDoc) session.getAttribute(Globals.LOCALE_KEY);
290
291         // used in computeURL
292
JspFactory JavaDoc factory = JspFactory.getDefaultFactory();
293         PageContext JavaDoc pageContext = factory.getPageContext(getServlet(), request,
294                 response, "", true, 512, true);
295
296         // if there is an id in the request, that means find a new item
297
SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
298         if (requestId != null) {
299             itemForm.setItem(item = library.findItemByPrimaryKey(securitySession,
300                     requestId));
301             itemForm.setPreview(null);
302             itemForm.setEdit(null);
303
304             // otherwise, if there is a meeting id in the request, find the item
305
// by the meeting id
306
/* } else if (requestMeetingId != null) {
307             itemForm.setItem(item = library.findItemByMeetingId(
308                         requestMeetingId));
309             itemForm.setPreview(null);
310             itemForm.setEdit(null);
311 */

312         }
313
314 /*TODO
315             DirectoryDO attachmentDirectoryDO = drive.findDirectoryByParentIdName(DirectoryConstants.LIBRARY_DIRECTORY,
316                     itemForm.getItem().getId().toString(), userName);
317
318             // the list of already attached files
319             itemForm.setFileList((java.util.Vector) drive.findFilesByPath(
320                     "/library/" + item.getId().toString(), userName));
321
322             //list of new uploads
323             Vector uploadList = (java.util.Vector) drive.getUploads("drive",
324                     userName);
325
326             itemForm.setUploadingFileList(uploadList);
327
328             // is user selected any file to remove and he did click on deleteFile button
329             if (!StringHandling.isNullOrEmpty(itemForm.getDeleteFileButton())) {
330                 //remove attached files
331                 for (Iterator i = itemForm.getFileList().iterator();
332                         i.hasNext();) {
333                     DriveFileDO tmpFile = (DriveFileDO) i.next();
334
335                     if (itemForm.getSelectedAttachedFilesIds(
336                                 tmpFile.getId().toString()) != null) {
337                         drive.removeFile(tmpFile.getId(), userName);
338                     }
339                 }
340
341                 itemForm.setDeleteFileButton("");
342             }
343
344             // attach is a flag set by javaScript, when something attachment
345             // related happened
346             if (!StringHandling.isNullOrEmpty(request.getParameter("attach"))) {
347                 // if there are some uploads, commit them
348                 if (!uploadList.isEmpty()) {
349                     Vector uploadedFileNames = new Vector();
350
351                     for (Iterator i = uploadList.iterator(); i.hasNext();) {
352                         FileDO fileDO = (FileDO) i.next();
353                         DriveFileDO driveFileDO = new DriveFileDO();
354
355                         driveFileDO.setFileName(fileDO.getFileName());
356                         driveFileDO.setDirectoryId(attachmentDirectoryDO.getId());
357                         driveFileDO.setHeadRevision(new FileRevisionDO());
358                         driveFileDO.getHeadRevision().setComment(fileDO.getComment());
359                         driveFileDO.setCreatedBy(userName);
360                         driveFileDO.setMimeType(fileDO.getMimeType());
361                         driveFileDO.setSize(fileDO.getSize());
362                         uploadedFileNames.add(fileDO.getFileName());
363
364                         try {
365                             drive.commitFile(driveFileDO, userName, null);
366                         } catch (RemoteException e) {
367                             throw new SystemException(e);
368                         }
369                     }
370
371                     // remove uploaded files
372                     try {
373                         itemForm.setUploadingFileList(new Vector(
374                                 drive.removeUploads(uploadedFileNames, "drive",
375                                     userName)));
376                     } catch (RemoteException e) {
377                         throw new SystemException(e);
378                     }
379                 }
380
381                 // no wonder that we request back the current attachment list....
382                 itemForm.setFileList((java.util.Vector) drive.findFilesByPath(
383                         "/library/" + item.getId().toString(), userName));
384             }
385 */

386
387         // if edit was pressed, forward to the submit
388
if (!StringHandling.isNullOrEmpty(itemForm.getEdit())) {
389             return "librarySubmitAction";
390         }
391
392         chooseItemType(itemForm);
393         setPageNumber("/library/display.action", request, response, itemForm);
394
395         // add a printer to the page links, if we're not in submit
396
HashMap JavaDoc printParameters = new HashMap JavaDoc();
397         TagUtils tagUtils = TagUtils.getInstance();
398
399         printParameters.put("id", item.getId().toString());
400         printParameters.put("print", "true");
401
402         String JavaDoc pageLink1 = "";
403         String JavaDoc pageLink2 = "";
404
405         try {
406             pageLink1 = tagUtils.computeURL(pageContext, null, null,
407                     "/library/display.action", null, null, printParameters,
408                     null, true);
409 // TODO pageLink2 = RequestUtils.computeURL(pageContext, null, null,
410
// "/library/downLoad.jsp", null, printParameters, null, true);
411
} catch (MalformedURLException JavaDoc e) {
412             throw new SystemException(e);
413         }
414
415         StringBuffer JavaDoc newLinks = new StringBuffer JavaDoc();
416         if (request.getParameter("print") == null) {
417             newLinks.append(itemForm.getPageLinks());
418             newLinks.append("<a HREF='");
419             newLinks.append(pageLink1);
420             newLinks.append("' target='_blank'><img class='printer' SRC='");
421             newLinks.append(RewriteHandling.getContextPath(request));
422             newLinks.append("/library/images/printer.gif' border='0' alt='");
423             newLinks.append(libraryResources.getMessage(locale,
424                     "displayItem.label.print"));
425             newLinks.append("' title='");
426             newLinks.append(libraryResources.getMessage(locale,
427                     "displayItem.label.print"));
428             newLinks.append("' width='32' height='32'/></a>");
429 /* TODO
430         newLinks.append(
431             "<td><nobr><img SRC='/images/empty.gif' width='20' height='1'/><a HREF='");
432         newLinks.append(pageLink2);
433         newLinks.append("' target='_blank'><img SRC='");
434         newLinks.append(RewriteHandling.getContextPath(request));
435         newLinks.append("/library/images/downloadHTML.gif' border='0' alt='");
436         newLinks.append(libraryResources.getMessage(locale,
437                 "displayItem.label.download"));
438         newLinks.append("' title='");
439         newLinks.append(libraryResources.getMessage(locale,
440                 "displayItem.label.download"));
441         newLinks.append(
442             "' width='32' height='32'/></a></nobr></td>");
443 */

444         }
445         itemForm.setPageLinks(newLinks.toString());
446
447         return null;
448     }
449
450     /**
451      * <p>This method is called if the delete (confirm, not warn) button
452      * is pressed.</p>
453      * @param mapping current action mapping from <em>Struts</em> config.
454      * @param errors valid errors object to append errors to. If there are
455      * any errors, the action will return to the input.
456      * @param form optional ActionForm bean for this request (if any)
457      * @param request non-HTTP request we are processing
458      * @param response The non-HTTP response we are creating
459      * @param session returned from the <code>request</code> parameter.
460      * @param log valid logging object to write messages to.
461      * @param userName valid, non-null user name from session.
462      * @param settings valid, non-null settings from session.
463      *
464      * @exception SystemException if there is any problem which
465      * prevents processing. It will result in the webapp being forwarded
466      * to
467      * the standard error page.
468      * @return this method returns the string used to identify the correct
469      * <em>Struts</em> <code>ActionForward</code> which should follow this
470      * page, or <code>null</code> if it should return to the input.
471      *
472      */

473     public String JavaDoc onDelete(final ActionMapping mapping,
474             final ActionErrors errors,
475             final ActionForm form,
476             final HttpServletRequest JavaDoc request,
477             final HttpServletResponse JavaDoc response,
478             final HttpSession JavaDoc session, final String JavaDoc defaultForward) throws SystemException {
479         LibraryItemDO item = ((ItemForm) form).getItem();
480         SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
481
482         // remove library item
483
library.removeItem(securitySession, item);
484 /*TODO
485         // remove files
486         for (Iterator i = ((ItemForm) form).getFileList().iterator();
487                 i.hasNext();) {
488             drive.removeFile(((DriveFileDO) i.next()).getId(), userName);
489         }
490 */

491
492         return "libraryIndex";
493     }
494
495     /**
496      * <p>Implementation of
497      * <code>setPageNumber</code> for document types.</p>
498      *
499      * @pageContext used to create links.
500      * @param linkPage the page to link the page numbers to.
501      * @param request current request to check for a 'page' parameter
502      * and to create links.
503      * @param itemForm form to set page number <code>displayPage</code>.
504      * @throws SystemException if there is any exception creating
505      * the <code>URL</code>s.
506      * @see setPageNumber
507      */

508     protected void setDocumentPageNumber(final PageContext JavaDoc pageContext,
509             final String JavaDoc linkPage,
510             final HttpServletRequest JavaDoc request,
511             final ItemForm itemForm)
512         throws SystemException {
513         int pageInt = itemForm.getDisplayPage();
514         LibraryItemDO item = itemForm.getItem();
515         MessageResources libraryResources = getResources(request, "library");
516         Locale JavaDoc locale = (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY);
517         StringBuffer JavaDoc links = new StringBuffer JavaDoc();
518
519         if (item.getPages().size() > 1) {
520             HashMap JavaDoc pageLinkParameters = new HashMap JavaDoc();
521             int numberOfPages = item.getPages().size();
522
523             for (int i = 0; i < numberOfPages; ++i) {
524                 // don't link to the current page; just show it
525
if (i == pageInt) {
526                     links.append("&nbsp;");
527                     links.append(i + 1);
528                 } else {
529                     pageLinkParameters.put("page", new Integer JavaDoc(i + 1).toString());
530
531                     String JavaDoc pageLink;
532
533                     try {
534                         pageLink = TagUtils.getInstance().computeURL(pageContext, null,
535                                 null, linkPage, null, null, pageLinkParameters, null, true);
536                     } catch (MalformedURLException JavaDoc e) {
537                         throw new SystemException(e);
538                     }
539
540                     links.append("&nbsp;<a HREF='");
541                     links.append(pageLink);
542                     links.append("'>");
543                     links.append(i + 1);
544                     links.append("</a>");
545                 }
546             }
547         }
548
549         itemForm.setPageLinks(links.toString());
550     }
551
552     /**
553      * <p>Implementation of
554      * <code>setPageNumber</code> for FAQ types.</p>
555      *
556      * @pageContext used to create links.
557      * @param linkPage the page to link the page numbers to.
558      * @param request current request to check for a 'page' parameter
559      * and to create links.
560      * @param itemForm form to set page number <code>displayPage</code>.
561      * @throws SystemException if there is any exception creating
562      * the <code>URL</code>s.
563      * @see setPageNumber
564      */

565     protected void setFaqPageNumber(final PageContext JavaDoc pageContext,
566             final String JavaDoc linkPage,
567             final HttpServletRequest JavaDoc request,
568             final ItemForm itemForm)
569         throws SystemException {
570         int pageInt = itemForm.getDisplayPage();
571         LibraryItemDO item = itemForm.getItem();
572         MessageResources libraryResources = getResources(request, "library");
573         Locale JavaDoc locale = (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY);
574
575         // always show the contents
576
// in this case, don't show a link as the contents is already displayed
577
String JavaDoc link;
578         HashMap JavaDoc linkParameters = new HashMap JavaDoc();
579         StringBuffer JavaDoc links = new StringBuffer JavaDoc();
580
581         // first put out the agenda
582
if (pageInt == 0) {
583             links = new StringBuffer JavaDoc(libraryResources.getMessage(locale,
584                         "displayItem.label.contents"));
585         } else {
586             linkParameters.put("page", "0");
587
588             try {
589                 link = TagUtils.getInstance().computeURL(pageContext, null, null,
590                         linkPage, null, null, linkParameters, null, true);
591             } catch (MalformedURLException JavaDoc e) {
592                 throw new SystemException(e);
593             }
594
595             links = new StringBuffer JavaDoc("<a HREF='");
596             links.append(link);
597             links.append("'>");
598             links.append(libraryResources.getMessage(locale,
599                     "displayItem.label.contents"));
600             links.append("</a>");
601         }
602
603         // now show each category numbered from 1
604
int categoryLinkNumber = 0;
605
606         for (Iterator JavaDoc i = item.getFAQCategories().iterator(); i.hasNext();) {
607             FAQCategoryDO category = (FAQCategoryDO) i.next();
608
609             // don't link to the current page; just show it
610
if (++categoryLinkNumber == itemForm.getDisplayPage()) {
611                 links.append("&nbsp;");
612                 links.append(categoryLinkNumber);
613             } else {
614                 linkParameters.put("page",
615                     new Integer JavaDoc(categoryLinkNumber).toString());
616
617                 try {
618                     link = TagUtils.getInstance().computeURL(pageContext, null, null,
619                             linkPage, null, null, linkParameters, null, true);
620                 } catch (MalformedURLException JavaDoc e) {
621                     throw new SystemException(e);
622                 }
623
624                 links.append("&nbsp;<a HREF='");
625                 links.append(link);
626                 links.append("' title='");
627                 links.append(category.getName());
628                 links.append("'>");
629                 links.append(categoryLinkNumber);
630                 links.append("</a>");
631             }
632         }
633
634         itemForm.setPageLinks(links.toString());
635     }
636
637     /**
638      * <p>Implementation of
639      * <code>setPageNumber</code> for meeting types.</p>
640      *
641      * @pageContext used to create links.
642      * @param linkPage the page to link the page numbers to.
643      * @param request current request to check for a 'page' parameter
644      * and to create links.
645      * @param itemForm form to set page number <code>displayPage</code>.
646      * @throws SystemException if there is any exception creating
647      * the <code>URL</code>s.
648      * @see setPageNumber
649      */

650     protected void setMeetingPageNumber(final PageContext JavaDoc pageContext,
651             final String JavaDoc linkPage,
652             final HttpServletRequest JavaDoc request,
653             final ItemForm itemForm)
654         throws SystemException {
655         int pageInt = itemForm.getDisplayPage();
656         LibraryItemDO item = itemForm.getItem();
657
658         MessageResources libraryResources = getResources(request, "library");
659         Locale JavaDoc locale = (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY);
660
661         // always show the agenda
662
// in this case, don't show a link as the agenda is already displayed
663
String JavaDoc agendaLink;
664         HashMap JavaDoc agendaLinkParameters = new HashMap JavaDoc();
665         StringBuffer JavaDoc links = new StringBuffer JavaDoc();
666
667         // first put out the agenda
668
if (pageInt == 0) {
669             links = new StringBuffer JavaDoc(libraryResources.getMessage(locale,
670                         "displayItem.label.agenda"));
671         } else {
672             agendaLinkParameters.put("page", "0");
673
674             try {
675                 agendaLink = TagUtils.getInstance().computeURL(pageContext, null,
676                         null, linkPage, null, null, agendaLinkParameters, null,
677                         true);
678             } catch (MalformedURLException JavaDoc e) {
679                 throw new SystemException(e);
680             }
681
682             links = new StringBuffer JavaDoc("<a HREF='");
683             links.append(agendaLink);
684             links.append("'>");
685             links.append(libraryResources.getMessage(locale,
686                     "displayItem.label.agenda"));
687             links.append("</a>");
688         }
689
690         // now show each category numbered from 1
691
int categoryLinkNumber = 0;
692
693         /**
694          * TODO: we should subclass/fire events to get around this
695         MeetingDO meeting = item.getMeeting();
696         for (Iterator i = meeting.getCategories().iterator(); i.hasNext();) {
697             String category = (String) i.next();
698
699             // don't link to the current page; just show it
700             if (++categoryLinkNumber == itemForm.getDisplayPage()) {
701                 links.append("&nbsp;");
702                 links.append(categoryLinkNumber);
703             } else {
704                 agendaLinkParameters.put("page",
705                     new Integer(categoryLinkNumber).toString());
706
707                 try {
708                     agendaLink = RequestUtils.computeURL(pageContext, null,
709                             null, linkPage, agendaLinkParameters, null, false,
710                             true);
711                 } catch (MalformedURLException e) {
712                     throw new SystemException(e);
713                 }
714
715                 links.append("&nbsp;<a HREF='");
716                 links.append(agendaLink);
717                 links.append("' title='");
718                 links.append(category);
719                 links.append("'>");
720                 links.append(categoryLinkNumber);
721                 links.append("</a>");
722             }
723         }
724          */

725
726         itemForm.setPageLinks(links.toString());
727     }
728
729     /**
730      * <p>Set the current page number from the request or form to the
731      * <code>displayPage</code> attribute on the form.</p>
732      *
733      * <p>This method also sets the appropriate page links.</p>
734      *
735      * @param linkPage the page to link the page numbers to.
736      * @param request current request to check for a 'page' parameter
737      * and to create links.
738      * @param response used to create links.
739      * @param itemForm form to set page number <code>displayPage</code>.
740      * @throws SystemException if there is any exception creating
741      * the <code>URL</code>s.
742      */

743     public final void setPageNumber(final String JavaDoc linkPage,
744             final HttpServletRequest JavaDoc request,
745             final HttpServletResponse JavaDoc response,
746             final ItemForm itemForm)
747         throws SystemException {
748         LibraryItemDO item = itemForm.getItem();
749         JspFactory JavaDoc factory = JspFactory.getDefaultFactory();
750         PageContext JavaDoc pageContext = factory.getPageContext(getServlet(), request,
751                 response, "", true, 512, true);
752
753         itemForm.setLinkPage(linkPage);
754
755         // offset is the difference between the counting routine and the request
756
// parameter - we want page 1 to be 1, not 0 for documents
757
int lastPage;
758
759         // offset is the difference between the counting routine and the request
760
// parameter - we want page 1 to be 1, not 0 for documents
761
int offset;
762
763         // find last page for meeeting
764
Integer JavaDoc itemType = item.getType();
765         assert (itemType != null);
766         /**
767          * TODO: fix this via subclassing/fire events
768         if (itemType.equals(LibraryItemConstants.ITEM_MEETING)) {
769             lastPage = item.getMeeting().getCategories().size();
770             offset = 0;
771
772             // calculate last page for fequently asked questions
773         } else */

774          if (itemType.equals(LibraryItemConstants.ITEM_FAQ)) {
775             lastPage = item.getFAQCategories().size();
776             offset = 0;
777
778             // calculate last page for normal pages
779
} else {
780             lastPage = item.getPages().size() - 1;
781             offset = 1;
782         }
783
784         int pageInt = -1;
785
786         if (request.getParameter("page") != null) {
787             try {
788                 pageInt = new Integer JavaDoc(request.getParameter("page")).intValue() -
789                     offset;
790             } catch (NumberFormatException JavaDoc e) { // ignore all number conversion exceptions: the page will default to the first one
791
}
792         } else {
793             pageInt = itemForm.getDisplayPage();
794         }
795
796         if (pageInt > lastPage) {
797             pageInt = lastPage;
798         }
799
800         // page indices can't normally be < 0
801
if (pageInt < 0) {
802             pageInt = 0;
803         }
804
805         itemForm.setDisplayPage(pageInt);
806
807         // calculate links for meetings
808
if (itemType.equals(LibraryItemConstants.ITEM_MEETING)) {
809             setMeetingPageNumber(pageContext, linkPage, request, itemForm);
810
811             // calculate links for fequently asked questions
812
} else if (itemType.equals(LibraryItemConstants.ITEM_FAQ)) {
813             setFaqPageNumber(pageContext, linkPage, request, itemForm);
814
815             // calculate links for normal pages
816
} else {
817             setDocumentPageNumber(pageContext, linkPage, request, itemForm);
818         }
819
820         // now the links for the next and previous page
821
HashMap JavaDoc displayDocumentParameters = new HashMap JavaDoc();
822         StringBuffer JavaDoc previousPageLink = new StringBuffer JavaDoc();
823         StringBuffer JavaDoc nextPageLink = new StringBuffer JavaDoc();
824         MessageResources libraryResources = getResources(request, "library");
825         Locale JavaDoc locale = (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY);
826
827         // no previous page for the first page!
828
if (pageInt > 0) {
829             // 'human-readable' numbers start with 1
830
displayDocumentParameters.put("page",
831                 new Integer JavaDoc((pageInt + offset) - 1).toString());
832             previousPageLink.append("<a HREF='");
833
834             try {
835                 previousPageLink.append(TagUtils.getInstance().computeURL(pageContext,
836                         null, null, linkPage, null, null,
837                         displayDocumentParameters, null, true));
838             } catch (MalformedURLException JavaDoc e) {
839                 throw new SystemException(e);
840             }
841
842             previousPageLink.append("'>");
843             previousPageLink.append(libraryResources.getMessage(locale,
844                     "displayDocument.link.previousPage"));
845             previousPageLink.append("</a>");
846         }
847
848         itemForm.setPreviousPageLink(previousPageLink.toString());
849
850         // no next page for the last page!
851
if (pageInt < lastPage) {
852             displayDocumentParameters.put("page",
853                 new Integer JavaDoc(pageInt + offset + 1).toString());
854             nextPageLink.append("<a HREF='");
855
856             try {
857                 nextPageLink.append(TagUtils.getInstance().computeURL(
858                         pageContext, null, null, linkPage, null,
859                         null, displayDocumentParameters, null, true));
860             } catch (MalformedURLException JavaDoc e) {
861                 throw new SystemException(e);
862             }
863
864             nextPageLink.append("'>");
865             nextPageLink.append(libraryResources.getMessage(locale,
866                     "displayDocument.link.nextPage"));
867             nextPageLink.append("</a>");
868         }
869
870         itemForm.setNextPageLink(nextPageLink.toString());
871     }
872 }
873
Popular Tags