KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > impl > preferences > MappedPreferenceSet


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.impl.preferences;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.apache.log4j.Logger;
18 import org.jboss.portal.common.FQN;
19 import org.jboss.portal.common.value.Value;
20 import org.jboss.portal.server.plugins.preferences.Preference;
21 import org.jboss.portal.server.plugins.preferences.PreferenceSet;
22
23 /**
24  * @hibernate.class
25  * table="test_pref_set"
26  *
27  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
28  * @version $Revision: 1.4 $
29  */

30 public class MappedPreferenceSet
31    implements PreferenceSet
32 {
33
34    /** Our logger. */
35    private static Logger log = Logger.getLogger(MappedPreferenceSet.class);
36
37    private Integer JavaDoc id;
38    private MappedPreferenceSet parent;
39    private Set JavaDoc children;
40    private String JavaDoc name;
41    private FQN fqn;
42    protected Map JavaDoc map;
43
44    /**
45     * Hibernate constructor.
46     */

47    public MappedPreferenceSet()
48    {
49       this.children = new HashSet JavaDoc();
50       this.id = null;
51       this.name = null;
52       this.parent = null;
53       this.fqn = null;
54       this.map = null;
55    }
56
57    public MappedPreferenceSet(String JavaDoc name)
58    {
59       this.children = new HashSet JavaDoc();
60       this.id = null;
61       this.name = name;
62       this.parent = null;
63       this.fqn = new FQN();
64       this.map = new HashMap JavaDoc();
65    }
66
67    /**
68     * @hibernate.id
69     * column="jbp_id"
70     * generator-class="native"
71     */

72    public Integer JavaDoc getID()
73    {
74       return id;
75    }
76
77    /**
78     * Called by hibernate.
79     */

80    protected void setID(Integer JavaDoc id)
81    {
82       this.id = id;
83    }
84
85    /**
86     * @hibernate.property
87     * columns="jbp_name"
88     * not-null="true"
89     * update="false"
90     */

91    public String JavaDoc getName()
92    {
93       return name;
94    }
95
96    /**
97     * Called by hibernate.
98     */

99    protected void setName(String JavaDoc name)
100    {
101       this.name = name;
102    }
103
104    /**
105     * @hibernate.many-to-one
106     * column="jbp_parent_id"
107     * cascade="none"
108     */

109    public MappedPreferenceSet getParent()
110    {
111       return parent;
112    }
113
114    public void setParent(MappedPreferenceSet parent)
115    {
116       this.parent = parent;
117    }
118
119    public void addChild(MappedPreferenceSet child)
120    {
121       children.add(child);
122       child.setParent(this);
123    }
124
125    /**
126     * @hibernate.set
127     * inverse="true"
128     * lazy="false"
129     * cascade="all-delete-orphan"
130     * @hibernate.collection-key
131     * column="jbp_parent_id"
132     * @hibernate.collection-one-to-many
133     * class="org.jboss.portal.core.impl.preferences.MappedPreferenceSet"
134     */

135    public Set JavaDoc getChildren()
136    {
137       return children;
138    }
139
140    public void setChildren(Set JavaDoc children)
141    {
142       this.children = children;
143    }
144
145    public MappedPreferenceSet getDescendant(FQN fqn)
146    {
147       // We start by ourself
148
boolean debug = log.isDebugEnabled();
149       MappedPreferenceSet current = this;
150
151       // Iterate on the fqn
152
for (int i = 0;i < fqn.size();i++)
153       {
154          // Current name
155
String JavaDoc name = fqn.getName(i).toString();
156          if (debug)
157          {
158             log.debug("Using for " + this.id + "/" + this.name + " name (" + i + "," + name + ")");
159          }
160
161          // Try to get next psc
162
MappedPreferenceSet next = null;
163          for (Iterator JavaDoc j = current.getChildren().iterator();j.hasNext();)
164          {
165             MappedPreferenceSet child = (MappedPreferenceSet)j.next();
166             String JavaDoc childName = child.getName();
167             if (name.equals(childName))
168             {
169                next = child;
170                if (debug)
171                {
172                   log.debug("Found for " + this.id + "/" + this.name + " child " + childName);
173                }
174                break;
175             }
176          }
177
178          // It does not exist, create it
179
if (next == null)
180          {
181             if (debug)
182             {
183                log.debug("Creating child for " + this.id + "/" + this.name);
184             }
185             next = newChild(name);
186             current.addChild(next);
187          }
188
189          current = next;
190       }
191
192       return current;
193    }
194
195    public FQN getFQN()
196    {
197       if (fqn == null)
198       {
199          if (parent == null)
200          {
201             this.fqn = new FQN();
202          }
203          else
204          {
205             this.fqn = new FQN(parent.getFQN(), name);
206          }
207       }
208       return fqn;
209    }
210
211    /**
212     * @hibernate.map
213     * cascade="all"
214     * inverse="false"
215     * @hibernate.collection-key
216     * column="jbp_set_id"
217     * @hibernate.collection-index
218     * column="jbp_name"
219     * type="string"
220     * @hibernate.collection-one-to-many
221     * class="org.jboss.portal.core.impl.preferences.MappedPreference"
222     */

223    public Map JavaDoc getContent()
224    {
225       return map;
226    }
227
228    public void setContent(Map JavaDoc content)
229    {
230       this.map = content;
231    }
232
233    //
234

235    public Set JavaDoc keySet()
236    {
237       return map.keySet();
238    }
239
240    public Preference getPreference(String JavaDoc key)
241    {
242       if (key == null)
243       {
244          throw new IllegalArgumentException JavaDoc("key must not be null");
245       }
246       return (Preference)map.get(key);
247    }
248
249    public Value getValue(String JavaDoc key)
250    {
251       if (key == null)
252       {
253          throw new IllegalArgumentException JavaDoc("key must not be null");
254       }
255       Preference preference = getPreference(key);
256       if (preference != null)
257       {
258          return preference.getValue();
259       }
260       else
261       {
262          return null;
263       }
264    }
265
266    public void setValue(String JavaDoc key, Value value)
267    {
268       if (key == null)
269       {
270          throw new IllegalArgumentException JavaDoc("key must not be null");
271       }
272       if (value == null)
273       {
274          map.remove(key);
275       }
276       else
277       {
278          MappedPreference pref = (MappedPreference)map.get(key);
279          if (pref != null)
280          {
281             pref.setValue(value);
282          }
283          else
284          {
285             map.put(key, newPreference(key, value, false));
286          }
287       }
288    }
289
290    public boolean isReadOnly(String JavaDoc key)
291    {
292       if (key == null)
293       {
294          throw new IllegalArgumentException JavaDoc("key must not be null");
295       }
296       return false;
297    }
298
299    public void setReadOnly(String JavaDoc key, boolean readOnly)
300    {
301       throw new UnsupportedOperationException JavaDoc("The mapping does not support the read only flag");
302    }
303
304    // Overrrided *******************************************************************************************************
305

306    /**
307     * Override to provide the right object.
308     */

309    protected MappedPreferenceSet newChild(String JavaDoc name)
310    {
311       return new MappedPreferenceSet(name);
312    }
313
314    /**
315     * Override to provide the right object.
316     */

317    protected Preference newPreference(String JavaDoc name, Value value, boolean readOnly)
318    {
319       MappedPreference preference = new MappedPreference(name);
320       preference.setValue(value);
321       return preference;
322    }
323 }
324
Popular Tags