KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > type > GeneratorTableType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.type;
30
31 import com.caucho.amber.idgen.AmberTableGenerator;
32 import com.caucho.amber.manager.AmberPersistenceUnit;
33 import com.caucho.amber.table.Column;
34 import com.caucho.amber.table.Table;
35 import com.caucho.config.ConfigException;
36 import com.caucho.util.L10N;
37
38 import java.sql.SQLException JavaDoc;
39 import java.util.HashMap JavaDoc;
40
41 /**
42  * A type which represents a table or a portion.
43  */

44 public class GeneratorTableType extends Type {
45   private static final L10N L = new L10N(GeneratorTableType.class);
46
47   private AmberPersistenceUnit _amberPersistenceUnit;
48
49   private Table _table;
50
51   private String JavaDoc _keyColumn = "GEN_KEY";
52   private String JavaDoc _valueColumn = "GEN_VALUE";
53
54   private HashMap JavaDoc<String JavaDoc,AmberTableGenerator> _genMap =
55     new HashMap JavaDoc<String JavaDoc,AmberTableGenerator>();
56
57   public GeneratorTableType(AmberPersistenceUnit amberPersistenceUnit, String JavaDoc name)
58   {
59     _amberPersistenceUnit = amberPersistenceUnit;
60
61     _table = amberPersistenceUnit.createTable(name);
62   }
63
64   /**
65    * Returns the amber manager.
66    */

67   public AmberPersistenceUnit getAmberManager()
68   {
69     return _amberPersistenceUnit;
70   }
71
72   /**
73    * Returns the name.
74    */

75   public String JavaDoc getName()
76   {
77     return "Generator";
78   }
79
80   /**
81    * Returns the table.
82    */

83   public Table getTable()
84   {
85     return _table;
86   }
87
88   /**
89    * Returns the key name.
90    */

91   public String JavaDoc getKeyColumn()
92   {
93     return _keyColumn;
94   }
95
96   /**
97    * Returns the value name.
98    */

99   public String JavaDoc getValueColumn()
100   {
101     return _valueColumn;
102   }
103   
104   /**
105    * Creates a new generator.
106    */

107   public AmberTableGenerator createGenerator(String JavaDoc name)
108   {
109     synchronized (_genMap) {
110       AmberTableGenerator gen = _genMap.get(name);
111
112       if (gen == null) {
113     gen = new AmberTableGenerator(getAmberManager(), this, name);
114     _genMap.put(name, gen);
115       }
116
117       return gen;
118     }
119   }
120
121   /**
122    * Initialize the table.
123    */

124   public void init()
125     throws ConfigException
126   {
127     Column keyColumn = getTable().createColumn(_keyColumn,
128                            StringType.create());
129     keyColumn.setPrimaryKey(true);
130     keyColumn.setLength(254);
131
132     Column valueColumn = getTable().createColumn(_valueColumn,
133                          LongType.create());
134
135     if (getAmberManager().getCreateDatabaseTables())
136       getTable().createDatabaseTable(getAmberManager());
137
138     for (AmberTableGenerator gen : _genMap.values()) {
139       try {
140     gen.init(getAmberManager());
141       } catch (SQLException JavaDoc e) {
142     throw new ConfigException(e);
143       }
144     }
145   }
146
147   /**
148    * Printable version of the entity.
149    */

150   public String JavaDoc toString()
151   {
152     return "GeneratorTableType[" + getName() + "]";
153   }
154 }
155
Popular Tags