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 181 public class MessageLoopTag extends BodyTagSupport 182 implements Paging, GetNestedMessage 183 { 184 private YazdState js = null; 185 private YazdRequest jr = null; 186 private int begin = 0; 188 private String query = null; 189 190 private int size = 0; 191 private int message_num = 0; 193 private boolean next_page = false; 195 private boolean prev_page = false; 197 private boolean is_entry = true; 199 private boolean is_exit = false; 201 private Iterator mit = null; 203 private ForumMessage cm = null; 205 206 211 public final int doStartTag() throws JspException 212 { 213 js = (YazdState)pageContext.getAttribute("yazdUserState", 215 PageContext.SESSION_SCOPE); 216 if( js == null ) 217 throw new JspException("Yazd message_loop tag, could not find yazdUserState"); 218 219 jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest", 221 PageContext.REQUEST_SCOPE); 222 if( jr == null ) 223 throw new JspException("Could not find request"); 224 225 ForumTag ft = null; 227 try { 228 ft = (ForumTag)this.findAncestorWithClass(this, 229 Class.forName("com.Yasna.forum.tags.ForumTag")); 230 } catch(Exception e) { 231 } 232 233 if( ft == null ) { 234 throw new JspException("Yazd message_loop tag could not find forum."); 235 } 236 237 ServletRequest req = pageContext.getRequest(); 240 String tmp = req.getParameter("begin"); 241 if( tmp != null && tmp.length() > 0 ) { 242 try { 243 begin = Integer.valueOf(tmp).intValue(); 244 } catch(NumberFormatException e) { 245 } 246 } 247 248 QueryTag qt = null; 249 try { 250 qt = (QueryTag)pageContext.getAttribute(query,PageContext.PAGE_SCOPE); 251 } catch(Exception e) { 252 throw new JspException( 253 "Yazd message tag, could not find query tag with id: " + query); 254 } 255 Map properties = qt.getQuery(); 256 Query q = ft.getForum().createQuery(); 257 if( properties.get("beforeDate") != null ) { 258 tmp = (String )properties.get("beforeDate"); 259 try { 260 if( tmp != null && tmp.length() > 0 ) { 261 Date date = new Date(Long.valueOf(tmp).longValue()); 262 q.setBeforeDate(date); 263 } 264 } catch(Exception e) { 265 throw new JspException("Yazd message tag, bad query tag beforeDate"); 266 } 267 } 268 if( properties.get("afterDate") != null ) { 269 tmp = (String )properties.get("afterDate"); 270 try { 271 if( tmp != null && tmp.length() > 0 ) { 272 Date date = new Date(Long.valueOf(tmp).longValue()); 273 q.setAfterDate(date); 274 } 275 } catch(Exception e) { 276 throw new JspException("Yazd message tag, bad query tag afterDate"); 277 } 278 } 279 if( properties.get("queryString") != null ) { 280 tmp = (String )properties.get("queryString"); 281 if( tmp == null ) { 282 throw new JspException("Yazd message tag, bad query tag queryString"); 283 } 284 if( tmp.length() > 0 ) 285 q.setQueryString(tmp); 286 } 287 if( properties.get("userID") != null ) { 288 tmp = (String )properties.get("userID"); 289 try { 290 ProfileManager pm = jr.getProfileManager(); 291 User user = pm.getUser(tmp); 292 q.filterOnUser(user); 293 } catch(UserNotFoundException ue) { 294 } catch(Exception e) { 296 throw new JspException("Yazd message tag, bad query tag userID:" + 297 e.getMessage()); 298 } 299 } 300 mit = q.results(begin,js.getItemsPerPage()); 301 302 if( mit == null ) { 303 throw new JspException("Yazd message_loop tag could not get Iterator for forum messages."); 304 } 305 306 if( !mit.hasNext() ) 307 return SKIP_BODY; 308 cm = (ForumMessage)mit.next(); 309 message_num++; 310 if( !mit.hasNext() ) { 311 is_exit = true; 312 if( message_num >= js.getItemsPerPage() ) 313 next_page = true; 314 } 315 if( begin > 0 ) 316 prev_page = true; 317 318 return EVAL_BODY_TAG; 319 } 320 321 326 public final int doAfterBody() throws JspException 327 { 328 is_entry = prev_page = false; 329 message_num++; 330 if( !mit.hasNext() ) { 331 return SKIP_BODY; 332 } 333 cm = (ForumMessage)mit.next(); 334 if( !mit.hasNext() ) { 335 is_exit = true; 336 if( message_num >= js.getItemsPerPage() ) 337 next_page = true; 338 } 339 return EVAL_BODY_TAG; 340 } 341 342 346 public final int doEndTag() throws JspException 347 { 348 try 349 { 350 if(bodyContent != null) 351 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 352 } catch(java.io.IOException e) 353 { 354 throw new JspException("IO Error: " + e.getMessage()); 355 } 356 return EVAL_PAGE; 357 } 358 359 362 public final void setQuery(String qry) 363 { 364 query = qry; 365 } 366 367 372 public final ForumMessage getMessage() 373 { 374 return cm; 375 } 376 377 383 public final int getTotal() 384 { 385 return 0; 386 } 387 388 394 public final boolean isEntry() 395 { 396 return is_entry; 397 } 398 399 405 public final boolean isExit() 406 { 407 return is_exit; 408 } 409 410 416 public final boolean isNextPage() 417 { 418 return next_page; 419 } 420 421 427 public final boolean isPrevPage() 428 { 429 return prev_page; 430 } 431 432 438 public final String nextItem() 439 { 440 return "begin=" + (begin+message_num); 441 } 442 443 449 public final String prevItem() 450 { 451 int prev = begin - js.getItemsPerPage(); 452 if( prev < 0 ) 453 prev = 0; 454 return "begin=" + prev; 455 } 456 457 } 458 | Popular Tags |