View Javadoc

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