KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > forum > ForumService


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.forum;
20
21 import org.lucane.applications.forum.model.ForumInfo;
22 import org.lucane.applications.forum.model.ForumMessage;
23 import org.lucane.common.*;
24 import org.lucane.common.net.ObjectConnection;
25 import org.lucane.server.*;
26 import org.lucane.server.acl.AccessController;
27 import org.lucane.server.database.*;
28 import org.lucane.server.database.util.Sequence;
29
30 import java.sql.*;
31 import java.util.ArrayList JavaDoc;
32
33
34 public class ForumService
35 extends Service
36 {
37     private DatabaseAbstractionLayer layer = null;
38     private AccessController acl = null;
39     private Sequence sequence;
40
41     /**
42      * Creates a new ForumService object.
43      */

44     public ForumService()
45     {
46     }
47     
48     public void init(Server parent)
49     {
50         layer = parent.getDBLayer();
51         acl = parent.getAccessController();
52         sequence = new Sequence("forumMessage", "id");
53     }
54     
55     public void install()
56     {
57         try {
58             String JavaDoc dbDescription = getDirectory() + "db-forum.xml";
59             layer.getTableCreator().createFromXml(dbDescription);
60         } catch (Exception JavaDoc e) {
61             Logging.getLogger().severe("Unable to install ForumService !");
62             e.printStackTrace();
63         }
64     }
65     
66     public void process(ObjectConnection oc, Message message)
67     {
68         ForumAction fa = (ForumAction)message.getData();
69         String JavaDoc user = message.getSender().getName();
70         
71         try {
72             switch(fa.action)
73             {
74             case ForumAction.LIST_FORUMS:
75                 ArrayList JavaDoc forums = listForums(user);
76                 oc.write("OK");
77                 oc.write(forums);
78                 break;
79             
80             case ForumAction.LIST_MESSAGES:
81                 ArrayList JavaDoc messages = listMessages(user, fa.forum);
82                 oc.write("OK");
83                 oc.write(messages);
84                 break;
85             
86             case ForumAction.GET_MESSAGE_CONTENT:
87                 ForumMessage fmessage = getMessageContent(user, fa.forum, ((Integer JavaDoc)fa.param).intValue());
88                 oc.write("OK");
89                 oc.write(fmessage);
90                 break;
91             
92             case ForumAction.POST_MESSAGE:
93                 post(user, fa.forum, (ForumMessage)fa.param);
94                 oc.write("OK");
95                 break;
96             }
97         } catch(Exception JavaDoc e) {
98             try {
99                 oc.write("FAILED " + e);
100             } catch(Exception JavaDoc e2) {}
101             
102             e.printStackTrace();
103         }
104     }
105     
106     public ArrayList JavaDoc listForums(String JavaDoc user)
107     throws Exception JavaDoc
108     {
109         ArrayList JavaDoc forums = new ArrayList JavaDoc();
110         
111         Connection connection = layer.getConnection();
112         PreparedStatement select = connection.prepareStatement(
113             "SELECT name FROM forum");
114         ResultSet rs = select.executeQuery();
115         
116         while(rs.next())
117         {
118             String JavaDoc forumName = rs.getString(1);
119             if(canModerate(forumName, user))
120                 forums.add(new ForumInfo(forumName, ForumInfo.MODERATE));
121             else if(canWrite(forumName, user))
122                 forums.add(new ForumInfo(forumName, ForumInfo.WRITE));
123             else if(canRead(forumName, user))
124                 forums.add(new ForumInfo(forumName, ForumInfo.READ));
125         }
126         
127         rs.close();
128         select.close();
129         connection.close();
130         
131         return forums;
132     }
133     
134     
135     public ArrayList JavaDoc listMessages(String JavaDoc user, String JavaDoc forum)
136     throws Exception JavaDoc
137     {
138         if(!canRead(forum, user))
139             throw new Exception JavaDoc("User '" + user + "' has no access to '" + forum + "'.");
140         
141         ArrayList JavaDoc messages = new ArrayList JavaDoc();
142         
143         Connection connection = layer.getConnection();
144         PreparedStatement select;
145         if(canModerate(forum, user))
146         {
147             select = connection.prepareStatement(
148                     "SELECT id, idref, author, datum, title, visible " +
149                     "FROM forumMessage WHERE forum=?" +
150                     "ORDER BY idref desc, id");
151         }
152         else
153         {
154             select = connection.prepareStatement(
155                     "SELECT id, idref, author, datum, title, visible " +
156                     "FROM forumMessage WHERE forum=? AND visible=1" +
157                     "ORDER BY idref desc, id");
158         }
159         select.setString(1, forum);
160         ResultSet rs = select.executeQuery();
161         
162         while(rs.next())
163         {
164             int id = rs.getInt(1);
165             int idref = rs.getInt(2);
166             String JavaDoc author = rs.getString(3);
167             Date date = new Date(rs.getLong(4));
168             String JavaDoc title = rs.getString(5);
169             boolean visible = (rs.getInt(6) == 1);
170             messages.add(new ForumMessage(id, idref, title, date, author, "", visible));
171         }
172         
173         rs.close();
174         select.close();
175         connection.close();
176         
177         return messages;
178     }
179     
180     public ForumMessage getMessageContent(String JavaDoc user, String JavaDoc forum, int messageId)
181     throws Exception JavaDoc
182     {
183         if(!canRead(forum, user))
184             throw new Exception JavaDoc("User '" + user + "' has no access to '" + forum + "'.");
185         
186         ForumMessage message = null;
187         
188         Connection connection = layer.getConnection();
189         PreparedStatement select = connection.prepareStatement(
190                 "SELECT idref, author, datum, title, content, visible " +
191                 "FROM forumMessage WHERE forum=? AND id=?");
192         
193         select.setString(1, forum);
194         select.setInt(2, messageId);
195         ResultSet rs = select.executeQuery();
196         
197         if(rs.next())
198         {
199             int idref = rs.getInt(1);
200             String JavaDoc author = rs.getString(2);
201             Date date = new Date(rs.getLong(3));
202             String JavaDoc title = rs.getString(4);
203             String JavaDoc content = rs.getString(5);
204             boolean visible = (rs.getInt(6) == 1);
205             message = new ForumMessage(messageId, idref, title, date, author, content, visible);
206         }
207         
208         rs.close();
209         select.close();
210         connection.close();
211         
212         return message;
213     }
214
215     public void post(String JavaDoc user, String JavaDoc forum, ForumMessage message)
216     throws Exception JavaDoc
217     {
218         int id = message.getId();
219         if(id < 0)
220             doPost(user, forum, message);
221         else
222             doEdit(user, forum, message);
223     }
224     
225     private void doPost(String JavaDoc user, String JavaDoc forum, ForumMessage message)
226     throws Exception JavaDoc
227     {
228         if(!canWrite(forum, user))
229             throw new Exception JavaDoc("User '" + user + "' has no write access to '" + forum + "'.");
230         
231         Connection connection = layer.getConnection();
232         PreparedStatement insert = connection.prepareStatement(
233             "INSERT INTO forumMessage VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
234         
235         insert.setInt(1, sequence.getNextId());
236         insert.setInt(2, message.getIdRef());
237         insert.setString(3, forum);
238         insert.setString(4, message.getAuthor());
239         insert.setString(5, message.getTitle());
240         insert.setLong(6, message.getDate().getTime());
241         insert.setString(7, message.getContent());
242         insert.setInt(8, message.isVisible() ? 1 : 0);
243         insert.execute();
244         
245         insert.close();
246         connection.close();
247     }
248     
249     private void doEdit(String JavaDoc user, String JavaDoc forum, ForumMessage message)
250     throws Exception JavaDoc
251     {
252         if(!canModerate(forum, user))
253             throw new Exception JavaDoc("User '" + user + "' can't moderate '" + forum + "'.");
254         
255         Connection connection = layer.getConnection();
256         PreparedStatement update = connection.prepareStatement(
257             "UPDATE forumMessage SET title=?, content=?, visible=? where id=?");
258         
259         update.setString(1, message.getTitle());
260         update.setString(2, message.getContent());
261         update.setInt(3, message.isVisible() ? 1 : 0);
262         update.setInt(4, message.getId());
263         update.execute();
264         
265         update.close();
266         connection.close();
267     }
268     
269     
270     //-- access rights
271

272     public boolean canRead(String JavaDoc forum, String JavaDoc user)
273     throws Exception JavaDoc
274     {
275         return acl.hasAccess(getName(), forum, ForumInfo.READ, user)
276         || canWrite(forum, user);
277         
278     }
279     
280     public boolean canWrite(String JavaDoc forum, String JavaDoc user)
281     throws Exception JavaDoc
282     {
283         return acl.hasAccess(getName(), forum, ForumInfo.WRITE, user)
284         || canModerate(forum, user);
285         
286     }
287     
288     public boolean canModerate(String JavaDoc forum, String JavaDoc user)
289     throws Exception JavaDoc
290     {
291         return acl.hasAccess(getName(), forum, ForumInfo.MODERATE, user);
292     }
293 }
294
Popular Tags