KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > todolist > TodolistService


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Jonathan Riboux <jonathan.riboux@wanadoo.Fr>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.applications.todolist;
21
22 import java.sql.Connection JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
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 JavaDoc ACCESS_WRITE = "write";
46     public static final String JavaDoc 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 JavaDoc dbDescription = getDirectory() + "db-todolist.xml";
55             layer.getTableCreator().createFromXml(dbDescription);
56         } catch (Exception JavaDoc 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 JavaDoc tl = getTodolists((String JavaDoc)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 JavaDoc tli = getTodolistItems(((Integer JavaDoc)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 JavaDoc 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 JavaDoc[] tmpobj = (Object JavaDoc[])tla.getParam();
104                     if (modifyTodolist(((Integer JavaDoc)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 JavaDoc)tla.getParam()).intValue()))
113                         oc.write("OK");
114                     else
115                         oc.write("FAILED");
116                     break;
117
118                 case TodolistAction.ADD_TODOLISTITEM :
119                     Integer JavaDoc 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 JavaDoc[] tmpobj = (Object JavaDoc[])tla.getParam();
130                     if (modifyTodolistItem(((Integer JavaDoc)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 JavaDoc)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 JavaDoc)tla.getParam());
146                     oc.write("OK");
147                     oc.write(acl);
148                     break;
149                     
150                 case TodolistAction.GET_GROUPS:
151                     ArrayList JavaDoc groups = getAllGroups();
152                     oc.write("OK");
153                     oc.write(groups);
154                     break;
155                     
156                 case TodolistAction.GET_USERS:
157                     ArrayList JavaDoc users = getAllUsers();
158                     oc.write("OK");
159                     oc.write(users);
160                     break;
161                     
162                 case TodolistAction.ADD_ACL:
163                     addAcl((String JavaDoc)tla.getParam(), (AclInfo)tla.getOption());
164                     oc.write("OK");
165                     break;
166                     
167                 case TodolistAction.REMOVE_ACL:
168                     removeAcl((String JavaDoc)tla.getParam(), (AclInfo)tla.getOption());
169                     oc.write("OK");
170                     break;
171
172             }
173         } catch (Exception JavaDoc e) {
174             e.printStackTrace();
175             try {
176                 oc.write("FAILED " + e);
177             } catch (Exception JavaDoc e2) {
178                 e2.printStackTrace();
179             }
180         }
181     }
182     
183     public ArrayList JavaDoc getTodolists(String JavaDoc userName) throws Exception JavaDoc {
184         Connection JavaDoc conn = layer.getConnection();
185         
186         PreparedStatement JavaDoc st = conn.prepareStatement(
187         "SELECT id, user_name, name, comment FROM todolists");
188         ResultSet JavaDoc res = st.executeQuery();
189         
190         AccessController acl = Server.getInstance().getAccessController();
191         ArrayList JavaDoc todolists = new ArrayList JavaDoc();
192         while (res.next()) {
193             int id = res.getInt(1);
194             String JavaDoc 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 JavaDoc userName, int id) throws Exception JavaDoc {
209         Connection JavaDoc conn = layer.getConnection();
210
211         PreparedStatement JavaDoc st = conn.prepareStatement(
212         "SELECT id, user_name, name, comment FROM todolists WHERE id = ?");
213         st.setInt(1, id);
214         ResultSet JavaDoc res = st.executeQuery();
215
216         AccessController acl = Server.getInstance().getAccessController();
217         ArrayList JavaDoc todolists = new ArrayList JavaDoc();
218         while (res.next()) {
219             String JavaDoc 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 JavaDoc userName, int id) throws Exception JavaDoc {
237         Connection JavaDoc conn = layer.getConnection();
238
239         PreparedStatement JavaDoc 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 JavaDoc res = st.executeQuery();
243
244         ArrayList JavaDoc todolistitems = new ArrayList JavaDoc();
245         while (res.next()) {
246             long tmp;
247             tmp = res.getLong(7);
248             Date JavaDoc start = (tmp > 0) ? new Date JavaDoc(tmp) : null;
249             tmp = res.getLong(8);
250             Date JavaDoc end = (tmp > 0) ? new Date JavaDoc(tmp) : null;
251             tmp = res.getLong(9);
252             Date JavaDoc estimatedStart = (tmp > 0) ? new Date JavaDoc(tmp) : null;
253             tmp = res.getLong(10);
254             Date JavaDoc estimatedEnd = (tmp > 0) ? new Date JavaDoc(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 JavaDoc getTodolistItems(int idList) throws Exception JavaDoc {
269         Connection JavaDoc conn = layer.getConnection();
270
271         PreparedStatement JavaDoc 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 JavaDoc res = st.executeQuery();
275         
276         ArrayList JavaDoc todolistitems = new ArrayList JavaDoc();
277         while (res.next()) {
278             long tmp;
279             tmp = res.getLong(7);
280             Date JavaDoc start = (tmp > 0) ? new Date JavaDoc(tmp) : null;
281             tmp = res.getLong(8);
282             Date JavaDoc end = (tmp > 0) ? new Date JavaDoc(tmp) : null;
283             tmp = res.getLong(9);
284             Date JavaDoc estimatedStart = (tmp > 0) ? new Date JavaDoc(tmp) : null;
285             tmp = res.getLong(10);
286             Date JavaDoc estimatedEnd = (tmp > 0) ? new Date JavaDoc(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 JavaDoc addTodolist(Todolist newTodolist) throws Exception JavaDoc {
298         Connection JavaDoc conn = layer.getConnection();
299         
300         PreparedStatement JavaDoc 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 JavaDoc(newTodolist.getId());
312     }
313     
314     public boolean modifyTodolist(int oldTodolistId, Todolist newTodolist) throws Exception JavaDoc {
315         Connection JavaDoc conn = layer.getConnection();
316
317         PreparedStatement JavaDoc 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 JavaDoc {
331         Connection JavaDoc conn = layer.getConnection();
332
333         PreparedStatement JavaDoc 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 JavaDoc addTodolistItem(TodolistItem newTodolistItem) throws Exception JavaDoc {
350         Connection JavaDoc conn = layer.getConnection();
351         
352         PreparedStatement JavaDoc st = conn.prepareStatement(
353         "SELECT count(*) FROM todolists WHERE id=?");
354         st.setInt(1, newTodolistItem.getParentTodolistId());
355         ResultSet JavaDoc 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 JavaDoc(newTodolistItem.getId());
399     }
400     
401     public boolean modifyTodolistItem(int oldTodolistItemId, TodolistItem newTodolistItem) throws Exception JavaDoc {
402         Connection JavaDoc conn = layer.getConnection();
403
404         PreparedStatement JavaDoc 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 JavaDoc {
439         Connection JavaDoc conn = layer.getConnection();
440
441         PreparedStatement JavaDoc 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     //-- acl
452
public AclInfo[] getAcls(String JavaDoc listId)
453     throws Exception JavaDoc
454     {
455         AccessController acl = Server.getInstance().getAccessController();
456         return acl.getAcls(getName(), listId);
457     }
458     
459     public ArrayList JavaDoc getAllGroups()
460     throws Exception JavaDoc
461     {
462         ArrayList JavaDoc groups = new ArrayList JavaDoc();
463         Iterator JavaDoc 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 JavaDoc getAllUsers()
474     throws Exception JavaDoc
475     {
476         ArrayList JavaDoc users = new ArrayList JavaDoc();
477         Iterator JavaDoc 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 JavaDoc listId, AclInfo aclInfo)
488     throws Exception JavaDoc
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 JavaDoc listId, AclInfo aclInfo)
508     throws Exception JavaDoc
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     //introduced for acl webapp
518
public String JavaDoc[] getAccesses() {
519         return new String JavaDoc[] {ACCESS_WRITE, ACCESS_READ};
520     }
521
522     public String JavaDoc getItemName(String JavaDoc itemId) throws Exception JavaDoc {
523         String JavaDoc name = itemId;
524
525         Connection JavaDoc conn = layer.getConnection();
526
527         PreparedStatement JavaDoc st = conn.prepareStatement(
528         "SELECT name FROM todolists WHERE id = ?");
529         st.setString(1, itemId);
530         ResultSet JavaDoc 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