View Javadoc

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  	public Domain() {
46  		this.name = null;
47  	}
48  
49  	/**
50  	 * Creates a new Domain and set the name
51  	 * 
52  	 * @param name
53  	 *            column name
54  	 */
55  	public Domain(String name) {
56  		this.name = name;
57  	}
58  
59  	/**
60  	 * Creates a new Domain and set the name
61  	 */
62  	public Domain(SchemaType type) {
63  		this.name = null;
64  		this.torqueType = type;
65  		this.sqlType = type.getName();
66  	}
67  
68  	/**
69  	 * Creates a new Domain and set the name
70  	 */
71  	public Domain(SchemaType type, String sqlType) {
72  		this.name = null;
73  		this.torqueType = type;
74  		this.sqlType = sqlType;
75  	}
76  
77  	/**
78  	 * Creates a new Domain and set the name
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  	 * Creates a new Domain and set the name
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 	 * 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 }