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.util.List;
50  
51  import org.apache.commons.logging.Log;
52  
53  import pl.kernelpanic.dbmonster.DBMonster;
54  import pl.kernelpanic.dbmonster.DBMonsterContext;
55  import pl.kernelpanic.dbmonster.generator.Initializable;
56  import pl.kernelpanic.dbmonster.generator.KeyGenerator;
57  
58  /***
59   * The primary key.
60   *
61   * @author Piotr Maj <piotr.maj@kernelpanic.pl>
62   *
63   * @version $Id: Key.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
64   */
65  public class Key {
66  
67      /***
68       * The table.
69       */
70      private Table table = null;
71  
72      /***
73       * The key generator.
74       */
75      private KeyGenerator generator = null;
76  
77      /***
78       * DBMonsterContext.
79       */
80      private DBMonsterContext context = null;
81  
82      /***
83       * Use the database default value. This results in not
84       * including this column in the generation process.
85       */
86      private boolean databaseDefault = false;
87  
88      /***
89       * Returns a generator.
90       *
91       * @return generator.
92       */
93      public final KeyGenerator getGenerator() {
94          return generator;
95      }
96  
97      /***
98       * Sets the generator.
99       *
100      * @param gen generator
101      */
102     public final void setGenerator(final KeyGenerator gen) {
103         generator = gen;
104         generator.setKey(this);
105     }
106 
107     /***
108      * Initializes the key.
109      *
110      * @param ctx dbmonster context.
111      *
112      * @throws Exception if initialization fails
113      */
114     public final void initialize(final DBMonsterContext ctx) throws Exception {
115         context = ctx;
116 
117         Log log = (Log) context.getProperty(DBMonster.LOGGER_KEY);
118         if (log.isDebugEnabled()) {
119             log.debug(
120                 "Initializing key for table <" + table.getName() + ">.");
121         }
122 
123         if (generator instanceof Initializable) {
124             ((Initializable) generator).initialize(ctx);
125         }
126 
127         if (log.isDebugEnabled()) {
128             log.debug("Initialization of key for table <"
129                 + table.getName() + "> finished.");
130         }
131     }
132 
133     /***
134      * Generates the key.
135      *
136      * @return List a list of columns.
137      *
138      * @throws Exception if generation fails
139      */
140     public final List generate() throws Exception {
141         return generator.generate();
142     }
143 
144     /***
145      * Sets the table.
146      *
147      * @param t table
148      */
149     public final void setTable(final Table t) {
150         table = t;
151     }
152 
153     /***
154      * Returns a table.
155      *
156      * @return a table.
157      */
158     public final Table getTable() {
159         return table;
160     }
161 
162     /***
163      * Returns true if this key should not be included
164      * in INSERT query allowing the database to put the
165      * default value for this column(s).
166      *
167      * @return defaultDatabase value
168      */
169     public final boolean getDatabaseDefault() {
170         return databaseDefault;
171     }
172 
173     /***
174      * Sets database default parameter
175      *
176      * @param isDatabaseDefault <code>true</code> if DBMonster should
177      *        use database default value.
178      */
179     public final void setDatabaseDefault(final boolean isDatabaseDefault) {
180         databaseDefault = isDatabaseDefault;
181     }
182 }