KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > blog > Blog


1 /*
2  * Created on Feb 18, 2005
3  */

4 package com.openedit.blog;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
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 /**
29  * @author cburkey
30  */

31 public class Blog
32 {
33     protected PageManager fieldPageManager;
34     protected LinkTree fieldLinkTree;
35     protected String JavaDoc fieldBlogHome;
36     protected String JavaDoc fieldArchiveRootDirectory;
37     protected String JavaDoc fieldHostName;
38     protected String JavaDoc fieldDescription;
39     protected String JavaDoc fieldTitle;
40     protected String JavaDoc fieldAuthor;
41     protected int fieldRecentMaxCount = 5;
42     protected String JavaDoc fieldPermission;
43     protected boolean fieldAllowAnonymous;
44     protected boolean fieldAutoPublishingComments;
45     protected boolean fieldAutoPublishEntries;
46     protected Map JavaDoc fieldCache;
47     protected UserManager fieldUserManager;
48     protected List JavaDoc fieldQuestions;
49     protected boolean fieldIsUsingNotification;
50     protected EntryArchive fieldEntryArchive;
51     
52     private static final Log log = LogFactory.getLog(Blog.class);
53
54     public List JavaDoc getRecentEntries() throws OpenEditException
55     {
56         return getRecentEntries(getRecentMaxCount());
57     }
58     public List JavaDoc getVisibleEntries(int start, int end) throws OpenEditException
59     {
60         return getEntries(start, end, true);
61     }
62     public List JavaDoc getEntries(int start, int end, boolean visibleOnly ) throws OpenEditException
63     {
64         if( getLinkTree().getRootLink() == null)
65         {
66             return Collections.EMPTY_LIST;
67         }
68         List JavaDoc 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 ) //end == 0 means get all entries
75
{
76             end = links.size();
77         }
78         //read in the links xml file and take off the top entries
79
//then for each link go get the entry from the xconf file
80
List JavaDoc entries = new ArrayList JavaDoc();
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     /**
107      * Returns the given number of most recent entries.
108      *
109      * @param inCount The number of entries to return
110      *
111      * @return A {@link List} of {@link BlogEntry}s
112      */

113     public List JavaDoc getRecentEntries(int inCount) throws OpenEditException
114     {
115         return getEntries(0,inCount, false);
116     }
117     
118     public List JavaDoc 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 JavaDoc getLink()
141     {
142         return getBlogHome();
143     }
144
145     public String JavaDoc getBlogHome()
146     {
147         return fieldBlogHome;
148     }
149     public void setBlogHome(String JavaDoc inBlogRoot)
150     {
151         fieldBlogHome = inBlogRoot;
152     }
153     public String JavaDoc getHostName()
154     {
155         return fieldHostName;
156     }
157     public void setHostName(String JavaDoc inHostName)
158     {
159         fieldHostName = inHostName;
160     }
161
162     public String JavaDoc getDescription()
163     {
164         return fieldDescription;
165     }
166     public void setDescription(String JavaDoc inDescription)
167     {
168         fieldDescription = inDescription;
169     }
170     public String JavaDoc getTitle()
171     {
172         return fieldTitle;
173     }
174     public void setTitle(String JavaDoc 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     /**
188      * @param inEntryId
189      * @return
190      */

191     public BlogEntry getEntry(String JavaDoc inPath) throws OpenEditException
192     {
193         Page page = getPageManager().getPage(inPath);
194         if( !page.exists())
195         {
196             //strip off the ending and look in the archive root
197
if( inPath.startsWith(getArchiveRootDirectory()))
198             {
199                 String JavaDoc ending = inPath.substring(getArchiveRootDirectory().length(),inPath.length());
200                 //entry.setPath(ending);
201
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 JavaDoc 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 JavaDoc getAuthor()
241     {
242         return fieldAuthor;
243     }
244     public void setAuthor(String JavaDoc inAuthor)
245     {
246         fieldAuthor = inAuthor;
247     }
248     public String JavaDoc getEditPermission()
249     {
250         return fieldPermission;
251     }
252     public void setEditPermission(String JavaDoc inEditors)
253     {
254         fieldPermission = inEditors;
255     }
256     /**
257      * @return
258      */

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 JavaDoc 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 JavaDoc());
287         comment.setId(String.valueOf(System.currentTimeMillis()));
288         comment.setVisible(isAutoPublishingComments());
289         return comment;
290     }
291     public List JavaDoc getQuestions()
292     {
293         return fieldQuestions;
294     }
295     public void setQuestions(List JavaDoc 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 JavaDoc inId)
306     {
307         for (Iterator JavaDoc 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 JavaDoc getArchiveRootDirectory()
326     {
327         return fieldArchiveRootDirectory;
328     }
329     public void setArchiveRootDirectory(String JavaDoc inArchiveRootDirectory)
330     {
331         fieldArchiveRootDirectory = inArchiveRootDirectory;
332     }
333     public Map JavaDoc 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 JavaDoc 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