KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.portal.common.value.StringValues;
12 import org.jboss.portal.common.value.Value;
13 import org.jboss.portal.server.plugins.preferences.Preference;
14
15 /**
16  * @hibernate.class
17  * table="test_pref"
18  *
19  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
20  * @version $Revision: 1.2 $
21  */

22 public class MappedPreference
23    implements Preference
24 {
25
26    private static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
27    private static final Value EMPTY_VALUE = new StringValues(EMPTY_STRING_ARRAY);
28
29    // The id
30
private Integer JavaDoc id;
31
32    // The name
33
private String JavaDoc name;
34
35    // The db value
36
private int type;
37    private String JavaDoc[] strings;
38
39    // The value which is actually equals to type+strings
40
private Value value;
41
42    // This flag is used because the value is made up of strings and type
43
// and it is not possible to update the value when strings or type
44
// is updated, so when strings of type is modified with set dirty
45
private boolean dirty;
46
47    public MappedPreference()
48    {
49       this.id = null;
50       this.name = null;
51       this.type = 0;
52       this.strings = EMPTY_STRING_ARRAY;
53       this.value = EMPTY_VALUE;
54       this.dirty = false;
55    }
56
57    public MappedPreference(String JavaDoc name)
58    {
59       this.id = null;
60       this.name = name;
61       this.type = 0;
62       this.strings = EMPTY_STRING_ARRAY;
63       this.value = EMPTY_VALUE;
64    }
65
66    /**
67     * @hibernate.id
68     * column="jbp_id"
69     * generator-class="native"
70     */

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

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

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

98    protected void setName(String JavaDoc name)
99    {
100       this.name = name;
101    }
102
103    /**
104     * @hibernate.property
105     * column="jbp_type"
106     */

107    public int getType()
108    {
109       return type;
110    }
111
112    /**
113     * Called by hibernate.
114     */

115    protected void setType(int type)
116    {
117       this.type = type;
118       this.dirty = true;
119    }
120
121    /**
122     * @hibernate.array
123     * table="test_prop_value"
124     * cascade="all"
125     * @hibernate.collection-key
126     * column="jbp_prop_id"
127     * @hibernate.collection-index
128     * column="jbp_idx"
129     * @hibernate.collection-element
130     * column="jbp_value"
131     * type="string"
132     */

133    public String JavaDoc[] getStrings()
134    {
135       return strings;
136    }
137
138    public void setValue(Value value)
139    {
140       TypedStringArray tsa = ValueManager.createTypedStringArray(value);
141       this.value = value;
142       this.type = tsa.getType();
143       this.strings = tsa.getStrings();
144    }
145
146    /**
147     * Called by hibernate.
148     */

149    private void setStrings(String JavaDoc[] strings)
150    {
151       this.strings = strings;
152       this.dirty = true;
153    }
154
155    public Value getValue()
156    {
157       if (dirty)
158       {
159          value = ValueManager.createValue(new TypedStringArray(type, strings));
160          dirty = false;
161       }
162       return value;
163    }
164
165    /**
166     * By default it's never read only. Subclasses may provide custom behaviour here.
167     */

168    public boolean isReadOnly()
169    {
170       return false;
171    }
172
173    /**
174     * Provide a default impl.
175     */

176    public String JavaDoc toString()
177    {
178       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("[");
179       buffer.append(id).append(",")
180             .append(name).append(",");
181       if (strings == null)
182       {
183          buffer.append("null,");
184       }
185       else
186       {
187          buffer.append("(");
188          for (int i = 0;i < strings.length;i++)
189          {
190             String JavaDoc s = strings[i];
191             buffer.append(i > 0 ? "," : "").append(s);
192          }
193          buffer.append("),");
194       }
195       buffer.append(value).append("]");
196       return buffer.toString();
197    }
198 }
199
Popular Tags