1 53 54 106 107 package com.Yasna.forum.tags; 108 109 import java.util.*; 110 import java.text.*; 111 import javax.servlet.*; 112 import javax.servlet.jsp.*; 113 import javax.servlet.jsp.tagext.*; 114 import javax.servlet.http.*; 115 import com.Yasna.forum.*; 116 import com.Yasna.util.*; 117 118 133 134 public class YazdRequest 135 { 136 private YazdState js = null; 137 private ForumFactory ff = null; 138 private PageContext pageContext = null; 139 private ProfileManager pm = null; 140 private List forums = null; 141 private List error = new java.util.LinkedList (); 142 143 149 public final void setYazdState(YazdState j) 150 { 151 js = j; 152 } 153 154 160 161 public final List getForums(Map match) throws JspException 162 { 163 checkYazdState(); 164 if( forums == null ) { 165 forums = new java.util.LinkedList (); 166 Iterator vf = ff.forums(); 167 while( vf.hasNext() ) { 168 Forum forum = (Forum)vf.next(); 169 boolean match_made = true; 170 String prop; 171 String value; 172 for(Iterator it=match.keySet().iterator();it.hasNext();) { 173 prop = (String )it.next(); 174 if( (value = forum.getProperty(prop)) == null ) { 175 match_made = false; 176 break; 177 } 178 if( !value.equals((String )match.get(prop)) ) { 179 match_made = false; 180 break; 181 } 182 } 183 if( match_made ) { 184 forums.add( forum ); 185 } 186 } 187 } 188 return forums; 189 } 190 204 205 206 214 public final Forum getForum() throws JspException 215 { 216 Forum cf = null; 217 checkYazdState(); 218 219 int threadid = js.getForumID(); 220 if( threadid < 0 ) { 221 java.util.LinkedList forums = (java.util.LinkedList )getForums(new HashMap()); 222 if( forums.size() > 0 ) { 223 cf = (Forum)forums.get(0); 224 js.setForumID(cf.getID()); 225 } 226 } else { 227 try { 228 cf = ff.getForum(threadid); 229 } catch(ForumNotFoundException e) { 230 throw new JspException("Yazd forum tag, could not find forum with ID:" + 231 threadid); 232 } catch(UnauthorizedException e) { 233 throw new JspException("Yazd forum tag, user not authorized for forum with ID:" + 234 threadid); 235 } 236 } 237 if( cf == null ) { 238 throw new JspException("Yazd forum tag, no forums available for user."); 239 } 240 241 return cf; 242 } 243 public final ThreadType getThreadType(int id) throws JspException{ 244 checkYazdState(); 245 return ff.getThreadType(id); 246 } 247 248 256 public final ForumThread getThread() throws JspException 257 { 258 Forum cf = getForum(); 259 ForumThread ct = null; 260 261 int threadid = js.getThreadID(); 262 if( threadid < 0 ) { 263 Iterator threads = cf.threads(0,1,Forum.SORT_BY_CREATE_DATE); 264 if( threads.hasNext() ) { 265 ct = (ForumThread)threads.next(); 266 } 267 } else { 268 try { 269 ct = cf.getThread(threadid); 270 } catch(ForumThreadNotFoundException e) { 271 throw new JspException("Could not find thread with ID:" + threadid); 272 } 273 } 274 if( ct == null ) { 275 throw new JspException("Yazd thread tag, no threads available for user."); 276 } 277 278 return ct; 279 } 280 281 289 public final ForumMessage getMessage() throws JspException 290 { 291 ForumThread ct = getThread(); 292 ForumMessage cm = null; 293 294 int messageid = js.getMessageID(); 295 try { 296 if( messageid < 0 ) { 297 cm = ct.getRootMessage(); 298 } else { 299 cm = ct.getMessage(messageid); 300 } 301 } catch(ForumMessageNotFoundException e) { 302 throw new JspException("Could not find message with messageID: " + 303 messageid); 304 } 305 if( cm == null ) { 306 throw new JspException("Yazd message tag, no messages available for user."); 307 } 308 309 return cm; 310 } 311 312 319 public final String getNumberOfForums() throws JspException 320 { 321 return "" + getForumFactory().getForumCount(); 322 } 323 324 331 public final String getLastVisit() 332 { 333 return "" + js.getLastVisit().getTime(); 334 } 335 336 340 public final void addError(String err) 341 { 342 error.add((Object )new StringBuffer (err)); 343 } 344 345 351 public final List getErrorList() 352 { 353 return error; 354 } 355 356 361 public final void setError(String err) 362 { 363 error.clear(); 364 error.add((Object )new StringBuffer (err)); 365 } 366 367 372 public final boolean isAnonymous() 373 { 374 if( js.getLoggedIn() ) 375 return false; 376 return true; 377 } 378 379 386 public final String getMessageDepth() 387 { 388 return "" + js.getMessageDepth(); 389 } 390 391 396 public final void setMessageDepth(String s) 397 { 398 try { 399 js.setMessageDepth(Integer.valueOf(s).intValue()); 400 } catch(NumberFormatException e) { 401 } 402 } 403 404 411 public final String getThreadDepth() 412 { 413 return "" + js.getThreadDepth(); 414 } 415 416 421 public final void setThreadDepth(String s) 422 { 423 try { 424 js.setThreadDepth(Integer.valueOf(s).intValue()); 425 } catch(NumberFormatException e) { 426 } 427 } 428 429 436 public final String getItemsPerPage() 437 { 438 return "" + js.getItemsPerPage(); 439 } 440 441 446 public final void setItemsPerPage(String s) 447 { 448 try { 449 js.setItemsPerPage(Integer.valueOf(s).intValue()); 450 } catch(NumberFormatException e) { 451 } 452 } 453 454 459 public final String GeneratePassword() 460 { 461 return StringUtils.randomString(8); 462 } 463 464 469 public final ForumFactory getForumFactory() throws JspException 470 { 471 checkYazdState(); 472 return ff; 473 } 474 475 480 public final ProfileManager getProfileManager() throws JspException 481 { 482 checkYazdState(); 483 if( pm == null ) 484 pm = ff.getProfileManager(); 485 if( pm == null ) 486 throw new JspException("Could not get ProfileManager."); 487 return pm; 488 } 489 490 496 private void checkYazdState() throws JspException 497 { 498 if( js == null || js.getAuthorization() == null ) 499 throw new JspException("YazdState not set for YazdRequest"); 500 if( ff == null ) 501 ff = ForumFactory.getInstance(js.getAuthorization()); 502 if( ff == null ) 503 throw new JspException("Could not get ForumFactory."); 504 } 505 } 506 | Popular Tags |