View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package pl.kernelpanic.dbmonster.schema;
48  
49  import java.sql.Types;
50  import java.util.ArrayList;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  import pl.kernelpanic.dbmonster.DBMonsterContext;
55  import pl.kernelpanic.dbmonster.generator.DataGenerator;
56  import pl.kernelpanic.dbmonster.generator.Initializable;
57  
58  /***
59   * The column.
60   *
61   * @author Piotr Maj <piotr.maj@kernelpanic.pl>
62   *
63   * @version $Id: Column.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
64   */
65  public class Column implements Comparable {
66  
67      /***
68       * The name of the column.
69       */
70      private String name = null;
71  
72      /***
73       * The table.
74       */
75      private Table table = null;
76  
77      /***
78       * Generated value.
79       */
80      private Object value = null;
81  
82      /***
83       * The data generator for this column.
84       */
85      private DataGenerator generator = null;
86  
87      /***
88       * A list of tables this column depends on.
89       */
90      private List dependencies = new ArrayList();
91  
92      /***
93       * Use the database default value. This results in not
94       * including this column in the generation process.
95       */
96      private boolean databaseDefault = false;
97  
98      /***
99       * Internal status whether the column is already generated.
100      */
101     private boolean generated = false;
102 
103     /***
104      * Target SQL type.
105      */
106     private int targetType = Types.VARCHAR;
107 
108     /***
109      * Returns the name of the column.
110      *
111      * @return the name of the column.
112      */
113     public final String getName() {
114         return name;
115     }
116 
117     /***
118      * Sets the name of the column.
119      *
120      * @param s the name of the column.
121      */
122     public final void setName(final String s) {
123         name = s;
124     }
125 
126     /***
127      * Returns a data generator.
128      *
129      * @return generator.
130      */
131     public final DataGenerator getGenerator() {
132         return generator;
133     }
134 
135     /***
136      * Sets the data generator.
137      *
138      * @param gen data generator
139      */
140     public final void setGenerator(final DataGenerator gen) {
141         generator = gen;
142         generator.setColumn(this);
143     }
144 
145     /***
146      * Initializes the column.
147      *
148      * @param ctx dbmonster context.
149      *
150      * @throws Exception if initialization fails
151      */
152     public final void initialize(final DBMonsterContext ctx) throws Exception {
153         if (generator instanceof Initializable) {
154             ((Initializable) generator).initialize(ctx);
155         }
156     }
157 
158     /***
159      * Generates this column.
160      *
161      * @throws Exception if this column could not be generated
162      */
163     public final void generate() throws Exception {
164         if (!generated) {
165             Iterator it = dependencies.iterator();
166             while (it.hasNext()) {
167                 String tableName = (String) it.next();
168                 if (!tableName.equals(table.getName())) {
169                     Schema schema = table.getSchema();
170                     Table t = schema.findTable(tableName);
171                     t.generate();
172                 }
173             }
174             value = generator.generate();
175             generated = true;
176         }
177     }
178 
179     /***
180      * Resets the column.
181      */
182     public final void reset() {
183         value = null;
184         generated = false;
185     }
186 
187     /***
188      * Returns true if this column should not be included
189      * in INSERT query allowing the database to put the
190      * default value for this column.
191      *
192      * @return defaultDatabase value
193      */
194     public final boolean getDatabaseDefault() {
195         return databaseDefault;
196     }
197 
198     /***
199      * Sets database default parameter
200      *
201      * @param isDatabaseDefault <code>true</code> if DBMonster should
202      *        use database default value.
203      */
204     public final void setDatabaseDefault(final boolean isDatabaseDefault) {
205         databaseDefault = isDatabaseDefault;
206     }
207 
208     /***
209      * Sets the value.
210      *
211      * @param o the value
212      */
213     public final void setValue(final Object o) {
214         value = o;
215     }
216 
217     /***
218      * Returns the value.
219      *
220      * @return the value.
221      */
222     public final Object getValue() {
223         return value;
224     }
225 
226     /***
227      * Sets the table.
228      *
229      * @param t table
230      */
231     public final void setTable(final Table t) {
232         table = t;
233     }
234 
235     /***
236      * Returns a table.
237      *
238      * @return a table.
239      */
240     public final Table getTable() {
241         return table;
242     }
243 
244     /***
245      * @see java.lang.Comparable#compareTo(java.lang.Object)
246      */
247     public final int compareTo(final Object o) {
248         Column s = (Column) o;
249         return name.compareTo(s.name);
250     }
251     /***
252      * @return Returns the targetType.
253      */
254     public int getTargetType() {
255         return targetType;
256     }
257     /***
258      * @param targetType The targetType to set.
259      */
260     public void setTargetType(int targetType) {
261         this.targetType = targetType;
262     }
263 }