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