1 4 package com.openedit.blog; 5 6 import java.util.ArrayList ; 7 import java.util.Collections ; 8 import java.util.Date ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 13 import org.apache.commons.collections.map.ReferenceMap; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.openedit.blog.archive.EntryArchive; 17 import org.openedit.links.Link; 18 import org.openedit.links.LinkTree; 19 20 import com.openedit.BaseWebPageRequest; 21 import com.openedit.OpenEditException; 22 import com.openedit.page.Page; 23 import com.openedit.page.manage.PageManager; 24 import com.openedit.users.User; 25 import com.openedit.users.UserManager; 26 import com.openedit.util.strainer.Filter; 27 28 31 public class Blog 32 { 33 protected PageManager fieldPageManager; 34 protected LinkTree fieldLinkTree; 35 protected String fieldBlogHome; 36 protected String fieldArchiveRootDirectory; 37 protected String fieldHostName; 38 protected String fieldDescription; 39 protected String fieldTitle; 40 protected String fieldAuthor; 41 protected int fieldRecentMaxCount = 5; 42 protected String fieldPermission; 43 protected boolean fieldAllowAnonymous; 44 protected boolean fieldAutoPublishingComments; 45 protected boolean fieldAutoPublishEntries; 46 protected Map fieldCache; 47 protected UserManager fieldUserManager; 48 protected List fieldQuestions; 49 protected boolean fieldIsUsingNotification; 50 protected EntryArchive fieldEntryArchive; 51 52 private static final Log log = LogFactory.getLog(Blog.class); 53 54 public List getRecentEntries() throws OpenEditException 55 { 56 return getRecentEntries(getRecentMaxCount()); 57 } 58 public List getVisibleEntries(int start, int end) throws OpenEditException 59 { 60 return getEntries(start, end, true); 61 } 62 public List getEntries(int start, int end, boolean visibleOnly ) throws OpenEditException 63 { 64 if( getLinkTree().getRootLink() == null) 65 { 66 return Collections.EMPTY_LIST; 67 } 68 List links = getLinkTree().getRootLink().getChildren(); 69 start = Math.min(start,links.size()); 70 if ( end > links.size()) 71 { 72 end = links.size(); 73 } 74 if ( end == 0 ) { 76 end = links.size(); 77 } 78 List entries = new ArrayList (); 81 for (int i = start; i < end; i++) 82 { 83 Link link = (Link)links.get(i); 84 BlogEntry entry = getEntry(link.getUrl()); 85 if ( entry != null && (!visibleOnly || entry.isVisible())) 86 { 87 entries.add(entry); 88 } 89 else 90 { 91 if ( end < links.size()) 92 { 93 end++; 94 } 95 } 96 } 97 return entries; 98 99 } 100 public void removeEntry(BlogEntry inEntry) throws OpenEditException 101 { 102 Link link = getLinkTree().getLink(inEntry.getId()); 103 getLinkTree().removeLink(link); 104 } 105 106 113 public List getRecentEntries(int inCount) throws OpenEditException 114 { 115 return getEntries(0,inCount, false); 116 } 117 118 public List getRecentVisibleEntries(int inI) throws OpenEditException 119 { 120 return getEntries(0,inI, true); 121 } 122 123 124 public LinkTree getLinkTree() 125 { 126 return fieldLinkTree; 127 } 128 public void setLinkTree(LinkTree inLinkTree) 129 { 130 fieldLinkTree = inLinkTree; 131 } 132 public PageManager getPageManager() 133 { 134 return fieldPageManager; 135 } 136 public void setPageManager(PageManager inPageManager) 137 { 138 fieldPageManager = inPageManager; 139 } 140 public String getLink() 141 { 142 return getBlogHome(); 143 } 144 145 public String getBlogHome() 146 { 147 return fieldBlogHome; 148 } 149 public void setBlogHome(String inBlogRoot) 150 { 151 fieldBlogHome = inBlogRoot; 152 } 153 public String getHostName() 154 { 155 return fieldHostName; 156 } 157 public void setHostName(String inHostName) 158 { 159 fieldHostName = inHostName; 160 } 161 162 public String getDescription() 163 { 164 return fieldDescription; 165 } 166 public void setDescription(String inDescription) 167 { 168 fieldDescription = inDescription; 169 } 170 public String getTitle() 171 { 172 return fieldTitle; 173 } 174 public void setTitle(String inTitle) 175 { 176 fieldTitle = inTitle; 177 } 178 public int getRecentMaxCount() 179 { 180 return fieldRecentMaxCount; 181 } 182 public void setRecentMaxCount(int inRecentMaxCount) 183 { 184 fieldRecentMaxCount = inRecentMaxCount; 185 } 186 187 191 public BlogEntry getEntry(String inPath) throws OpenEditException 192 { 193 Page page = getPageManager().getPage(inPath); 194 if( !page.exists()) 195 { 196 if( inPath.startsWith(getArchiveRootDirectory())) 198 { 199 String ending = inPath.substring(getArchiveRootDirectory().length(),inPath.length()); 200 inPath = ending; 202 page = getPageManager().getPage(inPath); 203 } 204 } 205 206 BlogEntry entry = (BlogEntry)getCache().get(inPath); 207 if( entry == null) 208 { 209 entry = getEntryArchive().createEntry(this, page); 210 getCache().put( inPath, entry); 211 } 212 return entry; 213 } 214 215 public BlogEntry createNewEntry(User inUser) throws OpenEditException 216 { 217 return getEntryArchive().createEntry(this, inUser); 218 } 219 220 221 public boolean canEdit(User inUser) throws OpenEditException 222 { 223 if( inUser == null) 224 { 225 return false; 226 } 227 String slink = getBlogHome() + "/links.xml"; 228 229 Page linkpage = getPageManager().getPage(slink); 230 Filter filter = linkpage.getEditFilter(); 231 BaseWebPageRequest req = new BaseWebPageRequest(); 232 req.setUser(inUser); 233 req.setContentPage(linkpage); 234 req.setPage(linkpage); 235 boolean value= ((filter == null) || filter.passes( req )); 236 237 return value; 238 } 239 240 public String getAuthor() 241 { 242 return fieldAuthor; 243 } 244 public void setAuthor(String inAuthor) 245 { 246 fieldAuthor = inAuthor; 247 } 248 public String getEditPermission() 249 { 250 return fieldPermission; 251 } 252 public void setEditPermission(String inEditors) 253 { 254 fieldPermission = inEditors; 255 } 256 259 public boolean hasChanged() 260 { 261 return false; 262 } 263 public boolean getAllowAnonymous() 264 { 265 return fieldAllowAnonymous; 266 } 267 public void setAllowAnonymous(boolean inAllowAnonymous) 268 { 269 fieldAllowAnonymous = inAllowAnonymous; 270 } 271 public boolean isAutoPublishingComments() 272 { 273 return fieldAutoPublishingComments; 274 } 275 public void setAutoPublishingComments(boolean inAutoPublishingComments) 276 { 277 fieldAutoPublishingComments = inAutoPublishingComments; 278 } 279 280 public Comment createNewComment(User inAuthor, String inContent) 281 { 282 Comment comment = new Comment(); 283 comment.setAuthor(inAuthor.getShortDescription()); 284 comment.setUser(inAuthor); 285 comment.setContent(inContent); 286 comment.setDateTime(new Date ()); 287 comment.setId(String.valueOf(System.currentTimeMillis())); 288 comment.setVisible(isAutoPublishingComments()); 289 return comment; 290 } 291 public List getQuestions() 292 { 293 return fieldQuestions; 294 } 295 public void setQuestions(List inQuestions) 296 { 297 fieldQuestions = inQuestions; 298 } 299 public Question getRandomQuestion() 300 { 301 double d = Math.random(); 302 int hit = (int)Math.round(d * ((double)getQuestions().size()-1D) ); 303 return (Question)getQuestions().get(hit); 304 } 305 public Question getQuestion(String inId) 306 { 307 for (Iterator iter = getQuestions().iterator(); iter.hasNext();) 308 { 309 Question q = (Question) iter.next(); 310 if ( q.getId().equals(inId) ) 311 { 312 return q; 313 } 314 } 315 return null; 316 } 317 public boolean isAutoPublishEntries() 318 { 319 return fieldAutoPublishEntries; 320 } 321 public void setAutoPublishEntries(boolean inAutoPublishEntries) 322 { 323 fieldAutoPublishEntries = inAutoPublishEntries; 324 } 325 public String getArchiveRootDirectory() 326 { 327 return fieldArchiveRootDirectory; 328 } 329 public void setArchiveRootDirectory(String inArchiveRootDirectory) 330 { 331 fieldArchiveRootDirectory = inArchiveRootDirectory; 332 } 333 public Map getCache() 334 { 335 if( fieldCache == null) 336 { 337 fieldCache = new ReferenceMap(ReferenceMap.HARD,ReferenceMap.HARD); 338 } 339 return fieldCache; 340 } 341 public void setCache(Map inCache) 342 { 343 fieldCache = inCache; 344 } 345 public UserManager getUserManager() 346 { 347 return fieldUserManager; 348 } 349 public void setUserManager(UserManager inUserManager) 350 { 351 fieldUserManager = inUserManager; 352 } 353 public boolean isUseNotification() 354 { 355 return fieldIsUsingNotification; 356 } 357 public void setUseNotification( boolean inBol) 358 { 359 fieldIsUsingNotification = inBol; 360 } 361 public EntryArchive getEntryArchive() 362 { 363 return fieldEntryArchive; 364 } 365 public void setEntryArchive(EntryArchive inEntryArchive) 366 { 367 fieldEntryArchive = inEntryArchive; 368 } 369 } 370 | Popular Tags |