1 19 20 package org.lucane.applications.todolist; 21 22 import java.sql.Connection ; 23 import java.util.Date ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 29 import org.lucane.common.Logging; 30 import org.lucane.common.Message; 31 import org.lucane.common.acl.AclInfo; 32 import org.lucane.common.acl.AclProducer; 33 import org.lucane.common.concepts.GroupConcept; 34 import org.lucane.common.concepts.UserConcept; 35 import org.lucane.common.net.ObjectConnection; 36 import org.lucane.server.Server; 37 import org.lucane.server.Service; 38 import org.lucane.server.acl.AccessController; 39 import org.lucane.server.database.DatabaseAbstractionLayer; 40 import org.lucane.server.database.util.Sequence; 41 42 public class TodolistService extends Service 43 implements AclProducer 44 { 45 public static final String ACCESS_WRITE = "write"; 46 public static final String ACCESS_READ = "read"; 47 48 private DatabaseAbstractionLayer layer; 49 private Sequence listsSequence; 50 private Sequence itemsSequence; 51 52 public void install() { 53 try { 54 String dbDescription = getDirectory() + "db-todolist.xml"; 55 layer.getTableCreator().createFromXml(dbDescription); 56 } catch (Exception e) { 57 Logging.getLogger().severe("Unable to install TodolistService !"); 58 e.printStackTrace(); 59 } 60 } 61 62 public void init(Server parent) { 63 layer = parent.getDBLayer(); 64 listsSequence = new Sequence("todolists", "id"); 65 itemsSequence = new Sequence("todolistitems", "id"); 66 } 67 68 public void process(ObjectConnection oc, Message message) { 69 TodolistAction tla = (TodolistAction) message.getData(); 70 try { 71 switch (tla.action) { 72 case TodolistAction.GET_TODOLISTS : 73 ArrayList tl = getTodolists((String )tla.getParam()); 74 if (tl != null) { 75 oc.write("OK"); 76 oc.write(tl); 77 } else { 78 oc.write("FAILED"); 79 } 80 break; 81 82 case TodolistAction.GET_TODOLISTITEMS : 83 ArrayList tli = getTodolistItems(((Integer )tla.getParam()).intValue()); 84 if (tli != null) { 85 oc.write("OK"); 86 oc.write(tli); 87 } else { 88 oc.write("FAILED"); 89 } 90 break; 91 92 case TodolistAction.ADD_TODOLIST : 93 Integer tlid = addTodolist((Todolist)tla.getParam()); 94 if (tlid != null) { 95 oc.write("OK"); 96 oc.write(tlid); 97 } else { 98 oc.write("FAILED"); 99 } 100 break; 101 102 case TodolistAction.MOD_TODOLIST : { 103 Object [] tmpobj = (Object [])tla.getParam(); 104 if (modifyTodolist(((Integer )tmpobj[0]).intValue(), (Todolist)tmpobj[1])) 105 oc.write("OK"); 106 else 107 oc.write("FAILED"); 108 break; 109 } 110 111 case TodolistAction.DEL_TODOLIST : 112 if (deleteTodolist(((Integer )tla.getParam()).intValue())) 113 oc.write("OK"); 114 else 115 oc.write("FAILED"); 116 break; 117 118 case TodolistAction.ADD_TODOLISTITEM : 119 Integer tliid = addTodolistItem((TodolistItem)tla.getParam()); 120 if (tliid != null) { 121 oc.write("OK"); 122 oc.write(tliid); 123 } else { 124 oc.write("FAILED"); 125 } 126 break; 127 128 case TodolistAction.MOD_TODOLISTITEM : { 129 Object [] tmpobj = (Object [])tla.getParam(); 130 if (modifyTodolistItem(((Integer )tmpobj[0]).intValue(), (TodolistItem)tmpobj[1])) 131 oc.write("OK"); 132 else 133 oc.write("FAILED"); 134 break; 135 } 136 137 case TodolistAction.DEL_TODOLISTITEM : 138 if (deleteTodolistItem(((Integer )tla.getParam()).intValue())) 139 oc.write("OK"); 140 else 141 oc.write("FAILED"); 142 break; 143 144 case TodolistAction.GET_ACLS: 145 AclInfo[] acl = getAcls((String )tla.getParam()); 146 oc.write("OK"); 147 oc.write(acl); 148 break; 149 150 case TodolistAction.GET_GROUPS: 151 ArrayList groups = getAllGroups(); 152 oc.write("OK"); 153 oc.write(groups); 154 break; 155 156 case TodolistAction.GET_USERS: 157 ArrayList users = getAllUsers(); 158 oc.write("OK"); 159 oc.write(users); 160 break; 161 162 case TodolistAction.ADD_ACL: 163 addAcl((String )tla.getParam(), (AclInfo)tla.getOption()); 164 oc.write("OK"); 165 break; 166 167 case TodolistAction.REMOVE_ACL: 168 removeAcl((String )tla.getParam(), (AclInfo)tla.getOption()); 169 oc.write("OK"); 170 break; 171 172 } 173 } catch (Exception e) { 174 e.printStackTrace(); 175 try { 176 oc.write("FAILED " + e); 177 } catch (Exception e2) { 178 e2.printStackTrace(); 179 } 180 } 181 } 182 183 public ArrayList getTodolists(String userName) throws Exception { 184 Connection conn = layer.getConnection(); 185 186 PreparedStatement st = conn.prepareStatement( 187 "SELECT id, user_name, name, comment FROM todolists"); 188 ResultSet res = st.executeQuery(); 189 190 AccessController acl = Server.getInstance().getAccessController(); 191 ArrayList todolists = new ArrayList (); 192 while (res.next()) { 193 int id = res.getInt(1); 194 String user = res.getString(2); 195 if(user.equals(userName) || acl.hasAccess(getName(), String.valueOf(id), ACCESS_WRITE, userName)) 196 todolists.add(new Todolist(id, user, res.getString(3), res.getString(4))); 197 else if(acl.hasAccess(getName(), String.valueOf(id), ACCESS_READ, userName)) 198 todolists.add(new Todolist(id, user, res.getString(3), res.getString(4), true)); 199 } 200 201 res.close(); 202 st.close(); 203 conn.close(); 204 205 return todolists; 206 } 207 208 public Todolist getTodolist(String userName, int id) throws Exception { 209 Connection conn = layer.getConnection(); 210 211 PreparedStatement st = conn.prepareStatement( 212 "SELECT id, user_name, name, comment FROM todolists WHERE id = ?"); 213 st.setInt(1, id); 214 ResultSet res = st.executeQuery(); 215 216 AccessController acl = Server.getInstance().getAccessController(); 217 ArrayList todolists = new ArrayList (); 218 while (res.next()) { 219 String user = res.getString(2); 220 if(user.equals(userName) || acl.hasAccess(getName(), String.valueOf(id), ACCESS_WRITE, userName)) 221 todolists.add(new Todolist(id, user, res.getString(3), res.getString(4))); 222 else if(acl.hasAccess(getName(), String.valueOf(id), ACCESS_READ, userName)) 223 todolists.add(new Todolist(id, user, res.getString(3), res.getString(4), true)); 224 } 225 226 res.close(); 227 st.close(); 228 conn.close(); 229 230 if (todolists!=null && todolists.size()==1) 231 return (Todolist)todolists.get(0); 232 else 233 return null; 234 } 235 236 public TodolistItem getTodolistItem(String userName, int id) throws Exception { 237 Connection conn = layer.getConnection(); 238 239 PreparedStatement st = conn.prepareStatement( 240 "SELECT id, id_list, name, comment, priority, completed, start_date, end_date, estimated_start_date, estimated_end_date FROM todolistitems WHERE id=?"); 241 st.setInt(1, id); 242 ResultSet res = st.executeQuery(); 243 244 ArrayList todolistitems = new ArrayList (); 245 while (res.next()) { 246 long tmp; 247 tmp = res.getLong(7); 248 Date start = (tmp > 0) ? new Date (tmp) : null; 249 tmp = res.getLong(8); 250 Date end = (tmp > 0) ? new Date (tmp) : null; 251 tmp = res.getLong(9); 252 Date estimatedStart = (tmp > 0) ? new Date (tmp) : null; 253 tmp = res.getLong(10); 254 Date estimatedEnd = (tmp > 0) ? new Date (tmp) : null; 255 todolistitems.add(new TodolistItem(res.getInt(1), res.getInt(2), res.getString(3), res.getString(4), res.getInt(5), res.getInt(6)==1, start, end, estimatedStart, estimatedEnd)); 256 } 257 258 res.close(); 259 st.close(); 260 conn.close(); 261 262 if (todolistitems!=null && todolistitems.size()==1) 263 return (TodolistItem)todolistitems.get(0); 264 else 265 return null; 266 } 267 268 public ArrayList getTodolistItems(int idList) throws Exception { 269 Connection conn = layer.getConnection(); 270 271 PreparedStatement st = conn.prepareStatement( 272 "SELECT id, id_list, name, comment, priority, completed, start_date, end_date, estimated_start_date, estimated_end_date FROM todolistitems WHERE id_list=?"); 273 st.setInt(1, idList); 274 ResultSet res = st.executeQuery(); 275 276 ArrayList todolistitems = new ArrayList (); 277 while (res.next()) { 278 long tmp; 279 tmp = res.getLong(7); 280 Date start = (tmp > 0) ? new Date (tmp) : null; 281 tmp = res.getLong(8); 282 Date end = (tmp > 0) ? new Date (tmp) : null; 283 tmp = res.getLong(9); 284 Date estimatedStart = (tmp > 0) ? new Date (tmp) : null; 285 tmp = res.getLong(10); 286 Date estimatedEnd = (tmp > 0) ? new Date (tmp) : null; 287 todolistitems.add(new TodolistItem(res.getInt(1), res.getInt(2), res.getString(3), res.getString(4), res.getInt(5), res.getInt(6)==1, start, end, estimatedStart, estimatedEnd)); 288 } 289 290 res.close(); 291 st.close(); 292 conn.close(); 293 294 return todolistitems; 295 } 296 297 public Integer addTodolist(Todolist newTodolist) throws Exception { 298 Connection conn = layer.getConnection(); 299 300 PreparedStatement st = conn.prepareStatement( 301 "INSERT INTO todolists (id, user_name, name, comment) VALUES (?, ?, ?, ?)"); 302 newTodolist.setId(listsSequence.getNextId()); 303 st.setInt(1, newTodolist.getId()); 304 st.setString(2, newTodolist.getUserName()); 305 st.setString(3, newTodolist.getName()); 306 st.setString(4, newTodolist.getComment()); 307 st.executeUpdate(); 308 st.close(); 309 conn.close(); 310 311 return new Integer (newTodolist.getId()); 312 } 313 314 public boolean modifyTodolist(int oldTodolistId, Todolist newTodolist) throws Exception { 315 Connection conn = layer.getConnection(); 316 317 PreparedStatement st = conn.prepareStatement( 318 "UPDATE todolists SET user_name=?, name=?, comment=? WHERE id=?"); 319 st.setString(1, newTodolist.getUserName()); 320 st.setString(2, newTodolist.getName()); 321 st.setString(3, newTodolist.getComment()); 322 st.setInt(4, oldTodolistId); 323 st.executeUpdate(); 324 st.close(); 325 conn.close(); 326 327 return true; 328 } 329 330 public boolean deleteTodolist(int id) throws Exception { 331 Connection conn = layer.getConnection(); 332 333 PreparedStatement st = conn.prepareStatement( 334 "DELETE FROM todolists WHERE id=?"); 335 st.setInt(1, id); 336 st.executeUpdate(); 337 st.close(); 338 339 st = conn.prepareStatement( 340 "DELETE FROM todolistitems WHERE id_list=?"); 341 st.setInt(1, id); 342 st.executeUpdate(); 343 st.close(); 344 conn.close(); 345 346 return true; 347 } 348 349 public Integer addTodolistItem(TodolistItem newTodolistItem) throws Exception { 350 Connection conn = layer.getConnection(); 351 352 PreparedStatement st = conn.prepareStatement( 353 "SELECT count(*) FROM todolists WHERE id=?"); 354 st.setInt(1, newTodolistItem.getParentTodolistId()); 355 ResultSet res = st.executeQuery(); 356 if (!res.next()||res.getInt(1)!=1) { 357 res.close(); 358 st.close(); 359 return null; 360 } 361 res.close(); 362 st.close(); 363 364 st = conn.prepareStatement( 365 "INSERT INTO todolistitems (id, name, comment, id_list, priority, completed, start_date, end_date, estimated_start_date, estimated_end_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 366 newTodolistItem.setId(itemsSequence.getNextId()); 367 st.setInt(1, newTodolistItem.getId()); 368 st.setString(2, newTodolistItem.getName()); 369 st.setString(3, newTodolistItem.getComment()); 370 st.setInt(4, newTodolistItem.getParentTodolistId()); 371 st.setInt(5, newTodolistItem.getPriority()); 372 st.setInt(6, newTodolistItem.isCompleted()?1:0); 373 if (newTodolistItem.getStartDate() != null) 374 st.setLong(7, newTodolistItem.getStartDate().getTime()); 375 else 376 st.setNull(7, java.sql.Types.BIGINT); 377 378 if (newTodolistItem.getEndDate() != null) 379 st.setLong(8, newTodolistItem.getEndDate().getTime()); 380 else 381 st.setNull(8, java.sql.Types.BIGINT); 382 383 if (newTodolistItem.getEstimatedStartDate() != null) 384 st.setLong(9, newTodolistItem.getEstimatedStartDate().getTime()); 385 else 386 st.setNull(9, java.sql.Types.BIGINT); 387 388 if (newTodolistItem.getEstimatedEndDate() != null) 389 st.setLong(10, newTodolistItem.getEstimatedEndDate().getTime()); 390 else 391 st.setNull(10, java.sql.Types.BIGINT); 392 393 st.executeUpdate(); 394 st.close(); 395 396 conn.close(); 397 398 return new Integer (newTodolistItem.getId()); 399 } 400 401 public boolean modifyTodolistItem(int oldTodolistItemId, TodolistItem newTodolistItem) throws Exception { 402 Connection conn = layer.getConnection(); 403 404 PreparedStatement st = conn.prepareStatement( 405 "UPDATE todolistitems SET name=?, comment=?, id_list=?, priority=?, completed=?, start_date=?, end_date=?, estimated_start_date=?, estimated_end_date=? WHERE id=?"); 406 st.setString(1, newTodolistItem.getName()); 407 st.setString(2, newTodolistItem.getComment()); 408 st.setInt(3, newTodolistItem.getParentTodolistId()); 409 st.setInt(4, newTodolistItem.getPriority()); 410 st.setInt(5, newTodolistItem.isCompleted()?1:0); 411 412 if (newTodolistItem.getStartDate() != null) 413 st.setLong(6, newTodolistItem.getStartDate().getTime()); 414 else 415 st.setNull(6, java.sql.Types.BIGINT); 416 if (newTodolistItem.getEndDate() != null) 417 st.setLong(7, newTodolistItem.getEndDate().getTime()); 418 else 419 st.setNull(7, java.sql.Types.BIGINT); 420 421 if (newTodolistItem.getEstimatedStartDate() != null) 422 st.setLong(8, newTodolistItem.getEstimatedStartDate().getTime()); 423 else 424 st.setNull(8, java.sql.Types.BIGINT); 425 if (newTodolistItem.getEstimatedEndDate() != null) 426 st.setLong(9, newTodolistItem.getEstimatedEndDate().getTime()); 427 else 428 st.setNull(9, java.sql.Types.BIGINT); 429 430 st.setInt(10, oldTodolistItemId); 431 st.executeUpdate(); 432 st.close(); 433 conn.close(); 434 435 return true; 436 } 437 438 public boolean deleteTodolistItem(int id) throws Exception { 439 Connection conn = layer.getConnection(); 440 441 PreparedStatement st = conn.prepareStatement( 442 "DELETE FROM todolistitems WHERE id=?"); 443 st.setInt(1, id); 444 st.executeUpdate(); 445 st.close(); 446 conn.close(); 447 448 return true; 449 } 450 451 public AclInfo[] getAcls(String listId) 453 throws Exception 454 { 455 AccessController acl = Server.getInstance().getAccessController(); 456 return acl.getAcls(getName(), listId); 457 } 458 459 public ArrayList getAllGroups() 460 throws Exception 461 { 462 ArrayList groups = new ArrayList (); 463 Iterator i = Server.getInstance().getStore().getGroupStore().getAllGroups(); 464 while(i.hasNext()) 465 { 466 GroupConcept concept = (GroupConcept)i.next(); 467 groups.add(concept.getName()); 468 } 469 470 return groups; 471 } 472 473 public ArrayList getAllUsers() 474 throws Exception 475 { 476 ArrayList users = new ArrayList (); 477 Iterator i = Server.getInstance().getStore().getUserStore().getAllUsers(); 478 while(i.hasNext()) 479 { 480 UserConcept concept = (UserConcept)i.next(); 481 users.add(concept.getName()); 482 } 483 484 return users; 485 } 486 487 public void addAcl(String listId, AclInfo aclInfo) 488 throws Exception 489 { 490 AccessController acl = Server.getInstance().getAccessController(); 491 if(aclInfo.getGroup() != null) 492 { 493 if(aclInfo.isAllow()) 494 acl.allowGroup(getName(), listId, aclInfo.getAccess(), aclInfo.getGroup()); 495 else 496 acl.denyGroup(getName(), listId, aclInfo.getAccess(), aclInfo.getGroup()); 497 } 498 else if(aclInfo.getUser() != null) 499 { 500 if(aclInfo.isAllow()) 501 acl.allowUser(getName(), listId, aclInfo.getAccess(), aclInfo.getUser()); 502 else 503 acl.denyUser(getName(), listId, aclInfo.getAccess(), aclInfo.getUser()); 504 } 505 } 506 507 public void removeAcl(String listId, AclInfo aclInfo) 508 throws Exception 509 { 510 AccessController acl = Server.getInstance().getAccessController(); 511 if(aclInfo.getGroup() != null) 512 acl.removeAclForGroup(getName(), listId, aclInfo.getAccess(), aclInfo.getGroup()); 513 else if(aclInfo.getUser() != null) 514 acl.removeAclForUser(getName(), listId, aclInfo.getAccess(), aclInfo.getUser()); 515 } 516 517 public String [] getAccesses() { 519 return new String [] {ACCESS_WRITE, ACCESS_READ}; 520 } 521 522 public String getItemName(String itemId) throws Exception { 523 String name = itemId; 524 525 Connection conn = layer.getConnection(); 526 527 PreparedStatement st = conn.prepareStatement( 528 "SELECT name FROM todolists WHERE id = ?"); 529 st.setString(1, itemId); 530 ResultSet res = st.executeQuery(); 531 if(res.next()) 532 name = res.getString(1); 533 534 res.close(); 535 st.close(); 536 conn.close(); 537 538 return name; 539 } 540 541 } 542 | Popular Tags |