001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Jonathan Riboux <jonathan.riboux@wanadoo.Fr>
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:
020: package org.lucane.applications.todolist;
021:
022: import java.sql.Connection;
023: import java.util.Date;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028:
029: import org.lucane.common.Logging;
030: import org.lucane.common.Message;
031: import org.lucane.common.acl.AclInfo;
032: import org.lucane.common.acl.AclProducer;
033: import org.lucane.common.concepts.GroupConcept;
034: import org.lucane.common.concepts.UserConcept;
035: import org.lucane.common.net.ObjectConnection;
036: import org.lucane.server.Server;
037: import org.lucane.server.Service;
038: import org.lucane.server.acl.AccessController;
039: import org.lucane.server.database.DatabaseAbstractionLayer;
040: import org.lucane.server.database.util.Sequence;
041:
042: public class TodolistService extends Service implements AclProducer {
043: public static final String ACCESS_WRITE = "write";
044: public static final String ACCESS_READ = "read";
045:
046: private DatabaseAbstractionLayer layer;
047: private Sequence listsSequence;
048: private Sequence itemsSequence;
049:
050: public void install() {
051: try {
052: String dbDescription = getDirectory() + "db-todolist.xml";
053: layer.getTableCreator().createFromXml(dbDescription);
054: } catch (Exception e) {
055: Logging.getLogger().severe(
056: "Unable to install TodolistService !");
057: e.printStackTrace();
058: }
059: }
060:
061: public void init(Server parent) {
062: layer = parent.getDBLayer();
063: listsSequence = new Sequence("todolists", "id");
064: itemsSequence = new Sequence("todolistitems", "id");
065: }
066:
067: public void process(ObjectConnection oc, Message message) {
068: TodolistAction tla = (TodolistAction) message.getData();
069: try {
070: switch (tla.action) {
071: case TodolistAction.GET_TODOLISTS:
072: ArrayList tl = getTodolists((String) tla.getParam());
073: if (tl != null) {
074: oc.write("OK");
075: oc.write(tl);
076: } else {
077: oc.write("FAILED");
078: }
079: break;
080:
081: case TodolistAction.GET_TODOLISTITEMS:
082: ArrayList tli = getTodolistItems(((Integer) tla
083: .getParam()).intValue());
084: if (tli != null) {
085: oc.write("OK");
086: oc.write(tli);
087: } else {
088: oc.write("FAILED");
089: }
090: break;
091:
092: case TodolistAction.ADD_TODOLIST:
093: Integer tlid = addTodolist((Todolist) tla.getParam());
094: if (tlid != null) {
095: oc.write("OK");
096: oc.write(tlid);
097: } else {
098: oc.write("FAILED");
099: }
100: break;
101:
102: case TodolistAction.MOD_TODOLIST: {
103: Object[] tmpobj = (Object[]) tla.getParam();
104: if (modifyTodolist(((Integer) tmpobj[0]).intValue(),
105: (Todolist) tmpobj[1]))
106: oc.write("OK");
107: else
108: oc.write("FAILED");
109: break;
110: }
111:
112: case TodolistAction.DEL_TODOLIST:
113: if (deleteTodolist(((Integer) tla.getParam())
114: .intValue()))
115: oc.write("OK");
116: else
117: oc.write("FAILED");
118: break;
119:
120: case TodolistAction.ADD_TODOLISTITEM:
121: Integer tliid = addTodolistItem((TodolistItem) tla
122: .getParam());
123: if (tliid != null) {
124: oc.write("OK");
125: oc.write(tliid);
126: } else {
127: oc.write("FAILED");
128: }
129: break;
130:
131: case TodolistAction.MOD_TODOLISTITEM: {
132: Object[] tmpobj = (Object[]) tla.getParam();
133: if (modifyTodolistItem(
134: ((Integer) tmpobj[0]).intValue(),
135: (TodolistItem) tmpobj[1]))
136: oc.write("OK");
137: else
138: oc.write("FAILED");
139: break;
140: }
141:
142: case TodolistAction.DEL_TODOLISTITEM:
143: if (deleteTodolistItem(((Integer) tla.getParam())
144: .intValue()))
145: oc.write("OK");
146: else
147: oc.write("FAILED");
148: break;
149:
150: case TodolistAction.GET_ACLS:
151: AclInfo[] acl = getAcls((String) tla.getParam());
152: oc.write("OK");
153: oc.write(acl);
154: break;
155:
156: case TodolistAction.GET_GROUPS:
157: ArrayList groups = getAllGroups();
158: oc.write("OK");
159: oc.write(groups);
160: break;
161:
162: case TodolistAction.GET_USERS:
163: ArrayList users = getAllUsers();
164: oc.write("OK");
165: oc.write(users);
166: break;
167:
168: case TodolistAction.ADD_ACL:
169: addAcl((String) tla.getParam(), (AclInfo) tla
170: .getOption());
171: oc.write("OK");
172: break;
173:
174: case TodolistAction.REMOVE_ACL:
175: removeAcl((String) tla.getParam(), (AclInfo) tla
176: .getOption());
177: oc.write("OK");
178: break;
179:
180: }
181: } catch (Exception e) {
182: e.printStackTrace();
183: try {
184: oc.write("FAILED " + e);
185: } catch (Exception e2) {
186: e2.printStackTrace();
187: }
188: }
189: }
190:
191: public ArrayList getTodolists(String userName) throws Exception {
192: Connection conn = layer.getConnection();
193:
194: PreparedStatement st = conn
195: .prepareStatement("SELECT id, user_name, name, comment FROM todolists");
196: ResultSet res = st.executeQuery();
197:
198: AccessController acl = Server.getInstance()
199: .getAccessController();
200: ArrayList todolists = new ArrayList();
201: while (res.next()) {
202: int id = res.getInt(1);
203: String user = res.getString(2);
204: if (user.equals(userName)
205: || acl.hasAccess(getName(), String.valueOf(id),
206: ACCESS_WRITE, userName))
207: todolists.add(new Todolist(id, user, res.getString(3),
208: res.getString(4)));
209: else if (acl.hasAccess(getName(), String.valueOf(id),
210: ACCESS_READ, userName))
211: todolists.add(new Todolist(id, user, res.getString(3),
212: res.getString(4), true));
213: }
214:
215: res.close();
216: st.close();
217: conn.close();
218:
219: return todolists;
220: }
221:
222: public Todolist getTodolist(String userName, int id)
223: throws Exception {
224: Connection conn = layer.getConnection();
225:
226: PreparedStatement st = conn
227: .prepareStatement("SELECT id, user_name, name, comment FROM todolists WHERE id = ?");
228: st.setInt(1, id);
229: ResultSet res = st.executeQuery();
230:
231: AccessController acl = Server.getInstance()
232: .getAccessController();
233: ArrayList todolists = new ArrayList();
234: while (res.next()) {
235: String user = res.getString(2);
236: if (user.equals(userName)
237: || acl.hasAccess(getName(), String.valueOf(id),
238: ACCESS_WRITE, userName))
239: todolists.add(new Todolist(id, user, res.getString(3),
240: res.getString(4)));
241: else if (acl.hasAccess(getName(), String.valueOf(id),
242: ACCESS_READ, userName))
243: todolists.add(new Todolist(id, user, res.getString(3),
244: res.getString(4), true));
245: }
246:
247: res.close();
248: st.close();
249: conn.close();
250:
251: if (todolists != null && todolists.size() == 1)
252: return (Todolist) todolists.get(0);
253: else
254: return null;
255: }
256:
257: public TodolistItem getTodolistItem(String userName, int id)
258: throws Exception {
259: Connection conn = layer.getConnection();
260:
261: PreparedStatement st = conn
262: .prepareStatement("SELECT id, id_list, name, comment, priority, completed, start_date, end_date, estimated_start_date, estimated_end_date FROM todolistitems WHERE id=?");
263: st.setInt(1, id);
264: ResultSet res = st.executeQuery();
265:
266: ArrayList todolistitems = new ArrayList();
267: while (res.next()) {
268: long tmp;
269: tmp = res.getLong(7);
270: Date start = (tmp > 0) ? new Date(tmp) : null;
271: tmp = res.getLong(8);
272: Date end = (tmp > 0) ? new Date(tmp) : null;
273: tmp = res.getLong(9);
274: Date estimatedStart = (tmp > 0) ? new Date(tmp) : null;
275: tmp = res.getLong(10);
276: Date estimatedEnd = (tmp > 0) ? new Date(tmp) : null;
277: todolistitems.add(new TodolistItem(res.getInt(1), res
278: .getInt(2), res.getString(3), res.getString(4), res
279: .getInt(5), res.getInt(6) == 1, start, end,
280: estimatedStart, estimatedEnd));
281: }
282:
283: res.close();
284: st.close();
285: conn.close();
286:
287: if (todolistitems != null && todolistitems.size() == 1)
288: return (TodolistItem) todolistitems.get(0);
289: else
290: return null;
291: }
292:
293: public ArrayList getTodolistItems(int idList) throws Exception {
294: Connection conn = layer.getConnection();
295:
296: PreparedStatement st = conn
297: .prepareStatement("SELECT id, id_list, name, comment, priority, completed, start_date, end_date, estimated_start_date, estimated_end_date FROM todolistitems WHERE id_list=?");
298: st.setInt(1, idList);
299: ResultSet res = st.executeQuery();
300:
301: ArrayList todolistitems = new ArrayList();
302: while (res.next()) {
303: long tmp;
304: tmp = res.getLong(7);
305: Date start = (tmp > 0) ? new Date(tmp) : null;
306: tmp = res.getLong(8);
307: Date end = (tmp > 0) ? new Date(tmp) : null;
308: tmp = res.getLong(9);
309: Date estimatedStart = (tmp > 0) ? new Date(tmp) : null;
310: tmp = res.getLong(10);
311: Date estimatedEnd = (tmp > 0) ? new Date(tmp) : null;
312: todolistitems.add(new TodolistItem(res.getInt(1), res
313: .getInt(2), res.getString(3), res.getString(4), res
314: .getInt(5), res.getInt(6) == 1, start, end,
315: estimatedStart, estimatedEnd));
316: }
317:
318: res.close();
319: st.close();
320: conn.close();
321:
322: return todolistitems;
323: }
324:
325: public Integer addTodolist(Todolist newTodolist) throws Exception {
326: Connection conn = layer.getConnection();
327:
328: PreparedStatement st = conn
329: .prepareStatement("INSERT INTO todolists (id, user_name, name, comment) VALUES (?, ?, ?, ?)");
330: newTodolist.setId(listsSequence.getNextId());
331: st.setInt(1, newTodolist.getId());
332: st.setString(2, newTodolist.getUserName());
333: st.setString(3, newTodolist.getName());
334: st.setString(4, newTodolist.getComment());
335: st.executeUpdate();
336: st.close();
337: conn.close();
338:
339: return new Integer(newTodolist.getId());
340: }
341:
342: public boolean modifyTodolist(int oldTodolistId,
343: Todolist newTodolist) throws Exception {
344: Connection conn = layer.getConnection();
345:
346: PreparedStatement st = conn
347: .prepareStatement("UPDATE todolists SET user_name=?, name=?, comment=? WHERE id=?");
348: st.setString(1, newTodolist.getUserName());
349: st.setString(2, newTodolist.getName());
350: st.setString(3, newTodolist.getComment());
351: st.setInt(4, oldTodolistId);
352: st.executeUpdate();
353: st.close();
354: conn.close();
355:
356: return true;
357: }
358:
359: public boolean deleteTodolist(int id) throws Exception {
360: Connection conn = layer.getConnection();
361:
362: PreparedStatement st = conn
363: .prepareStatement("DELETE FROM todolists WHERE id=?");
364: st.setInt(1, id);
365: st.executeUpdate();
366: st.close();
367:
368: st = conn
369: .prepareStatement("DELETE FROM todolistitems WHERE id_list=?");
370: st.setInt(1, id);
371: st.executeUpdate();
372: st.close();
373: conn.close();
374:
375: return true;
376: }
377:
378: public Integer addTodolistItem(TodolistItem newTodolistItem)
379: throws Exception {
380: Connection conn = layer.getConnection();
381:
382: PreparedStatement st = conn
383: .prepareStatement("SELECT count(*) FROM todolists WHERE id=?");
384: st.setInt(1, newTodolistItem.getParentTodolistId());
385: ResultSet res = st.executeQuery();
386: if (!res.next() || res.getInt(1) != 1) {
387: res.close();
388: st.close();
389: return null;
390: }
391: res.close();
392: st.close();
393:
394: st = conn
395: .prepareStatement("INSERT INTO todolistitems (id, name, comment, id_list, priority, completed, start_date, end_date, estimated_start_date, estimated_end_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
396: newTodolistItem.setId(itemsSequence.getNextId());
397: st.setInt(1, newTodolistItem.getId());
398: st.setString(2, newTodolistItem.getName());
399: st.setString(3, newTodolistItem.getComment());
400: st.setInt(4, newTodolistItem.getParentTodolistId());
401: st.setInt(5, newTodolistItem.getPriority());
402: st.setInt(6, newTodolistItem.isCompleted() ? 1 : 0);
403: if (newTodolistItem.getStartDate() != null)
404: st.setLong(7, newTodolistItem.getStartDate().getTime());
405: else
406: st.setNull(7, java.sql.Types.BIGINT);
407:
408: if (newTodolistItem.getEndDate() != null)
409: st.setLong(8, newTodolistItem.getEndDate().getTime());
410: else
411: st.setNull(8, java.sql.Types.BIGINT);
412:
413: if (newTodolistItem.getEstimatedStartDate() != null)
414: st.setLong(9, newTodolistItem.getEstimatedStartDate()
415: .getTime());
416: else
417: st.setNull(9, java.sql.Types.BIGINT);
418:
419: if (newTodolistItem.getEstimatedEndDate() != null)
420: st.setLong(10, newTodolistItem.getEstimatedEndDate()
421: .getTime());
422: else
423: st.setNull(10, java.sql.Types.BIGINT);
424:
425: st.executeUpdate();
426: st.close();
427:
428: conn.close();
429:
430: return new Integer(newTodolistItem.getId());
431: }
432:
433: public boolean modifyTodolistItem(int oldTodolistItemId,
434: TodolistItem newTodolistItem) throws Exception {
435: Connection conn = layer.getConnection();
436:
437: PreparedStatement st = conn
438: .prepareStatement("UPDATE todolistitems SET name=?, comment=?, id_list=?, priority=?, completed=?, start_date=?, end_date=?, estimated_start_date=?, estimated_end_date=? WHERE id=?");
439: st.setString(1, newTodolistItem.getName());
440: st.setString(2, newTodolistItem.getComment());
441: st.setInt(3, newTodolistItem.getParentTodolistId());
442: st.setInt(4, newTodolistItem.getPriority());
443: st.setInt(5, newTodolistItem.isCompleted() ? 1 : 0);
444:
445: if (newTodolistItem.getStartDate() != null)
446: st.setLong(6, newTodolistItem.getStartDate().getTime());
447: else
448: st.setNull(6, java.sql.Types.BIGINT);
449: if (newTodolistItem.getEndDate() != null)
450: st.setLong(7, newTodolistItem.getEndDate().getTime());
451: else
452: st.setNull(7, java.sql.Types.BIGINT);
453:
454: if (newTodolistItem.getEstimatedStartDate() != null)
455: st.setLong(8, newTodolistItem.getEstimatedStartDate()
456: .getTime());
457: else
458: st.setNull(8, java.sql.Types.BIGINT);
459: if (newTodolistItem.getEstimatedEndDate() != null)
460: st.setLong(9, newTodolistItem.getEstimatedEndDate()
461: .getTime());
462: else
463: st.setNull(9, java.sql.Types.BIGINT);
464:
465: st.setInt(10, oldTodolistItemId);
466: st.executeUpdate();
467: st.close();
468: conn.close();
469:
470: return true;
471: }
472:
473: public boolean deleteTodolistItem(int id) throws Exception {
474: Connection conn = layer.getConnection();
475:
476: PreparedStatement st = conn
477: .prepareStatement("DELETE FROM todolistitems WHERE id=?");
478: st.setInt(1, id);
479: st.executeUpdate();
480: st.close();
481: conn.close();
482:
483: return true;
484: }
485:
486: //-- acl
487: public AclInfo[] getAcls(String listId) throws Exception {
488: AccessController acl = Server.getInstance()
489: .getAccessController();
490: return acl.getAcls(getName(), listId);
491: }
492:
493: public ArrayList getAllGroups() throws Exception {
494: ArrayList groups = new ArrayList();
495: Iterator i = Server.getInstance().getStore().getGroupStore()
496: .getAllGroups();
497: while (i.hasNext()) {
498: GroupConcept concept = (GroupConcept) i.next();
499: groups.add(concept.getName());
500: }
501:
502: return groups;
503: }
504:
505: public ArrayList getAllUsers() throws Exception {
506: ArrayList users = new ArrayList();
507: Iterator i = Server.getInstance().getStore().getUserStore()
508: .getAllUsers();
509: while (i.hasNext()) {
510: UserConcept concept = (UserConcept) i.next();
511: users.add(concept.getName());
512: }
513:
514: return users;
515: }
516:
517: public void addAcl(String listId, AclInfo aclInfo) throws Exception {
518: AccessController acl = Server.getInstance()
519: .getAccessController();
520: if (aclInfo.getGroup() != null) {
521: if (aclInfo.isAllow())
522: acl.allowGroup(getName(), listId, aclInfo.getAccess(),
523: aclInfo.getGroup());
524: else
525: acl.denyGroup(getName(), listId, aclInfo.getAccess(),
526: aclInfo.getGroup());
527: } else if (aclInfo.getUser() != null) {
528: if (aclInfo.isAllow())
529: acl.allowUser(getName(), listId, aclInfo.getAccess(),
530: aclInfo.getUser());
531: else
532: acl.denyUser(getName(), listId, aclInfo.getAccess(),
533: aclInfo.getUser());
534: }
535: }
536:
537: public void removeAcl(String listId, AclInfo aclInfo)
538: throws Exception {
539: AccessController acl = Server.getInstance()
540: .getAccessController();
541: if (aclInfo.getGroup() != null)
542: acl.removeAclForGroup(getName(), listId, aclInfo
543: .getAccess(), aclInfo.getGroup());
544: else if (aclInfo.getUser() != null)
545: acl.removeAclForUser(getName(), listId,
546: aclInfo.getAccess(), aclInfo.getUser());
547: }
548:
549: //introduced for acl webapp
550: public String[] getAccesses() {
551: return new String[] { ACCESS_WRITE, ACCESS_READ };
552: }
553:
554: public String getItemName(String itemId) throws Exception {
555: String name = itemId;
556:
557: Connection conn = layer.getConnection();
558:
559: PreparedStatement st = conn
560: .prepareStatement("SELECT name FROM todolists WHERE id = ?");
561: st.setString(1, itemId);
562: ResultSet res = st.executeQuery();
563: if (res.next())
564: name = res.getString(1);
565:
566: res.close();
567: st.close();
568: conn.close();
569:
570: return name;
571: }
572:
573: }
|