1 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 ; 39 import java.util.HashMap ; 40 41 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 _keyColumn = "GEN_KEY"; 52 private String _valueColumn = "GEN_VALUE"; 53 54 private HashMap <String ,AmberTableGenerator> _genMap = 55 new HashMap <String ,AmberTableGenerator>(); 56 57 public GeneratorTableType(AmberPersistenceUnit amberPersistenceUnit, String name) 58 { 59 _amberPersistenceUnit = amberPersistenceUnit; 60 61 _table = amberPersistenceUnit.createTable(name); 62 } 63 64 67 public AmberPersistenceUnit getAmberManager() 68 { 69 return _amberPersistenceUnit; 70 } 71 72 75 public String getName() 76 { 77 return "Generator"; 78 } 79 80 83 public Table getTable() 84 { 85 return _table; 86 } 87 88 91 public String getKeyColumn() 92 { 93 return _keyColumn; 94 } 95 96 99 public String getValueColumn() 100 { 101 return _valueColumn; 102 } 103 104 107 public AmberTableGenerator createGenerator(String 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 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 e) { 142 throw new ConfigException(e); 143 } 144 } 145 } 146 147 150 public String toString() 151 { 152 return "GeneratorTableType[" + getName() + "]"; 153 } 154 } 155 | Popular Tags |