001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.type;
030:
031: import com.caucho.amber.idgen.AmberTableGenerator;
032: import com.caucho.amber.manager.AmberPersistenceUnit;
033: import com.caucho.amber.table.Column;
034: import com.caucho.amber.table.Table;
035: import com.caucho.config.ConfigException;
036: import com.caucho.util.L10N;
037:
038: import java.sql.SQLException;
039: import java.util.HashMap;
040:
041: /**
042: * A type which represents a table or a portion.
043: */
044: public class GeneratorTableType extends Type {
045: private static final L10N L = new L10N(GeneratorTableType.class);
046:
047: private AmberPersistenceUnit _amberPersistenceUnit;
048:
049: private Table _table;
050:
051: private String _keyColumn = "GEN_KEY";
052: private String _valueColumn = "GEN_VALUE";
053:
054: private HashMap<String, AmberTableGenerator> _genMap = new HashMap<String, AmberTableGenerator>();
055:
056: public GeneratorTableType(
057: AmberPersistenceUnit amberPersistenceUnit, String name) {
058: _amberPersistenceUnit = amberPersistenceUnit;
059:
060: _table = amberPersistenceUnit.createTable(name);
061: }
062:
063: /**
064: * Returns the amber manager.
065: */
066: public AmberPersistenceUnit getAmberManager() {
067: return _amberPersistenceUnit;
068: }
069:
070: /**
071: * Returns the name.
072: */
073: public String getName() {
074: return "Generator";
075: }
076:
077: /**
078: * Returns the table.
079: */
080: public Table getTable() {
081: return _table;
082: }
083:
084: /**
085: * Returns the key name.
086: */
087: public String getKeyColumn() {
088: return _keyColumn;
089: }
090:
091: /**
092: * Returns the value name.
093: */
094: public String getValueColumn() {
095: return _valueColumn;
096: }
097:
098: /**
099: * Creates a new generator.
100: */
101: public AmberTableGenerator createGenerator(String name) {
102: synchronized (_genMap) {
103: AmberTableGenerator gen = _genMap.get(name);
104:
105: if (gen == null) {
106: gen = new AmberTableGenerator(getAmberManager(), this ,
107: name);
108: _genMap.put(name, gen);
109: }
110:
111: return gen;
112: }
113: }
114:
115: /**
116: * Initialize the table.
117: */
118: public void init() throws ConfigException {
119: Column keyColumn = getTable().createColumn(_keyColumn,
120: StringType.create());
121: keyColumn.setPrimaryKey(true);
122: keyColumn.setLength(254);
123:
124: Column valueColumn = getTable().createColumn(_valueColumn,
125: LongType.create());
126:
127: if (getAmberManager().getCreateDatabaseTables())
128: getTable().createDatabaseTable(getAmberManager());
129:
130: for (AmberTableGenerator gen : _genMap.values()) {
131: try {
132: gen.init(getAmberManager());
133: } catch (SQLException e) {
134: throw ConfigException.create(e);
135: }
136: }
137: }
138:
139: /**
140: * Printable version of the entity.
141: */
142: public String toString() {
143: return "GeneratorTableType[" + getName() + "]";
144: }
145: }
|