KickJava   Java API By Example, From Geeks To Geeks.

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


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 simple lists.
28  */

29 package net.killingar.forum.internal.managers;
30
31 import java.sql.Connection JavaDoc;
32 import java.sql.PreparedStatement JavaDoc;
33 import java.sql.ResultSet JavaDoc;
34 import java.sql.SQLException JavaDoc;
35
36 public final class ItemListManager extends AbstractManager implements java.io.Serializable JavaDoc
37 {
38     /**
39      * Add an item.
40      */

41     public void addItem(String JavaDoc list, String JavaDoc item) throws SQLException JavaDoc
42     {
43         Connection JavaDoc c = null;
44         PreparedStatement JavaDoc statement = null;
45         ResultSet JavaDoc result = null;
46
47         try
48         {
49             c = getNewConnection();
50             statement = c.prepareStatement("insert into ItemList (User, List, Item) values (?, ?, ?)");
51             statement.setLong(1, manager.getUserID());
52             statement.setString(2, list);
53             statement.setString(3, item);
54
55             statement.executeUpdate();
56         }
57         finally { closeAll(c, statement, result); }
58     }
59
60     /**
61      * Remove an item.
62      */

63     public void removeItem(String JavaDoc list, String JavaDoc item) throws SQLException JavaDoc
64     {
65         Connection JavaDoc c = null;
66         PreparedStatement JavaDoc statement = null;
67         ResultSet JavaDoc result = null;
68
69         try
70         {
71             c = getNewConnection();
72             statement = c.prepareStatement("delete from ItemList where List = ? AND Item = ? AND User = ?");
73             statement.setString(1, list);
74             statement.setString(2, item);
75             statement.setLong(3, manager.getUserID());
76
77             statement.executeUpdate();
78         }
79         finally { closeAll(c, statement, result); }
80     }
81
82     /**
83      * Returns true if the user has the specified item.
84      */

85     public boolean hasItem(String JavaDoc list, String JavaDoc item) throws SQLException JavaDoc
86     {
87         Connection JavaDoc c = null;
88         PreparedStatement JavaDoc statement = null;
89         ResultSet JavaDoc result = null;
90
91         try
92         {
93             c = getNewConnection();
94             statement = c.prepareStatement("select User from ItemList where List = ? AND Item = ? AND User = ?");
95             statement.setString(1, list);
96             statement.setString(2, item);
97             statement.setLong(3, manager.getUserID());
98
99             result = statement.executeQuery();
100             boolean r = false;
101             if (result.next())
102                 r = true;
103             return r;
104         }
105         finally { closeAll(c, statement, result); }
106     }
107
108     /**
109      * Get the list.
110      */

111     public String JavaDoc[] getList(String JavaDoc list) throws SQLException JavaDoc
112     {
113         Connection JavaDoc c = null;
114         PreparedStatement JavaDoc statement = null;
115         ResultSet JavaDoc result = null;
116
117         try
118         {
119             c = getNewConnection();
120             statement = c.prepareStatement("select Item from ItemList where List = ? AND User = ?");
121             statement.setString(1, list);
122             statement.setLong(2, manager.getUserID());
123
124             result = statement.executeQuery();
125
126             java.util.ArrayList JavaDoc a = new java.util.ArrayList JavaDoc();
127             for(int i = 0; result.next(); i++)
128                 a.add(result.getString(1));
129
130             String JavaDoc r[] = new String JavaDoc[a.size()];
131             a.toArray(r);
132
133             return r;
134         }
135         finally { closeAll(c, statement, result); }
136     }
137 }
Popular Tags