1
2
3
4
5
6
7
8
9
10 package com.atlassian.core.user.preferences;
11
12 import com.atlassian.core.AtlassianCoreException;
13 import com.atlassian.core.util.PropertyUtils;
14 import com.opensymphony.module.propertyset.PropertySet;
15 import com.opensymphony.module.propertyset.PropertySetManager;
16 import com.opensymphony.user.User;
17
18 import java.io.Serializable;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.HashSet;
23
24 public class UserPreferences implements Preferences, Serializable
25 {
26 private PropertySet backingPS = null;
27
28
29 private Set defaultKeys = null;
30
31 public UserPreferences()
32 {
33 this((PropertySet) null, true);
34 }
35
36 public UserPreferences(User pUser)
37 {
38 this(pUser, true);
39 }
40
41 public UserPreferences(PropertySet propertySet)
42 {
43 backingPS = propertySet;
44 defaultKeys = new HashSet();
45 }
46
47 public UserPreferences(PropertySet propertySet, boolean bulkload)
48 {
49
50
51
52 defaultKeys = new HashSet();
53
54 if (propertySet != null)
55 {
56 PropertySet userPs = propertySet;
57
58 Map params = new HashMap(2);
59 params.put("PropertySet", userPs);
60 params.put("bulkload", new Boolean(bulkload));
61
62 backingPS = PropertySetManager.getInstance("cached", params);
63 }
64 }
65
66 public UserPreferences(User pUser, boolean bulkload)
67 {
68
69
70
71 defaultKeys = new HashSet();
72
73 if (pUser != null)
74 {
75 PropertySet userPs = pUser.getPropertySet();
76
77 Map params = new HashMap(2);
78 params.put("PropertySet", userPs);
79 params.put("bulkload", new Boolean(bulkload));
80
81 backingPS = PropertySetManager.getInstance("cached", params);
82 }
83 }
84
85 public long getLong(String key)
86 {
87
88 if (defaultKeys.contains(key))
89 {
90
91 return DefaultPreferences.getPreferences().getLong(key);
92 }
93 else
94 {
95 if (backingPS != null && backingPS.exists(key))
96 {
97 return backingPS.getLong(key);
98 }
99 else
100 {
101
102
103 defaultKeys.add(key);
104
105 return DefaultPreferences.getPreferences().getLong(key);
106 }
107 }
108 }
109
110 public void setLong(String key, long i) throws AtlassianCoreException
111 {
112 if (backingPS == null)
113 {
114 throw new AtlassianCoreException("Trying to set a property on a null user this is not allowed");
115 }
116 else
117 {
118
119 defaultKeys.remove(key);
120 backingPS.setLong(key, i);
121 }
122 }
123
124 public String getString(String key)
125 {
126
127 if (defaultKeys.contains(key))
128 {
129
130 return DefaultPreferences.getPreferences().getString(key);
131 }
132 else
133 {
134 if (backingPS != null && backingPS.exists(key))
135 {
136 return backingPS.getString(key);
137 }
138 else
139 {
140
141 defaultKeys.add(key);
142 return DefaultPreferences.getPreferences().getString(key);
143 }
144 }
145 }
146
147 public void setString(String key, String value) throws AtlassianCoreException
148 {
149 if (backingPS == null)
150 {
151 throw new AtlassianCoreException("Trying to set a property on a null user this is not allowed");
152 }
153 else
154 {
155
156 defaultKeys.remove(key);
157 backingPS.setString(key, value);
158 }
159 }
160
161 public boolean getBoolean(String key)
162 {
163
164 if (defaultKeys.contains(key))
165 {
166
167 return DefaultPreferences.getPreferences().getBoolean(key);
168 }
169 else
170 {
171 if (backingPS != null && backingPS.exists(key))
172 {
173 return backingPS.getBoolean(key);
174 }
175 else
176 {
177
178 defaultKeys.add(key);
179 return DefaultPreferences.getPreferences().getBoolean(key);
180 }
181 }
182 }
183
184 public void setBoolean(String key, boolean b) throws AtlassianCoreException
185 {
186 if (backingPS == null)
187 {
188 throw new AtlassianCoreException("Trying to set a property on a null user this is not allowed");
189 }
190 else
191 {
192
193 defaultKeys.remove(key);
194 backingPS.setBoolean(key, b);
195 }
196 }
197
198 public void remove(String key) throws AtlassianCoreException
199 {
200 if (backingPS == null)
201 {
202 throw new AtlassianCoreException("Trying to remove a property on a null user this is not allowed");
203 }
204 else
205 {
206 if (backingPS.exists(key))
207 {
208
209 defaultKeys.remove(key);
210 backingPS.remove(key);
211 }
212 else
213 {
214 throw new AtlassianCoreException("The property with key '" + key + "' does not exist.");
215 }
216 }
217 }
218
219 public boolean equals(Object o)
220 {
221
222 if (this == o) return true;
223 if (!(o instanceof UserPreferences)) return false;
224
225 final UserPreferences userPreferences = (UserPreferences) o;
226
227 if (backingPS != null ? !PropertyUtils.identical(backingPS, userPreferences.backingPS) : userPreferences.backingPS != null) return false;
228
229 return true;
230 }
231
232 public int hashCode()
233 {
234
235 return (backingPS != null ? backingPS.hashCode() : 0);
236 }
237 }