1 43 package net.jforum.view.forum.common; 44 45 import java.text.SimpleDateFormat ; 46 import java.util.Date ; 47 import java.util.Map ; 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 65 public final class ViewCommon 66 { 67 83 public static void contextToPagination(int start, int totalRecords, int recordsPerPage) 84 { 85 SimpleHash context = JForumExecutionContext.getTemplateContext(); 86 87 context.put("totalPages", new Double (Math.ceil((double) totalRecords / (double) recordsPerPage))); 88 context.put("recordsPerPage", new Integer (recordsPerPage)); 89 context.put("totalRecords", new Integer (totalRecords)); 90 context.put("thisPage", new Double (Math.ceil((double) (start + 1) / (double) recordsPerPage))); 91 context.put("start", new Integer (start)); 92 } 93 94 public static String contextToLogin() 95 { 96 String uri = JForumExecutionContext.getRequest().getRequestURI(); 97 String query = JForumExecutionContext.getRequest().getQueryString(); 98 String 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 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 118 public static int getStartPage() 119 { 120 String 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 142 public static String getForumLink() 143 { 144 String forumLink = SystemGlobals.getValue(ConfigKeys.FORUM_LINK); 145 146 if (forumLink.charAt(forumLink.length() - 1) != '/') { 147 forumLink += "/"; 148 } 149 150 return forumLink; 151 } 152 153 160 public static boolean needReprocessRequest() 161 { 162 return (SessionFacade.getAttribute(ConfigKeys.REQUEST_DUMP) != null); 163 } 164 165 173 public static void reprocessRequest() throws Exception 174 { 175 Map data = (Map )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 module = (String )data.get("module"); 181 String action = (String )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 moduleClass = ModulesRepository.getModuleClass(module); 192 ((Command)Class.forName(moduleClass).newInstance()).process(JForumExecutionContext.getRequest(), 193 JForumExecutionContext.getResponse(), JForumExecutionContext.getTemplateContext()); 194 } 195 196 public static String toUtf8String(String s) 197 { 198 StringBuffer sb = new StringBuffer (); 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 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 formatDate(Date date) 233 { 234 SimpleDateFormat df = new SimpleDateFormat (SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT)); 235 return df.format(date); 236 } 237 238 public static void prepareUserSignature(User u) throws Exception 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 |