1 package org.apache.torque.engine.database.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.torque.engine.platform.Platform;
24 import org.xml.sax.Attributes;
25
26 /***
27 * A Class for holding data about a column used in an Application.
28 *
29 * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a>
30 * @version $Id: Domain.java 473814 2006-11-11 22:30:30Z tv $
31 */
32 public class Domain
33 {
34 private String name;
35 private String description;
36 private String size;
37 private String scale;
38 /*** type as defined in schema.xml */
39 private SchemaType torqueType;
40 private String sqlType;
41 private String defaultValue;
42
43 /***
44 * Creates a new instance with a <code>null</code> name.
45 */
46 public Domain()
47 {
48 this.name = null;
49 }
50
51 /***
52 * Creates a new Domain and set the name
53 *
54 * @param name column name
55 */
56 public Domain(String name)
57 {
58 this.name = name;
59 }
60
61 /***
62 * Creates a new Domain and set the name
63 */
64 public Domain(SchemaType type)
65 {
66 this.name = null;
67 this.torqueType = type;
68 this.sqlType = type.getName();
69 }
70
71 /***
72 * Creates a new Domain and set the name
73 */
74 public Domain(SchemaType type, String sqlType)
75 {
76 this.name = null;
77 this.torqueType = type;
78 this.sqlType = sqlType;
79 }
80
81 /***
82 * Creates a new Domain and set the name
83 */
84 public Domain(SchemaType type, String sqlType, String size, String scale)
85 {
86 this.name = null;
87 this.torqueType = type;
88 this.sqlType = sqlType;
89 this.size = size;
90 this.scale = scale;
91 }
92
93 /***
94 * Creates a new Domain and set the name
95 */
96 public Domain(SchemaType type, String sqlType, String size)
97 {
98 this.name = null;
99 this.torqueType = type;
100 this.sqlType = sqlType;
101 this.size = size;
102 }
103
104 public Domain(Domain domain)
105 {
106 copy(domain);
107 }
108
109 public void copy(Domain domain)
110 {
111 this.defaultValue = domain.getDefaultValue();
112 this.description = domain.getDescription();
113 this.name = domain.getName();
114 this.scale = domain.getScale();
115 this.size = domain.getSize();
116 this.sqlType = domain.getSqlType();
117 this.torqueType = domain.getType();
118 }
119
120 /***
121 * Imports a column from an XML specification
122 */
123 public void loadFromXML(Attributes attrib, Platform platform)
124 {
125 SchemaType schemaType = SchemaType.getEnum(attrib.getValue("type"));
126 copy(platform.getDomainForSchemaType(schemaType));
127
128 name = attrib.getValue("name");
129
130 defaultValue = attrib.getValue("default");
131 size = attrib.getValue("size");
132 scale = attrib.getValue("scale");
133
134 description = attrib.getValue("description");
135 }
136
137 /***
138 * @return Returns the description.
139 */
140 public String getDescription()
141 {
142 return description;
143 }
144
145 /***
146 * @param description The description to set.
147 */
148 public void setDescription(String description)
149 {
150 this.description = description;
151 }
152
153 /***
154 * @return Returns the name.
155 */
156 public String getName()
157 {
158 return name;
159 }
160
161 /***
162 * @param name The name to set.
163 */
164 public void setName(String name)
165 {
166 this.name = name;
167 }
168
169 /***
170 * @return Returns the scale.
171 */
172 public String getScale()
173 {
174 return scale;
175 }
176
177 /***
178 * @param scale The scale to set.
179 */
180 public void setScale(String scale)
181 {
182 this.scale = scale;
183 }
184
185 /***
186 * Replaces the size if the new value is not null.
187 *
188 * @param value The size to set.
189 */
190 public void replaceScale(String value)
191 {
192 this.scale = StringUtils.defaultString(value, getScale());
193 }
194
195 /***
196 * @return Returns the size.
197 */
198 public String getSize()
199 {
200 return size;
201 }
202
203 /***
204 * @param size The size to set.
205 */
206 public void setSize(String size)
207 {
208 this.size = size;
209 }
210
211 /***
212 * Replaces the size if the new value is not null.
213 *
214 * @param value The size to set.
215 */
216 public void replaceSize(String value)
217 {
218 this.size = StringUtils.defaultString(value, getSize());
219 }
220
221 /***
222 * @return Returns the torqueType.
223 */
224 public SchemaType getType()
225 {
226 return torqueType;
227 }
228
229 /***
230 * @param torqueType The torqueType to set.
231 */
232 public void setType(SchemaType torqueType)
233 {
234 this.torqueType = torqueType;
235 }
236
237 /***
238 * @param torqueType The torqueType to set.
239 */
240 public void setType(String torqueType)
241 {
242 this.torqueType = SchemaType.getEnum(torqueType);
243 }
244
245 /***
246 * Replaces the default value if the new value is not null.
247 *
248 * @param value The defaultValue to set.
249 */
250 public void replaceType(String value)
251 {
252 this.torqueType = SchemaType.getEnum(
253 StringUtils.defaultString(value, getType().getName()));
254 }
255
256 /***
257 * @return Returns the defaultValue.
258 */
259 public String getDefaultValue()
260 {
261 return defaultValue;
262 }
263
264 /***
265 * Return a string that will give this column a default value.
266 * @deprecated
267 */
268 public String getDefaultSetting()
269 {
270 StringBuffer dflt = new StringBuffer(0);
271 if (getDefaultValue() != null)
272 {
273 dflt.append("default ");
274 if (TypeMap.isTextType(getType()))
275 {
276
277 dflt.append('\'').append(getDefaultValue()).append('\'');
278 }
279 else
280 {
281 dflt.append(getDefaultValue());
282 }
283 }
284 return dflt.toString();
285 }
286
287 /***
288 * @param defaultValue The defaultValue to set.
289 */
290 public void setDefaultValue(String defaultValue)
291 {
292 this.defaultValue = defaultValue;
293 }
294
295 /***
296 * Replaces the default value if the new value is not null.
297 *
298 * @param value The defaultValue to set.
299 */
300 public void replaceDefaultValue(String value)
301 {
302 this.defaultValue = StringUtils.defaultString(value, getDefaultValue());
303 }
304
305 /***
306 * @return Returns the sqlType.
307 */
308 public String getSqlType()
309 {
310 return sqlType;
311 }
312
313 /***
314 * @param sqlType The sqlType to set.
315 */
316 public void setSqlType(String sqlType)
317 {
318 this.sqlType = sqlType;
319 }
320
321 /***
322 * Return the size and scale in brackets for use in an sql schema.
323 *
324 * @return size and scale or an empty String if there are no values
325 * available.
326 */
327 public String printSize()
328 {
329 if (size != null && scale != null)
330 {
331 return '(' + size + ',' + scale + ')';
332 }
333 else if (size != null)
334 {
335 return '(' + size + ')';
336 }
337 else
338 {
339 return "";
340 }
341 }
342
343 }