001: /*
002: * Created on Feb 18, 2005
003: */
004: package com.openedit.blog;
005:
006: import java.util.ArrayList;
007: import java.util.Collections;
008: import java.util.Date;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.Random;
012:
013: import org.apache.commons.collections.map.ReferenceMap;
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016: import org.openedit.blog.archive.EntryArchive;
017: import org.openedit.links.Link;
018: import org.openedit.links.LinkTree;
019:
020: import com.openedit.BaseWebPageRequest;
021: import com.openedit.OpenEditException;
022: import com.openedit.page.Page;
023: import com.openedit.page.manage.PageManager;
024: import com.openedit.users.User;
025: import com.openedit.users.UserManager;
026: import com.openedit.util.strainer.Filter;
027:
028: /**
029: * @author cburkey
030: */
031: public class Blog {
032: protected PageManager fieldPageManager;
033: protected LinkTree fieldLinkTree;
034: protected String fieldBlogHome;
035: protected String fieldArchiveRootDirectory;
036: protected String fieldHostName;
037: protected String fieldDescription;
038: protected String fieldTitle;
039:
040: protected String fieldAuthor;
041: protected int fieldRecentMaxCount = 5;
042: protected String fieldPermission;
043: protected boolean fieldAllowAnonymous;
044: protected boolean fieldAutoPublishingComments;
045: protected boolean fieldAutoPublishEntries;
046: protected boolean fieldIsUsingNotification;
047:
048: protected Map fieldCache;
049: protected UserManager fieldUserManager;
050: protected EntryArchive fieldEntryArchive;
051: protected Date fieldLastModified;
052:
053: Random fieldRandom;
054:
055: private static final Log log = LogFactory.getLog(Blog.class);
056:
057: public List getRecentEntries() throws OpenEditException {
058: return getRecentEntries(getRecentMaxCount());
059: }
060:
061: public List getVisibleEntries(int start, int end)
062: throws OpenEditException {
063: return getEntries(start, end, true, false);
064: }
065:
066: /**
067: * Returns the given number of most recent entries.
068: *
069: * @param inCount The number of entries to return
070: *
071: * @return A {@link List} of {@link BlogEntry}s
072: */
073: public List getRecentEntries(int inCount) throws OpenEditException {
074: return getEntries(0, inCount, false, false);
075: }
076:
077: public List getRecentVisibleEntries(int inI)
078: throws OpenEditException {
079: return getEntries(0, inI, true, false);
080: }
081:
082: public List getRandomRecentEntries(int inCount)
083: throws OpenEditException {
084: return getEntries(0, inCount, false, true);
085: }
086:
087: public List getRandomRecentVisibleEntries(int inI)
088: throws OpenEditException {
089: return getEntries(0, inI, true, true);
090: }
091:
092: public List getEntries(int start, int end, boolean visibleOnly,
093: boolean sortRandom) throws OpenEditException {
094: int num;
095: if (getLinkTree().getRootLink() == null) {
096: return Collections.EMPTY_LIST;
097: }
098: // added "new ArrayList" because when i call more than once without it, links get empty
099: List links = new ArrayList(getLinkTree().getRootLink()
100: .getChildren());
101: start = Math.min(start, links.size());
102: if (end > links.size()) {
103: end = links.size();
104: }
105: if (end == 0) //end == 0 means get all entries
106: {
107: end = links.size();
108: }
109: //read in the links xml file and take off the top entries
110: //then for each link go get the entry from the xconf file
111: List entries = new ArrayList();
112: for (int i = start; i < end; i++) {
113: if (sortRandom) {
114: num = getRandom().nextInt(links.size());
115: } else {
116: num = i;
117: }
118: Link link = (Link) links.get(num);
119: BlogEntry entry = getEntry(link.getUrl());
120: if (entry != null && (!visibleOnly || entry.isVisible())) {
121: entries.add(entry);
122: if (sortRandom) {
123: links.remove(num);
124: }
125: } else {
126: if (end < links.size()) {
127: end++;
128: }
129: }
130: }
131: return entries;
132:
133: }
134:
135: public Random getRandom() {
136: if (fieldRandom == null) {
137: fieldRandom = new Random();
138: }
139: return fieldRandom;
140: }
141:
142: public void setRandom(Random inRandom) {
143: fieldRandom = inRandom;
144: }
145:
146: public void removeEntry(BlogEntry inEntry) throws OpenEditException {
147: Link link = getLinkTree().getLink(inEntry.getId());
148: getLinkTree().removeLink(link);
149: }
150:
151: public List sortByRank(List inEntries) {
152: return inEntries;
153: }
154:
155: public LinkTree getLinkTree() {
156: return fieldLinkTree;
157: }
158:
159: public void setLinkTree(LinkTree inLinkTree) {
160: fieldLinkTree = inLinkTree;
161: }
162:
163: public PageManager getPageManager() {
164: return fieldPageManager;
165: }
166:
167: public void setPageManager(PageManager inPageManager) {
168: fieldPageManager = inPageManager;
169: }
170:
171: public String getLink() {
172: return getBlogHome();
173: }
174:
175: public String getBlogHome() {
176: return fieldBlogHome;
177: }
178:
179: public void setBlogHome(String inBlogRoot) {
180: fieldBlogHome = inBlogRoot;
181: }
182:
183: public String getHostName() {
184: return fieldHostName;
185: }
186:
187: public void setHostName(String inHostName) {
188: fieldHostName = inHostName;
189: }
190:
191: public String getDescription() {
192: return fieldDescription;
193: }
194:
195: public void setDescription(String inDescription) {
196: fieldDescription = inDescription;
197: }
198:
199: public String getTitle() {
200: return fieldTitle;
201: }
202:
203: public void setTitle(String inTitle) {
204: fieldTitle = inTitle;
205: }
206:
207: public int getRecentMaxCount() {
208: return fieldRecentMaxCount;
209: }
210:
211: public void setRecentMaxCount(int inRecentMaxCount) {
212: fieldRecentMaxCount = inRecentMaxCount;
213: }
214:
215: /**
216: * @param inEntryId
217: * @return
218: */
219: public BlogEntry getEntry(String inPath) throws OpenEditException {
220: Page page = getPageManager().getPage(inPath);
221: if (!page.exists()) {
222: //strip off the ending and look in the archive root
223: if (inPath.startsWith(getArchiveRootDirectory())) {
224: String ending = inPath.substring(
225: getArchiveRootDirectory().length(), inPath
226: .length());
227: //entry.setPath(ending);
228: inPath = ending;
229: page = getPageManager().getPage(inPath);
230: }
231: }
232:
233: BlogEntry entry = (BlogEntry) getCache().get(inPath);
234: if (entry == null) {
235: entry = getEntryArchive().createEntry(this , page);
236: getCache().put(inPath, entry);
237: }
238: return entry;
239: }
240:
241: public BlogEntry createNewEntry(User inUser)
242: throws OpenEditException {
243: return getEntryArchive().createEntry(this , inUser);
244: }
245:
246: public boolean canEdit(User inUser) throws OpenEditException {
247: if (inUser == null) {
248: return false;
249: }
250: String slink = getBlogHome() + "/permalinks.xml";
251:
252: Page linkpage = getPageManager().getPage(slink);
253: Filter filter = linkpage.getEditFilter();
254: BaseWebPageRequest req = new BaseWebPageRequest();
255: req.setUser(inUser);
256: req.setContentPage(linkpage);
257: req.setPage(linkpage);
258: boolean value = ((filter == null) || filter.passes(req));
259:
260: return value;
261: }
262:
263: public String getAuthor() {
264: return fieldAuthor;
265: }
266:
267: public void setAuthor(String inAuthor) {
268: fieldAuthor = inAuthor;
269: }
270:
271: public String getEditPermission() {
272: return fieldPermission;
273: }
274:
275: public void setEditPermission(String inEditors) {
276: fieldPermission = inEditors;
277: }
278:
279: public boolean getAllowAnonymous() {
280: return fieldAllowAnonymous;
281: }
282:
283: public void setAllowAnonymous(boolean inAllowAnonymous) {
284: fieldAllowAnonymous = inAllowAnonymous;
285: }
286:
287: public boolean isAutoPublishingComments() {
288: return fieldAutoPublishingComments;
289: }
290:
291: public void setAutoPublishingComments(
292: boolean inAutoPublishingComments) {
293: fieldAutoPublishingComments = inAutoPublishingComments;
294: }
295:
296: public Comment createNewComment(User inAuthor, String inContent) {
297: Comment comment = new Comment();
298: comment.setAuthor(inAuthor.getShortDescription());
299: comment.setUser(inAuthor);
300: comment.setContent(inContent);
301: comment.setDateTime(new Date());
302: comment.setId(String.valueOf(System.currentTimeMillis()));
303: comment.setVisible(isAutoPublishingComments());
304: return comment;
305: }
306:
307: public boolean isAutoPublishEntries() {
308: return fieldAutoPublishEntries;
309: }
310:
311: public void setAutoPublishEntries(boolean inAutoPublishEntries) {
312: fieldAutoPublishEntries = inAutoPublishEntries;
313: }
314:
315: public String getArchiveRootDirectory() {
316: return fieldArchiveRootDirectory;
317: }
318:
319: public void setArchiveRootDirectory(String inArchiveRootDirectory) {
320: fieldArchiveRootDirectory = inArchiveRootDirectory;
321: }
322:
323: public Map getCache() {
324: if (fieldCache == null) {
325: fieldCache = new ReferenceMap(ReferenceMap.HARD,
326: ReferenceMap.HARD);
327: }
328: return fieldCache;
329: }
330:
331: public void setCache(Map inCache) {
332: fieldCache = inCache;
333: }
334:
335: public UserManager getUserManager() {
336: return fieldUserManager;
337: }
338:
339: public void setUserManager(UserManager inUserManager) {
340: fieldUserManager = inUserManager;
341: }
342:
343: public boolean isUseNotification() {
344: return fieldIsUsingNotification;
345: }
346:
347: public void setUseNotification(boolean inBol) {
348: fieldIsUsingNotification = inBol;
349: }
350:
351: public EntryArchive getEntryArchive() {
352: return fieldEntryArchive;
353: }
354:
355: public void setEntryArchive(EntryArchive inEntryArchive) {
356: fieldEntryArchive = inEntryArchive;
357: }
358:
359: public Date getLastModified() {
360: return fieldLastModified;
361: }
362:
363: public void setLastModified(Date inLastModified) {
364: fieldLastModified = inLastModified;
365: }
366: }
|