001 package org.apache.torque.engine.database.model;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.commons.lang.StringUtils;
023 import org.apache.torque.engine.platform.Platform;
024 import org.xml.sax.Attributes;
025
026 /**
027 * A Class for holding data about a column used in an Application.
028 *
029 * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a>
030 * @version $Id: Domain.java,v 1.1 2007-10-21 07:57:27 abyrne Exp $
031 */
032 public class Domain {
033 private String name;
034 private String description;
035 private String size;
036 private String scale;
037 /** type as defined in schema.xml */
038 private SchemaType torqueType;
039 private String sqlType;
040 private String defaultValue;
041
042 /**
043 * Creates a new instance with a <code>null</code> name.
044 */
045 public Domain() {
046 this.name = null;
047 }
048
049 /**
050 * Creates a new Domain and set the name
051 *
052 * @param name
053 * column name
054 */
055 public Domain(String name) {
056 this.name = name;
057 }
058
059 /**
060 * Creates a new Domain and set the name
061 */
062 public Domain(SchemaType type) {
063 this.name = null;
064 this.torqueType = type;
065 this.sqlType = type.getName();
066 }
067
068 /**
069 * Creates a new Domain and set the name
070 */
071 public Domain(SchemaType type, String sqlType) {
072 this.name = null;
073 this.torqueType = type;
074 this.sqlType = sqlType;
075 }
076
077 /**
078 * Creates a new Domain and set the name
079 */
080 public Domain(SchemaType type, String sqlType, String size, String scale) {
081 this.name = null;
082 this.torqueType = type;
083 this.sqlType = sqlType;
084 this.size = size;
085 this.scale = scale;
086 }
087
088 /**
089 * Creates a new Domain and set the name
090 */
091 public Domain(SchemaType type, String sqlType, String size) {
092 this.name = null;
093 this.torqueType = type;
094 this.sqlType = sqlType;
095 this.size = size;
096 }
097
098 public Domain(Domain domain) {
099 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 * Imports a column from an XML specification
114 */
115 public void loadFromXML(Attributes attrib, Platform platform) {
116 SchemaType schemaType = SchemaType.getEnum(attrib.getValue("type"));
117 copy(platform.getDomainForSchemaType(schemaType));
118 // Name
119 name = attrib.getValue("name");
120 // Default column value.
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 * @return Returns the description.
130 */
131 public String getDescription() {
132 return description;
133 }
134
135 /**
136 * @param description
137 * The description to set.
138 */
139 public void setDescription(String description) {
140 this.description = description;
141 }
142
143 /**
144 * @return Returns the name.
145 */
146 public String getName() {
147 return name;
148 }
149
150 /**
151 * @param name
152 * The name to set.
153 */
154 public void setName(String name) {
155 this.name = name;
156 }
157
158 /**
159 * @return Returns the scale.
160 */
161 public String getScale() {
162 return scale;
163 }
164
165 /**
166 * @param scale
167 * The scale to set.
168 */
169 public void setScale(String scale) {
170 this.scale = scale;
171 }
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 this.scale = StringUtils.defaultString(value, getScale());
181 }
182
183 /**
184 * @return Returns the size.
185 */
186 public String getSize() {
187 return size;
188 }
189
190 /**
191 * @param size
192 * The size to set.
193 */
194 public void setSize(String size) {
195 this.size = size;
196 }
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 this.size = StringUtils.defaultString(value, getSize());
206 }
207
208 /**
209 * @return Returns the torqueType.
210 */
211 public SchemaType getType() {
212 return torqueType;
213 }
214
215 /**
216 * @param torqueType
217 * The torqueType to set.
218 */
219 public void setType(SchemaType torqueType) {
220 this.torqueType = torqueType;
221 }
222
223 /**
224 * @param torqueType
225 * The torqueType to set.
226 */
227 public void setType(String torqueType) {
228 this.torqueType = SchemaType.getEnum(torqueType);
229 }
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 this.torqueType = SchemaType.getEnum(StringUtils.defaultString(value, getType().getName()));
239 }
240
241 /**
242 * @return Returns the defaultValue.
243 */
244 public String getDefaultValue() {
245 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 StringBuffer dflt = new StringBuffer(0);
255 if (getDefaultValue() != null) {
256 dflt.append("default ");
257 if (TypeMap.isTextType(getType())) {
258 // TODO: Properly SQL-escape the text.
259 dflt.append('\'').append(getDefaultValue()).append('\'');
260 } else {
261 dflt.append(getDefaultValue());
262 }
263 }
264 return dflt.toString();
265 }
266
267 /**
268 * @param defaultValue
269 * The defaultValue to set.
270 */
271 public void setDefaultValue(String defaultValue) {
272 this.defaultValue = defaultValue;
273 }
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 this.defaultValue = StringUtils.defaultString(value, getDefaultValue());
283 }
284
285 /**
286 * @return Returns the sqlType.
287 */
288 public String getSqlType() {
289 return sqlType;
290 }
291
292 /**
293 * @param sqlType
294 * The sqlType to set.
295 */
296 public void setSqlType(String sqlType) {
297 this.sqlType = sqlType;
298 }
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 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 }