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 473814 2006-11-11 22:30:30Z tv $
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 column name
053: */
054: public Domain(String name) {
055: this .name = name;
056: }
057:
058: /**
059: * Creates a new Domain and set the name
060: */
061: public Domain(SchemaType type) {
062: this .name = null;
063: this .torqueType = type;
064: this .sqlType = type.getName();
065: }
066:
067: /**
068: * Creates a new Domain and set the name
069: */
070: public Domain(SchemaType type, String sqlType) {
071: this .name = null;
072: this .torqueType = type;
073: this .sqlType = sqlType;
074: }
075:
076: /**
077: * Creates a new Domain and set the name
078: */
079: public Domain(SchemaType type, String sqlType, String size,
080: 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
117: .getValue("type"));
118: copy(platform.getDomainForSchemaType(schemaType));
119: //Name
120: name = attrib.getValue("name");
121: //Default column value.
122: defaultValue = attrib.getValue("default");
123: size = attrib.getValue("size");
124: scale = attrib.getValue("scale");
125:
126: description = attrib.getValue("description");
127: }
128:
129: /**
130: * @return Returns the description.
131: */
132: public String getDescription() {
133: return description;
134: }
135:
136: /**
137: * @param description 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 The name to set.
152: */
153: public void setName(String name) {
154: this .name = name;
155: }
156:
157: /**
158: * @return Returns the scale.
159: */
160: public String getScale() {
161: return scale;
162: }
163:
164: /**
165: * @param scale The scale to set.
166: */
167: public void setScale(String scale) {
168: this .scale = scale;
169: }
170:
171: /**
172: * Replaces the size if the new value is not null.
173: *
174: * @param value The size to set.
175: */
176: public void replaceScale(String value) {
177: this .scale = StringUtils.defaultString(value, getScale());
178: }
179:
180: /**
181: * @return Returns the size.
182: */
183: public String getSize() {
184: return size;
185: }
186:
187: /**
188: * @param size The size to set.
189: */
190: public void setSize(String size) {
191: this .size = size;
192: }
193:
194: /**
195: * Replaces the size if the new value is not null.
196: *
197: * @param value The size to set.
198: */
199: public void replaceSize(String value) {
200: this .size = StringUtils.defaultString(value, getSize());
201: }
202:
203: /**
204: * @return Returns the torqueType.
205: */
206: public SchemaType getType() {
207: return torqueType;
208: }
209:
210: /**
211: * @param torqueType The torqueType to set.
212: */
213: public void setType(SchemaType torqueType) {
214: this .torqueType = torqueType;
215: }
216:
217: /**
218: * @param torqueType The torqueType to set.
219: */
220: public void setType(String torqueType) {
221: this .torqueType = SchemaType.getEnum(torqueType);
222: }
223:
224: /**
225: * Replaces the default value if the new value is not null.
226: *
227: * @param value The defaultValue to set.
228: */
229: public void replaceType(String value) {
230: this .torqueType = SchemaType.getEnum(StringUtils.defaultString(
231: value, getType().getName()));
232: }
233:
234: /**
235: * @return Returns the defaultValue.
236: */
237: public String getDefaultValue() {
238: return defaultValue;
239: }
240:
241: /**
242: * Return a string that will give this column a default value.
243: * @deprecated
244: */
245: public String getDefaultSetting() {
246: StringBuffer dflt = new StringBuffer(0);
247: if (getDefaultValue() != null) {
248: dflt.append("default ");
249: if (TypeMap.isTextType(getType())) {
250: // TODO: Properly SQL-escape the text.
251: dflt.append('\'').append(getDefaultValue())
252: .append('\'');
253: } else {
254: dflt.append(getDefaultValue());
255: }
256: }
257: return dflt.toString();
258: }
259:
260: /**
261: * @param defaultValue The defaultValue to set.
262: */
263: public void setDefaultValue(String defaultValue) {
264: this .defaultValue = defaultValue;
265: }
266:
267: /**
268: * Replaces the default value if the new value is not null.
269: *
270: * @param value The defaultValue to set.
271: */
272: public void replaceDefaultValue(String value) {
273: this .defaultValue = StringUtils.defaultString(value,
274: getDefaultValue());
275: }
276:
277: /**
278: * @return Returns the sqlType.
279: */
280: public String getSqlType() {
281: return sqlType;
282: }
283:
284: /**
285: * @param sqlType The sqlType to set.
286: */
287: public void setSqlType(String sqlType) {
288: this .sqlType = sqlType;
289: }
290:
291: /**
292: * Return the size and scale in brackets for use in an sql schema.
293: *
294: * @return size and scale or an empty String if there are no values
295: * available.
296: */
297: public String printSize() {
298: if (size != null && scale != null) {
299: return '(' + size + ',' + scale + ')';
300: } else if (size != null) {
301: return '(' + size + ')';
302: } else {
303: return "";
304: }
305: }
306:
307: }
|