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
28
29
30
31
32 public class Domain {
33 private String name;
34 private String description;
35 private String size;
36 private String scale;
37
38 private SchemaType torqueType;
39 private String sqlType;
40 private String defaultValue;
41
42
43
44
45 public Domain() {
46 this.name = null;
47 }
48
49
50
51
52
53
54
55 public Domain(String name) {
56 this.name = name;
57 }
58
59
60
61
62 public Domain(SchemaType type) {
63 this.name = null;
64 this.torqueType = type;
65 this.sqlType = type.getName();
66 }
67
68
69
70
71 public Domain(SchemaType type, String sqlType) {
72 this.name = null;
73 this.torqueType = type;
74 this.sqlType = sqlType;
75 }
76
77
78
79
80 public Domain(SchemaType type, String sqlType, String size, String scale) {
81 this.name = null;
82 this.torqueType = type;
83 this.sqlType = sqlType;
84 this.size = size;
85 this.scale = scale;
86 }
87
88
89
90
91 public Domain(SchemaType type, String sqlType, String size) {
92 this.name = null;
93 this.torqueType = type;
94 this.sqlType = sqlType;
95 this.size = size;
96 }
97
98 public Domain(Domain domain) {
99 copy(domain);
100 }
101
102 public void copy(Domain domain) {
103 this.defaultValue = domain.getDefaultValue();
104 this.description = domain.getDescription();
105 this.name = domain.getName();
106 this.scale = domain.getScale();
107 this.size = domain.getSize();
108 this.sqlType = domain.getSqlType();
109 this.torqueType = domain.getType();
110 }
111
112
113
114
115 public void loadFromXML(Attributes attrib, Platform platform) {
116 SchemaType schemaType = SchemaType.getEnum(attrib.getValue("type"));
117 copy(platform.getDomainForSchemaType(schemaType));
118
119 name = attrib.getValue("name");
120
121 defaultValue = attrib.getValue("default");
122 size = attrib.getValue("size");
123 scale = attrib.getValue("scale");
124
125 description = attrib.getValue("description");
126 }
127
128
129
130
131 public String getDescription() {
132 return description;
133 }
134
135
136
137
138
139 public void setDescription(String description) {
140 this.description = description;
141 }
142
143
144
145
146 public String getName() {
147 return name;
148 }
149
150
151
152
153
154 public void setName(String name) {
155 this.name = name;
156 }
157
158
159
160
161 public String getScale() {
162 return scale;
163 }
164
165
166
167
168
169 public void setScale(String scale) {
170 this.scale = scale;
171 }
172
173
174
175
176
177
178
179 public void replaceScale(String value) {
180 this.scale = StringUtils.defaultString(value, getScale());
181 }
182
183
184
185
186 public String getSize() {
187 return size;
188 }
189
190
191
192
193
194 public void setSize(String size) {
195 this.size = size;
196 }
197
198
199
200
201
202
203
204 public void replaceSize(String value) {
205 this.size = StringUtils.defaultString(value, getSize());
206 }
207
208
209
210
211 public SchemaType getType() {
212 return torqueType;
213 }
214
215
216
217
218
219 public void setType(SchemaType torqueType) {
220 this.torqueType = torqueType;
221 }
222
223
224
225
226
227 public void setType(String torqueType) {
228 this.torqueType = SchemaType.getEnum(torqueType);
229 }
230
231
232
233
234
235
236
237 public void replaceType(String value) {
238 this.torqueType = SchemaType.getEnum(StringUtils.defaultString(value, getType().getName()));
239 }
240
241
242
243
244 public String getDefaultValue() {
245 return defaultValue;
246 }
247
248
249
250
251
252
253 public String getDefaultSetting() {
254 StringBuffer dflt = new StringBuffer(0);
255 if (getDefaultValue() != null) {
256 dflt.append("default ");
257 if (TypeMap.isTextType(getType())) {
258
259 dflt.append('\'').append(getDefaultValue()).append('\'');
260 } else {
261 dflt.append(getDefaultValue());
262 }
263 }
264 return dflt.toString();
265 }
266
267
268
269
270
271 public void setDefaultValue(String defaultValue) {
272 this.defaultValue = defaultValue;
273 }
274
275
276
277
278
279
280
281 public void replaceDefaultValue(String value) {
282 this.defaultValue = StringUtils.defaultString(value, getDefaultValue());
283 }
284
285
286
287
288 public String getSqlType() {
289 return sqlType;
290 }
291
292
293
294
295
296 public void setSqlType(String sqlType) {
297 this.sqlType = sqlType;
298 }
299
300
301
302
303
304
305 public String printSize() {
306 if (size != null && scale != null) {
307 return '(' + size + ',' + scale + ')';
308 } else if (size != null) {
309 return '(' + size + ')';
310 } else {
311 return "";
312 }
313 }
314
315 }