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 java.io.Serializable;
22 import java.util.Comparator;
23
24 import org.apache.commons.lang.builder.CompareToBuilder;
25 import org.apache.commons.lang.builder.ToStringBuilder;
26
27 /**
28 * A configuration item.
29 *
30 * @author ready
31 */
32 public class Configuration extends AbstractEntity implements Comparable<Entity> {
33
34 public static final ConfigurationOrderComparator CONFIGURATION_ORDER_COMPARATOR = new ConfigurationOrderComparator();
35 /**
36 *
37 */
38 private static final long serialVersionUID = 1L;
39 /**
40 * PENDING: this field doesn't exist in the database!?
41 *
42 * <p>
43 * TODO : every configuration item should have a name, similar to a Java
44 * property in a properties file. A description would be nice to have too.
45 * name + version should be the natural key. (note: we shouldn't allow 2
46 * configuration items with the same name and version, but with different
47 * types).
48 * </p>
49 *
50 * <p>
51 * But since <code>name</code> is nullable, only the type and value can be
52 * used as natural key at the moment. This should be a temporary situation,
53 * because the value is allowed to change.
54 * </p>
55 */
56 private String name;
57
58 /** ITracker version in which this configuration item was added. */
59 private String version;
60
61 /** The real type of the value stored as a string. */
62 private int type;
63
64 /** The configuration value as a string. */
65 private String value;
66
67 /**
68 * Display order.
69 *
70 * <p>
71 * Several instances may have the same display order.
72 * </p>
73 */
74 private int order;
75
76 /**
77 * Default constructor (required by Hibernate).
78 *
79 * <p>
80 * PENDING: should be <code>private</code> so that it can only be used by
81 * Hibernate, to ensure that the fields which form an instance's identity
82 * are always initialized/never <tt>null</tt>.
83 * </p>
84 */
85 public Configuration() {
86 }
87
88 public Configuration(int type, String value) {
89 setType(type);
90 setValue(value);
91 }
92
93 public Configuration(int type, NameValuePair pair) {
94 this(type, pair.getValue());
95 setName(pair.getName());
96 }
97
98 public Configuration(int type, String value, String version) {
99 this(type, value);
100 setVersion(version);
101 }
102
103 public Configuration(int type, String value, int order) {
104 this(type, value);
105 setOrder(order);
106 }
107
108 public Configuration(int type, String value, String version, int order) {
109 this(type, value, version);
110 setOrder(order);
111 }
112
113 public String getName() {
114 return name;
115 }
116
117 public void setName(String name) {
118 this.name = name;
119 }
120
121 public int getOrder() {
122 return order;
123 }
124
125 public void setOrder(int order) {
126 this.order = order;
127 }
128
129 public int getType() {
130 return type;
131 }
132
133 public void setType(int type) {
134 this.type = type;
135 }
136
137 public String getValue() {
138 return value;
139 }
140
141 public void setValue(String value) {
142 if (value == null) {
143 throw new IllegalArgumentException("null value");
144 }
145 this.value = value;
146 }
147
148 public String getVersion() {
149 return version;
150 }
151
152 public void setVersion(String version) {
153 if (version == null) {
154 throw new IllegalArgumentException("null version");
155 }
156 this.version = version;
157 }
158
159 // /**
160 // * Compares by natural key (type and value).
161 // */
162 // public int compareTo(Configuration other) {
163 // final int typeComparison = this.type - other.type;
164 //
165 // if (typeComparison == 0) {
166 // return this.value.compareTo(other.value);
167 // }
168 // return typeComparison;
169 // }
170
171 // /**
172 // * Compares configuration items by order, value.
173 // */
174 // public int compareTo(Configuration other) {
175 // final int orderComparison = this.order - other.order;
176 //
177 // if (orderComparison == 0) {
178 // return this.value.compareTo(other.value);
179 // }
180 // return orderComparison;
181 // }
182 //
183 // /**
184 // * Compares by natural key (type and value).
185 // */
186 // @Override
187 // public boolean equals(Object obj) {
188 // if (this == obj) {
189 // return true;
190 // }
191 //
192 // if (obj instanceof Configuration) {
193 // final Configuration other = (Configuration)obj;
194 //
195 // return (this.type == other.type)
196 // && this.value.equals(other.value);
197 // }
198 // return false;
199 // }
200 //
201 // /**
202 // * Natural key (type and value) hash code.
203 // */
204 // @Override
205 // public int hashCode() {
206 // return this.type + this.value.hashCode();
207 // }
208
209 // /**
210 // * Compares by natural key (name and version).
211 // */
212 // public int compareTo(Configuration other) {
213 // final int nameComparison = this.name.compareTo(other.name);
214 //
215 // if (nameComparison == 0) {
216 // return this.version.compareTo(other.version);
217 // }
218 // return nameComparison;
219 // }
220 //
221 // /**
222 // * Compares by natural key (name and version).
223 // */
224 // @Override
225 // public boolean equals(Object obj) {
226 // if (this == obj) {
227 // return true;
228 // }
229 //
230 // if (obj instanceof Configuration) {
231 // final Configuration other = (Configuration)obj;
232 //
233 // return this.name.equals(other.name)
234 // && this.version.equals(other.version);
235 // }
236 // return false;
237 // }
238 //
239 // /**
240 // * Natural key (name and version) hash code.
241 // */
242 // @Override
243 // public int hashCode() {
244 // return this.name.hashCode() + this.version.hashCode();
245 // }
246
247 /**
248 * String composed of system ID and natural key (name and version).
249 */
250 @Override
251 public String toString() {
252 return new ToStringBuilder(this).append("id", getId()).append("type", getType())
253 .append("name", getName()).append("version", getVersion()).append(
254 "value", getValue()).toString();
255
256 }
257
258 public static final class ConfigurationOrderComparator implements
259 Comparator<Configuration>, Serializable {
260 /**
261 *
262 */
263 private static final long serialVersionUID = 1L;
264
265 public int compare(Configuration o1, Configuration o2) {
266 return new CompareToBuilder().append(o1.getOrder(), o2.getOrder()).append(o1.getValue(), o2.getValue())
267 .toComparison();
268 }
269
270
271 }
272
273 }