KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > notes > NotesService


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
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 package org.lucane.applications.notes;
20
21 import org.lucane.common.*;
22 import org.lucane.common.net.ObjectConnection;
23 import org.lucane.server.*;
24 import org.lucane.server.database.*;
25 import org.lucane.server.database.util.Sequence;
26
27 import java.util.*;
28 import java.sql.*;
29 import java.io.*;
30
31 public class NotesService
32 extends Service
33 {
34   private DatabaseAbstractionLayer layer = null;
35   private Sequence notesSequence;
36   private Sequence commentsSequence;
37
38   public void init(Server parent)
39   {
40       layer = parent.getDBLayer();
41       notesSequence = new Sequence("notes", "id");
42       commentsSequence = new Sequence("notes_comments", "id");
43   }
44
45   public void install()
46   {
47     try {
48         String JavaDoc dbDescription = getDirectory() + "db-notes.xml";
49         layer.getTableCreator().createFromXml(dbDescription);
50     } catch (Exception JavaDoc e) {
51         Logging.getLogger().severe("Unable to install NotesService !");
52         e.printStackTrace();
53     }
54   }
55
56   public void process(ObjectConnection oc, Message message)
57   {
58     String JavaDoc user = message.getSender().getName();
59     NotesAction action = (NotesAction)message.getData();
60     try {
61         Serializable res = executeAction(action, user);
62         oc.write("OK");
63         
64         if(res != null)
65             oc.write(res);
66     } catch(Exception JavaDoc e) {
67         try {
68             oc.write("FAILED " + e.getMessage());
69         } catch(IOException ioe) {
70             Logging.getLogger().warning("Error : " + ioe);
71         }
72     }
73   }
74
75   private Serializable executeAction(NotesAction action, String JavaDoc user)
76   throws Exception JavaDoc
77   {
78     switch(action.getAction())
79     {
80         case NotesAction.SAVE_NOTE:
81             return saveNote((Note)action.getParam());
82         case NotesAction.DELETE_NOTE:
83             deleteNote((String JavaDoc)action.getParam());
84             return null;
85         case NotesAction.GET_PERSONNAL_NOTES:
86             return getPersonnalNotes(user);
87         case NotesAction.GET_PUBLISHED_AUTHORS:
88             return getPublishedAuthors();
89         case NotesAction.GET_RECENT_PUBLISHED_NOTES:
90             return getRecentPublishedNotes((Integer JavaDoc)action.getParam());
91         case NotesAction.GET_PUBLISHED_NOTES_BY_AUTHOR:
92             return getPublishedNotesByAuthor((String JavaDoc)action.getParam());
93         case NotesAction.SAVE_COMMENT:
94             saveComment((Comment)action.getParam());
95             return null;
96         case NotesAction.GET_COMMENTS_FOR_NOTE:
97             return getCommentsForNote((String JavaDoc)action.getParam());
98     }
99     
100     return null;
101   }
102
103
104   //-- actions (notes)
105

106   public synchronized Note saveNote(Note note)
107     throws Exception JavaDoc
108   {
109     long editionDate = 0;
110
111     Connection connex = layer.getConnection();
112
113     
114     //new note, fetch a new id
115
if(note.getId() == null)
116     {
117         note.setId(String.valueOf(notesSequence.getNextId()));
118     }
119     //edition, remove old note
120
else
121     {
122       editionDate = new java.util.Date JavaDoc().getTime();
123
124         PreparedStatement delete = connex.prepareStatement("DELETE FROM notes WHERE id=?");
125         delete.setString(1, note.getId());
126         delete.execute();
127         delete.close();
128     }
129     
130     PreparedStatement insert = connex.prepareStatement("INSERT INTO notes VALUES(?,?,?,?,?,?,?,?)");
131     insert.setString(1, note.getId());
132     insert.setString(2, note.getAuthor());
133     insert.setString(3, note.getTitle());
134     insert.setString(4, note.getContent());
135     insert.setLong(5, note.getCreationDate().getTime());
136     insert.setLong(6, editionDate);
137     insert.setString(7, note.isPublic() ? "1":"0");
138     insert.setString(8, note.isCommentable() ? "1":"0");
139     insert.execute();
140     insert.close();
141     
142
143     connex.close();
144     
145     return note;
146   }
147
148   public void deleteNote(String JavaDoc noteId)
149     throws Exception JavaDoc
150   {
151     Connection connex = layer.getConnection();
152
153     PreparedStatement delete = connex.prepareStatement("DELETE FROM notes WHERE id=?");
154     delete.setString(1, noteId);
155     delete.execute();
156     delete.close();
157   
158     delete = connex.prepareStatement("DELETE FROM notes_comments WHERE idnote=?");
159     delete.setString(1, noteId);
160     delete.execute();
161     delete.close();
162     
163     connex.close();
164   }
165
166   
167   public Note getNoteById(String JavaDoc noteId)
168     throws Exception JavaDoc
169   {
170     Note note = null;
171       Connection connex = layer.getConnection();
172
173     PreparedStatement select = connex.prepareStatement(
174         "SELECT * FROM notes WHERE id=?");
175     select.setString(1, noteId);
176     ResultSet rs = select.executeQuery();
177         
178     if(rs.next())
179     {
180         note = new Note(
181                 rs.getString(1), //id
182
rs.getString(2), //author
183
rs.getString(3), //title
184
rs.getString(4), //content
185
rs.getString(5), //creationDate
186
rs.getString(6), //editionDate
187
rs.getString(7), //isPublic
188
rs.getString(8)); //commentable
189

190     
191     }
192
193     rs.close();
194     select.close();
195     connex.close();
196         
197         if(note == null)
198             throw new Exception JavaDoc("No such note : " + noteId);
199     
200     return note;
201   }
202
203   
204   public Object JavaDoc[] getPersonnalNotes(String JavaDoc author)
205     throws Exception JavaDoc
206   {
207     Connection connex = layer.getConnection();
208
209     ArrayList result = new ArrayList();
210     PreparedStatement select = connex.prepareStatement(
211         "SELECT * FROM notes WHERE author=? ORDER BY author");
212     select.setString(1, author);
213     ResultSet rs = select.executeQuery();
214
215     while(rs.next())
216     {
217         Note n = new Note(
218                 rs.getString(1), //id
219
rs.getString(2), //author
220
rs.getString(3), //title
221
rs.getString(4), //content
222
rs.getString(5), //creationDate
223
rs.getString(6), //editionDate
224
rs.getString(7), //isPublic
225
rs.getString(8)); //commentable
226

227         result.add(n);
228     }
229
230     rs.close();
231     select.close();
232     connex.close();
233     
234     return result.toArray();
235   }
236
237   public Object JavaDoc[] getPublishedAuthors()
238     throws Exception JavaDoc
239   {
240     Connection connex = layer.getConnection();
241
242     ArrayList result = new ArrayList();
243     PreparedStatement select = connex.prepareStatement(
244         "SELECT distinct author FROM notes WHERE isPublic=1");
245     ResultSet rs = select.executeQuery();
246
247     while(rs.next())
248     {
249         String JavaDoc author = rs.getString(1);
250         result.add(author);
251     }
252
253     rs.close();
254     select.close();
255     connex.close();
256     
257     return result.toArray();
258   }
259
260   public Object JavaDoc[] getRecentPublishedNotes(Integer JavaDoc max)
261     throws Exception JavaDoc
262   {
263     Connection connex = layer.getConnection();
264
265     ArrayList result = new ArrayList();
266     PreparedStatement select = connex.prepareStatement(
267         "SELECT * FROM notes WHERE isPublic=1 ORDER BY creationDate");
268     ResultSet rs = select.executeQuery();
269
270
271     for(int i=0;i<max.intValue() && rs.next();i++)
272     {
273         Note n = new Note(
274                 rs.getString(1), //id
275
rs.getString(2), //author
276
rs.getString(3), //title
277
rs.getString(4), //content
278
rs.getString(5), //creationDate
279
rs.getString(6), //editionDate
280
rs.getString(7), //isPublic
281
rs.getString(8)); //commentable
282

283         result.add(n);
284     }
285
286     rs.close();
287     select.close();
288     connex.close();
289     
290     return result.toArray();
291   }
292
293   public Object JavaDoc[] getPublishedNotesByAuthor(String JavaDoc author)
294     throws Exception JavaDoc
295   {
296     Connection connex = layer.getConnection();
297
298     ArrayList result = new ArrayList();
299     PreparedStatement select = connex.prepareStatement(
300         "SELECT * FROM notes WHERE author=? AND isPublic=1 ORDER BY creationDate");
301     select.setString(1, author);
302     ResultSet rs = select.executeQuery();
303
304     while(rs.next())
305     {
306         Note n = new Note(
307                 rs.getString(1), //id
308
rs.getString(2), //author
309
rs.getString(3), //title
310
rs.getString(4), //content
311
rs.getString(5), //creationDate
312
rs.getString(6), //editionDate
313
rs.getString(7), //isPublic
314
rs.getString(8)); //commentable
315

316         result.add(n);
317     }
318
319     rs.close();
320     select.close();
321     connex.close();
322     
323     return result.toArray();
324   }
325
326   
327   //-- actions (comments)
328

329   public synchronized void saveComment(Comment comment)
330     throws Exception JavaDoc
331   {
332     Connection connex = layer.getConnection();
333
334     //fetch a new id
335
comment.setId(String.valueOf(commentsSequence.getNextId()));
336
337     PreparedStatement insert = connex.prepareStatement(
338         "INSERT INTO notes_comments VALUES(?,?,?,?,?,?)");
339     insert.setString(1, comment.getId());
340     insert.setString(2, comment.getNoteId());
341     insert.setString(3, comment.getAuthor());
342     insert.setString(4, comment.getTitle());
343     insert.setString(5, comment.getContent());
344     insert.setLong(6, comment.getCreationDate().getTime());
345     insert.execute();
346     insert.close();
347     
348     connex.close();
349   }
350
351   public Object JavaDoc[] getCommentsForNote(String JavaDoc idNote)
352     throws Exception JavaDoc
353   {
354     Connection connex = layer.getConnection();
355
356     ArrayList result = new ArrayList();
357     PreparedStatement select = connex.prepareStatement(
358         "SELECT * FROM notes_comments WHERE idnote=? ORDER BY creationDate");
359     select.setString(1, idNote);
360     ResultSet rs = select.executeQuery();
361
362     while(rs.next())
363     {
364         Comment c = new Comment(
365                 rs.getString(1), //id
366
rs.getString(2), //idnote
367
rs.getString(3), //author
368
rs.getString(4), //title
369
rs.getString(5), //content
370
rs.getString(6)); //creationDate
371

372         result.add(c);
373     }
374
375     rs.close();
376     select.close();
377     connex.close();
378     
379     return result.toArray();
380   }
381 }
382
383
Popular Tags