001: /**********************************************************************************
002:
003: Feedzeo!
004: A free and open source RSS/Atom/RDF feed aggregator
005:
006: Copyright (C) 2005-2006 Anand Rao (anandrao@users.sourceforge.net)
007:
008: This library is free software; you can redistribute it and/or
009: modify it under the terms of the GNU Lesser General Public
010: License as published by the Free Software Foundation; either
011: version 2.1 of the License, or (at your option) any later version.
012:
013: This library is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: Lesser General Public License for more details.
017:
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
021:
022: ************************************************************************************/package data;
023:
024: import java.util.*;
025: import util.*;
026:
027: /**
028: *
029: * @author Anand Rao
030: */
031: public class DataManager {
032:
033: private boolean DEBUG = false; // debug flag
034:
035: private Hashtable CtgyCollectiontbl; /* hashtable storing categorydata objects and
036: provides fast 'categoryname' -to-> categorydata
037: object mapping */
038:
039: /** Creates a new instance of DataManager */
040: public DataManager() {
041: CtgyCollectiontbl = new Hashtable();
042: }
043:
044: private void removeCategory(String Catname) {
045: if (Catname == null)
046: return;
047: CtgyCollectiontbl.remove(Catname);
048: }
049:
050: private void addCategory(CategoryData catData) {
051: if (catData != null) {
052: String catName = catData.getCategoryName();
053: CtgyCollectiontbl.put(catName, catData);
054: }
055: }
056:
057: private CategoryData findCategory(String Category) {
058: return (CategoryData) CtgyCollectiontbl.get(Category);
059: }
060:
061: private ChannelData retreiveChannelData(String url) {
062: FeedRetriever fr;
063:
064: System.out.println(" ### Retreiving data from url:" + url);
065: fr = new FeedRetriever(url);
066: ChannelData chd = fr.getChannelData();
067: System.out.println(" .... Done");
068: return chd;
069: }
070:
071: /**** NOTE BEGIN: The following routines are synchronized to maintain data consistency */
072:
073: /* add/update channel data */
074: private synchronized int addChannelData(String Category,
075: String LinkUrl, ChannelData chd) {
076: CategoryData cd = findCategory(Category);
077: if (cd == null) { // create a new category and add it
078: cd = new CategoryData(Category);
079: addCategory(cd);
080: }
081: cd.addChannel(chd);
082:
083: if (DEBUG)
084: System.out.println("Link added Cat:" + Category + " Link:"
085: + LinkUrl);
086: return 0;
087: }
088:
089: private synchronized int delChannelData(String Category,
090: String FeedSourceLink) {
091: CategoryData cd = findCategory(Category);
092: if (cd == null)
093: return -1;
094: if (cd.removeChannel(FeedSourceLink) == 0) {
095: // we sucessfully removed the channel from this category;
096: // now check if this was the last channel in this category, if so,
097: // remove the category as well
098: if (cd.getNumChannels() == 0) // category empty
099: removeCategory(Category);
100: return 0;
101: }
102: return -1;
103: }
104:
105: public synchronized Vector getFeedNames(String Category) {
106: CategoryData cd = findCategory(Category);
107: if (cd == null)
108: return null;
109: return cd.getFeedNames();
110: }
111:
112: public synchronized String getFeedName(String Category,
113: String feedSourceLink) {
114: CategoryData cd = findCategory(Category);
115: if (cd == null)
116: return null;
117: return cd.getFeedName(feedSourceLink);
118: }
119:
120: public synchronized Vector getAllFeedNames() {
121: Vector AllFeedNames = new Vector();
122: Iterator i = CtgyCollectiontbl.values().iterator();
123:
124: while (i.hasNext()) {
125: CategoryData cd = (CategoryData) i.next();
126: Vector v = cd.getFeedNames();
127: AllFeedNames.addAll(v);
128: }
129: return AllFeedNames;
130: }
131:
132: public synchronized Vector getFeedPageLinks(String Category) {
133: CategoryData cd = findCategory(Category);
134: if (cd == null)
135: return null;
136: return cd.getFeedPageLinks();
137: }
138:
139: public synchronized Vector getFeedSourceLinks(String Category) {
140: CategoryData cd = findCategory(Category);
141: if (cd == null)
142: return null;
143: return cd.getFeedSourceLinks();
144: }
145:
146: public synchronized Vector getAllFeedPageLinks() {
147: Vector AllFeedPageLinks = new Vector();
148: Iterator i = CtgyCollectiontbl.values().iterator();
149:
150: while (i.hasNext()) {
151: CategoryData cd = (CategoryData) i.next();
152: Vector v = cd.getFeedPageLinks();
153: AllFeedPageLinks.addAll(v);
154: }
155: return AllFeedPageLinks;
156: }
157:
158: public synchronized Vector getFeedTableDataPageLinks(String Category) {
159: CategoryData cd = findCategory(Category);
160: if (cd == null)
161: return null;
162: return cd.getFeedTableDataPageLinks();
163: }
164:
165: public synchronized String getFeedTableDataPageLink(
166: String Category, String link) {
167: CategoryData cd = findCategory(Category);
168: if (cd == null)
169: return null;
170: return cd.getFeedTableDataPageLink(link);
171: }
172:
173: public synchronized Vector getAllFeedTableDataPageLinks() {
174: Vector AllFeedTablePageLinks = new Vector();
175: Iterator i = CtgyCollectiontbl.values().iterator();
176:
177: while (i.hasNext()) {
178: CategoryData cd = (CategoryData) i.next();
179: Vector v = cd.getFeedTableDataPageLinks();
180: AllFeedTablePageLinks.addAll(v);
181: }
182:
183: return AllFeedTablePageLinks;
184: }
185:
186: public synchronized Vector getCategorynames() {
187: Vector cNames = new Vector();
188: Iterator i = CtgyCollectiontbl.values().iterator();
189: while (i.hasNext()) {
190: CategoryData cd = (CategoryData) i.next();
191: cNames.addElement(cd.getCategoryName());
192: }
193: if (cNames.size() > 0)
194: return cNames;
195: return null;
196: }
197:
198: public synchronized void setChannelHTMLPageLink(String Category,
199: String feedSourceLink, String filename) {
200: CategoryData cd = findCategory(Category);
201: if (cd == null)
202: return;
203: cd.setChannelHTMLfilename(feedSourceLink, filename);
204: if (DEBUG)
205: System.out.println("Link:" + feedSourceLink
206: + " HTML page link set to " + filename);
207: }
208:
209: public synchronized String getChannelHTMLPageLink(String Category,
210: String feedSourceLink) {
211: CategoryData cd = findCategory(Category);
212: if (cd == null)
213: return null;
214: return cd.getChannelHTMLfilename(feedSourceLink);
215: }
216:
217: public synchronized String getChannelHTMLTableDataPageLink(
218: String Category, String feedSourceLink) {
219: CategoryData cd = findCategory(Category);
220: if (cd == null)
221: return null;
222: return cd.getChannelHTMLTableDatafilename(feedSourceLink);
223: }
224:
225: public synchronized void setChannelHTMLTableDataPageLink(
226: String Category, String feedSourceLink, String filename) {
227: CategoryData cd = findCategory(Category);
228: if (cd == null)
229: return;
230: cd.setChannelHTMLTableDatafilename(feedSourceLink, filename);
231: if (DEBUG)
232: System.out.println("Link:" + feedSourceLink
233: + " HTML Tabledata page link set to" + filename);
234: }
235:
236: public synchronized String getCategoryData(String DataType,
237: String Category) {
238: CategoryData cd = findCategory(Category);
239: if (cd == null)
240: return ("");
241: return cd.getData(DataType);
242: }
243:
244: public synchronized String getChannelData(String DataType,
245: String Category, String feedSourceLink) {
246: CategoryData cd = findCategory(Category);
247: if (cd == null)
248: return ("");
249: return cd.getChannelData(DataType, feedSourceLink);
250: }
251:
252: public synchronized String getItemData(String DataType,
253: String Category, String feedSourceLink, int ItemIndex) {
254: CategoryData cd = findCategory(Category);
255: if (cd == null)
256: return ("");
257: return cd.getChannelItemData(DataType, feedSourceLink,
258: ItemIndex);
259: }
260:
261: /**** NOTE END: The following routines are synchronized to maintain data consistency */
262:
263: /*
264: * Returns 0 on success and -1 on error;
265: * Note: The same routine is used for both adding a new link
266: * as well as updating the channeldata of an existing link
267: */
268: public int addLink(String Category, String LinkUrl) {
269: /*
270: * 1. Fetch the data from the url
271: * 2. The category could be an existing category or a new category
272: * If existing category, then add the channel data to that category
273: * If new category, then add channel data to new category and add
274: * category to the category collection
275: */
276: ChannelData chd = retreiveChannelData(LinkUrl);
277: if (chd == null)
278: return -1;
279:
280: return addChannelData(Category, LinkUrl, chd);
281: }
282:
283: /*
284: * Returns -1 on error;
285: * else returns 0 on success;
286: */
287: public int delLink(String Category, String FeedSourceLink) {
288: return delChannelData(Category, FeedSourceLink);
289: }
290:
291: /* returns 0 if channel was refreshed sucessfully; returns -1 if
292: error in refreshing or no need to refresh channel
293: */
294: public int refreshChannel(String Category, String feedSourceLink) {
295: CategoryData cd = findCategory(Category);
296: if (cd == null)
297: return -1;
298:
299: /* Check if the link has really changed; we do this by comparing
300: the feed's last timestamp and current timestamp */
301:
302: if (cd.isChannelContentChanged(feedSourceLink)) {
303: System.out.println("Link:" + feedSourceLink
304: + " timestamp changed");
305: System.out.println(" Updating link:" + feedSourceLink);
306: /* addlink takes care of updating an existing channel */
307: addLink(Category, feedSourceLink);
308: return 0;
309: }
310: return -1;
311: }
312:
313: }
|