KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Quote Manager.
28  */

29 package net.killingar.forum.internal.managers;
30
31 import net.killingar.forum.internal.AccessDeniedException;
32 import net.killingar.forum.internal.AccessLevel;
33 import net.killingar.forum.internal.Quote;
34 import net.killingar.forum.internal.Utils;
35
36 import java.sql.Connection JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Statement JavaDoc;
40
41 public class QuoteManager extends AbstractManager implements java.io.Serializable JavaDoc
42 {
43     /**
44      * Get a quote.
45      */

46     public Quote getQuote(long id) throws SQLException JavaDoc
47     {
48         Connection JavaDoc c = null;
49         Statement JavaDoc statement = null;
50         ResultSet JavaDoc result = null;
51
52         try
53         {
54             c = getNewConnection();
55             statement = c.createStatement();
56             result = statement.executeQuery("select Body, Origin from Quotes where ID = " + id);
57             Quote r = null;
58             if (result.next())
59                 r = new Quote(id, result.getString(1), result.getString(2));
60             return r;
61         }
62         finally { closeAll(c, statement, result); }
63     }
64
65     /**
66      * Get a random quote.
67      */

68     public Quote getRandomQuote() throws SQLException JavaDoc
69     {
70         Quote[] quotes = getQuotes();
71         if (quotes.length == 0)
72             return null;
73
74         return quotes[(int)(Math.random()*(double)quotes.length)];
75     }
76
77     /**
78      * Get all quotes.
79      */

80     public Quote[] getQuotes() throws SQLException JavaDoc
81     {
82         Connection JavaDoc c = null;
83         Statement JavaDoc statement = null;
84         ResultSet JavaDoc result = null;
85
86         try
87         {
88             c = getNewConnection();
89             statement = c.createStatement();
90             result = statement.executeQuery("select ID, Body, Origin from Quotes");
91
92             java.util.ArrayList JavaDoc v = new java.util.ArrayList JavaDoc();
93
94             while (result.next())
95                 v.add(new Quote(result.getLong(1), result.getString(2), result.getString(3)));
96             Quote quotes[] = new Quote[v.size()];
97             v.toArray(quotes);
98
99             return quotes;
100         }
101         finally { closeAll(c, statement, result); }
102     }
103
104     /**
105      * Add a quote.
106      */

107     public void addQuote(Quote quote) throws SQLException JavaDoc, AccessDeniedException
108     {
109         manager.checkMyAccess(AccessLevel.addQuote);
110         Connection JavaDoc c = null;
111         Statement JavaDoc statement = null;
112         ResultSet JavaDoc result = null;
113
114         try
115         {
116             c = getNewConnection();
117             statement = c.createStatement();
118
119             statement.executeUpdate("insert into Quotes (Body, Origin) values('"+Utils.disableSQL(quote.body)+"', '"+Utils.disableSQL(quote.origin)+"')");
120         }
121         finally { closeAll(c, statement, result); }
122     }
123
124     /**
125      * Remove a quote.
126      */

127     public void removeQuote(long id) throws SQLException JavaDoc, AccessDeniedException
128     {
129         manager.checkMyAccess(AccessLevel.removeQuote);
130         Connection JavaDoc c = null;
131         Statement JavaDoc statement = null;
132         ResultSet JavaDoc result = null;
133
134         try
135         {
136             c = getNewConnection();
137             statement = c.createStatement();
138
139             statement.executeUpdate("delete from Quotes where ID = "+id);
140         }
141         finally { closeAll(c, statement, result); }
142     }
143
144     /**
145      * Change a quote.
146      */

147     public void changeQuote(Quote quote) throws SQLException JavaDoc, AccessDeniedException
148     {
149         manager.checkMyAccess(AccessLevel.changeQuote);
150         Connection JavaDoc c = null;
151         Statement JavaDoc statement = null;
152         ResultSet JavaDoc result = null;
153
154         try
155         {
156             c = getNewConnection();
157             statement = c.createStatement();
158
159             statement.executeUpdate("update Quotes set Origin = '"+Utils.disableSQL(quote.origin)+"', Body = '"+Utils.disableSQL(quote.body)+"' where ID = "+quote.ID);
160         }
161         finally { closeAll(c, statement, result); }
162     }
163 }
Popular Tags