KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Handles generic personla options.
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.Utils;
34
35 import java.sql.Connection JavaDoc;
36 import java.sql.ResultSet JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.sql.Statement JavaDoc;
39 import java.sql.PreparedStatement JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Properties JavaDoc;
42
43 public class OptionsManager extends AbstractManager implements java.io.Serializable JavaDoc
44 {
45     private HashMap JavaDoc cache;
46     static private Properties JavaDoc defaults;
47
48     /**
49      * Sets the ForumManager.
50      */

51     public void initialize(ForumManager p)
52     {
53         super.initialize(p);
54
55         try
56         {
57             loadSettings();
58         }
59         catch (SQLException JavaDoc e)
60         {
61             throw new RuntimeException JavaDoc(e.toString());
62         }
63     }
64
65     /**
66      * Loads all the settings from the database. If the settings are loaded they are refreshed.
67      * If the user is not logged in this will clear the internal cache.
68      * If the cache is already loaded it will be cleared first.
69      */

70     public void loadSettings() throws SQLException JavaDoc
71     {
72     if (!manager.isLoggedIn())
73         {
74             cache = null;
75             return;
76         }
77
78         if (cache == null)
79             cache = new HashMap JavaDoc();
80         else
81             cache.clear();
82
83         if (defaults == null)
84             defaults = net.killingar.Utils.getProperties("net.killingar.forum.OptionsManager");
85
86         Connection JavaDoc c = null;
87         Statement JavaDoc statement = null;
88         ResultSet JavaDoc result = null;
89
90         try
91         {
92             c = getNewConnection();
93             statement = c.createStatement();
94             result = statement.executeQuery("select Name, Setting from Options where User = "+manager.getUserID());
95
96             while (result.next())
97                 cache.put(result.getString(1), result.getString(2));
98         }
99         finally { closeAll(c, statement, result); }
100     }
101
102     /**
103      * Get the option data for the given key from the internal cache. Returns null if none is found.
104      */

105     public String JavaDoc get(String JavaDoc name) throws SQLException JavaDoc
106     {
107         return get(name, null);
108     }
109
110     /**
111      * Get the option data for the given key from the internal cache. Returns the sent argument if none is found.
112      */

113     public String JavaDoc get(String JavaDoc name, String JavaDoc defaultSetting) throws SQLException JavaDoc
114     {
115         if (cache == null)
116             loadSettings();
117
118         if (cache == null) // still? then it's way too early for a result, annoying
119
return defaultSetting;
120
121         // get value from cache table
122
String JavaDoc s = (String JavaDoc)cache.get(name);
123         if (s == null)
124             s = defaults.getProperty(name, defaultSetting);
125
126     /*
127     // debug
128     if (s == null && name.equals("logo url"))
129         {
130             for (java.util.Enumeration e = defaults.propertyNames(); e.hasMoreElements();)
131           s += e.nextElement()+"\n";
132         }*/

133
134         return s;
135     }
136
137     /**
138      * Sets the option data for the given key. This will store to the database.
139      */

140     public void set(String JavaDoc name, String JavaDoc setting) throws SQLException JavaDoc, AccessDeniedException
141     {
142         if (name == null)return;
143
144         if (!manager.isLoggedIn())
145             return;
146
147         manager.checkMyAccess(AccessLevel.options);
148
149         Connection JavaDoc c = null;
150         PreparedStatement JavaDoc statement = null;
151         ResultSet JavaDoc result = null;
152
153         try
154         {
155             c = getNewConnection();
156
157             // update database
158
if (setting != null)
159             {
160                 statement = c.prepareStatement("delete from Options where User = ? AND Name = ?");
161                 statement.setLong(1, manager.getUserID());
162                 statement.setString(2, name);
163                 statement.executeUpdate();
164                 statement.close();
165
166                 statement = c.prepareStatement("insert into Options (Name, User, Setting) values (?, ?, ?)");
167                 statement.setString(1, name);
168                 statement.setLong(2, manager.getUserID());
169                 statement.setString(3, setting);
170
171                 statement.executeUpdate();
172             }
173             else
174             {
175                 statement = c.prepareStatement("delete from Options where User = ? AND Name = ?");
176                 statement.setLong(1, manager.getUserID());
177                 statement.setString(2, name);
178
179                 statement.executeUpdate();
180             }
181
182             // update cache table
183
cache.put(name, setting);
184         }
185         finally { closeAll(c, statement, result); }
186     }
187 }
Popular Tags