1 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 ; 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 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 dbDescription = getDirectory() + "db-forum.xml"; 59 layer.getTableCreator().createFromXml(dbDescription); 60 } catch (Exception 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 user = message.getSender().getName(); 70 71 try { 72 switch(fa.action) 73 { 74 case ForumAction.LIST_FORUMS: 75 ArrayList forums = listForums(user); 76 oc.write("OK"); 77 oc.write(forums); 78 break; 79 80 case ForumAction.LIST_MESSAGES: 81 ArrayList 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 )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 e) { 98 try { 99 oc.write("FAILED " + e); 100 } catch(Exception e2) {} 101 102 e.printStackTrace(); 103 } 104 } 105 106 public ArrayList listForums(String user) 107 throws Exception 108 { 109 ArrayList forums = new ArrayList (); 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 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 listMessages(String user, String forum) 136 throws Exception 137 { 138 if(!canRead(forum, user)) 139 throw new Exception ("User '" + user + "' has no access to '" + forum + "'."); 140 141 ArrayList messages = new ArrayList (); 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 author = rs.getString(3); 167 Date date = new Date(rs.getLong(4)); 168 String 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 user, String forum, int messageId) 181 throws Exception 182 { 183 if(!canRead(forum, user)) 184 throw new Exception ("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 author = rs.getString(2); 201 Date date = new Date(rs.getLong(3)); 202 String title = rs.getString(4); 203 String 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 user, String forum, ForumMessage message) 216 throws Exception 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 user, String forum, ForumMessage message) 226 throws Exception 227 { 228 if(!canWrite(forum, user)) 229 throw new Exception ("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 user, String forum, ForumMessage message) 250 throws Exception 251 { 252 if(!canModerate(forum, user)) 253 throw new Exception ("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 272 public boolean canRead(String forum, String user) 273 throws Exception 274 { 275 return acl.hasAccess(getName(), forum, ForumInfo.READ, user) 276 || canWrite(forum, user); 277 278 } 279 280 public boolean canWrite(String forum, String user) 281 throws Exception 282 { 283 return acl.hasAccess(getName(), forum, ForumInfo.WRITE, user) 284 || canModerate(forum, user); 285 286 } 287 288 public boolean canModerate(String forum, String user) 289 throws Exception 290 { 291 return acl.hasAccess(getName(), forum, ForumInfo.MODERATE, user); 292 } 293 } 294 | Popular Tags |