Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/roller/presentation/weblog/tags/ViewWeblogEntriesTag.java


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;
14  import java.util.ArrayList;
15  import java.util.Date;
16  import java.util.Iterator;
17  import java.util.Map;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.tagext.Tag;
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 getDayTemplate() { return mDayTemplate; }
36      public void setDayTemplate( String n ) { mDayTemplate = n; }
37      private String mDayTemplate = null;
38  
39      /** @jsp.attribute */
40      public String getCatName() { return mCatName; }
41      public void setCatName( String n ) { mCatName = n; }
42      private String 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 view( String catName )
52    {
53      mCatName = catName;
54      return emit();
55    }
56  
57    public String view( String 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 pw ) throws JspException 
70    {
71      try
72      {
73        HttpServletRequest req = 
74          (HttpServletRequest)pageContext.getRequest();
75  
76        RollerRequest rreq = RollerRequest.getRollerRequest(req);
77  //      UserManager userMgr = 
78  //        rreq.getRoller().getUserManager(); 
79  
80        // need website so we can get weblog day template
81        WebsiteData hd = rreq.getWebsite( );
82              
83              String catName = mCatName;
84              if (catName == null)
85              {
86                  catName= req.getParameter(RollerRequest.WEBLOGCATEGORYNAME_KEY);
87              }
88  
89              String name = null;
90              if ( rreq.getUser() != null )
91              {
92                  name = rreq.getUser().getUserName();
93              }
94  
95        // get recent weblog entries
96        int max = (mMaxEntries == -1) ? 15 : mMaxEntries;
97              String dayParam = rreq.getDateString(true);
98              WeblogManager mgr = rreq.getRoller().getWeblogManager();
99              Map map = mgr.getRecentWeblogEntries( 
100                 name, dayParam, catName, max, true );
101 
102             // Get page id if daytemplate is specified
103             String pid = null; 
104             if ( mDayTemplate != null )
105             {
106                 PageData page =
107                     rreq.getRoller().getUserManager().getPageByLink(
108                         rreq.getUser().getUserName(), mDayTemplate );
109                 if (page != null)
110                 {
111                     pid = page.getId();
112                 }            
113             }
114             if ( pid == null )
115             {
116                 pid = hd.getWeblogDayPageId();
117             }
118             
119             // get day template and run it through Velocity
120             Template vtemplate = RuntimeSingleton.getTemplate( pid );
121 
122       // through entries, one day per iteration 
123       int count = 0;
124       Iterator iter = map.keySet().iterator();
125       while ( iter.hasNext() )
126       {
127         // get date and entries for that date
128         Date d = (Date)iter.next();
129 
130                 VelocityContext vcontext = new VelocityContext();
131                 WeblogEntryMacros macros = 
132                     new WeblogEntryMacros( pageContext, d );
133                 vcontext.put( "macros", macros );
134 
135         ArrayList entries = (ArrayList)map.get( d );                
136                 vcontext.put( "entries", entries ); 
137 
138         vtemplate.merge( vcontext, pw );
139 
140         if ( mMaxEntries != -1 && count > mMaxEntries ) break;
141         count++;
142       }
143     }
144     catch (Exception e)
145     {
146             mLogger.error("Unexpected exception",e);
147       throw new JspException(e);
148     }
149     return Tag.SKIP_BODY;
150     }
151 }
152