View Javadoc

1   package net.sourceforge.blogentis.feed;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: Feed.java,v 1.1 2004/10/22 17:34:05 tassos Exp $
23  //
24  
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  import net.sourceforge.blogentis.turbine.BlogParameterParser;
32  import net.sourceforge.blogentis.turbine.BlogRunData;
33  import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
34  import net.sourceforge.blogentis.utils.DateSpecification;
35  import net.sourceforge.blogentis.utils.PresentationalProperties;
36  
37  import org.apache.commons.beanutils.BeanUtils;
38  import org.apache.ecs.xml.XML;
39  import org.apache.torque.TorqueException;
40  import org.apache.torque.util.Criteria;
41  import org.apache.turbine.util.RunData;
42  import org.apache.velocity.context.Context;
43  
44  /***
45   * @author abas
46   */
47  public abstract class Feed {
48      public static final String FEED_VERSION = "version";
49  
50      public static final String FEED_LIMIT = "limit";
51  
52      public static final String DATE_SPECIFICATION = "dateSpecification";
53  
54      public static final String CLEAR = "clear";
55  
56      public static final String FEED_OFFSET = "offset";
57  
58      protected String version = "rdf";
59  
60      protected int limit = 15;
61  
62      protected int offset = 0;
63  
64      protected List cm = new ArrayList();
65  
66      protected List items = null;
67  
68      protected BlogRunData data;
69  
70      protected BlogParameterParser pp;
71  
72      protected PresentationalProperties properties;
73  
74      protected abstract List getItems(Criteria c)
75              throws TorqueException;
76  
77      public abstract String getTimeColumn();
78  
79      public abstract String[] getSearchColumns();
80  
81      public Feed(RunData data) {
82          this.data = (BlogRunData)data;
83          this.pp = (BlogParameterParser)data.getParameters();
84          properties = getProperties();
85          clear();
86      }
87  
88      protected abstract PresentationalProperties getProperties();
89  
90      public void addCriteriaModifier(FeedModifier feedMod) {
91          this.cm.add(feedMod);
92      }
93  
94      public void clear() {
95          limit = (properties != null) ? properties.getFeedSize() : 15;
96          offset = 0;
97          version = "rdf";
98          items = null;
99          // we don't clear the modifiers, they are part of the feed!
100     }
101 
102     public void setClear(Object o) {
103         clear();
104     }
105 
106     public void setFeedProperty(String property, Object o) {
107         try {
108             BeanUtils.setProperty(this, property, o);
109         } catch (Exception ignored) {}
110     }
111 
112     public int getLimit() {
113         return limit;
114     }
115 
116     public int getOffset() {
117         return offset;
118     }
119 
120     public String getVersion() {
121         return version;
122     }
123 
124     public void setLimit(int i) {
125         limit = i;
126     }
127 
128     public void setOffset(int i) {
129         offset = i;
130     }
131 
132     public void setVersion(String string) {
133         version = string;
134     }
135 
136     public DateSpecification getDateSpecification() {
137         for(Iterator i = cm.iterator(); i.hasNext();) {
138             FeedModifier fm = (FeedModifier)i.next();
139             if (fm instanceof DateModifier) {
140                 DateModifier dm = (DateModifier)fm;
141                 return dm.getDateSpecification();
142             }
143         }
144         return null;
145     }
146 
147     public void setDateSpecification(DateSpecification specification) {
148         for(Iterator i = cm.iterator(); i.hasNext();) {
149             FeedModifier fm = (FeedModifier)i.next();
150             if (fm instanceof DateModifier) {
151                 DateModifier dm = (DateModifier)fm;
152                 dm.setDateSpecification(specification);
153                 return;
154             }
155         }
156         addCriteriaModifier(new DateModifier(specification, getTimeColumn()));
157     }
158 
159     public void setContextProperties(Context context) {
160         context.put("feed", this);
161         context.put("properties", properties);
162     }
163 
164     public List getItems()
165             throws TorqueException {
166         if (items == null) {
167             Criteria c = new Criteria();
168             c.addDescendingOrderByColumn(getTimeColumn());
169             if (getLimit() > 0) {
170                 c.setLimit(getLimit());
171                 if (offset > 0)
172                     c.setOffset(offset);
173             }
174             for(Iterator i = cm.iterator(); i.hasNext();) {
175                 FeedModifier f = (FeedModifier)i.next();
176                 f.modify(c);
177             }
178             items = getItems(c);
179         }
180         return items;
181     }
182 
183     public List getItemsTranslated() throws TorqueException {
184         return getItems();
185     }
186 
187     public AbsoluteLinkURL getLink() {
188         AbsoluteLinkURL link = new AbsoluteLinkURL();
189         for(Iterator i = cm.iterator(); i.hasNext();) {
190             FeedModifier fm = (FeedModifier)i.next();
191             fm.modify(link);
192         }
193         return link;
194     }
195 
196     protected abstract void fixFeedDescription(Map m);
197 
198     public List getFeedDescription() {
199         HashMap m = new HashMap();
200         AbsoluteLinkURL feedLink = getLink();
201         m.put(FeedModifier.FEED_PAGE_LINK, feedLink.toString());
202         m.put(FeedModifier.FEED_LINK, getLink().setPage("RSS/ver/" + version));
203         for(Iterator i = cm.iterator(); i.hasNext();) {
204             FeedModifier fm = (FeedModifier)i.next();
205             fm.modify(m);
206         }
207         fixFeedDescription(m);
208         m.put(FeedModifier.FEED_LINK, m.get(FeedModifier.FEED_LINK).toString());
209         List l = new ArrayList(m.size());
210         for(Iterator i = m.keySet().iterator(); i.hasNext();) {
211             String k = (String)i.next();
212             l.add(new XML(k).addElement((String)m.get(k)));
213         }
214         return l;
215     }
216 }