1 25 26 29 package net.killingar.forum.internal.managers; 30 31 import it.unimi.dsi.fastutil.longs.LongArrayList; 32 import net.killingar.forum.internal.*; 33 34 import java.sql.*; 35 import java.util.ArrayList ; 36 import java.util.List ; 37 38 public final class TaskManager extends AbstractManager implements java.io.Serializable 39 { 40 public final static int NO_VOTE = -2; 41 42 46 public void addTask(Task task) throws SQLException, AccessDeniedException 47 { 48 manager.checkMyAccess(AccessLevel.addTask); 49 boolean denied = true; 50 if (task.groupID != -1) 52 { 53 Group groups[] = manager.getGroupsOfUser(manager.getUserID()); 54 for (int i = 0; i < groups.length; i++) 55 { 56 if (groups[i].ID == task.groupID) 57 { 58 denied = false; 59 break; 60 } 61 } 62 } 63 64 if (denied) 66 throw new AccessDeniedException("attempt to add task to a group you are not a member of"); 67 68 if ("".equals(task.description)) 69 task.description = null; 70 71 Connection c = null; 72 PreparedStatement statement = null; 73 ResultSet result = null; 74 75 try 76 { 77 c = getNewConnection(); 78 statement = c.prepareStatement("insert into Tasks (User, UserGroup, Name, Description, Folder, Created, LastChanged, LastChangedUser) values(?, ?, ?, ?, ?, NOW(), NOW(), ?)"); 79 statement.setLong(1, manager.getUserID()); 80 statement.setLong(2, task.groupID); 81 statement.setString(3, task.name); 82 statement.setString(4, task.description); 83 statement.setString(5, task.folder); 84 statement.setLong(6, manager.getUserID()); 85 86 statement.executeUpdate(); 87 } 88 finally { closeAll(c, statement, result); } 89 } 90 91 94 public void changeTask(Task task) throws SQLException, AccessDeniedException 95 { 96 if (getTask(task.ID).ownerID != manager.getUserID()) 97 manager.checkMyAccess(AccessLevel.changeTask); 98 99 Connection c = null; 100 PreparedStatement statement = null; 101 ResultSet result = null; 102 103 if ("".equals(task.description)) 104 task.description = null; 105 106 107 try 108 { 109 c = getNewConnection(); 110 statement = c.prepareStatement("update Tasks set Name = ?, Description = ?, Folder = ?, UserGroup = ?, LastChanged = NOW(), LastChangedUser = ? where ID = ?"); 111 statement.setString(1, task.name); 112 statement.setString(2, task.description); 113 statement.setString(3, task.folder); 114 statement.setLong(4, task.groupID); 115 statement.setLong(5, manager.getUserID()); 116 statement.setLong(6, task.ID); 117 118 statement.executeUpdate(); 119 } 120 finally { closeAll(c, statement, result); } 121 } 122 123 126 public void removeTask(long taskID) throws SQLException, AccessDeniedException 127 { 128 if (getTask(taskID).ownerID != manager.getUserID()) 129 manager.checkMyAccess(AccessLevel.removeTask); 130 131 Connection c = null; 132 Statement statement = null; 133 ResultSet result = null; 134 135 try 136 { 137 c = getNewConnection(); 138 statement = c.createStatement(); 139 140 statement.executeUpdate("update Tasks set Visible = 0 where ID = " + taskID); 141 } 142 finally { closeAll(c, statement, result); } 143 } 144 145 148 public boolean isUnread() throws ClassNotFoundException , InstantiationException , IllegalAccessException , SQLException 149 { 150 Connection c = null; 151 Statement statement = null; 152 ResultSet result = null; 153 154 try 155 { 156 c = getNewConnection(); 157 statement = c.createStatement(); 158 159 result = statement.executeQuery( 161 "select "+ 162 "MAX(LastChanged) "+ 163 "from "+ 164 "Tasks "+ 165 "where "+ 166 "Visible = 1 AND"+ 167 "(Usergroup is null"+manager.getGroupsString("UserGroup", " OR ")+") "); 168 169 boolean r = false; 171 if (result.next() && result.getTimestamp(1).after(getTime())) 172 r = true; 173 return r; 174 } 175 finally { closeAll(c, statement, result); } 176 } 177 178 181 public Timestamp getTime() throws ClassNotFoundException , InstantiationException , IllegalAccessException , SQLException 182 { 183 return ((TimeManager)manager.getManager(TimeManager.class.getName())).getUserTime(TimeManager.systemTasks); 184 } 185 186 189 protected Task getTask(ResultSet result) throws SQLException 190 { 191 return new Task( 192 result.getLong("ID"), 193 result.getLong("User"), 194 result.getLong("UserGroup"), 195 result.getString("Name"), 196 result.getString("Description"), 197 result.getString("Folder"), 198 result.getTimestamp("Created"), 199 result.getInt("Prio"), 200 result.getInt("VoteCount"), 201 result.getTimestamp("LastChanged"), 202 result.getLong("LastChangedUser")); 203 } 204 205 208 public List getTasks() throws SQLException 209 { 210 String groupsString; 211 { 212 Group groups[] = manager.getGroupsOfUser(manager.getUserID()); 213 if (groups.length > 0) 214 { 215 StringBuffer s = new StringBuffer ("where Visible = 1 AND ("); 216 for (int i = 0; i < groups.length; i++) 217 { 218 if (i != 0) 219 s.append(" OR "); 220 s.append(" UserGroup = "); 221 s.append(groups[i].ID); 222 } 223 224 s.append(")"); 225 groupsString = s.toString(); 226 } 227 else 228 groupsString = ""; 229 } 230 231 Connection c = null; 232 Statement statement = null; 233 ResultSet result = null; 234 235 try 236 { 237 c = getNewConnection(); 238 statement = c.createStatement(); 239 240 result = statement.executeQuery("select Tasks.*, SUM(TaskData.Data) as Prio, count(TaskData.Data) as VoteCount from Tasks left join TaskData on Tasks.ID = TaskData.Task "+groupsString+" group by (Tasks.ID) order by Folder, Prio desc"); 241 ArrayList v = new ArrayList(); 242 while (result.next()) 243 { 244 if (result.getString("ID") != null) 245 v.add(getTask(result)); 246 } 247 248 return v; 249 } 250 finally { closeAll(c, statement, result); } 251 } 252 253 256 public Task getTask(long taskID) throws SQLException 257 { 258 Connection c = null; 259 Statement statement = null; 260 ResultSet result = null; 261 262 try 263 { 264 c = getNewConnection(); 265 statement = c.createStatement(); 266 267 result = statement.executeQuery("select Tasks.*, SUM(TaskData.Data) as Prio, count(TaskData.Data) as VoteCount from Tasks left join TaskData on Tasks.ID = TaskData.Task where Tasks.ID = "+taskID+" group by (Tasks.ID)"); 268 Task r = null; 269 if (result.next()) 270 { 271 r = getTask(result); 272 } 273 274 return r; 275 } 276 finally { closeAll(c, statement, result); } 277 } 278 279 282 public TaskData getTaskData(long taskID, long userID) throws SQLException 283 { 284 Connection c = null; 285 Statement statement = null; 286 ResultSet result = null; 287 288 try 289 { 290 c = getNewConnection(); 291 statement = c.createStatement(); 292 293 result = statement.executeQuery("select * from TaskData where Task = "+taskID+" AND User = "+userID); 294 TaskData r = null; 295 if (result.next()) 296 r = new TaskData(taskID, userID, result.getTimestamp("UserTime"), result.getInt("Data")); 297 return r; 298 } 299 finally { closeAll(c, statement, result); } 300 } 301 302 305 public void vote(long taskID, int data) throws SQLException, AccessDeniedException 306 { 307 if (data < -1 || data > 1) 308 throw new AccessDeniedException("invalid vote "+data); 309 310 Connection c = null; 311 PreparedStatement statement = null; 312 ResultSet result = null; 313 314 try 315 { 316 c = getNewConnection(); 317 statement = c.prepareStatement("delete from TaskData where Task = ? and User = ?"); 318 statement.setLong(1, taskID); 319 statement.setLong(2, manager.getUserID()); 320 statement.executeUpdate(); 321 statement.close(); 322 323 statement = c.prepareStatement("insert into TaskData (Task, User, Data) values(?, ?, ?)"); 324 statement.setLong(1, taskID); 325 statement.setLong(2, manager.getUserID()); 326 statement.setInt(3, data); 327 statement.executeUpdate(); 328 } 329 finally { closeAll(c, statement, result); } 330 } 331 332 335 public int getVote(long taskID) throws SQLException, AccessDeniedException 336 { 337 Connection c = null; 338 PreparedStatement statement = null; 339 ResultSet result = null; 340 341 try 342 { 343 c = getNewConnection(); 344 statement = c.prepareStatement("select Data from TaskData where Task = ? and User = ?"); 345 statement.setLong(1, taskID); 346 statement.setLong(2, manager.getUserID()); 347 348 result = statement.executeQuery(); 349 if (!result.next()) 350 return NO_VOTE; 351 else 352 return result.getInt(1); 353 } 354 finally { closeAll(c, statement, result); } 355 } 356 357 358 361 public void setRead() throws Exception 362 { 363 ((TimeManager)manager.getManager(TimeManager.class.getName())).setUserTime(TimeManager.systemTasks); 364 } 365 366 369 public void volunteer(long taskID) throws SQLException 370 { 371 Connection c = null; 372 PreparedStatement statement = null; 373 ResultSet result = null; 374 375 try 376 { 377 c = getNewConnection(); 378 statement = c.prepareStatement("insert into TaskVolunteers(Task, User, UserTime) values(?, ?, NOW())"); 379 statement.setLong(1, taskID); 380 statement.setLong(2, manager.getUserID()); 381 statement.executeUpdate(); 382 } 383 finally { closeAll(c, statement, result); } 384 } 385 386 389 public void unVolunteer(long taskID) throws SQLException 390 { 391 Connection c = null; 392 PreparedStatement statement = null; 393 ResultSet result = null; 394 395 try 396 { 397 c = getNewConnection(); 398 statement = c.prepareStatement("delete from TaskVolunteers where Task = ? and User = ?"); 399 statement.setLong(1, taskID); 400 statement.setLong(2, manager.getUserID()); 401 statement.executeUpdate(); 402 } 403 finally { closeAll(c, statement, result); } 404 } 405 406 409 public LongArrayList getVolunteers(long taskID) throws SQLException 410 { 411 Connection c = null; 412 PreparedStatement statement = null; 413 ResultSet result = null; 414 415 try 416 { 417 c = getNewConnection(); 418 statement = c.prepareStatement("select * from TaskVolunteers where Task = ? order by UserTime"); 419 statement.setLong(1, taskID); 420 result = statement.executeQuery(); 421 422 LongArrayList l = new LongArrayList(); 423 if (result.next()) 424 { 425 l.add(result.getLong("User")); 426 } 427 428 return l; 429 } 430 finally { closeAll(c, statement, result); } 431 } 432 } | Popular Tags |