001: package com.avaje.util.codegen;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.Iterator;
006: import java.util.logging.Logger;
007: import java.util.regex.Pattern;
008:
009: import com.avaje.lib.log.LogFactory;
010: import com.avaje.lib.sql.DictionaryInfo;
011: import com.avaje.lib.sql.TableInfo;
012:
013: /**
014: *
015: * <pre>
016: * <code>
017: *
018: * ## patterns for database dictionary loading
019: * ebean.codegen.catalog
020: * ebean.codegen.schema
021: * ebean.codegen.table
022: * </code>
023: * </pre>
024: */
025: public class GenerateController {
026:
027: private static final Logger logger = LogFactory
028: .get(GenerateController.class);
029:
030: GenerateConfiguration config;
031:
032: DictionaryInfo dictionaryInfo;
033:
034: BeanWriter beanWriter;
035:
036: TableNaming tableNaming;
037:
038: BuildBean build;
039:
040: SourceCodeMerger sourceMerger;
041:
042: /**
043: * The java source directory.
044: */
045: String sourceDir = "src";
046:
047: /**
048: * Catalog pattern for registering Dictionary.
049: */
050: String catalogPattern;
051:
052: /**
053: * Schema pattern for registering Dictionary.
054: */
055: String schemaPattern;
056:
057: /**
058: * Table pattern for registering Dictionary.
059: */
060: String tablePattern;
061:
062: public GenerateController(GenerateConfiguration config) {
063: this .config = config;
064: this .dictionaryInfo = config.getDictionaryInfo();
065: this .tableNaming = config.getTableNaming();
066:
067: build = new BuildBean(config);
068: beanWriter = new BeanWriter(config);
069: sourceMerger = new SourceCodeMerger(config);
070:
071: sourceDir = config.getProperty("codegen.source.directory",
072: "src");
073:
074: // load the patterns for reading database meta data
075: catalogPattern = config.getProperty("codegen.catalog", null);
076: schemaPattern = config.getProperty("codegen.schema", null);
077: tablePattern = config.getProperty("codegen.table", null);
078: }
079:
080: /**
081: * Register the tables into the Dictionary.
082: */
083: private void dictionaryRegister() throws IOException {
084:
085: // register all the tables that we will generate deployment for
086: String m = "Dictionary registering [" + catalogPattern + "]["
087: + schemaPattern + "][" + tablePattern + "]";
088: logger.info(m);
089:
090: dictionaryInfo.registerTables(catalogPattern, schemaPattern,
091: tablePattern, false);
092: }
093:
094: public void generate(String regex) throws IOException {
095:
096: Pattern p = Pattern.compile("[a-z_]");
097: p.matcher("junk").matches();
098:
099: dictionaryRegister();
100:
101: Pattern pattern = null;
102: if (regex != null) {
103: pattern = Pattern.compile(regex);
104: } else {
105: Pattern p2 = Pattern.compile("[a-z_]");
106: p2.matcher("junk").matches();
107: }
108:
109: GenerateLog log = new GenerateLog();
110:
111: config
112: .println("==================================================");
113: if (config.isWriteMode()) {
114: config.println("Generate:");
115: } else {
116: config.println("Generate: Test Mode (no files written)");
117: }
118: config
119: .println("==================================================");
120:
121: Iterator it = dictionaryInfo.getTableInfoIterator();
122: while (it.hasNext()) {
123: TableInfo tableInfo = (TableInfo) it.next();
124: String tableName = tableInfo.getName().toLowerCase();
125: if (pattern != null
126: && !pattern.matcher(tableName).matches()) {
127:
128: } else {
129: generate(log, tableInfo, null);
130: }
131: }
132:
133: config
134: .println("==================================================");
135: config.println("Summary:");
136: config
137: .println("==================================================");
138:
139: config.println(log.summary(true));
140: config
141: .println("==================================================");
142: }
143:
144: /**
145: *
146: */
147: private boolean generate(GenerateLog log, TableInfo tableInfo,
148: String beanClassName) throws IOException {
149:
150: GenerateInfo info = new GenerateInfo(log);
151: info.setTableInfo(tableInfo);
152:
153: if (tableInfo.hasOnlyPrimaryKey()) {
154: // pure intersection table
155: // no bean generated and use ManyToMany
156: log.addIntersection(info);
157: return false;
158: }
159:
160: String dbTableName = tableInfo.getName();
161:
162: info.setWithAnnotations(config.isWithAnnotations());
163:
164: if (beanClassName == null) {
165: beanClassName = tableNaming.toBeanClassName(dbTableName);
166: }
167:
168: String beanPackage = tableNaming.toPackage(dbTableName);
169:
170: info.setTableInfo(tableInfo);
171: info.setPackageClass(beanPackage, beanClassName);
172:
173: build.build(info);
174:
175: // set the full destination file name
176: setFullFileName(info);
177:
178: // add additional properties, methods and comments from source code
179: sourceMerger.mergeSource(info);
180:
181: GenerateInfo embId = info.getEmbeddedId();
182: if (embId != null) {
183: // generate the EmbeddedId bean
184: setFullFileName(embId);
185: sourceMerger.mergeSource(embId);
186: beanWriter.write(embId);
187: }
188:
189: beanWriter.write(info);
190:
191: return true;
192: }
193:
194: private void setFullFileName(GenerateInfo info) {
195:
196: String beanClassName = info.getClassName();
197: String beanPackage = info.getPackage();
198:
199: String packageDir = sourceDir;
200: if (beanPackage != null) {
201: packageDir += File.separator + beanPackage;
202: }
203: String beanFn = packageDir + File.separator + beanClassName;
204:
205: packageDir = packageDir.replace('.', File.separatorChar);
206: File packageDirFile = new File(packageDir);
207: if (!packageDirFile.exists()) {
208: // create the package directory
209: packageDirFile.mkdirs();
210: }
211:
212: beanFn = beanFn.replace('.', File.separatorChar) + ".java";
213:
214: // the full file name of the source file
215: info.setFullFileName(beanFn);
216: }
217: }
|