View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.model;
20  
21  import org.apache.commons.lang.builder.CompareToBuilder;
22  import org.apache.commons.lang.builder.ToStringBuilder;
23  import org.itracker.model.util.IssueUtilities;
24  
25  import java.io.Serializable;
26  import java.util.Comparator;
27  
28  /**
29   * A configuration item.
30   *
31   * @author ready
32   */
33  public class Configuration extends AbstractEntity implements Comparable<Entity> {
34  
35      public static enum Type implements IntCodeEnum<Type> {
36          initialized(-1, 0),
37          locale(1, 0),
38          status(2, IssueUtilities.FIELD_STATUS),
39          severity(3, IssueUtilities.FIELD_SEVERITY),
40          resolution(4, IssueUtilities.FIELD_RESOLUTION),
41          customfield(5, 0);
42  
43  
44          private final Integer code;
45          private final Integer legacyCode;
46  
47          private Type(Integer code, Integer legacyCode) {
48              this.code = code;
49              this.legacyCode = legacyCode;
50          }
51  
52          public Integer getCode() {
53              return code;
54          }
55  
56          public Type fromCode(Integer code) {
57              return Type.valueOf(code);
58          }
59  
60          public Integer getLegacyCode() {
61              return legacyCode;
62          }
63  
64          public static Type valueOf(Integer code) {
65              for (Type val: values()) {
66                  if (val.code.compareTo(code) == 0) {
67                      return val;
68                  }
69              }
70              throw new IllegalArgumentException("Unknown code : " + code);
71          }
72          /**
73           * Returns the key for a particular configuration item. This is made up of a
74           * static part based on the type of configuration item, and the unique value
75           * of the configuration item.
76           *
77           * @param configuration the Configuration to return the key for
78           * @return the key for the item
79           */
80          public String getLanguageKey(final Configuration configuration) {
81              if (configuration != null) {
82                  final Type type = configuration.getType();
83                  String key = "itracker." + type.name() + ".";
84  
85                  if (type == Type.locale) {
86                      key += "name.";
87                  }
88  
89                  key += configuration.getValue();
90  
91                  if (type == Type.customfield) {
92                      key += ".label";
93                  }
94                  return key;
95              }
96              return "";
97          }
98          public String getTypeLanguageKey() {
99              final String base = "itracker.web.attr.";
100 
101             return base + name();
102         }
103 
104 
105 
106     }
107 
108     public static final ConfigurationOrderComparator CONFIGURATION_ORDER_COMPARATOR = new ConfigurationOrderComparator();
109     /**
110      *
111      */
112     private static final long serialVersionUID = 1L;
113     /**
114      * PENDING: this field doesn't exist in the database!?
115      * <p/>
116      * <p>
117      * TODO : every configuration item should have a name, similar to a Java
118      * property in a properties file. A description would be nice to have too.
119      * name + version should be the natural key. (note: we shouldn't allow 2
120      * configuration items with the same name and version, but with different
121      * types).
122      * </p>
123      * <p/>
124      * <p>
125      * But since <code>name</code> is nullable, only the type and value can be
126      * used as natural key at the moment. This should be a temporary situation,
127      * because the value is allowed to change.
128      * </p>
129      */
130     private String name;
131 
132     /**
133      * ITracker version in which this configuration item was added.
134      */
135     private String version;
136 
137     /**
138      * The real type of the value stored as a string.
139      */
140     private Type type;
141 
142     /**
143      * The configuration value as a string.
144      */
145     private String value;
146 
147     /**
148      * Display order.
149      * <p/>
150      * <p>
151      * Several instances may have the same display order.
152      * </p>
153      */
154     private int order;
155 
156     /**
157      * Default constructor (required by Hibernate).
158      * <p/>
159      * <p>
160      * PENDING: should be <code>private</code> so that it can only be used by
161      * Hibernate, to ensure that the fields which form an instance's identity
162      * are always initialized/never <tt>null</tt>.
163      * </p>
164      */
165     public Configuration() {
166     }
167 
168     public Configuration(Type type, String value) {
169         setType(type);
170         setValue(value);
171     }
172 
173     public Configuration(Type type, NameValuePair pair) {
174         this(type, pair.getValue());
175         setName(pair.getName());
176     }
177 
178     public Configuration(Type type, String value, String version) {
179         this(type, value);
180         setVersion(version);
181     }
182 
183     public Configuration(Type type, String value, int order) {
184         this(type, value);
185         setOrder(order);
186     }
187 
188     public Configuration(Type type, String value, String version, int order) {
189         this(type, value, version);
190         setOrder(order);
191     }
192 
193     public String getName() {
194         return name;
195     }
196 
197     public void setName(String name) {
198         this.name = name;
199     }
200 
201     public int getOrder() {
202         return order;
203     }
204 
205     public void setOrder(int order) {
206         this.order = order;
207     }
208 
209     public Type getType() {
210         return type;
211     }
212 
213     public void setType(Type type) {
214         this.type = type;
215     }
216 
217     public String getValue() {
218         return value;
219     }
220 
221     public void setValue(String value) {
222         if (value == null) {
223             throw new IllegalArgumentException("null value");
224         }
225         this.value = value;
226     }
227 
228     public String getVersion() {
229         return version;
230     }
231 
232     public void setVersion(String version) {
233         if (version == null) {
234             throw new IllegalArgumentException("null version");
235         }
236         this.version = version;
237     }
238 
239     /**
240      * String composed of system ID and natural key (name and version).
241      */
242     @Override
243     public String toString() {
244         return new ToStringBuilder(this).append("id", getId()).append("type", getType())
245                 .append("name", getName()).append("version", getVersion()).append(
246                         "value", getValue()).toString();
247 
248     }
249 
250     public static final class ConfigurationOrderComparator implements
251             Comparator<Configuration>, Serializable {
252         /**
253          *
254          */
255         private static final long serialVersionUID = 1L;
256 
257         public int compare(Configuration./../../org/itracker/model/Configuration.html#Configuration">Configuration o1, Configuration o2) {
258             return new CompareToBuilder().append(o1.getOrder(), o2.getOrder()).append(o1.getValue(), o2.getValue())
259                     .toComparison();
260         }
261 
262 
263     }
264 
265 }