KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > tags > ViewWeblogEntriesTag


1 package org.roller.presentation.weblog.tags;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.apache.velocity.Template;
6 import org.apache.velocity.VelocityContext;
7 import org.apache.velocity.runtime.RuntimeSingleton;
8 import org.roller.model.WeblogManager;
9 import org.roller.pojos.PageData;
10 import org.roller.pojos.WebsiteData;
11 import org.roller.presentation.RollerRequest;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.tagext.Tag JavaDoc;
22
23
24 //////////////////////////////////////////////////////////////////////////////
25
/**
26  * @jsp.tag name="ViewWeblogEntries"
27  */

28 public class ViewWeblogEntriesTag
29     extends org.roller.presentation.tags.HybridTag
30 {
31     private static Log mLogger =
32         LogFactory.getFactory().getInstance(RollerRequest.class);
33
34     /** @jsp.attribute */
35     public String JavaDoc getDayTemplate() { return mDayTemplate; }
36     public void setDayTemplate( String JavaDoc n ) { mDayTemplate = n; }
37     private String JavaDoc mDayTemplate = null;
38
39     /** @jsp.attribute */
40     public String JavaDoc getCatName() { return mCatName; }
41     public void setCatName( String JavaDoc n ) { mCatName = n; }
42     private String JavaDoc mCatName = null;
43
44     /** @jsp.attribute */
45     public int getMaxEntries() { return mMaxEntries; }
46     public void setMaxEntries( int v ) { mMaxEntries = v; }
47     private int mMaxEntries = -1;
48
49     //------------------------------------------------------------------------
50

51     public String JavaDoc view( String JavaDoc catName )
52     {
53         mCatName = catName;
54         return emit();
55     }
56
57     public String JavaDoc view( String JavaDoc catName, int maxEntries )
58     {
59         mCatName = catName;
60         mMaxEntries = maxEntries;
61         return emit();
62     }
63
64     //------------------------------------------------------------------------
65
/**
66      * This doStartTag is for the weblog template implementation
67      * @return EVAL_SKIP_BODY
68      */

69     public int doStartTag( PrintWriter JavaDoc pw ) throws JspException JavaDoc
70     {
71         try
72         {
73             HttpServletRequest JavaDoc req =
74                 (HttpServletRequest JavaDoc)pageContext.getRequest();
75
76             RollerRequest rreq = RollerRequest.getRollerRequest(req);
77
78             // need website so we can get weblog day template
79
WebsiteData website = rreq.getWebsite( );
80             
81             String JavaDoc catName = mCatName;
82             if (catName == null)
83             {
84                 catName= req.getParameter(RollerRequest.WEBLOGCATEGORYNAME_KEY);
85             }
86
87             String JavaDoc name = null;
88             if ( rreq.getUser() != null )
89             {
90                 name = rreq.getUser().getUserName();
91             }
92
93             // get recent weblog entries
94
int max = (mMaxEntries == -1) ? 15 : mMaxEntries;
95             Date JavaDoc dayParam = rreq.getDate(true);
96             WeblogManager mgr = rreq.getRoller().getWeblogManager();
97             
98             //Map map = mgr.getRecentWeblogEntries(
99
//name, dayParam, catName, max, true );
100

101             Map JavaDoc map = mgr.getWeblogEntryObjectMap(
102                             rreq.getWebsite(), // userName
103
null, // startDate
104
dayParam, // endDate
105
catName, // catName
106
WeblogManager.PUB_ONLY, // status
107
new Integer JavaDoc(max)); // maxEntries
108

109             
110             // Get page id if daytemplate is specified
111
String JavaDoc pid = null;
112             if ( mDayTemplate != null )
113             {
114                 PageData page =
115                     rreq.getRoller().getUserManager().getPageByLink(
116                         website, mDayTemplate );
117                 if (page != null)
118                 {
119                     pid = page.getId();
120                 }
121             }
122             if ( pid == null )
123             {
124                 pid = website.getWeblogDayPageId();
125             }
126             
127             // get day template and run it through Velocity
128
Template vtemplate = RuntimeSingleton.getTemplate( pid );
129
130             // through entries, one day per iteration
131
int count = 0;
132             Iterator JavaDoc iter = map.keySet().iterator();
133             while ( iter.hasNext() )
134             {
135                 // get date and entries for that date
136
Date JavaDoc d = (Date JavaDoc)iter.next();
137
138                 VelocityContext vcontext = new VelocityContext();
139                 WeblogEntryMacros macros =
140                     new WeblogEntryMacros( pageContext, d );
141                 vcontext.put( "macros", macros );
142
143                 ArrayList JavaDoc entries = (ArrayList JavaDoc)map.get( d );
144                 vcontext.put( "entries", entries );
145
146                 vtemplate.merge( vcontext, pw );
147
148                 if ( mMaxEntries != -1 && count > mMaxEntries ) break;
149                 count++;
150             }
151         }
152         catch (Exception JavaDoc e)
153         {
154             mLogger.error("Unexpected exception",e);
155             throw new JspException JavaDoc(e);
156         }
157         return Tag.SKIP_BODY;
158     }
159 }
160
161
Popular Tags