KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > rssreader > RssService


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.rssreader;
20
21 import org.lucane.applications.rssreader.rss.ChannelInfo;
22 import org.lucane.common.*;
23 import org.lucane.common.net.ObjectConnection;
24 import org.lucane.server.*;
25 import org.lucane.server.database.*;
26 import org.lucane.server.store.Store;
27
28 import java.sql.*;
29 import java.util.*;
30
31 public class RssService
32 extends Service
33 {
34     private DatabaseAbstractionLayer layer = null;
35     private Store store = null;
36
37     public RssService()
38     {
39     }
40
41     public void init(Server parent)
42     {
43         layer = parent.getDBLayer();
44     }
45
46     public void install()
47     {
48         try {
49             String JavaDoc dbDescription = getDirectory() + "db-rssreader.xml";
50             layer.getTableCreator().createFromXml(dbDescription);
51         } catch (Exception JavaDoc e) {
52             Logging.getLogger().severe("Unable to install RssService !");
53             e.printStackTrace();
54         }
55     }
56
57     public void process(ObjectConnection oc, Message message)
58     {
59         RssAction ra = (RssAction)message.getData();
60         String JavaDoc user = message.getSender().getName();
61         ChannelInfo channel = (ChannelInfo)ra.channel;
62
63         try {
64             switch(ra.action)
65             {
66             case RssAction.GET_CHANNELS:
67                 ArrayList channels = this.getChannels(user);
68                 oc.write("OK");
69                 oc.write(channels);
70                 break;
71
72             case RssAction.ADD_CHANNEL:
73                 this.addChannel(user, channel);
74                 oc.write("OK");
75                 break;
76                 
77             case RssAction.REMOVE_CHANNEL:
78                 this.removeChannel(user, channel);
79                 oc.write("OK");
80             }
81         } catch(Exception JavaDoc e) {
82             try {
83                 oc.write("FAILED " + e);
84             } catch(Exception JavaDoc e2) {}
85             e.printStackTrace();
86         }
87     }
88
89     public ArrayList getChannels(String JavaDoc user)
90     throws Exception JavaDoc
91     {
92         ArrayList channels = new ArrayList();
93         
94         Connection connection = layer.getConnection();
95         PreparedStatement select = connection.prepareStatement(
96                 "SELECT name, url FROM rssChannels WHERE userName=?");
97         
98         select.setString(1, user);
99         
100         ResultSet result = select.executeQuery();
101         while(result.next())
102         {
103             String JavaDoc name = result.getString(1);
104             String JavaDoc url = result.getString(2);
105             channels.add(new ChannelInfo(name, url));
106         }
107         
108         result.close();
109         select.close();
110         connection.close();
111         
112         return channels;
113     }
114
115     public void addChannel(String JavaDoc user, ChannelInfo channel)
116     throws Exception JavaDoc
117     {
118         Connection connection = layer.getConnection();
119         PreparedStatement insert = connection.prepareStatement(
120             "INSERT INTO rssChannels VALUES(?, ?, ?)");
121         
122         insert.setString(1, user);
123         insert.setString(2, channel.getName());
124         insert.setString(3, channel.getUrl());
125         insert.execute();
126         
127         insert.close();
128         connection.close();
129     }
130
131     public void removeChannel(String JavaDoc user, ChannelInfo channel)
132     throws Exception JavaDoc
133     {
134         Connection connection = layer.getConnection();
135         PreparedStatement delete = connection.prepareStatement(
136             "DELETE FROM rssChannels WHERE userName=? AND name=?");
137         
138         delete.setString(1, user);
139         delete.setString(2, channel.getName());
140         delete.execute();
141         
142         delete.close();
143         connection.close();
144     }
145 }
146
147
Popular Tags