KickJava   Java API By Example, From Geeks To Geeks.

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


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 FAQs.
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
39 public class FAQManager extends AbstractManager implements java.io.Serializable JavaDoc
40 {
41     /**
42      * Get FAQ topic.
43      */

44     public FAQTopic getFAQTopic(long ID) throws SQLException JavaDoc
45     {
46         Connection JavaDoc c = null;
47         Statement JavaDoc statement = null;
48         ResultSet JavaDoc result = null;
49
50         try
51         {
52             c = getNewConnection();
53             statement = c.createStatement();
54
55             result = statement.executeQuery("select User, Topic, Time, Priority from FAQTopics where ID = "+ID);
56             FAQTopic r = null;
57             if (result.next())
58             {
59                 r = new FAQTopic(
60                     ID,
61                     result.getLong(1),
62                     result.getString(2),
63                     result.getDate(3),
64                     result.getInt(4));
65             }
66
67             return r;
68         }
69         finally { closeAll(c, statement, result); }
70     }
71
72     /**
73      * Get FAQ topic.
74      */

75     public FAQTopic getFAQTopic(String JavaDoc topic) throws SQLException JavaDoc
76     {
77         Connection JavaDoc c = null;
78         Statement JavaDoc statement = null;
79         ResultSet JavaDoc result = null;
80
81         try
82         {
83             c = getNewConnection();
84             statement = c.createStatement();
85
86             result = statement.executeQuery("select ID, User, Topic, Time, Priority from FAQTopics where Topic = '"+Utils.disableSQL(topic)+"'");
87             FAQTopic r = null;
88             if (result.next())
89             {
90                 r = new FAQTopic(
91                     result.getLong(1),
92                     result.getLong(2),
93                     result.getString(3),
94                     result.getDate(4),
95                     result.getInt(5));
96             }
97
98             return r;
99         }
100         finally { closeAll(c, statement, result); }
101     }
102
103     /**
104      * Get a list of FAQ Topics.
105      */

106     public FAQTopic[] getFAQTopics() throws SQLException JavaDoc
107     {
108         Connection JavaDoc c = null;
109         Statement JavaDoc statement = null;
110         ResultSet JavaDoc result = null;
111
112         try
113         {
114             c = getNewConnection();
115             statement = c.createStatement();
116
117             result = statement.executeQuery("select ID, User, Topic, Time, Priority from FAQTopics");
118             ArrayList JavaDoc l = new ArrayList JavaDoc();
119             while (result.next())
120             {
121                 l.add(new FAQTopic(
122                     result.getLong(1),
123                     result.getLong(2),
124                     result.getString(3),
125                     result.getDate(4),
126                     result.getInt(5)));
127             }
128
129             FAQTopic r[] = new FAQTopic[l.size()];
130             l.toArray(r);
131             return r;
132         }
133         finally { closeAll(c, statement, result); }
134     }
135
136     /**
137      * Add a FAQ.
138      */

139     public void addFAQTopic(FAQTopic topic) throws SQLException JavaDoc, AccessDeniedException
140     {
141         manager.checkMyAccess(AccessLevel.addFAQTopic);
142
143         Connection JavaDoc c = null;
144         Statement JavaDoc statement = null;
145         ResultSet JavaDoc result = null;
146
147         try
148         {
149             c = getNewConnection();
150             statement = c.createStatement();
151
152             statement.executeUpdate("insert into FAQTopics (User, Topic, Priority) values("+
153                 manager.getUserID()+", '"+
154                 Utils.disableSQL(topic.topic)+"', "+
155                 topic.priority+")");
156         }
157         finally { closeAll(c, statement, result); }
158     }
159
160     /**
161      * Remove a FAQ.
162      */

163     public void removeFAQTopic(long ID) throws SQLException JavaDoc, AccessDeniedException
164     {
165         manager.checkMyAccess(AccessLevel.removeFAQTopic);
166
167         Connection JavaDoc c = null;
168         Statement JavaDoc statement = null;
169         ResultSet JavaDoc result = null;
170
171         try
172         {
173             c = getNewConnection();
174             statement = c.createStatement();
175
176             statement.executeUpdate("delete from FAQTopics where ID = "+ID);
177         }
178         finally { closeAll(c, statement, result); }
179     }
180
181     /**
182      * Change a FAQ.
183      */

184     public void changeFAQTopic(FAQTopic topic) throws SQLException JavaDoc, AccessDeniedException
185     {
186         manager.checkMyAccess(AccessLevel.changeFAQ);
187
188         Connection JavaDoc c = null;
189         Statement JavaDoc statement = null;
190         ResultSet JavaDoc result = null;
191
192         try
193         {
194             c = getNewConnection();
195             statement = c.createStatement();
196
197             statement.executeUpdate("update FAQTopics set User = "+manager.getUserID()+", Topic = '"+Utils.disableSQL(topic.topic)+", Priority = "+topic.priority);
198         }
199         finally { closeAll(c, statement, result); }
200     }
201
202     /**
203      * Get FAQ data.
204      */

205     public FAQ getFAQ(long ID) throws SQLException JavaDoc
206     {
207         Connection JavaDoc c = null;
208         Statement JavaDoc statement = null;
209         ResultSet JavaDoc result = null;
210
211         try
212         {
213             c = getNewConnection();
214             statement = c.createStatement();
215
216             result = statement.executeQuery("select User, Topic, Question, Answer, Time, Priority from FAQs where ID = "+ID);
217             FAQ r = null;
218             if (result.next())
219             {
220                 r = new FAQ(
221                     ID,
222                     result.getLong(1),
223                     result.getLong(2),
224                     result.getString(3),
225                     result.getString(4),
226                     result.getDate(5),
227                     result.getInt(6));
228             }
229
230             return r;
231         }
232         finally { closeAll(c, statement, result); }
233     }
234
235     /**
236      * Get a list of FAQs.
237      */

238     public FAQ[] getFAQs(long topicID) throws SQLException JavaDoc
239     {
240         Connection JavaDoc c = null;
241         Statement JavaDoc statement = null;
242         ResultSet JavaDoc result = null;
243
244         try
245         {
246             c = getNewConnection();
247             statement = c.createStatement();
248
249             result = statement.executeQuery("select ID, User, Question, Answer, Time, Priority from FAQs where Topic = "+topicID);
250             ArrayList JavaDoc l = new ArrayList JavaDoc();
251             while (result.next())
252             {
253                 l.add(new FAQ(
254                     result.getLong(1),
255                     result.getLong(2),
256                     topicID,
257                     result.getString(3),
258                     result.getString(4),
259                     result.getDate(5),
260                     result.getInt(6)));
261             }
262
263             FAQ r[] = new FAQ[l.size()];
264             l.toArray(r);
265             return r;
266         }
267         finally { closeAll(c, statement, result); }
268     }
269
270     /**
271      * Add a FAQ.
272      */

273     public void addFAQ(FAQ faq) throws SQLException JavaDoc, AccessDeniedException
274     {
275         manager.checkMyAccess(AccessLevel.addFAQ);
276         if (getFAQTopic(faq.topicID) == null)
277             throw new AccessDeniedException("no such topic id ("+faq.topicID+")");
278
279         Connection JavaDoc c = null;
280         Statement JavaDoc statement = null;
281         ResultSet JavaDoc result = null;
282
283         try
284         {
285             c = getNewConnection();
286             statement = c.createStatement();
287
288             statement.executeUpdate("insert into FAQs (User, Topic, Question, Answer, Priority) values("+
289                 manager.getUserID()+", "+
290                 faq.topicID+", '"+
291                 Utils.disableSQL(faq.question)+"', '"+
292                 Utils.disableSQL(faq.answer)+"', "+
293                 faq.priority+")");
294         }
295         finally { closeAll(c, statement, result); }
296     }
297
298     /**
299      * Remove a FAQ.
300      */

301     public void removeFAQ(long ID) throws SQLException JavaDoc, AccessDeniedException
302     {
303         manager.checkMyAccess(AccessLevel.removeFAQ);
304
305         Connection JavaDoc c = null;
306         Statement JavaDoc statement = null;
307         ResultSet JavaDoc result = null;
308
309         try
310         {
311             c = getNewConnection();
312             statement = c.createStatement();
313
314             statement.executeUpdate("delete from FAQs where ID = "+ID);
315         }
316         finally { closeAll(c, statement, result); }
317     }
318
319     /**
320      * Change a FAQ.
321      */

322     public void changeFAQ(FAQ faq) throws SQLException JavaDoc, AccessDeniedException
323     {
324         manager.checkMyAccess(AccessLevel.changeFAQ);
325
326         Connection JavaDoc c = null;
327         Statement JavaDoc statement = null;
328         ResultSet JavaDoc result = null;
329
330         try
331         {
332             c = getNewConnection();
333             statement = c.createStatement();
334
335             statement.executeUpdate("update FAQs set User = "+manager.getUserID()+", Topic = "+faq.topicID+", Question = '"+Utils.disableSQL(faq.question)+"', Answer = '"+Utils.disableSQL(faq.answer)+"', Priority = "+faq.priority+" where ID = "+faq.getId());
336         }
337         finally { closeAll(c, statement, result); }
338     }
339 }
340
Popular Tags