1 53 54 106 107 package com.Yasna.forum.tags; 108 109 import java.io.*; 110 import java.util.*; 111 import java.text.*; 112 import javax.servlet.*; 113 import javax.servlet.jsp.*; 114 import javax.servlet.jsp.tagext.*; 115 import javax.servlet.http.*; 116 import com.Yasna.forum.*; 117 import com.Yasna.forum.tags.*; 118 119 159 public class ThreadTag extends TagSupport implements GetNestedMessage, 160 ChangeForum, NewMessages 161 { 162 private ForumThread ct = null; 163 private YazdRequest jr = null; 164 private YazdState js = null; 165 private int prevThreadID = -1; 167 private int nextThreadID = -1; 168 private Forum mf = null; 170 private int mtid = 0; 171 private int mmid = 0; 172 173 178 public final int doStartTag() throws JspException 179 { 180 js = (YazdState)pageContext.getAttribute("yazdUserState", 182 PageContext.SESSION_SCOPE); 183 if( js == null ) { 184 throw new JspException("Yazd thread tag could not get yazd state."); 185 } 186 187 jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest", 189 PageContext.REQUEST_SCOPE); 190 if( jr == null ) 191 throw new JspException("Yazd thread tag, could not find request"); 192 193 ThreadLoopTag tl = null; 195 try { 196 tl = (ThreadLoopTag)this.findAncestorWithClass(this, 197 Class.forName("com.Yasna.forum.tags.ThreadLoopTag")); 198 } catch(Exception e) { 199 } 200 201 if( tl != null ) { 202 ct = tl.getThread(); 203 } else { 204 ct = jr.getThread(); 206 } 207 208 if( ct == null ) 209 return SKIP_BODY; 210 211 Iterator thrit = jr.getForum().threads(); 213 int nextid; 214 int previd = -1; 215 while( thrit.hasNext() ) { 216 nextid = ((ForumThread)thrit.next()).getID(); 217 if( nextid == ct.getID() ) { 218 prevThreadID = previd; 219 if( thrit.hasNext() ) 220 nextThreadID = ((ForumThread)thrit.next()).getID(); 221 break; 222 } 223 previd = nextid; 224 } 225 226 pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE); 228 return EVAL_BODY_INCLUDE; 229 } 230 231 236 public final ForumThread getThread() 237 { 238 return ct; 239 } 240 241 247 public final String getThreadid() 248 { 249 return "" + ct.getID(); 250 } 251 252 258 public final String getMessagecount() 259 { 260 return "" + ct.getMessageCount(); 261 } 262 263 269 public final String getMessagereplies() 270 { 271 return "" + (ct.getMessageCount() - 1); 272 } 273 274 280 public final int getTotal() 281 { 282 return ct.getMessageCount(); 283 } 284 285 291 public final String getName() 292 { 293 return ct.getName(); 294 } 295 296 302 public final String getForumName() 303 { 304 return ct.getForum().getName(); 305 } 306 307 313 public final String getForumid() 314 { 315 return "" + ct.getForum().getID(); 316 } 317 318 325 public final String getCreationDate() 326 { 327 return "" + ct.getCreationDate().getTime(); 328 } 329 330 337 public final String getModifiedDate() 338 { 339 return "" + ct.getModifiedDate().getTime(); 340 } 341 342 348 public final String getNextThreadid() 349 { 350 return "" + nextThreadID; 351 } 352 353 359 public final String getPrevThreadid() 360 { 361 return "" + prevThreadID; 362 } 363 364 369 public final ForumMessage getMessage() { 370 return ct.getRootMessage(); 371 } 372 373 378 public final boolean moveThread() throws JspException 379 { 380 Forum cf = ct.getForum(); 381 int threadid = ct.getID(); 382 if( mf == null ) 383 throw new JspException( 384 "Yazd thread tag, you must set a forum using change_forum tag before using move_thread tag"); 385 try { 386 cf.moveThread(ct,mf); 387 return true; 388 389 399 400 } catch(UnauthorizedException ue) { 401 jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.authorize.failed")); 402 } 403 return false; 404 } 405 406 412 public final boolean moveMessage() throws JspException 413 { 414 Forum cf = ct.getForum(); 415 int threadid = ct.getID(); 416 ForumMessage cm = null; 417 ForumThread mt = null; 418 419 cm = ct.getRootMessage(); 420 if( cm == null ) 421 throw new JspException( 422 "Yazd thread tag, could not find message to move using move_message tag"); 423 424 if( ct.getMessageCount() != 1 ) 425 throw new JspException( 426 "Yazd thread tag, you can not move a message thread that has replies using move_message tag"); 427 428 if( mf == null ) 429 throw new JspException( 430 "Yazd thread tag, you must set a forum using change_forum tag before using move_message tag"); 431 432 if( mtid == 0 ) 433 throw new JspException( 434 "Yazd thread tag, you must set a thread using set_thread tag before using move_message tag"); 435 436 if( mmid == 0 ) 437 throw new JspException( 438 "Yazd thread tag, you must set a parent message using set_parent_message tag before using move_message tag"); 439 440 cm = ct.getRootMessage(); 441 if( cm == null ) { 442 throw new JspException( 443 "Yazd thread tag, could not find thread root message for move_message tag"); 444 } 445 446 try { 447 mt = mf.getThread(mtid); 448 } catch(ForumThreadNotFoundException e) { 449 throw new JspException( 450 "Yazd thread tag, could not find thread to move message to"); 451 } 452 try { 453 ForumMessage parentMessage = mt.getMessage( mmid ); 454 ct.moveMessage(cm,mt,parentMessage); 455 ct = mt; 456 return true; 457 458 472 473 } catch(UnauthorizedException ue) { 474 jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.authorize.failed")); 475 } catch(ForumMessageNotFoundException e) { 476 throw new JspException( 477 "Yazd thread tag, could not find parent message for move_message tag"); 478 } 479 return false; 480 } 481 482 485 public final void changeForum(Forum apf) 486 { 487 mf = apf; 488 } 489 490 495 public final boolean newMessages() 496 { 497 if( js.getLastForumVisitDate(ct.getForum(),jr).getTime() < 498 ct.getModifiedDate().getTime() ) 499 return true; 500 return false; 501 } 502 503 508 public final boolean prevThread() 509 { 510 if( prevThreadID > 0 ) 511 return true; 512 return false; 513 } 514 515 520 public final boolean nextThread() 521 { 522 if( nextThreadID > 0 ) 523 return true; 524 return false; 525 } 526 527 530 public final void setThread(int tid) 531 { 532 mtid = tid; 533 } 534 535 538 public final void changeThread(int at) throws JspException 539 { 540 ForumTag ft = null; 542 try { 543 ft = (ForumTag)this.findAncestorWithClass(this, 544 Class.forName("com.Yasna.forum.tags.ForumTag")); 545 } catch(Exception e) { 546 } 547 if( ft == null ) { 548 throw new JspException("Yazd thread tag must be nested inside a forum tag"); 549 } 550 551 try { 552 ForumThread nt = ft.getForum().getThread(at); 553 ct = nt; 554 } catch(ForumThreadNotFoundException e) { 555 throw new JspException( 556 "Yazd thread tag, could not find thread to change_thread to"); 557 } 558 } 559 560 563 public final void setParentMessage(int mid) 564 { 565 mmid = mid; 566 } 567 568 569 572 public final void release() 573 { 574 if( id != null && id.length() > 0 ) 575 pageContext.removeAttribute(id,PageContext.PAGE_SCOPE); 576 } 577 578 } 579 | Popular Tags |