KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > internal > managers > ComicManager


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Manager for the comic area.
28  */

29 package net.killingar.forum.internal.managers;
30
31 import net.killingar.forum.internal.*;
32
33 import java.sql.Connection JavaDoc;
34 import java.sql.ResultSet JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Statement JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39
40 public class ComicManager extends AbstractManager implements java.io.Serializable JavaDoc
41 {
42     /**
43      * Add a comic.
44      */

45     public void addComic(Comic comic) throws SQLException JavaDoc, AccessDeniedException
46     {
47         manager.checkMyAccess(AccessLevel.addComic);
48
49         Connection JavaDoc c = null;
50         Statement JavaDoc statement = null;
51         ResultSet JavaDoc result = null;
52
53         try
54         {
55             c = getNewConnection();
56             statement = c.createStatement();
57
58             statement.executeUpdate("insert into Comics (Name, URL, System, Position) values ('"+
59                 Utils.disableSQL(comic.name)+"', '"+
60                 Utils.disableSQL(comic.URL)+"', '"+
61                 Utils.disableSQL(comic.system)+"', '"+
62                 Utils.disableSQL(comic.position)+"')");
63         }
64         finally { closeAll(c, statement, result); }
65     }
66
67     /**
68      * Remove a comic and thereby all strips it contains.
69      */

70     public void removeComic(long comicID) throws SQLException JavaDoc, AccessDeniedException
71     {
72         manager.checkMyAccess(AccessLevel.removeComic);
73
74         Connection JavaDoc c = null;
75         Statement JavaDoc statement = null;
76         ResultSet JavaDoc result = null;
77
78         try
79         {
80             c = getNewConnection();
81             statement = c.createStatement();
82
83             statement.executeUpdate("delete from Comics where ID = "+comicID);
84             statement.executeUpdate("delete from Strips where manager = "+comicID);
85         }
86         finally { closeAll(c, statement, result); }
87     }
88
89     /**
90      * Change a comic. (You cannot change ID)
91      */

92     public void changeComic(Comic comic) throws SQLException JavaDoc, AccessDeniedException
93     {
94         manager.checkMyAccess(AccessLevel.changeComic);
95
96         Connection JavaDoc c = null;
97         Statement JavaDoc statement = null;
98         ResultSet JavaDoc result = null;
99
100         try
101         {
102             c = getNewConnection();
103             statement = c.createStatement();
104
105             statement.executeUpdate("update Comics set Name = '"+Utils.disableSQL(comic.name)+"', URL = '"+Utils.disableSQL(comic.URL)+"', Latest = "+comic.latestID+", First = "+comic.firstID+", Position = '"+Utils.disableSQL(comic.position)+"', System = '"+Utils.disableSQL(comic.system)+"' where ID = "+comic.ID);
106         }
107         finally { closeAll(c, statement, result); }
108     }
109
110     /**
111      * Add a strip.
112      */

113     public long addStrip(Strip strip) throws SQLException JavaDoc, AccessDeniedException
114     {
115         manager.checkMyAccess(AccessLevel.changeComic);
116
117         Connection JavaDoc c = null;
118         Statement JavaDoc statement = null;
119         ResultSet JavaDoc result = null;
120
121         try
122         {
123             c = getNewConnection();
124             statement = c.createStatement();
125
126             result = statement.executeQuery("select Latest from Comics where ID = "+strip.parentID);
127             result.next();
128
129             if (result.getString(1) != null)
130                 strip.prevID = result.getLong(1);
131             statement.executeUpdate("insert into Strips (Comic, URL, Prev, Time, Description, SystemDescription) values ("+strip.parentID+", '"+Utils.disableSQL(strip.URL)+"', "+result.getString(1)+", '"+strip.time+"', '"+Utils.disableSQL(strip.description)+"', '"+Utils.disableSQL(strip.systemDescription)+"')");
132             result = statement.getGeneratedKeys();
133             result.next();
134
135             strip.ID = result.getLong(1);
136             statement.executeUpdate("update Strips set Next = "+strip.ID+" where ID = "+strip.prevID);
137             statement.executeUpdate("update Comics set Latest = "+strip.ID+" where ID = "+strip.parentID);
138             result = statement.executeQuery("select First from Comics where ID = "+strip.parentID);
139
140             result.next();
141             if (result.getString(1) == null)
142                 statement.executeUpdate("update Comics set First = "+strip.ID+" where ID = "+strip.parentID);
143
144             return strip.ID;
145         }
146         finally { closeAll(c, statement, result); }
147     }
148
149     /**
150      * Remove a strip.
151      */

152     public void removeStrip(long ID) throws SQLException JavaDoc, AccessDeniedException
153     {
154         manager.checkMyAccess(AccessLevel.changeComic);
155
156         Connection JavaDoc c = null;
157         Statement JavaDoc statement = null;
158         ResultSet JavaDoc result = null;
159
160         try
161         {
162             c = getNewConnection();
163             statement = c.createStatement();
164
165             Strip strip = getStrip(ID);
166             if (strip.prevID == -1 && strip.nextID == -1) // only strip
167
{
168                 statement.executeUpdate("update Comics set First = NULL, Latest = NULL where ID = "+strip.parentID);
169
170                 statement.executeUpdate("delete from ComicPositions where Position = "+strip.ID);
171             }
172
173             else if (strip.prevID == -1) // firstID strip
174
{
175                 Strip nextID = getStrip(strip.nextID);
176                 statement.executeUpdate("update Comics set First = "+nextID.ID+" where ID = "+strip.parentID);
177                 statement.executeUpdate("update Strips set Prev = NULL where ID = "+nextID.ID);
178
179                 statement.executeUpdate("delete from ComicPositions where Position = "+strip.ID);
180             }
181
182             else if (strip.nextID == -1) // last strip
183
{
184                 Strip prevID = getStrip(strip.prevID);
185                 statement.executeUpdate("update Comics set Latest = "+prevID.ID+" where ID = "+strip.parentID);
186                 statement.executeUpdate("update Strips set Next = NULL where ID = "+prevID.ID);
187
188                 statement.executeUpdate("update ComicPositions set Position = "+prevID.ID+" where Position = "+strip.ID);
189             }
190
191             else // in the middle somewhere
192
{
193                 Strip
194                     nextID = getStrip(strip.nextID),
195                     prevID = getStrip(strip.prevID);
196                 statement.executeUpdate("update Strips set Prev = "+prevID.ID+" where ID = "+nextID.ID);
197                 statement.executeUpdate("update Strips set Next = "+nextID.ID+" where ID = "+prevID.ID);
198
199                 statement.executeUpdate("update ComicPositions set Position = "+prevID.ID+" where Position = "+strip.ID);
200             }
201
202             statement.executeUpdate("delete from Strips where ID = "+strip.ID);
203         }
204         finally { closeAll(c, statement, result); }
205     }
206
207     /**
208      * Change a strip. (You can not change id, manager or systemDescription).
209      */

210     public void changeStrip(Strip strip) throws SQLException JavaDoc, AccessDeniedException
211     {
212         manager.checkMyAccess(AccessLevel.changeComic);
213
214         Connection JavaDoc c = null;
215         Statement JavaDoc statement = null;
216         ResultSet JavaDoc result = null;
217
218         try
219         {
220             c = getNewConnection();
221             statement = c.createStatement();
222
223             statement.executeUpdate("update Strips set URL = '"+Utils.disableSQL(strip.URL)+"', LocalURL = "+((strip.localURL == null)?"null":"'"+Utils.disableSQL(strip.localURL)+"'")+", Next = "+strip.nextID+", Prev = "+strip.prevID+", Time = '"+strip.time+"', Description = "+((strip.description == null)?"null":"'"+Utils.disableSQL(strip.description)+"'")+" where ID = "+strip.ID);
224         }
225         finally { closeAll(c, statement, result); }
226     }
227
228     /**
229      * Get a list of the comics.
230      */

231     public Comic[] getComics() throws SQLException JavaDoc, AccessDeniedException
232     {
233         manager.checkMyAccess(AccessLevel.comics);
234
235         Connection JavaDoc c = null;
236         Statement JavaDoc statement = null;
237         ResultSet JavaDoc result = null;
238
239         try
240         {
241             c = getNewConnection();
242             statement = c.createStatement();
243
244             result = statement.executeQuery("select ID, Name, URL, Latest, First, System, Position from Comics");
245             List JavaDoc v = new ArrayList JavaDoc();
246             while(result.next())
247             {
248                 v.add(new Comic(
249                     result.getLong(1),
250                     result.getString(2),
251                     result.getString(3),
252                     result.getString(4) == null? -1: result.getLong(4),
253                     result.getString(5) == null? -1: result.getLong(5),
254                     result.getString(6),
255                     result.getString(7)));
256             }
257
258             Comic r[] = new Comic[v.size()];
259             v.toArray(r);
260
261             return r;
262         }
263         finally { closeAll(c, statement, result); }
264     }
265
266     /**
267      * Get the number of strips in all the comics.
268      */

269     public long[][] getNumberOfStripsInComics() throws SQLException JavaDoc, AccessDeniedException
270     {
271         manager.checkMyAccess(AccessLevel.comics);
272
273         Connection JavaDoc c = null;
274         Statement JavaDoc statement = null;
275         ResultSet JavaDoc result = null;
276
277         try
278         {
279             c = getNewConnection();
280             statement = c.createStatement();
281
282             result = statement.executeQuery("select Comics.ID, count(Strips.ID) from Comics, Strip where Comic.ID = Strip.Comic group by Comic.ID");
283             List JavaDoc
284                 a = new ArrayList JavaDoc(),
285                 b = new ArrayList JavaDoc();
286             for(int i = 0; result.next(); i++)
287             {
288                 a.add(result.getString(1));
289                 b.add(result.getString(2));
290             }
291
292             if (a.size() == 0 || b.size() != a.size())return null;
293             long r[][] = new long[2][a.size()];
294             for(int i = 0; i < a.size(); i++)
295             {
296                 r[0][i] = Long.parseLong((String JavaDoc)a.get(i));
297                 r[1][i] = Long.parseLong((String JavaDoc)b.get(i));
298             }
299
300
301             return r;
302         }
303         finally { closeAll(c, statement, result); }
304     }
305
306     /**
307      * Get the number of comics.
308      */

309     public long getNumberOfComics() throws SQLException JavaDoc, AccessDeniedException
310     {
311         manager.checkMyAccess(AccessLevel.comics);
312
313         Connection JavaDoc c = null;
314         Statement JavaDoc statement = null;
315         ResultSet JavaDoc result = null;
316
317         try
318         {
319             c = getNewConnection();
320             statement = c.createStatement();
321
322             result = statement.executeQuery("select count(*) from Comic");
323             long r = 0;
324             if (result.next())
325                 r = result.getLong(1);
326
327             return r;
328         }
329         finally { closeAll(c, statement, result); }
330     }
331
332     /**
333      * Get the data for a comic.
334      */

335     public Comic getComic(long comicID) throws SQLException JavaDoc, AccessDeniedException
336     {
337         manager.checkMyAccess(AccessLevel.comics);
338
339         Connection JavaDoc c = null;
340         Statement JavaDoc statement = null;
341         ResultSet JavaDoc result = null;
342
343         try
344         {
345             c = getNewConnection();
346             statement = c.createStatement();
347
348             result = statement.executeQuery("select ID, Name, URL, Latest, First, System, Position from Comics where ID = "+comicID);
349             Comic r = null;
350             if (result.next())
351             {
352                 r = new Comic(
353                     result.getLong(1),
354                     result.getString(2),
355                     result.getString(3),
356                     result.getString(4) == null? -1: result.getLong(4),
357                     result.getString(5) == null? -1: result.getLong(5),
358                     result.getString(6),
359                     result.getString(7));
360             }
361
362             return r;
363         }
364         finally { closeAll(c, statement, result); }
365     }
366
367     /**
368      * Get the data for a strip.
369      */

370     public Strip getStrip(long stripID) throws SQLException JavaDoc, AccessDeniedException
371     {
372         manager.checkMyAccess(AccessLevel.comics);
373
374         Connection JavaDoc c = null;
375         Statement JavaDoc statement = null;
376         ResultSet JavaDoc result = null;
377
378         try
379         {
380             c = getNewConnection();
381             statement = c.createStatement();
382
383             result = statement.executeQuery("select ID, Comic, URL, LocalURL, Next, Prev, Time, Description, SystemDescription from Strips where ID = "+stripID);
384             Strip r = null;
385
386             if (result.next())
387             {
388                 r = new Strip(
389                     result.getLong(1),
390                     result.getLong(2),
391                     result.getString(3),
392                     result.getString(4),
393                     result.getString(5) == null? -1: result.getLong(5),
394                     result.getString(6) == null? -1: result.getLong(6),
395                     result.getDate(7),
396                     result.getString(8),
397                     result.getString(9));
398             }
399
400             return r;
401         }
402         finally { closeAll(c, statement, result); }
403     }
404
405     /**
406      * Get the strips in a comic.
407      */

408     public Strip[] getStrips(long comicID) throws SQLException JavaDoc, AccessDeniedException
409     {
410         manager.checkMyAccess(AccessLevel.comics);
411
412         Connection JavaDoc c = null;
413         Statement JavaDoc statement = null;
414         ResultSet JavaDoc result = null;
415
416         try
417         {
418             c = getNewConnection();
419             statement = c.createStatement();
420
421             result = statement.executeQuery("select ID, Comic, URL, LocalURL, Next, Prev, Time, Description, SystemDescription from Strips where Comic = "+comicID);
422             List JavaDoc v = new ArrayList JavaDoc();
423             while(result.next())
424             {
425                 v.add(new Strip(
426                     result.getLong(1),
427                     result.getLong(2),
428                     result.getString(3),
429                     result.getString(4),
430                     result.getString(5) == null? -1: result.getLong(5),
431                     result.getString(6) == null? -1: result.getLong(6),
432                     result.getDate(7),
433                     result.getString(8),
434                     result.getString(9)));
435             }
436
437             Strip r[] = new Strip[v.size()];
438             v.toArray(r);
439
440             return r;
441         }
442         finally { closeAll(c, statement, result); }
443     }
444
445     /**
446      * Get the position in a comic of the specified strip.
447      * Note that this counts the number of strips with ID lower than stripID in the current comic, this might not be the same as positin always.
448      */

449     public long getStripPosition(long stripID) throws SQLException JavaDoc, AccessDeniedException
450     {
451         manager.checkMyAccess(AccessLevel.comics);
452
453         Strip s = getStrip(stripID);
454
455         Connection JavaDoc c = null;
456         Statement JavaDoc statement = null;
457         ResultSet JavaDoc result = null;
458
459         try
460         {
461             c = getNewConnection();
462             statement = c.createStatement();
463
464             result = statement.executeQuery("select count(*) from Strips where Comic = "+s.parentID+" AND ID <= "+s.ID);
465             long r = 0;
466             if (result.next())
467                 r = result.getLong(1);
468
469             return r;
470         }
471         finally { closeAll(c, statement, result); }
472     }
473
474     /**
475      * Get the number of strips in the specified comic.
476      */

477     public long getNumberOfStrips(long comicID) throws SQLException JavaDoc, AccessDeniedException
478     {
479         manager.checkMyAccess(AccessLevel.comics);
480
481         Connection JavaDoc c = null;
482         Statement JavaDoc statement = null;
483         ResultSet JavaDoc result = null;
484
485         try
486         {
487             c = getNewConnection();
488             statement = c.createStatement();
489
490             result = statement.executeQuery("select count(*) from Strips where Comic = "+comicID);
491             long r = 0;
492             if (result.next())
493                 r = result.getLong(1);
494
495             return r;
496         }
497         finally { closeAll(c, statement, result); }
498     }
499
500     /**
501      * Get a users position in a comic.
502      */

503     public long getPosition(long userID, long comicID) throws SQLException JavaDoc, AccessDeniedException
504     {
505         manager.checkMyAccess(AccessLevel.comics);
506
507         Connection JavaDoc c = null;
508         Statement JavaDoc statement = null;
509         ResultSet JavaDoc result = null;
510
511         try
512         {
513             c = getNewConnection();
514             statement = c.createStatement();
515
516             result = statement.executeQuery("select Position from ComicPositions where User = "+userID+" AND Comic = "+comicID);
517             long r;
518             if (!result.next())
519                 r = getComic(comicID).firstID;
520             else
521                 r = result.getLong(1);
522
523             return r;
524         }
525         finally { closeAll(c, statement, result); }
526     }
527
528     /**
529      * Set a users position in a comic.
530      */

531     public void setPosition(long userID, long comicID, long position) throws SQLException JavaDoc, AccessDeniedException
532     {
533         if (manager.getUserID() != userID)
534             throw new AccessDeniedException("attempt to change another users comic position");
535
536         manager.checkMyAccess(AccessLevel.comics);
537
538         Connection JavaDoc c = null;
539         Statement JavaDoc statement = null;
540         ResultSet JavaDoc result = null;
541
542         try
543         {
544             c = getNewConnection();
545             statement = c.createStatement();
546
547             result = statement.executeQuery("select * from ComicPositions where User = "+userID+" AND Comic = "+comicID);
548             if (!result.next())
549                 statement.executeUpdate("insert into ComicPositions (User, Comic, Position) values ("+userID+", "+comicID+", "+position+")");
550             else
551                 statement.executeUpdate("update ComicPositions set Position = "+position+" where User = "+userID+" AND Comic = "+comicID);
552         }
553         finally { closeAll(c, statement, result); }
554     }
555 }
556
Popular Tags