1 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 ; 24 import java.util.HashMap ; 25 import java.util.LinkedHashMap ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.TreeSet ; 29 30 import org.dom4j.Element; 31 32 37 public class Field implements Serializable { 38 39 private Name name; 40 private String label; 41 private boolean optional; 42 private Map <String , String > options; 43 44 private static final Map <String , Name> nameMap; 45 46 static { 48 nameMap = new HashMap <String , Name>(); 49 for (Name n : Name.values()) { 50 nameMap.put(n.text, n); 51 } 52 } 53 54 57 public static Name convertToName(String text) { 58 Name n = nameMap.get(text); 59 if (n == null) { 60 throw new RuntimeException ("Bad name " + text); 61 } 62 return n; 63 } 64 65 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 text; 97 98 Name(int type, String text) { 99 this.type = type; 100 this.text = text; 101 } 102 103 public int getType() { 104 return type; 105 } 106 107 public String getText() { 108 return text; 109 } 110 111 public String 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 ("Unknown type " + type); 120 } 121 } 122 123 @Override 124 public String toString() { 125 return text; 126 } 127 } 128 129 131 public Field() { 132 } 134 135 public Field(String 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 o : e.elements(OPTION)) { 151 addOption((Element) o); 152 } 153 } 154 155 156 public void addAsChildOf(Element parent) { 157 Element e = parent.addElement(FIELD); 158 copyTo(e); 159 } 160 161 162 public Element getAsElement() { 163 Element e = XmlUtils.getNewElement(FIELD); 164 copyTo(e); 165 return e; 166 } 167 168 169 private void copyTo(Element e) { 170 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 <String , String > entry : options.entrySet()) { 180 Element option = e.addElement(OPTION); 181 option.addAttribute(VALUE, entry.getKey() + ""); 182 option.addText((String ) entry.getValue() + ""); 183 } 184 } 185 186 public void addOption(String value) { 187 if (options == null) { 188 addOption("1", value); 189 return; 190 } 191 Set <Integer > set = new TreeSet <Integer >(); 192 for (String s : options.keySet()) { 193 set.add(new Integer (s)); 194 } 195 int last = set.toArray(new Integer [set.size()])[set.size() -1]; 196 addOption(last + 1 + "", value); 197 } 198 199 public void addOption(String key, String value) { 200 if (options == null) { 201 options = new LinkedHashMap <String , String >(); 202 } 203 options.put(key, value); 204 } 205 206 public void addOption(Element e) { 207 String value = e.attributeValue(VALUE); 208 if (value == null) { 209 return; 210 } 211 String text = e.getTextTrim(); 212 if (text == null || text.equals("")) { 213 return; 214 } 215 addOption(value, text); 216 } 217 218 public String getCustomValue(String key) { 219 if (options == null || key == null) { 220 return ""; 221 } 222 String value = options.get(key); 223 if (value == null) { 224 return ""; 225 } 226 return value; 227 } 228 229 public boolean hasOption(String 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 public String getNameText() { 261 return name.text; 262 } 263 264 266 public Map <String , String > getOptions() { 267 return options; 268 } 269 270 public void setOptions(Map <String , String > options) { 271 this.options = options; 272 } 273 274 public String getLabel() { 275 return label; 276 } 277 278 public void setLabel(String 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 291 public void setName(String 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 304 public String toString() { 305 StringBuffer sb = new StringBuffer (); 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 315 public boolean equals(Object 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 327 public int hashCode() { 328 if (name == null) { 329 return 0; 330 } 331 return name.hashCode(); 332 } 333 334 } 335 | Popular Tags |