View Javadoc

1   /*
2    * Atlassian Source Code Template.
3    * User: owen
4    * Date: Oct 15, 2002
5    * Time: 11:30:01 AM
6    * CVS Revision: $Revision: 1.10 $
7    * Last CVS Commit: $Date: 2005/10/04 02:41:53 $
8    * Author of last CVS Commit: $Author: nfaiz $
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      // stores the keys for preferences that the default values should be used
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          // Use this set even if the propertySet is null
50          // This will save some operations on checking if the userPs is null before
51          // looking at the default properties, and look into the default properties straight away
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          // Use this set even if the pUser is null
69          // This will save some operations on checking if the userPs is null before
70          // looking at the default properties, and look into the default properties straight away
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          // Check if the default value should be used for this key
88          if (defaultKeys.contains(key))
89          {
90              // If the default value is used for the key just return it
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                 // Remember that the default value for this key is used for the user
102                 // So that we do not have look it up again
103                 defaultKeys.add(key);
104                 // Return the default value
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             // Do not use the default value (if one was not used before, it does not matter)
119             defaultKeys.remove(key);
120             backingPS.setLong(key, i);
121         }
122     }
123 
124     public String getString(String key)
125     {
126         // Check if the default value should be used for this key
127         if (defaultKeys.contains(key))
128         {
129             // If the default value is used for the key just return it
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                 // Remember that the default value for this key is used for the user
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             // Do not use the default value (if one was not used before, it does not matter)
156             defaultKeys.remove(key);
157             backingPS.setString(key, value);
158         }
159     }
160 
161     public boolean getBoolean(String key)
162     {
163         // Check if the default value should be used for this key
164         if (defaultKeys.contains(key))
165         {
166             // If the default value is used for the key just return it
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                 // Remember that the default value for this key is used for the user
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             // Do not use the default value (if one was not used before, it does not matter)
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                 // Do not use the default value (if one was not used before, it does not matter)
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         // NOTE: the defaultKeys is not used to determine equality of this object.
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         // NOTE: the defaultKeys is not used to determine the hashCode of this object.
235         return (backingPS != null ? backingPS.hashCode() : 0);
236     }
237 }