/*
* Lucane - a collaborative platform
* Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.lucane.applications.forum;
import org.lucane.applications.forum.model.ForumInfo;
import org.lucane.applications.forum.model.ForumMessage;
import org.lucane.common.*;
import org.lucane.common.net.ObjectConnection;
import org.lucane.server.*;
import org.lucane.server.acl.AccessController;
import org.lucane.server.database.*;
import org.lucane.server.database.util.Sequence;
import java.sql.*;
import java.util.ArrayList;
public class ForumService
extends Service
{
private DatabaseAbstractionLayer layer = null;
private AccessController acl = null;
private Sequence sequence;
/**
* Creates a new ForumService object.
*/
public ForumService()
{
}
public void init(Server parent)
{
layer = parent.getDBLayer();
acl = parent.getAccessController();
sequence = new Sequence("forumMessage", "id");
}
public void install()
{
try {
String dbDescription = getDirectory() + "db-forum.xml";
layer.getTableCreator().createFromXml(dbDescription);
} catch (Exception e) {
Logging.getLogger().severe("Unable to install ForumService !");
e.printStackTrace();
}
}
public void process(ObjectConnection oc, Message message)
{
ForumAction fa = (ForumAction)message.getData();
String user = message.getSender().getName();
try {
switch(fa.action)
{
case ForumAction.LIST_FORUMS:
ArrayList forums = listForums(user);
oc.write("OK");
oc.write(forums);
break;
case ForumAction.LIST_MESSAGES:
ArrayList messages = listMessages(user, fa.forum);
oc.write("OK");
oc.write(messages);
break;
case ForumAction.GET_MESSAGE_CONTENT:
ForumMessage fmessage = getMessageContent(user, fa.forum, ((Integer)fa.param).intValue());
oc.write("OK");
oc.write(fmessage);
break;
case ForumAction.POST_MESSAGE:
post(user, fa.forum, (ForumMessage)fa.param);
oc.write("OK");
break;
}
} catch(Exception e) {
try {
oc.write("FAILED " + e);
} catch(Exception e2) {}
e.printStackTrace();
}
}
public ArrayList listForums(String user)
throws Exception
{
ArrayList forums = new ArrayList();
Connection connection = layer.getConnection();
PreparedStatement select = connection.prepareStatement(
"SELECT name FROM forum");
ResultSet rs = select.executeQuery();
while(rs.next())
{
String forumName = rs.getString(1);
if(canModerate(forumName, user))
forums.add(new ForumInfo(forumName, ForumInfo.MODERATE));
else if(canWrite(forumName, user))
forums.add(new ForumInfo(forumName, ForumInfo.WRITE));
else if(canRead(forumName, user))
forums.add(new ForumInfo(forumName, ForumInfo.READ));
}
rs.close();
select.close();
connection.close();
return forums;
}
public ArrayList listMessages(String user, String forum)
throws Exception
{
if(!canRead(forum, user))
throw new Exception("User '" + user + "' has no access to '" + forum + "'.");
ArrayList messages = new ArrayList();
Connection connection = layer.getConnection();
PreparedStatement select;
if(canModerate(forum, user))
{
select = connection.prepareStatement(
"SELECT id, idref, author, datum, title, visible " +
"FROM forumMessage WHERE forum=?" +
"ORDER BY idref desc, id");
}
else
{
select = connection.prepareStatement(
"SELECT id, idref, author, datum, title, visible " +
"FROM forumMessage WHERE forum=? AND visible=1" +
"ORDER BY idref desc, id");
}
select.setString(1, forum);
ResultSet rs = select.executeQuery();
while(rs.next())
{
int id = rs.getInt(1);
int idref = rs.getInt(2);
String author = rs.getString(3);
Date date = new Date(rs.getLong(4));
String title = rs.getString(5);
boolean visible = (rs.getInt(6) == 1);
messages.add(new ForumMessage(id, idref, title, date, author, "", visible));
}
rs.close();
select.close();
connection.close();
return messages;
}
public ForumMessage getMessageContent(String user, String forum, int messageId)
throws Exception
{
if(!canRead(forum, user))
throw new Exception("User '" + user + "' has no access to '" + forum + "'.");
ForumMessage message = null;
Connection connection = layer.getConnection();
PreparedStatement select = connection.prepareStatement(
"SELECT idref, author, datum, title, content, visible " +
"FROM forumMessage WHERE forum=? AND id=?");
select.setString(1, forum);
select.setInt(2, messageId);
ResultSet rs = select.executeQuery();
if(rs.next())
{
int idref = rs.getInt(1);
String author = rs.getString(2);
Date date = new Date(rs.getLong(3));
String title = rs.getString(4);
String content = rs.getString(5);
boolean visible = (rs.getInt(6) == 1);
message = new ForumMessage(messageId, idref, title, date, author, content, visible);
}
rs.close();
select.close();
connection.close();
return message;
}
public void post(String user, String forum, ForumMessage message)
throws Exception
{
int id = message.getId();
if(id < 0)
doPost(user, forum, message);
else
doEdit(user, forum, message);
}
private void doPost(String user, String forum, ForumMessage message)
throws Exception
{
if(!canWrite(forum, user))
throw new Exception("User '" + user + "' has no write access to '" + forum + "'.");
Connection connection = layer.getConnection();
PreparedStatement insert = connection.prepareStatement(
"INSERT INTO forumMessage VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
insert.setInt(1, sequence.getNextId());
insert.setInt(2, message.getIdRef());
insert.setString(3, forum);
insert.setString(4, message.getAuthor());
insert.setString(5, message.getTitle());
insert.setLong(6, message.getDate().getTime());
insert.setString(7, message.getContent());
insert.setInt(8, message.isVisible() ? 1 : 0);
insert.execute();
insert.close();
connection.close();
}
private void doEdit(String user, String forum, ForumMessage message)
throws Exception
{
if(!canModerate(forum, user))
throw new Exception("User '" + user + "' can't moderate '" + forum + "'.");
Connection connection = layer.getConnection();
PreparedStatement update = connection.prepareStatement(
"UPDATE forumMessage SET title=?, content=?, visible=? where id=?");
update.setString(1, message.getTitle());
update.setString(2, message.getContent());
update.setInt(3, message.isVisible() ? 1 : 0);
update.setInt(4, message.getId());
update.execute();
update.close();
connection.close();
}
//-- access rights
public boolean canRead(String forum, String user)
throws Exception
{
return acl.hasAccess(getName(), forum, ForumInfo.READ, user)
|| canWrite(forum, user);
}
public boolean canWrite(String forum, String user)
throws Exception
{
return acl.hasAccess(getName(), forum, ForumInfo.WRITE, user)
|| canModerate(forum, user);
}
public boolean canModerate(String forum, String user)
throws Exception
{
return acl.hasAccess(getName(), forum, ForumInfo.MODERATE, user);
}
}
|