Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Domain |
|
| 1.2333333333333334;1.233 |
1 | package org.apache.torque.engine.database.model; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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,v 1.1 2007-10-21 07:57:27 abyrne Exp $ | |
31 | */ | |
32 | public class Domain { | |
33 | private String name; | |
34 | private String description; | |
35 | private String size; | |
36 | private String scale; | |
37 | /** type as defined in schema.xml */ | |
38 | private SchemaType torqueType; | |
39 | private String sqlType; | |
40 | private String defaultValue; | |
41 | ||
42 | /** | |
43 | * Creates a new instance with a <code>null</code> name. | |
44 | */ | |
45 | 0 | public Domain() { |
46 | 0 | this.name = null; |
47 | 0 | } |
48 | ||
49 | /** | |
50 | * Creates a new Domain and set the name | |
51 | * | |
52 | * @param name | |
53 | * column name | |
54 | */ | |
55 | 0 | public Domain(String name) { |
56 | 0 | this.name = name; |
57 | 0 | } |
58 | ||
59 | /** | |
60 | * Creates a new Domain and set the name | |
61 | */ | |
62 | 0 | public Domain(SchemaType type) { |
63 | 0 | this.name = null; |
64 | 0 | this.torqueType = type; |
65 | 0 | this.sqlType = type.getName(); |
66 | 0 | } |
67 | ||
68 | /** | |
69 | * Creates a new Domain and set the name | |
70 | */ | |
71 | 0 | public Domain(SchemaType type, String sqlType) { |
72 | 0 | this.name = null; |
73 | 0 | this.torqueType = type; |
74 | 0 | this.sqlType = sqlType; |
75 | 0 | } |
76 | ||
77 | /** | |
78 | * Creates a new Domain and set the name | |
79 | */ | |
80 | 0 | public Domain(SchemaType type, String sqlType, String size, String scale) { |
81 | 0 | this.name = null; |
82 | 0 | this.torqueType = type; |
83 | 0 | this.sqlType = sqlType; |
84 | 0 | this.size = size; |
85 | 0 | this.scale = scale; |
86 | 0 | } |
87 | ||
88 | /** | |
89 | * Creates a new Domain and set the name | |
90 | */ | |
91 | 0 | public Domain(SchemaType type, String sqlType, String size) { |
92 | 0 | this.name = null; |
93 | 0 | this.torqueType = type; |
94 | 0 | this.sqlType = sqlType; |
95 | 0 | this.size = size; |
96 | 0 | } |
97 | ||
98 | 0 | public Domain(Domain domain) { |
99 | 0 | copy(domain); |
100 | 0 | } |
101 | ||
102 | public void copy(Domain domain) { | |
103 | 0 | this.defaultValue = domain.getDefaultValue(); |
104 | 0 | this.description = domain.getDescription(); |
105 | 0 | this.name = domain.getName(); |
106 | 0 | this.scale = domain.getScale(); |
107 | 0 | this.size = domain.getSize(); |
108 | 0 | this.sqlType = domain.getSqlType(); |
109 | 0 | this.torqueType = domain.getType(); |
110 | 0 | } |
111 | ||
112 | /** | |
113 | * Imports a column from an XML specification | |
114 | */ | |
115 | public void loadFromXML(Attributes attrib, Platform platform) { | |
116 | 0 | SchemaType schemaType = SchemaType.getEnum(attrib.getValue("type")); |
117 | 0 | copy(platform.getDomainForSchemaType(schemaType)); |
118 | // Name | |
119 | 0 | name = attrib.getValue("name"); |
120 | // Default column value. | |
121 | 0 | defaultValue = attrib.getValue("default"); |
122 | 0 | size = attrib.getValue("size"); |
123 | 0 | scale = attrib.getValue("scale"); |
124 | ||
125 | 0 | description = attrib.getValue("description"); |
126 | 0 | } |
127 | ||
128 | /** | |
129 | * @return Returns the description. | |
130 | */ | |
131 | public String getDescription() { | |
132 | 0 | return description; |
133 | } | |
134 | ||
135 | /** | |
136 | * @param description | |
137 | * The description to set. | |
138 | */ | |
139 | public void setDescription(String description) { | |
140 | 0 | this.description = description; |
141 | 0 | } |
142 | ||
143 | /** | |
144 | * @return Returns the name. | |
145 | */ | |
146 | public String getName() { | |
147 | 0 | return name; |
148 | } | |
149 | ||
150 | /** | |
151 | * @param name | |
152 | * The name to set. | |
153 | */ | |
154 | public void setName(String name) { | |
155 | 0 | this.name = name; |
156 | 0 | } |
157 | ||
158 | /** | |
159 | * @return Returns the scale. | |
160 | */ | |
161 | public String getScale() { | |
162 | 0 | return scale; |
163 | } | |
164 | ||
165 | /** | |
166 | * @param scale | |
167 | * The scale to set. | |
168 | */ | |
169 | public void setScale(String scale) { | |
170 | 0 | this.scale = scale; |
171 | 0 | } |
172 | ||
173 | /** | |
174 | * Replaces the size if the new value is not null. | |
175 | * | |
176 | * @param value | |
177 | * The size to set. | |
178 | */ | |
179 | public void replaceScale(String value) { | |
180 | 0 | this.scale = StringUtils.defaultString(value, getScale()); |
181 | 0 | } |
182 | ||
183 | /** | |
184 | * @return Returns the size. | |
185 | */ | |
186 | public String getSize() { | |
187 | 0 | return size; |
188 | } | |
189 | ||
190 | /** | |
191 | * @param size | |
192 | * The size to set. | |
193 | */ | |
194 | public void setSize(String size) { | |
195 | 0 | this.size = size; |
196 | 0 | } |
197 | ||
198 | /** | |
199 | * Replaces the size if the new value is not null. | |
200 | * | |
201 | * @param value | |
202 | * The size to set. | |
203 | */ | |
204 | public void replaceSize(String value) { | |
205 | 0 | this.size = StringUtils.defaultString(value, getSize()); |
206 | 0 | } |
207 | ||
208 | /** | |
209 | * @return Returns the torqueType. | |
210 | */ | |
211 | public SchemaType getType() { | |
212 | 0 | return torqueType; |
213 | } | |
214 | ||
215 | /** | |
216 | * @param torqueType | |
217 | * The torqueType to set. | |
218 | */ | |
219 | public void setType(SchemaType torqueType) { | |
220 | 0 | this.torqueType = torqueType; |
221 | 0 | } |
222 | ||
223 | /** | |
224 | * @param torqueType | |
225 | * The torqueType to set. | |
226 | */ | |
227 | public void setType(String torqueType) { | |
228 | 0 | this.torqueType = SchemaType.getEnum(torqueType); |
229 | 0 | } |
230 | ||
231 | /** | |
232 | * Replaces the default value if the new value is not null. | |
233 | * | |
234 | * @param value | |
235 | * The defaultValue to set. | |
236 | */ | |
237 | public void replaceType(String value) { | |
238 | 0 | this.torqueType = SchemaType.getEnum(StringUtils.defaultString(value, getType().getName())); |
239 | 0 | } |
240 | ||
241 | /** | |
242 | * @return Returns the defaultValue. | |
243 | */ | |
244 | public String getDefaultValue() { | |
245 | 0 | return defaultValue; |
246 | } | |
247 | ||
248 | /** | |
249 | * Return a string that will give this column a default value. | |
250 | * | |
251 | * @deprecated | |
252 | */ | |
253 | public String getDefaultSetting() { | |
254 | 0 | StringBuffer dflt = new StringBuffer(0); |
255 | 0 | if (getDefaultValue() != null) { |
256 | 0 | dflt.append("default "); |
257 | 0 | if (TypeMap.isTextType(getType())) { |
258 | // TODO: Properly SQL-escape the text. | |
259 | 0 | dflt.append('\'').append(getDefaultValue()).append('\''); |
260 | } else { | |
261 | 0 | dflt.append(getDefaultValue()); |
262 | } | |
263 | } | |
264 | 0 | return dflt.toString(); |
265 | } | |
266 | ||
267 | /** | |
268 | * @param defaultValue | |
269 | * The defaultValue to set. | |
270 | */ | |
271 | public void setDefaultValue(String defaultValue) { | |
272 | 0 | this.defaultValue = defaultValue; |
273 | 0 | } |
274 | ||
275 | /** | |
276 | * Replaces the default value if the new value is not null. | |
277 | * | |
278 | * @param value | |
279 | * The defaultValue to set. | |
280 | */ | |
281 | public void replaceDefaultValue(String value) { | |
282 | 0 | this.defaultValue = StringUtils.defaultString(value, getDefaultValue()); |
283 | 0 | } |
284 | ||
285 | /** | |
286 | * @return Returns the sqlType. | |
287 | */ | |
288 | public String getSqlType() { | |
289 | 0 | return sqlType; |
290 | } | |
291 | ||
292 | /** | |
293 | * @param sqlType | |
294 | * The sqlType to set. | |
295 | */ | |
296 | public void setSqlType(String sqlType) { | |
297 | 0 | this.sqlType = sqlType; |
298 | 0 | } |
299 | ||
300 | /** | |
301 | * Return the size and scale in brackets for use in an sql schema. | |
302 | * | |
303 | * @return size and scale or an empty String if there are no values available. | |
304 | */ | |
305 | public String printSize() { | |
306 | 0 | if (size != null && scale != null) { |
307 | 0 | return '(' + size + ',' + scale + ')'; |
308 | 0 | } else if (size != null) { |
309 | 0 | return '(' + size + ')'; |
310 | } else { | |
311 | 0 | return ""; |
312 | } | |
313 | } | |
314 | ||
315 | } |