1 2 package org.roller.pojos; 3 4 import java.io.UnsupportedEncodingException ; 5 import java.net.URLEncoder ; 6 import java.sql.Timestamp ; 7 import java.text.SimpleDateFormat ; 8 import java.util.ArrayList ; 9 import java.util.Arrays ; 10 import java.util.Calendar ; 11 import java.util.Date ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.Set ; 17 import java.util.StringTokenizer ; 18 import java.util.TreeSet ; 19 20 import org.apache.commons.lang.StringUtils; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.roller.RollerException; 24 import org.roller.model.Roller; 25 import org.roller.model.RollerFactory; 26 import org.roller.model.WeblogManager; 27 import org.roller.util.DateUtil; 28 import org.roller.util.Utilities; 29 30 40 public class WeblogEntryData extends org.roller.pojos.PersistentObject 41 implements java.io.Serializable 42 { 43 private static Log mLogger = LogFactory.getFactory() 44 .getInstance(WeblogEntryData.class); 45 46 static final long serialVersionUID = 2341505386843044125L; 47 48 protected String id=null; 49 protected org.roller.pojos.WeblogCategoryData category=null; 50 protected String title=null; 51 protected String link=null; 52 protected String text=null; 53 protected String anchor=null; 54 protected Timestamp pubTime=null; 55 protected Timestamp updateTime=null; 56 protected Boolean publishEntry=null; 57 protected WebsiteData mWebsite=null; 58 protected String mPlugins; 59 protected Boolean allowComments = Boolean.TRUE; 60 protected Integer commentDays = new Integer (7); 61 protected Boolean rightToLeft = Boolean.FALSE; 62 protected Boolean pinnedToMain = Boolean.FALSE; 63 64 private Map attMap = new HashMap (); 65 private Set attSet = new TreeSet (); 66 67 69 public WeblogEntryData() 70 { 71 } 72 73 public WeblogEntryData( 74 java.lang.String id, 75 org.roller.pojos.WeblogCategoryData category, 76 WebsiteData website, 77 java.lang.String title, 78 java.lang.String link, 79 java.lang.String text, 80 java.lang.String anchor, 81 java.sql.Timestamp pubTime, 82 java.sql.Timestamp updateTime, 83 java.lang.Boolean publishEntry) 84 { 85 this.id = id; 86 this.category = category; 87 this.mWebsite = website; 88 this.title = title; 89 this.link = link; 90 this.text = text; 91 this.anchor = anchor; 92 this.pubTime = pubTime; 93 this.updateTime = updateTime; 94 this.publishEntry = publishEntry; 95 } 96 97 public WeblogEntryData(WeblogEntryData otherData) 98 { 99 setData(otherData); 100 } 101 102 104 107 public void setData(org.roller.pojos.PersistentObject otherData) 108 { 109 WeblogEntryData other = (WeblogEntryData)otherData; 110 this.id = other.id; 111 this.category = other.category; 112 this.mWebsite = other.mWebsite; 113 this.title = other.title; 114 this.link = other.link; 115 this.text = other.text; 116 this.anchor = other.anchor; 117 this.pubTime = other.pubTime; 118 this.updateTime = other.updateTime; 119 this.publishEntry = other.publishEntry; 120 this.mPlugins = other.mPlugins; 121 this.allowComments = other.allowComments; 122 this.commentDays = other.commentDays; 123 this.rightToLeft = other.rightToLeft; 124 this.pinnedToMain = other.pinnedToMain; 125 } 126 127 129 134 public java.lang.String getId() 135 { 136 return this.id; 137 } 138 139 140 public void setId(java.lang.String id) 141 { 142 this.id = id; 143 } 144 145 149 public org.roller.pojos.WeblogCategoryData getCategory() 150 { 151 return this.category; 152 } 153 154 155 public void setCategory(org.roller.pojos.WeblogCategoryData category) 156 { 157 this.category = category; 158 } 159 160 164 public void setCategoryId(String id) throws RollerException 165 { 166 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 167 setCategory(wmgr.retrieveWeblogCategory(id)); 168 } 169 170 174 public WebsiteData getWebsite() 175 { 176 return this.mWebsite; 177 } 178 179 180 public void setWebsite(WebsiteData website) 181 { 182 this.mWebsite = website; 183 } 184 185 189 public java.lang.String getTitle() 190 { 191 return this.title; 192 } 193 194 195 public void setTitle(java.lang.String title) 196 { 197 this.title = title; 198 } 199 200 204 public java.lang.String getText() 205 { 206 return this.text; 207 } 208 209 210 public void setText(java.lang.String text) 211 { 212 this.text = text; 213 } 214 215 219 public java.lang.String getAnchor() 220 { 221 return this.anchor; 222 } 223 224 225 public void setAnchor(java.lang.String anchor) 226 { 227 this.anchor = anchor; 228 } 229 230 238 public Set getEntryAttributes() 239 { 240 return attSet; 241 } 242 243 public void setEntryAttributes(Set attSet) 244 { 245 this.attSet = attSet; 246 247 if (attSet != null) 249 { 250 this.attSet = attSet; 251 this.attMap = new HashMap (); 252 Iterator iter = this.attSet.iterator(); 253 while (iter.hasNext()) 254 { 255 EntryAttributeData att = (EntryAttributeData)iter.next(); 256 attMap.put(att.getName(), att); 257 } 258 } 259 else 260 { 261 this.attSet = new TreeSet (); 262 this.attMap = new HashMap (); 263 } 264 } 265 266 public String findEntryAttribute(String name) 267 { 268 EntryAttributeData att = ((EntryAttributeData)attMap.get(name)); 269 return (att != null) ? att.getValue() : null; 270 } 271 public void putEntryAttribute(String name, String value) throws Exception 272 { 273 EntryAttributeData att = (EntryAttributeData)attMap.get(name); 274 if (att == null) 275 { 276 att = new EntryAttributeData(); 277 att.setEntry(this); 278 att.setName(name); 279 att.setValue(value); 280 attMap.put(name, att); 281 attSet.add(att); 282 } 283 else 284 { 285 att.setValue(value); 286 } 287 } 288 public void removeEntryAttribute(String name) throws RollerException 289 { 290 EntryAttributeData att = (EntryAttributeData)attMap.get(name); 291 if (att != null) 292 { 293 attMap.remove(att); 294 attSet.remove(att); 295 att.remove(); 296 } 297 } 298 300 313 public java.sql.Timestamp getPubTime() 314 { 315 return this.pubTime; 316 } 317 318 319 public void setPubTime(java.sql.Timestamp pubTime) 320 { 321 this.pubTime = pubTime; 322 } 323 324 338 public java.sql.Timestamp getUpdateTime() 339 { 340 return this.updateTime; 341 } 342 343 344 public void setUpdateTime(java.sql.Timestamp updateTime) 345 { 346 this.updateTime = updateTime; 347 } 348 349 353 public java.lang.Boolean getPublishEntry() 354 { 355 return this.publishEntry; 356 } 357 358 359 public void setPublishEntry(java.lang.Boolean publishEntry) 360 { 361 this.publishEntry = publishEntry; 362 } 363 364 371 public java.lang.String getLink() 372 { 373 return link; 374 } 375 376 380 public void setLink(java.lang.String link) 381 { 382 this.link = link; 383 } 384 385 390 public java.lang.String getPlugins() 391 { 392 return mPlugins; 393 } 394 395 396 public void setPlugins(java.lang.String string) 397 { 398 mPlugins = string; 399 } 400 401 402 407 public Boolean getAllowComments() { 408 return allowComments; 409 } 410 414 public void setAllowComments(Boolean allowComments) { 415 this.allowComments = allowComments; 416 } 417 418 423 public Integer getCommentDays() { 424 return commentDays; 425 } 426 430 public void setCommentDays(Integer commentDays) { 431 this.commentDays = commentDays; 432 } 433 434 439 public Boolean getRightToLeft() { 440 return rightToLeft; 441 } 442 446 public void setRightToLeft(Boolean rightToLeft) { 447 this.rightToLeft = rightToLeft; 448 } 449 450 457 public Boolean getPinnedToMain() 458 { 459 return pinnedToMain; 460 } 461 467 public void setPinnedToMain(Boolean pinnedToMain) 468 { 469 this.pinnedToMain = pinnedToMain; 470 } 471 472 474 478 public void save() throws RollerException 479 { 480 if (getAnchor()==null || getAnchor().trim().equals("")) 482 { 483 setAnchor(createAnchor()); 484 } 485 if (getPublishEntry() != null && getPublishEntry().booleanValue()) { 486 RollerFactory.getRoller().getAutopingManager().queueApplicableAutoPings(this); 488 } 489 super.save(); 490 } 491 492 494 498 public boolean getCommentsStillAllowed() 499 { 500 boolean ret = false; 501 if (getAllowComments() == null || getAllowComments().booleanValue()) 502 { 503 if (getCommentDays() == null || getCommentDays().intValue() == 0) 504 { 505 ret = true; 506 } 507 else 508 { 509 Calendar expireCal = Calendar.getInstance(getWebsite().getLocaleInstance()); 510 expireCal.setTime(getPubTime()); 511 expireCal.add(Calendar.DATE, getCommentDays().intValue()); 512 Date expireDay = expireCal.getTime(); 513 Date today = new Date (); 514 if (today.before(expireDay)) 515 { 516 ret = true; 517 } 518 } 519 } 520 return ret; 521 } 522 public void setCommentsStillAllowed(boolean ignored) { 523 } 525 526 527 529 535 public String formatPubTime(String pattern) 536 { 537 try 538 { 539 SimpleDateFormat format = new SimpleDateFormat (pattern, 540 this.getWebsite().getLocaleInstance()); 541 542 return format.format(getPubTime()); 543 } 544 catch (RuntimeException e) 545 { 546 mLogger.error("Unexpected exception", e); 547 } 548 549 return "ERROR: formatting date"; 550 } 551 552 554 560 public String formatUpdateTime(String pattern) 561 { 562 try 563 { 564 SimpleDateFormat format = new SimpleDateFormat (pattern); 565 566 return format.format(getUpdateTime()); 567 } 568 catch (RuntimeException e) 569 { 570 mLogger.error("Unexpected exception", e); 571 } 572 573 return "ERROR: formatting date"; 574 } 575 576 578 public List getComments() 579 { 580 return getComments(true); 581 } 582 583 public List getComments(boolean ignoreSpam) 584 { 585 List list = new ArrayList (); 586 try 587 { 588 return RollerFactory.getRoller().getWeblogManager().getComments(getId(), ignoreSpam); 589 } 590 catch (RollerException alreadyLogged) {} 591 return list; 592 } 593 594 596 public List getReferers() 597 { 598 List referers = null; 599 try { 600 referers = RollerFactory.getRoller().getRefererManager().getReferersToEntry(getId()); 601 } catch (RollerException e) { 602 mLogger.error("Unexpected exception", e); 603 } 604 return referers; 605 } 606 607 609 616 public void addTrackback( 617 String url, String title, String excerpt, String blogName) 618 throws RollerException 619 { 620 String modTitle = blogName + ": " + title; 621 if (modTitle.length() >= 250) 622 { 623 modTitle = modTitle.substring(0, 257); 624 modTitle += "..."; 625 } 626 627 CommentData comment = new CommentData(); 629 comment.setContent("[Trackback] "+excerpt); 630 comment.setName(blogName); 631 comment.setUrl(url); 632 comment.setWeblogEntry(this); 633 comment.setNotify(Boolean.FALSE); 634 comment.setPostTime(new Timestamp (new Date ().getTime())); 635 comment.save(); 636 637 } 652 653 658 public String getPermaLink() 659 { 660 String lAnchor = this.getAnchor(); 661 662 try 663 { 664 lAnchor = URLEncoder.encode(anchor, "UTF-8"); 665 } 666 catch (UnsupportedEncodingException e) 667 { 668 } 670 671 WebsiteData website = this.getWebsite(); 672 String plink = "/page/" + website.getUser().getUserName() + 673 "?entry=" + lAnchor; 674 675 return plink; 676 } 677 678 686 public String getPermaLink(String categoryPath) 687 { 688 691 return this.getPermaLink(); 692 } 693 694 public String getCommentsLink() 695 { 696 String dayString = DateUtil.format8chars(this.getPubTime()); 697 String lAnchor = this.getAnchor(); 698 try 699 { 700 lAnchor = URLEncoder.encode(anchor, "UTF-8"); 701 } 702 catch (UnsupportedEncodingException e) 703 { 704 } 706 String clink = "/page/" + this.getWebsite().getUser().getUserName() + "?anchor=" + lAnchor; 707 return clink; 708 } 709 710 public void setCommentsLink(String ignored) {} 711 712 718 public String getDisplayTitle() 719 { 720 if ( getTitle()==null || getTitle().trim().equals("") ) 721 { 722 return StringUtils.left(Utilities.removeHTML(text),255); 723 } 724 return Utilities.removeHTML(getTitle()); 725 } 726 727 729 public String toString() 730 { 731 StringBuffer str = new StringBuffer ("{"); 732 733 str.append("id=" + id + " " + 734 "category=" + category + " " + 735 "title=" + title + " " + 736 "text=" + text + " " + 737 "anchor=" + anchor + " " + 738 "pubTime=" + pubTime + " " + 739 "updateTime=" + updateTime + " " + 740 "publishEntry=" + publishEntry + " " + 741 "plugins=" + mPlugins); 742 str.append('}'); 743 744 return (str.toString()); 745 } 746 747 749 public boolean equals(Object pOther) 750 { 751 if (pOther instanceof WeblogEntryData) 752 { 753 WeblogEntryData lTest = (WeblogEntryData) pOther; 754 boolean lEquals = true; 755 756 if (this.id == null) 757 { 758 lEquals = lEquals && (lTest.id == null); 759 } 760 else 761 { 762 lEquals = lEquals && this.id.equals(lTest.id); 763 } 764 765 if (this.category == null) 766 { 767 lEquals = lEquals && (lTest.category == null); 768 } 769 else 770 { 771 lEquals = lEquals && this.category.equals(lTest.category); 772 } 773 774 if (this.mWebsite == null) 775 { 776 lEquals = lEquals && (lTest.mWebsite == null); 777 } 778 else 779 { 780 lEquals = lEquals && this.mWebsite.equals(lTest.mWebsite); 781 } 782 783 if (this.title == null) 784 { 785 lEquals = lEquals && (lTest.title == null); 786 } 787 else 788 { 789 lEquals = lEquals && this.title.equals(lTest.title); 790 } 791 792 if (this.text == null) 793 { 794 lEquals = lEquals && (lTest.text == null); 795 } 796 else 797 { 798 lEquals = lEquals && this.text.equals(lTest.text); 799 } 800 801 if (this.anchor == null) 802 { 803 lEquals = lEquals && (lTest.anchor == null); 804 } 805 else 806 { 807 lEquals = lEquals && this.anchor.equals(lTest.anchor); 808 } 809 810 if (this.pubTime == null) 811 { 812 lEquals = lEquals && (lTest.pubTime == null); 813 } 814 else 815 { 816 lEquals = lEquals && this.pubTime.equals(lTest.pubTime); 817 } 818 819 if (this.updateTime == null) 820 { 821 lEquals = lEquals && (lTest.updateTime == null); 822 } 823 else 824 { 825 lEquals = lEquals && 826 this.updateTime.equals(lTest.updateTime); 827 } 828 829 if (this.publishEntry == null) 830 { 831 lEquals = lEquals && (lTest.publishEntry == null); 832 } 833 else 834 { 835 lEquals = lEquals && 836 this.publishEntry.equals(lTest.publishEntry); 837 } 838 839 if (this.mPlugins == null) 840 { 841 lEquals = lEquals && (lTest.mPlugins == null); 842 } 843 else 844 { 845 lEquals = lEquals && 846 this.mPlugins.equals(lTest.mPlugins); 847 } 848 849 850 return lEquals; 851 } 852 else 853 { 854 return false; 855 } 856 } 857 858 860 public int hashCode() 861 { 862 int result = 17; 863 result = (37 * result) + 864 ((this.id != null) ? this.id.hashCode() : 0); 865 result = (37 * result) + 866 ((this.category != null) ? this.category.hashCode() : 0); 867 result = (37 * result) + 868 ((this.mWebsite != null) ? this.mWebsite.hashCode() : 0); 869 result = (37 * result) + 870 ((this.title != null) ? this.title.hashCode() : 0); 871 result = (37 * result) + 872 ((this.text != null) ? this.text.hashCode() : 0); 873 result = (37 * result) + 874 ((this.anchor != null) ? this.anchor.hashCode() : 0); 875 result = (37 * result) + 876 ((this.pubTime != null) ? this.pubTime.hashCode() : 0); 877 result = (37 * result) + 878 ((this.updateTime != null) ? this.updateTime.hashCode() : 0); 879 result = (37 * result) + 880 ((this.publishEntry != null) ? this.publishEntry.hashCode() : 0); 881 result = (37 * result) + 882 ((this.mPlugins != null) ? this.mPlugins.hashCode() : 0); 883 884 return result; 885 } 886 887 888 public String getRss09xDescription() 889 { 890 return getRss09xDescription(-1); 891 } 892 893 894 public String getRss09xDescription(int maxLength) 895 { 896 String ret = Utilities.escapeHTML(text); 897 if (maxLength != -1 && ret.length() > maxLength) 898 { 899 ret = ret.substring(0,maxLength-3)+"..."; 900 } 901 return ret; 902 } 903 904 905 protected String createAnchor() throws RollerException 906 { 907 return RollerFactory.getRoller().getWeblogManager().createAnchor(this); 908 } 909 910 911 public String createAnchorBase() 912 { 913 String base = getTitle(); 915 if (base == null || base.trim().equals("")) 916 { 917 base = getText(); 918 } 919 if (base != null && !base.trim().equals("")) 920 { 921 base = Utilities.replaceNonAlphanumeric(base, ' '); 922 923 StringTokenizer toker = new StringTokenizer (base); 925 String tmp = null; 926 int count = 0; 927 while (toker.hasMoreTokens() && count < 5) 928 { 929 String s = toker.nextToken(); 930 s = s.toLowerCase(); 931 tmp = (tmp == null) ? s : tmp + "_" + s; 932 count++; 933 } 934 base = tmp; 935 } 936 else 939 { 940 base = DateUtil.format8chars(getPubTime()); 941 } 942 943 return base; 944 } 945 946 951 public void setPermaLink(String string) 952 { 953 } 954 955 960 public void setDisplayTitle(String string) 961 { 962 } 963 964 969 public void setRss09xDescription(String string) 970 { 971 } 972 973 976 public void remove() throws RollerException 977 { 978 RollerFactory.getRoller().getWeblogManager().removeWeblogEntryContents(this); 979 super.remove(); 980 } 981 982 986 public List getPluginsList() 987 { 988 if (mPlugins != null) 989 { 990 return Arrays.asList( StringUtils.split(mPlugins, ",") ); 991 } 992 return new ArrayList (); 993 } 994 995 public boolean canSave() throws RollerException 996 { 997 Roller roller = RollerFactory.getRoller(); 998 if (roller.getUser().equals(UserData.SYSTEM_USER)) 999 { 1000 return true; 1001 } 1002 if (roller.getUser().equals(getWebsite().getUser())) 1003 { 1004 return true; 1005 } 1006 return false; 1007 } 1008} 1009 | Popular Tags |