1 18 19 package org.apache.roller.pojos; 20 21 import java.io.Serializable ; 22 import java.io.UnsupportedEncodingException ; 23 import java.net.URLEncoder ; 24 import java.sql.Timestamp ; 25 import java.text.SimpleDateFormat ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Calendar ; 29 import java.util.Date ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import java.util.StringTokenizer ; 36 import java.util.TreeSet ; 37 38 import org.apache.commons.lang.StringEscapeUtils; 39 40 import org.apache.commons.lang.StringUtils; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 import org.apache.roller.RollerException; 44 import org.apache.roller.config.RollerRuntimeConfig; 45 import org.apache.roller.model.RollerFactory; 46 import org.apache.roller.model.UserManager; 47 import org.apache.roller.model.WeblogEntryPlugin; 48 import org.apache.roller.model.WeblogManager; 49 import org.apache.roller.util.DateUtil; 50 import org.apache.roller.util.MessageUtilities; 51 import org.apache.roller.util.URLUtilities; 52 import org.apache.roller.util.Utilities; 53 54 62 public class WeblogEntryData extends PersistentObject implements Serializable { 63 private static Log mLogger = 64 LogFactory.getFactory().getInstance(WeblogEntryData.class); 65 66 public static final long serialVersionUID = 2341505386843044125L; 67 68 public static final String DRAFT = "DRAFT"; 69 public static final String PUBLISHED = "PUBLISHED"; 70 public static final String PENDING = "PENDING"; 71 72 private String id = null; 74 private String title = null; 75 private String link = null; 76 private String summary = null; 77 private String text = null; 78 private String contentType = null; 79 private String contentSrc = null; 80 private String anchor = null; 81 private Timestamp pubTime = null; 82 private Timestamp updateTime = null; 83 private String plugins = null; 84 private Boolean allowComments = Boolean.TRUE; 85 private Integer commentDays = new Integer (7); 86 private Boolean rightToLeft = Boolean.FALSE; 87 private Boolean pinnedToMain = Boolean.FALSE; 88 private String status = DRAFT; 89 private String locale = null; 90 91 private UserData creator = null; 93 private WebsiteData website = null; 94 private WeblogCategoryData category = null; 95 96 private Map attMap = new HashMap (); 98 private Set attSet = new TreeSet (); 99 100 102 public WeblogEntryData() { 103 } 104 105 public WeblogEntryData( 106 String id, 107 WeblogCategoryData category, 108 WebsiteData website, 109 UserData creator, 110 String title, 111 String link, 112 String text, 113 String anchor, 114 Timestamp pubTime, 115 Timestamp updateTime, 116 String status) { 117 this.id = id; 118 this.category = category; 119 this.website = website; 120 this.creator = creator; 121 this.title = title; 122 this.link = link; 123 this.text = text; 124 this.anchor = anchor; 125 this.pubTime = pubTime; 126 this.updateTime = updateTime; 127 this.status = status; 128 } 129 130 public WeblogEntryData(WeblogEntryData otherData) { 131 this.setData(otherData); 132 } 133 134 136 139 public void setData(PersistentObject otherData) { 140 WeblogEntryData other = (WeblogEntryData)otherData; 141 142 this.id = other.getId(); 143 this.category = other.getCategory(); 144 this.website = other.getWebsite(); 145 this.creator = other.getCreator(); 146 this.title = other.getTitle(); 147 this.link = other.getLink(); 148 this.text = other.getText(); 149 this.anchor = other.getAnchor(); 150 this.pubTime = other.getPubTime(); 151 this.updateTime = other.getUpdateTime(); 152 this.status = other.getStatus(); 153 this.plugins = other.getPlugins(); 154 this.allowComments = other.getAllowComments(); 155 this.commentDays = other.getCommentDays(); 156 this.rightToLeft = other.getRightToLeft(); 157 this.pinnedToMain = other.getPinnedToMain(); 158 } 159 160 162 167 public String getId() { 168 return this.id; 169 } 170 171 172 public void setId(String id) { 173 this.id = id; 174 } 175 176 181 public WeblogCategoryData getCategory() { 182 return this.category; 183 } 184 185 186 public void setCategory(WeblogCategoryData category) { 187 this.category = category; 188 } 189 190 194 public void setCategoryId(String id) throws RollerException { 195 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 196 setCategory(wmgr.getWeblogCategory(id)); 197 } 198 199 204 public List getCategories() { 205 List cats = new ArrayList (); 206 cats.add(getCategory()); 207 return cats; 208 } 209 210 211 public void setCategories(List cats) { 212 } 214 215 220 public WebsiteData getWebsite() { 221 return this.website; 222 } 223 224 225 public void setWebsite(WebsiteData website) { 226 this.website = website; 227 } 228 229 234 public UserData getCreator() { 235 return this.creator; 236 } 237 238 239 public void setCreator(UserData creator) { 240 this.creator = creator; 241 } 242 243 248 public String getTitle() { 249 return this.title; 250 } 251 252 253 public void setTitle(String title) { 254 this.title = title; 255 } 256 257 263 public String getSummary() { 264 return summary; 265 } 266 267 271 public void setSummary(String summary) { 272 this.summary = summary; 273 } 274 275 281 public String getText() { 282 return this.text; 283 } 284 285 289 public void setText(String text) { 290 this.text = text; 291 } 292 293 299 public String getContentType() { 300 return contentType; 301 } 302 303 307 public void setContentType(String contentType) { 308 this.contentType = contentType; 309 } 310 311 317 public String getContentSrc() { 318 return contentSrc; 319 } 320 321 325 public void setContentSrc(String contentSrc) { 326 this.contentSrc = contentSrc; 327 } 328 329 334 public String getAnchor() { 335 return this.anchor; 336 } 337 338 339 public void setAnchor(String anchor) { 340 this.anchor = anchor; 341 } 342 343 353 public Set getEntryAttributes() { 354 return attSet; 355 } 356 357 public void setEntryAttributes(Set attSet) { 358 this.attSet = attSet; 359 360 if (attSet != null) { 362 this.attSet = attSet; 363 this.attMap = new HashMap (); 364 Iterator iter = this.attSet.iterator(); 365 while (iter.hasNext()) { 366 EntryAttributeData att = (EntryAttributeData)iter.next(); 367 attMap.put(att.getName(), att); 368 } 369 } else { 370 this.attSet = new TreeSet (); 371 this.attMap = new HashMap (); 372 } 373 } 374 375 376 381 public String findEntryAttribute(String name) { 382 EntryAttributeData att = ((EntryAttributeData)attMap.get(name)); 383 return (att != null) ? att.getValue() : null; 384 } 385 386 387 public void putEntryAttribute(String name, String value) throws Exception { 388 EntryAttributeData att = (EntryAttributeData)attMap.get(name); 389 if (att == null) { 390 att = new EntryAttributeData(); 391 att.setEntry(this); 392 att.setName(name); 393 att.setValue(value); 394 attMap.put(name, att); 395 attSet.add(att); 396 } else { 397 att.setValue(value); 398 } 399 } 400 public void removeEntryAttribute(String name) throws RollerException { 401 EntryAttributeData att = (EntryAttributeData)attMap.get(name); 402 if (att != null) { 403 attMap.remove(att); 404 attSet.remove(att); 405 } 406 } 407 409 424 public Timestamp getPubTime() { 425 return this.pubTime; 426 } 427 428 429 public void setPubTime(Timestamp pubTime) { 430 this.pubTime = pubTime; 431 } 432 433 448 public Timestamp getUpdateTime() { 449 return this.updateTime; 450 } 451 452 453 public void setUpdateTime(Timestamp updateTime) { 454 this.updateTime = updateTime; 455 } 456 457 462 public String getStatus() { 463 return this.status; 464 } 465 466 467 public void setStatus(String status) { 468 this.status = status; 469 } 470 471 479 public String getLink() { 480 return link; 481 } 482 483 487 public void setLink(String link) { 488 this.link = link; 489 } 490 491 498 public String getPlugins() { 499 return plugins; 500 } 501 502 503 public void setPlugins(String string) { 504 plugins = string; 505 } 506 507 508 515 public Boolean getAllowComments() { 516 return allowComments; 517 } 518 522 public void setAllowComments(Boolean allowComments) { 523 this.allowComments = allowComments; 524 } 525 526 533 public Integer getCommentDays() { 534 return commentDays; 535 } 536 540 public void setCommentDays(Integer commentDays) { 541 this.commentDays = commentDays; 542 } 543 544 551 public Boolean getRightToLeft() { 552 return rightToLeft; 553 } 554 558 public void setRightToLeft(Boolean rightToLeft) { 559 this.rightToLeft = rightToLeft; 560 } 561 562 570 public Boolean getPinnedToMain() { 571 return pinnedToMain; 572 } 573 579 public void setPinnedToMain(Boolean pinnedToMain) { 580 this.pinnedToMain = pinnedToMain; 581 } 582 583 584 591 public String getLocale() { 592 return locale; 593 } 594 595 596 public void setLocale(String locale) { 597 this.locale = locale; 598 } 599 600 601 603 610 public boolean getCommentsStillAllowed() { 611 if(DRAFT.equals(this.status) || PENDING.equals(this.status)) { 612 return false; 613 } 614 if (!RollerRuntimeConfig.getBooleanProperty("users.comments.enabled")) { 615 return false; 616 } 617 if (website.getAllowComments() != null && !website.getAllowComments().booleanValue()) { 618 return false; 619 } 620 if (getAllowComments() != null && !getAllowComments().booleanValue()) { 621 return false; 622 } 623 boolean ret = false; 624 if (getCommentDays() == null || getCommentDays().intValue() == 0) { 625 ret = true; 626 } else { 627 Calendar expireCal = Calendar.getInstance( 628 getWebsite().getLocaleInstance()); 629 expireCal.setTime(getPubTime()); 630 expireCal.add(Calendar.DATE, getCommentDays().intValue()); 631 Date expireDay = expireCal.getTime(); 632 Date today = new Date (); 633 if (today.before(expireDay)) { 634 ret = true; 635 } 636 } 637 return ret; 638 } 639 public void setCommentsStillAllowed(boolean ignored) { 640 } 642 643 644 646 654 public String formatPubTime(String pattern) { 655 try { 656 SimpleDateFormat format = new SimpleDateFormat (pattern, 657 this.getWebsite().getLocaleInstance()); 658 659 return format.format(getPubTime()); 660 } catch (RuntimeException e) { 661 mLogger.error("Unexpected exception", e); 662 } 663 664 return "ERROR: formatting date"; 665 } 666 667 669 677 public String formatUpdateTime(String pattern) { 678 try { 679 SimpleDateFormat format = new SimpleDateFormat (pattern); 680 681 return format.format(getUpdateTime()); 682 } catch (RuntimeException e) { 683 mLogger.error("Unexpected exception", e); 684 } 685 686 return "ERROR: formatting date"; 687 } 688 689 691 694 public List getComments() { 695 return getComments(true, true); 696 } 697 698 701 public List getComments(boolean ignoreSpam, boolean approvedOnly) { 702 List list = new ArrayList (); 703 try { 704 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 705 return wmgr.getComments( 706 getWebsite(), 707 this, 708 null, null, null, null, approvedOnly ? Boolean.TRUE : null, ignoreSpam ? Boolean.FALSE : null, false, 0, -1); } catch (RollerException alreadyLogged) {} 718 return list; 719 } 720 721 724 public int getCommentCount() { 725 List comments = getComments(true, true); 726 return comments.size(); 727 } 728 729 730 public void setCommentCount(int ignored) { 731 } 733 734 736 739 public List getReferers() { 740 List referers = null; 741 try { 742 referers = RollerFactory.getRoller().getRefererManager().getReferersToEntry(getId()); 743 } catch (RollerException e) { 744 mLogger.error("Unexpected exception", e); 745 } 746 return referers; 747 } 748 749 751 755 public String getPermalink() { 756 return URLUtilities.getWeblogEntryURL(getWebsite(), null, anchor, true); 757 } 758 759 764 public String getPermaLink() { 765 String lAnchor = this.getAnchor(); 766 try { 767 lAnchor = URLEncoder.encode(anchor, "UTF-8"); 768 } catch (UnsupportedEncodingException e) { 769 } 771 WebsiteData website = this.getWebsite(); 772 return "/" + getWebsite().getHandle() + "/entry/" + lAnchor; 773 } 774 775 780 public String getCommentsLink() { 781 return getPermaLink() + "#comments"; 782 } 783 784 788 public void setCommentsLink(String ignored) {} 789 790 791 798 public String getDisplayTitle() { 799 if ( getTitle()==null || getTitle().trim().equals("") ) { 800 return StringUtils.left(Utilities.removeHTML(text),255); 801 } 802 return Utilities.removeHTML(getTitle()); 803 } 804 805 807 public String toString() { 808 StringBuffer str = new StringBuffer ("{"); 809 810 str.append("id=" + id + " " + 811 "category=" + category + " " + 812 "title=" + title + " " + 813 "text=" + text + " " + 814 "anchor=" + anchor + " " + 815 "pubTime=" + pubTime + " " + 816 "updateTime=" + updateTime + " " + 817 "status=" + status + " " + 818 "plugins=" + plugins); 819 str.append('}'); 820 821 return (str.toString()); 822 } 823 824 826 public boolean equals(Object pOther) { 827 if (pOther instanceof WeblogEntryData) { 828 WeblogEntryData lTest = (WeblogEntryData) pOther; 829 boolean lEquals = true; 830 831 if (this.id == null) { 832 lEquals = lEquals && (lTest.getId() == null); 833 } else { 834 lEquals = lEquals && this.id.equals(lTest.getId()); 835 } 836 837 if (this.category == null) { 838 lEquals = lEquals && (lTest.getCategory() == null); 839 } else { 840 lEquals = lEquals && this.category.equals(lTest.getCategory()); 841 } 842 843 if (this.website == null) { 844 lEquals = lEquals && (lTest.getWebsite() == null); 845 } else { 846 lEquals = lEquals && this.website.equals(lTest.getWebsite()); 847 } 848 849 if (this.title == null) { 850 lEquals = lEquals && (lTest.getTitle() == null); 851 } else { 852 lEquals = lEquals && this.title.equals(lTest.getTitle()); 853 } 854 855 if (this.text == null) { 856 lEquals = lEquals && (lTest.getText() == null); 857 } else { 858 lEquals = lEquals && this.text.equals(lTest.getText()); 859 } 860 861 if (this.anchor == null) { 862 lEquals = lEquals && (lTest.getAnchor() == null); 863 } else { 864 lEquals = lEquals && this.anchor.equals(lTest.getAnchor()); 865 } 866 867 if (this.pubTime == null) { 868 lEquals = lEquals && (lTest.getPubTime() == null); 869 } else { 870 lEquals = lEquals && this.pubTime.equals(lTest.getPubTime()); 871 } 872 873 if (this.updateTime == null) { 874 lEquals = lEquals && (lTest.getUpdateTime() == null); 875 } else { 876 lEquals = lEquals && 877 this.updateTime.equals(lTest.getUpdateTime()); 878 } 879 880 if (this.status == null) { 881 lEquals = lEquals && (lTest.getStatus() == null); 882 } else { 883 lEquals = lEquals && 884 this.status.equals(lTest.getStatus()); 885 } 886 887 if (this.plugins == null) { 888 lEquals = lEquals && (lTest.getPlugins() == null); 889 } else { 890 lEquals = lEquals && 891 this.plugins.equals(lTest.getPlugins()); 892 } 893 894 895 return lEquals; 896 } else { 897 return false; 898 } 899 } 900 901 903 public int hashCode() { 904 int result = 17; 905 result = (37 * result) + 906 ((this.id != null) ? this.id.hashCode() : 0); 907 result = (37 * result) + 908 ((this.category != null) ? this.category.hashCode() : 0); 909 result = (37 * result) + 910 ((this.website != null) ? this.website.hashCode() : 0); 911 result = (37 * result) + 912 ((this.title != null) ? this.title.hashCode() : 0); 913 result = (37 * result) + 914 ((this.text != null) ? this.text.hashCode() : 0); 915 result = (37 * result) + 916 ((this.anchor != null) ? this.anchor.hashCode() : 0); 917 result = (37 * result) + 918 ((this.pubTime != null) ? this.pubTime.hashCode() : 0); 919 result = (37 * result) + 920 ((this.updateTime != null) ? this.updateTime.hashCode() : 0); 921 result = (37 * result) + 922 ((this.status != null) ? this.status.hashCode() : 0); 923 result = (37 * result) + 924 ((this.plugins != null) ? this.plugins.hashCode() : 0); 925 926 return result; 927 } 928 929 934 public String getRss09xDescription() { 935 return getRss09xDescription(-1); 936 } 937 938 943 public String getRss09xDescription(int maxLength) { 944 String ret = StringEscapeUtils.escapeHtml(text); 945 if (maxLength != -1 && ret.length() > maxLength) { 946 ret = ret.substring(0,maxLength-3)+"..."; 947 } 948 return ret; 949 } 950 951 952 protected String createAnchor() throws RollerException { 953 return RollerFactory.getRoller().getWeblogManager().createAnchor(this); 954 } 955 956 957 public String createAnchorBase() { 958 959 String base = getTitle(); 961 if (base != null) { 962 base = Utilities.replaceNonAlphanumeric(base, ' '); 963 if (StringUtils.isEmpty(base.trim())) { 964 base = getText(); 965 if (base != null) { 966 base = Utilities.replaceNonAlphanumeric(base, ' '); 967 } 968 } 969 } 970 971 if (StringUtils.isEmpty(base.trim())) { 972 973 StringTokenizer toker = new StringTokenizer (base); 975 String tmp = null; 976 int count = 0; 977 while (toker.hasMoreTokens() && count < 5) { 978 String s = toker.nextToken(); 979 s = s.toLowerCase(); 980 tmp = (tmp == null) ? s : tmp + "_" + s; 981 count++; 982 } 983 base = tmp; 984 } 985 else { 988 base = DateUtil.format8chars(getPubTime()); 989 } 990 991 return base; 992 } 993 994 997 public void setPermalink(String string) {} 998 999 1002 public void setPermaLink(String string) {} 1003 1004 1009 public void setDisplayTitle(String string) { 1010 } 1011 1012 1017 public void setRss09xDescription(String string) { 1018 } 1019 1020 1021 1027 public List getPluginsList() { 1028 if (plugins != null) { 1029 return Arrays.asList( StringUtils.split(plugins, ",") ); 1030 } 1031 return new ArrayList (); 1032 } 1033 1034 1038 public void setCreatorId(String creatorId) throws RollerException { 1039 UserManager umgr = RollerFactory.getRoller().getUserManager(); 1040 setCreator(umgr.getUser(creatorId)); 1041 } 1042 1043 1044 public boolean isDraft() { 1045 return status.equals(DRAFT); 1046 } 1047 1048 public void setDraft(boolean value) { 1049 } 1050 1051 1052 public boolean isPending() { 1053 return status.equals(PENDING); 1054 } 1055 1056 public void setPending(boolean value) { 1057 } 1058 1059 1060 public boolean isPublished() { 1061 return status.equals(PUBLISHED); 1062 } 1063 1064 public void setPublished(boolean value) { 1065 } 1066 1067 1071 public String getTransformedText() { 1072 return render(text); 1073 } 1074 1077 public void setTransformedText(String t) { 1078 } 1080 1081 1085 public String getTransformedSummary() { 1086 return render(summary); 1087 } 1088 1091 public void setTransformedSummary(String t) { 1092 } 1094 1095 1098 public boolean hasWritePermissions(UserData user) throws RollerException { 1099 1100 if(user.hasRole("admin")) { 1102 return true; 1103 } 1104 1105 boolean author = getWebsite().hasUserPermissions( 1106 user, (short)(PermissionsData.AUTHOR)); 1107 boolean limited = getWebsite().hasUserPermissions( 1108 user, (short)(PermissionsData.LIMITED)); 1109 1110 if (author || (limited && isDraft()) || (limited && isPending())) { 1111 return true; 1112 } 1113 1114 return false; 1115 } 1116 1117 1120 private String render(String str) { 1121 String ret = str; 1122 mLogger.debug("Applying page plugins to string"); 1123 Map plugins = this.website.getInitializedPlugins(); 1124 if (str != null && plugins != null) { 1125 List entryPlugins = getPluginsList(); 1126 1127 if (entryPlugins != null && !entryPlugins.isEmpty()) { 1129 1130 Iterator iter = plugins.keySet().iterator(); 1134 while (iter.hasNext()) { 1135 String key = (String )iter.next(); 1136 if (entryPlugins.contains(key)) { 1137 WeblogEntryPlugin pagePlugin = (WeblogEntryPlugin)plugins.get(key); 1138 try { 1139 ret = pagePlugin.render(this, ret); 1140 } catch (Throwable t) { 1141 mLogger.error("ERROR from plugin: " + pagePlugin.getName(), t); 1142 } 1143 } 1144 } 1145 } 1146 } 1147 return ret; 1148 } 1149 1150 1151 1161 public String displayContent(String readMoreLink) { 1162 1163 String displayContent = null; 1164 1165 if(readMoreLink == null || readMoreLink.trim().length() < 1 || 1166 "nil".equals(readMoreLink)) { 1167 1168 if(StringUtils.isNotEmpty(this.getText())) { 1170 displayContent = this.getTransformedText(); 1171 } else { 1172 displayContent = this.getTransformedSummary(); 1173 } 1174 } else { 1175 if(StringUtils.isNotEmpty(this.getSummary())) { 1178 displayContent = this.getTransformedSummary(); 1179 if(StringUtils.isNotEmpty(this.getText())) { 1180 List args = new ArrayList (); 1182 args.add(readMoreLink); 1183 String readMore = MessageUtilities.getString("macro.weblog.readMoreLink", args); 1184 1185 displayContent += readMore; 1186 } 1187 } else { 1188 displayContent = this.getTransformedText(); 1189 } 1190 } 1191 1192 return displayContent; 1193 } 1194 1195 1196 1201 public String getDisplayContent() { 1202 return displayContent(null); 1203 } 1204 1205 1206 1207 public void setDisplayContent(String ignored) {} 1208 1209} 1210 | Popular Tags |