KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > domain > Field


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.domain;
18
19 import static info.jtrac.Constants.*;
20
21 import info.jtrac.util.XmlUtils;
22
23 import java.io.Serializable JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.LinkedHashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.TreeSet JavaDoc;
29
30 import org.dom4j.Element;
31
32 /**
33  * <code>Metadata</code> is composited of Field elements
34  * that represent each of the custom fields that may be
35  * used within an item
36  */

37 public class Field implements Serializable JavaDoc {
38     
39     private Name name;
40     private String JavaDoc label;
41     private boolean optional;
42     private Map JavaDoc<String JavaDoc, String JavaDoc> options;
43     
44     private static final Map JavaDoc<String JavaDoc, Name> nameMap;
45     
46     // set up a static Map to resolve a String to our Field.Name enum value
47
static {
48         nameMap = new HashMap JavaDoc<String JavaDoc, Name>();
49         for (Name n : Name.values()) {
50             nameMap.put(n.text, n);
51         }
52     }
53     
54     /**
55      * Resolve a String to a valid enum value for Field.Name
56      */

57     public static Name convertToName(String JavaDoc text) {
58         Name n = nameMap.get(text);
59         if (n == null) {
60             throw new RuntimeException JavaDoc("Bad name " + text);
61         }
62         return n;
63     }
64
65     /**
66      * the names that are used for the custom fields in the outside
67      * world - e.g. the XML representation of the metadata that is
68      * persisted to the database
69      */

70     public enum Name {
71         SEVERITY (1, "severity"),
72         PRIORITY (2, "priority"),
73         CUS_INT_01 (3, "cusInt01"),
74         CUS_INT_02 (3, "cusInt02"),
75         CUS_INT_03 (3, "cusInt03"),
76         CUS_INT_04 (3, "cusInt04"),
77         CUS_INT_05 (3, "cusInt05"),
78         CUS_INT_06 (3, "cusInt06"),
79         CUS_INT_07 (3, "cusInt07"),
80         CUS_INT_08 (3, "cusInt08"),
81         CUS_INT_09 (3, "cusInt09"),
82         CUS_INT_10 (3, "cusInt10"),
83         CUS_DBL_01 (4, "cusDbl01"),
84         CUS_DBL_02 (4, "cusDbl02"),
85         CUS_DBL_03 (4, "cusDbl03"),
86         CUS_STR_01 (5, "cusStr01"),
87         CUS_STR_02 (5, "cusStr02"),
88         CUS_STR_03 (5, "cusStr03"),
89         CUS_STR_04 (5, "cusStr04"),
90         CUS_STR_05 (5, "cusStr05"),
91         CUS_TIM_01 (6, "cusTim01"),
92         CUS_TIM_02 (6, "cusTim02"),
93         CUS_TIM_03 (6, "cusTim03");
94         
95         private final int type;
96         private final String JavaDoc text;
97         
98         Name(int type, String JavaDoc text) {
99             this.type = type;
100             this.text = text;
101         }
102         
103         public int getType() {
104             return type;
105         }
106         
107         public String JavaDoc getText() {
108             return text;
109         }
110         
111         public String JavaDoc getDescription() {
112             switch (type) {
113                 case 1: return "Severity (Drop Down)";
114                 case 2: return "Priority (Drop Down)";
115                 case 3: return "Drop Down List";
116                 case 4: return "Decimal Number";
117                 case 5: return "Free Text Field";
118                 case 6: return "Date Field";
119                 default: throw new RuntimeException JavaDoc("Unknown type " + type);
120             }
121         }
122         
123         @Override JavaDoc
124         public String JavaDoc toString() {
125             return text;
126         }
127     }
128     
129     //===================================================================
130

131     public Field() {
132         // zero arg constructor
133
}
134     
135     public Field(String JavaDoc fieldName) {
136         this.setName(fieldName);
137     }
138     
139     public Field(Name n) {
140         this.setName(n);
141     }
142     
143     public Field(Element e) {
144         setName(e.attributeValue(NAME));
145         label = e.attributeValue(LABEL);
146         if (e.attribute(OPTIONAL) != null
147                 && e.attributeValue(OPTIONAL).equals(TRUE)) {
148             optional = true;
149         }
150         for (Object JavaDoc o : e.elements(OPTION)) {
151             addOption((Element) o);
152         }
153     }
154     
155     /* append this object onto an existing XML document */
156     public void addAsChildOf(Element parent) {
157         Element e = parent.addElement(FIELD);
158         copyTo(e);
159     }
160     
161     /* marshal this object into a fresh new XML Element */
162     public Element getAsElement() {
163         Element e = XmlUtils.getNewElement(FIELD);
164         copyTo(e);
165         return e;
166     }
167     
168     /* copy object values into an existing XML Element */
169     private void copyTo(Element e) {
170         // appending empty strings to create new objects for "clone" support
171
e.addAttribute(NAME, name + "");
172         e.addAttribute(LABEL, label + "");
173         if (optional) {
174             e.addAttribute(OPTIONAL, TRUE);
175         }
176         if (options == null) {
177             return;
178         }
179         for (Map.Entry JavaDoc<String JavaDoc, String JavaDoc> entry : options.entrySet()) {
180             Element option = e.addElement(OPTION);
181             option.addAttribute(VALUE, entry.getKey() + "");
182             option.addText((String JavaDoc) entry.getValue() + "");
183         }
184     }
185     
186     public void addOption(String JavaDoc value) {
187         if (options == null) {
188             addOption("1", value);
189             return;
190         }
191         Set JavaDoc<Integer JavaDoc> set = new TreeSet JavaDoc<Integer JavaDoc>();
192         for (String JavaDoc s : options.keySet()) {
193             set.add(new Integer JavaDoc(s));
194         }
195         int last = set.toArray(new Integer JavaDoc[set.size()])[set.size() -1];
196         addOption(last + 1 + "", value);
197     }
198     
199     public void addOption(String JavaDoc key, String JavaDoc value) {
200         if (options == null) {
201             options = new LinkedHashMap JavaDoc<String JavaDoc, String JavaDoc>();
202         }
203         options.put(key, value);
204     }
205     
206     public void addOption(Element e) {
207         String JavaDoc value = e.attributeValue(VALUE);
208         if (value == null) {
209             return;
210         }
211         String JavaDoc text = e.getTextTrim();
212         if (text == null || text.equals("")) {
213             return;
214         }
215         addOption(value, text);
216     }
217     
218     public String JavaDoc getCustomValue(String JavaDoc key) {
219         if (options == null || key == null) {
220             return "";
221         }
222         String JavaDoc value = options.get(key);
223         if (value == null) {
224             return "";
225         }
226         return value;
227     }
228     
229     public boolean hasOption(String JavaDoc value) {
230         if (options == null) {
231             return false;
232         }
233         return options.containsValue(value);
234     }
235     
236     public Field getClone() {
237         return new Field(getAsElement());
238     }
239     
240     public void initOptions() {
241         if (name.type == 1) {
242             label = "Severity";
243             addOption("1", "Fatal");
244             addOption("2", "Major");
245             addOption("3", "Minor");
246             addOption("4", "Trivial");
247             addOption("5", "Suggestion");
248         } else if (name.type == 2) {
249             label = "Priority";
250             addOption("1", "Highest");
251             addOption("2", "High");
252             addOption("3", "Medium");
253             addOption("4", "Low");
254             addOption("5", "Lowest");
255         }
256     }
257     
258     // getter for JSTL only, avoid nasty Enum / EL incompatibilities
259
// e.g. Glassfish vs Apache
260
public String JavaDoc getNameText() {
261         return name.text;
262     }
263     
264     //===================================================================
265

266     public Map JavaDoc<String JavaDoc, String JavaDoc> getOptions() {
267         return options;
268     }
269
270     public void setOptions(Map JavaDoc<String JavaDoc, String JavaDoc> options) {
271         this.options = options;
272     }
273     
274     public String JavaDoc getLabel() {
275         return label;
276     }
277     
278     public void setLabel(String JavaDoc label) {
279         this.label = label;
280     }
281
282     public boolean isOptional() {
283         return optional;
284     }
285
286     public void setOptional(boolean optional) {
287         this.optional = optional;
288     }
289     
290     /* custom accessor */
291     public void setName(String JavaDoc nameAsString) {
292         setName(convertToName(nameAsString));
293     }
294     
295     public Name getName() {
296         return name;
297     }
298     
299     public void setName(Name name) {
300         this.name = name;
301     }
302     
303     @Override JavaDoc
304     public String JavaDoc toString() {
305         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
306         sb.append("name [").append(name);
307         sb.append("]; label [").append(label);
308         sb.append("]; optional [").append(optional);
309         sb.append("]; options [").append(options);
310         sb.append("]");
311         return sb.toString();
312     }
313     
314     @Override JavaDoc
315     public boolean equals(Object JavaDoc o) {
316         if (this == o) {
317             return true;
318         }
319         if (!(o instanceof Field)) {
320             return false;
321         }
322         final Field f = (Field) o;
323         return f.getName().equals(name);
324     }
325     
326     @Override JavaDoc
327     public int hashCode() {
328         if (name == null) {
329             return 0;
330         }
331         return name.hashCode();
332     }
333     
334 }
335
Popular Tags