KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > forum > common > ViewCommon


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: 02/04/2004 - 20:31:35
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum.common;
44
45 import java.text.SimpleDateFormat JavaDoc;
46 import java.util.Date JavaDoc;
47 import java.util.Map JavaDoc;
48
49 import net.jforum.Command;
50 import net.jforum.JForumExecutionContext;
51 import net.jforum.SessionFacade;
52 import net.jforum.entities.User;
53 import net.jforum.exceptions.RequestEmptyException;
54 import net.jforum.repository.ModulesRepository;
55 import net.jforum.repository.SmiliesRepository;
56 import net.jforum.util.preferences.ConfigKeys;
57 import net.jforum.util.preferences.SystemGlobals;
58 import net.jforum.util.preferences.TemplateKeys;
59 import freemarker.template.SimpleHash;
60
61 /**
62  * @author Rafael Steil
63  * @version $Id: ViewCommon.java,v 1.17 2006/01/29 15:07:12 rafaelsteil Exp $
64  */

65 public final class ViewCommon
66 {
67     /**
68      * Prepared the user context to use data pagination.
69      * The following variables are set to the context:
70      * <p>
71      * <ul>
72      * <li> <i>totalPages</i> - total number of pages
73      * <li> <i>recordsPerPage</i> - how many records will be shown on each page
74      * <li> <i>totalRecords</i> - number of records fount
75      * <li> <i>thisPage</i> - the current page being shown
76      * <li> <i>start</i> -
77      * </ul>
78      * </p>
79      * @param start
80      * @param totalRecords
81      * @param recordsPerPage
82      */

83     public static void contextToPagination(int start, int totalRecords, int recordsPerPage)
84     {
85         SimpleHash context = JForumExecutionContext.getTemplateContext();
86         
87         context.put("totalPages", new Double JavaDoc(Math.ceil((double) totalRecords / (double) recordsPerPage)));
88         context.put("recordsPerPage", new Integer JavaDoc(recordsPerPage));
89         context.put("totalRecords", new Integer JavaDoc(totalRecords));
90         context.put("thisPage", new Double JavaDoc(Math.ceil((double) (start + 1) / (double) recordsPerPage)));
91         context.put("start", new Integer JavaDoc(start));
92     }
93     
94     public static String JavaDoc contextToLogin()
95     {
96         String JavaDoc uri = JForumExecutionContext.getRequest().getRequestURI();
97         String JavaDoc query = JForumExecutionContext.getRequest().getQueryString();
98         String JavaDoc path = query == null ? uri : uri + "?" + query;
99         
100         JForumExecutionContext.getTemplateContext().put("returnPath", path);
101         
102         if (ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
103             String JavaDoc redirect = SystemGlobals.getValue(ConfigKeys.SSO_REDIRECT);
104             
105             if (redirect != null && redirect.trim().length() > 0) {
106                 JForumExecutionContext.setRedirect(JForumExecutionContext.getRequest().getContextPath() + redirect.trim() + path);
107             }
108         }
109         
110         return TemplateKeys.USER_LOGIN;
111     }
112     
113     /**
114      * Returns the initial page to start fetching records from.
115      *
116      * @return The initial page number
117      */

118     public static int getStartPage()
119     {
120         String JavaDoc s = JForumExecutionContext.getRequest().getParameter("start");
121         int start = 0;
122         
123         if (s == null || s.trim().equals("")) {
124             start = 0;
125         }
126         else {
127             start = Integer.parseInt(s);
128             
129             if (start < 0) {
130                 start = 0;
131             }
132         }
133         
134         return start;
135     }
136     
137     /**
138      * Gets the forum base link.
139      * The returned link has a trailing slash
140      * @return The forum link, with the trailing slash
141      */

142     public static String JavaDoc getForumLink()
143     {
144         String JavaDoc forumLink = SystemGlobals.getValue(ConfigKeys.FORUM_LINK);
145         
146         if (forumLink.charAt(forumLink.length() - 1) != '/') {
147             forumLink += "/";
148         }
149         
150         return forumLink;
151     }
152
153     /**
154      * Checks if some request needs to be reprocessed.
155      * This is likely to happen when @link net.jforum.ActionServletRequest#dumpRequest()
156      * is stored in the session.
157      *
158      * @return <code>true</code> of <code>false</code>, depending of the status.
159      */

160     public static boolean needReprocessRequest()
161     {
162         return (SessionFacade.getAttribute(ConfigKeys.REQUEST_DUMP) != null);
163     }
164     
165     /**
166      * Reprocess a request.
167      * The request data should be in the session, held by the key
168      * <code>ConfigKeys.REQUEST_DUMP</code> and the value as
169      * a <code>java.util.Map</code>. Usual behaviour is to have the return
170      * of @link net.jforum.ActionServletRequest#dumpRequest().
171      * @throws Exception, RequestEmptyException
172      */

173     public static void reprocessRequest() throws Exception JavaDoc
174     {
175         Map JavaDoc data = (Map JavaDoc)SessionFacade.getAttribute(ConfigKeys.REQUEST_DUMP);
176         if (data == null) {
177             throw new RequestEmptyException("A call to ViewCommon#reprocessRequest() was made, but no data found");
178         }
179         
180         String JavaDoc module = (String JavaDoc)data.get("module");
181         String JavaDoc action = (String JavaDoc)data.get("action");
182         
183         if (module == null || action == null) {
184             throw new RequestEmptyException("A call to ViewCommon#reprocessRequest() was made, "
185                     + "but no module or action name was found");
186         }
187         
188         JForumExecutionContext.getRequest().restoreDump(data);
189         SessionFacade.removeAttribute(ConfigKeys.REQUEST_DUMP);
190         
191         String JavaDoc moduleClass = ModulesRepository.getModuleClass(module);
192         ((Command)Class.forName(moduleClass).newInstance()).process(JForumExecutionContext.getRequest(),
193                 JForumExecutionContext.getResponse(), JForumExecutionContext.getTemplateContext());
194     }
195
196     public static String JavaDoc toUtf8String(String JavaDoc s)
197     {
198         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
199     
200         for (int i = 0; i < s.length(); i++) {
201             char c = s.charAt(i);
202     
203             if ((c >= 0) && (c <= 255)) {
204                 sb.append(c);
205             }
206             else {
207                 byte[] b;
208     
209                 try {
210                     b = Character.toString(c).getBytes("utf-8");
211                 }
212                 catch (Exception JavaDoc ex) {
213                     System.out.println(ex);
214                     b = new byte[0];
215                 }
216     
217                 for (int j = 0; j < b.length; j++) {
218                     int k = b[j];
219     
220                     if (k < 0) {
221                         k += 256;
222                     }
223     
224                     sb.append("%" + Integer.toHexString(k).toUpperCase());
225                 }
226             }
227         }
228     
229         return sb.toString();
230     }
231     
232     public static String JavaDoc formatDate(Date JavaDoc date)
233     {
234         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
235         return df.format(date);
236     }
237
238     public static void prepareUserSignature(User u) throws Exception JavaDoc
239     {
240         if (u.getSignature() != null) {
241             u.setSignature(u.getSignature().replaceAll("\n", "<br/>"));
242             u.setSignature(PostCommon.processText(u.getSignature()));
243             u.setSignature(PostCommon.processSmilies(u.getSignature(), SmiliesRepository.getSmilies()));
244         }
245     }
246 }
247
Popular Tags