001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.applications.forum;
020:
021: import org.lucane.applications.forum.model.ForumInfo;
022: import org.lucane.applications.forum.model.ForumMessage;
023: import org.lucane.common.*;
024: import org.lucane.common.net.ObjectConnection;
025: import org.lucane.server.*;
026: import org.lucane.server.acl.AccessController;
027: import org.lucane.server.database.*;
028: import org.lucane.server.database.util.Sequence;
029:
030: import java.sql.*;
031: import java.util.ArrayList;
032:
033: public class ForumService extends Service {
034: private DatabaseAbstractionLayer layer = null;
035: private AccessController acl = null;
036: private Sequence sequence;
037:
038: /**
039: * Creates a new ForumService object.
040: */
041: public ForumService() {
042: }
043:
044: public void init(Server parent) {
045: layer = parent.getDBLayer();
046: acl = parent.getAccessController();
047: sequence = new Sequence("forumMessage", "id");
048: }
049:
050: public void install() {
051: try {
052: String dbDescription = getDirectory() + "db-forum.xml";
053: layer.getTableCreator().createFromXml(dbDescription);
054: } catch (Exception e) {
055: Logging.getLogger().severe(
056: "Unable to install ForumService !");
057: e.printStackTrace();
058: }
059: }
060:
061: public void process(ObjectConnection oc, Message message) {
062: ForumAction fa = (ForumAction) message.getData();
063: String user = message.getSender().getName();
064:
065: try {
066: switch (fa.action) {
067: case ForumAction.LIST_FORUMS:
068: ArrayList forums = listForums(user);
069: oc.write("OK");
070: oc.write(forums);
071: break;
072:
073: case ForumAction.LIST_MESSAGES:
074: ArrayList messages = listMessages(user, fa.forum);
075: oc.write("OK");
076: oc.write(messages);
077: break;
078:
079: case ForumAction.GET_MESSAGE_CONTENT:
080: ForumMessage fmessage = getMessageContent(user,
081: fa.forum, ((Integer) fa.param).intValue());
082: oc.write("OK");
083: oc.write(fmessage);
084: break;
085:
086: case ForumAction.POST_MESSAGE:
087: post(user, fa.forum, (ForumMessage) fa.param);
088: oc.write("OK");
089: break;
090: }
091: } catch (Exception e) {
092: try {
093: oc.write("FAILED " + e);
094: } catch (Exception e2) {
095: }
096:
097: e.printStackTrace();
098: }
099: }
100:
101: public ArrayList listForums(String user) throws Exception {
102: ArrayList forums = new ArrayList();
103:
104: Connection connection = layer.getConnection();
105: PreparedStatement select = connection
106: .prepareStatement("SELECT name FROM forum");
107: ResultSet rs = select.executeQuery();
108:
109: while (rs.next()) {
110: String forumName = rs.getString(1);
111: if (canModerate(forumName, user))
112: forums
113: .add(new ForumInfo(forumName,
114: ForumInfo.MODERATE));
115: else if (canWrite(forumName, user))
116: forums.add(new ForumInfo(forumName, ForumInfo.WRITE));
117: else if (canRead(forumName, user))
118: forums.add(new ForumInfo(forumName, ForumInfo.READ));
119: }
120:
121: rs.close();
122: select.close();
123: connection.close();
124:
125: return forums;
126: }
127:
128: public ArrayList listMessages(String user, String forum)
129: throws Exception {
130: if (!canRead(forum, user))
131: throw new Exception("User '" + user
132: + "' has no access to '" + forum + "'.");
133:
134: ArrayList messages = new ArrayList();
135:
136: Connection connection = layer.getConnection();
137: PreparedStatement select;
138: if (canModerate(forum, user)) {
139: select = connection
140: .prepareStatement("SELECT id, idref, author, datum, title, visible "
141: + "FROM forumMessage WHERE forum=?"
142: + "ORDER BY idref desc, id");
143: } else {
144: select = connection
145: .prepareStatement("SELECT id, idref, author, datum, title, visible "
146: + "FROM forumMessage WHERE forum=? AND visible=1"
147: + "ORDER BY idref desc, id");
148: }
149: select.setString(1, forum);
150: ResultSet rs = select.executeQuery();
151:
152: while (rs.next()) {
153: int id = rs.getInt(1);
154: int idref = rs.getInt(2);
155: String author = rs.getString(3);
156: Date date = new Date(rs.getLong(4));
157: String title = rs.getString(5);
158: boolean visible = (rs.getInt(6) == 1);
159: messages.add(new ForumMessage(id, idref, title, date,
160: author, "", visible));
161: }
162:
163: rs.close();
164: select.close();
165: connection.close();
166:
167: return messages;
168: }
169:
170: public ForumMessage getMessageContent(String user, String forum,
171: int messageId) throws Exception {
172: if (!canRead(forum, user))
173: throw new Exception("User '" + user
174: + "' has no access to '" + forum + "'.");
175:
176: ForumMessage message = null;
177:
178: Connection connection = layer.getConnection();
179: PreparedStatement select = connection
180: .prepareStatement("SELECT idref, author, datum, title, content, visible "
181: + "FROM forumMessage WHERE forum=? AND id=?");
182:
183: select.setString(1, forum);
184: select.setInt(2, messageId);
185: ResultSet rs = select.executeQuery();
186:
187: if (rs.next()) {
188: int idref = rs.getInt(1);
189: String author = rs.getString(2);
190: Date date = new Date(rs.getLong(3));
191: String title = rs.getString(4);
192: String content = rs.getString(5);
193: boolean visible = (rs.getInt(6) == 1);
194: message = new ForumMessage(messageId, idref, title, date,
195: author, content, visible);
196: }
197:
198: rs.close();
199: select.close();
200: connection.close();
201:
202: return message;
203: }
204:
205: public void post(String user, String forum, ForumMessage message)
206: throws Exception {
207: int id = message.getId();
208: if (id < 0)
209: doPost(user, forum, message);
210: else
211: doEdit(user, forum, message);
212: }
213:
214: private void doPost(String user, String forum, ForumMessage message)
215: throws Exception {
216: if (!canWrite(forum, user))
217: throw new Exception("User '" + user
218: + "' has no write access to '" + forum + "'.");
219:
220: Connection connection = layer.getConnection();
221: PreparedStatement insert = connection
222: .prepareStatement("INSERT INTO forumMessage VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
223:
224: insert.setInt(1, sequence.getNextId());
225: insert.setInt(2, message.getIdRef());
226: insert.setString(3, forum);
227: insert.setString(4, message.getAuthor());
228: insert.setString(5, message.getTitle());
229: insert.setLong(6, message.getDate().getTime());
230: insert.setString(7, message.getContent());
231: insert.setInt(8, message.isVisible() ? 1 : 0);
232: insert.execute();
233:
234: insert.close();
235: connection.close();
236: }
237:
238: private void doEdit(String user, String forum, ForumMessage message)
239: throws Exception {
240: if (!canModerate(forum, user))
241: throw new Exception("User '" + user + "' can't moderate '"
242: + forum + "'.");
243:
244: Connection connection = layer.getConnection();
245: PreparedStatement update = connection
246: .prepareStatement("UPDATE forumMessage SET title=?, content=?, visible=? where id=?");
247:
248: update.setString(1, message.getTitle());
249: update.setString(2, message.getContent());
250: update.setInt(3, message.isVisible() ? 1 : 0);
251: update.setInt(4, message.getId());
252: update.execute();
253:
254: update.close();
255: connection.close();
256: }
257:
258: //-- access rights
259:
260: public boolean canRead(String forum, String user) throws Exception {
261: return acl.hasAccess(getName(), forum, ForumInfo.READ, user)
262: || canWrite(forum, user);
263:
264: }
265:
266: public boolean canWrite(String forum, String user) throws Exception {
267: return acl.hasAccess(getName(), forum, ForumInfo.WRITE, user)
268: || canModerate(forum, user);
269:
270: }
271:
272: public boolean canModerate(String forum, String user)
273: throws Exception {
274: return acl
275: .hasAccess(getName(), forum, ForumInfo.MODERATE, user);
276: }
277: }
|