/* * Copyright 2004-2015 the Seasar Foundation and the Others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package org.seasar.extension.jdbc.gen.internal.command; import java.io.File; import org.seasar.extension.jdbc.EntityMeta; import org.seasar.extension.jdbc.gen.command.Command; import org.seasar.extension.jdbc.gen.generator.GenerationContext; import org.seasar.extension.jdbc.gen.generator.Generator; import org.seasar.extension.jdbc.gen.internal.exception.RequiredPropertyNullRuntimeException; import org.seasar.extension.jdbc.gen.internal.util.FileUtil; import org.seasar.extension.jdbc.gen.meta.EntityMetaReader; import org.seasar.extension.jdbc.gen.model.ClassModel; import org.seasar.extension.jdbc.gen.model.ConditionModel; import org.seasar.extension.jdbc.gen.model.ConditionModelFactory; import org.seasar.framework.log.Logger; import org.seasar.framework.util.ClassUtil; /** * 条件クラスのJavaファイルを生成する{@link Command}の実装です。 * <p> * このコマンドは、エンティティクラスのメタデータから条件クラスのJavaファイルを生成します。 そのため、 * コマンドを実行するにはエンティティクラスを参照できるようにエンティティクラスが格納されたディレクトリをあらかじめクラスパスに設定しておく必要があります 。 * また、そのディレクトリは、プロパティ{@link #classpathDir}に設定しておく必要があります。 * </p> * <p> * このコマンドは、エンティティクラス1つにつき1つの条件クラスのJavaファイルを生成します。 * </p> * * @author taedium */ public class GenerateConditionCommand extends AbstractCommand { /** ロガー */ protected static Logger logger = Logger .getLogger(GenerateConditionCommand.class); /** クラスパスのディレクトリ */ protected File classpathDir; /** ルートパッケージ名 */ protected String rootPackageName = ""; /** エンティティクラスのパッケージ名 */ protected String entityPackageName = "entity"; /** 対象とするエンティティクラス名の正規表現 */ protected String entityClassNamePattern = ".*"; /** 対象としないエンティティクラス名の正規表現 */ protected String ignoreEntityClassNamePattern = ""; /** 条件クラスのパッケージ名 */ protected String conditionPackageName = "condition"; /** 条件クラス名のサフィックス */ protected String conditionClassNameSuffix = "Condition"; /** 条件クラスのテンプレート名 */ protected String conditionTemplateFileName = "java/condition.ftl"; /** テンプレートファイルのエンコーディング */ protected String templateFileEncoding = "UTF-8"; /** テンプレートファイルを格納したプライマリディレクトリ */ protected File templateFilePrimaryDir = null; /** 生成するJavaファイルの出力先ディレクトリ */ protected File javaFileDestDir = new File(new File("src", "main"), "java"); /** Javaファイルのエンコーディング */ protected String javaFileEncoding = "UTF-8"; /** 上書きをする場合{@code true}、しない場合{@code false} */ protected boolean overwrite = true; /** エンティティメタデータのリーダ */ protected EntityMetaReader entityMetaReader; /** 条件クラスのモデルのファクトリ */ protected ConditionModelFactory conditionModelFactory; /** ジェネレータ */ protected Generator generator; /** * インスタンスを構築します。 */ public GenerateConditionCommand() { } /** * クラスパスのディレクトリを返します。 * * @return クラスパスのディレクトリ */ public File getClasspathDir() { return classpathDir; } /** * クラスパスのディレクトリを設定します。 * * @param classpathDir * クラスパスのディレクトリ */ public void setClasspathDir(File classpathDir) { this.classpathDir = classpathDir; } /** * 条件クラス名のサフィックスを返します。 * * @return 条件クラス名のサフィックス */ public String getConditionClassNameSuffix() { return conditionClassNameSuffix; } /** * 条件クラス名のサフィックスを設定します。 * * @param conditionClassNameSuffix * 条件クラス名のサフィックス */ public void setConditionClassNameSuffix(String conditionClassNameSuffix) { this.conditionClassNameSuffix = conditionClassNameSuffix; } /** * 条件クラスのパッケージ名を返します。 * * @return 条件クラスのパッケージ名 */ public String getConditionPackageName() { return conditionPackageName; } /** * 条件クラスのパッケージ名を設定します。 * * @param conditionPackageName * 条件クラスのパッケージ名 */ public void setConditionPackageName(String conditionPackageName) { this.conditionPackageName = conditionPackageName; } /** * 条件クラスのテンプレート名を返します。 * * @return 条件クラスのテンプレート名 */ public String getConditionTemplateFileName() { return conditionTemplateFileName; } /** * 条件クラスのテンプレート名を設定します。 * * @param conditionTemplateFileName * 条件クラスのテンプレート名 */ public void setConditionTemplateFileName(String conditionTemplateFileName) { this.conditionTemplateFileName = conditionTemplateFileName; } /** * エンティティクラスのパッケージ名を返します。 * * @return エンティティクラスのパッケージ名 */ public String getEntityPackageName() { return entityPackageName; } /** * エンティティクラスのパッケージ名を設定します。 * * @param entityPackageName * エンティティクラスのパッケージ名 */ public void setEntityPackageName(String entityPackageName) { this.entityPackageName = entityPackageName; } /** * 対象とするエンティティクラス名の正規表現を返します。 * * @return 対象とするエンティティクラス名の正規表現 */ public String getEntityClassNamePattern() { return entityClassNamePattern; } /** * 対象とするエンティティクラス名の正規表現を設定します。 * * @param entityClassNamePattern * 対象とするエンティティクラス名の正規表現 */ public void setEntityClassNamePattern(String entityClassNamePattern) { this.entityClassNamePattern = entityClassNamePattern; } /** * 対象としないエンティティクラス名の正規表現を返します。 * * @return 対象としないエンティティクラス名の正規表現 */ public String getIgnoreEntityClassNamePattern() { return ignoreEntityClassNamePattern; } /** * 対象としないエンティティクラス名の正規表現を設定します。 * * @param ignoreEntityClassNamePattern * 対象としないエンティティクラス名の正規表現 */ public void setIgnoreEntityClassNamePattern( String ignoreEntityClassNamePattern) { this.ignoreEntityClassNamePattern = ignoreEntityClassNamePattern; } /** * 生成するJavaファイルの出力先ディレクトリを返します。 * * @return 生成するJavaファイルの出力先ディレクトリ */ public File getJavaFileDestDir() { return javaFileDestDir; } /** * 生成するJavaファイルの出力先ディレクトリを設定します。 * * @param javaFileDestDir * 生成するJavaファイルの出力先ディレクトリ */ public void setJavaFileDestDir(File javaFileDestDir) { this.javaFileDestDir = javaFileDestDir; } /** * Javaファイルのエンコーディングを返します。 * * @return Javaファイルのエンコーディング */ public String getJavaFileEncoding() { return javaFileEncoding; } /** * Javaファイルのエンコーディングを設定します。 * * @param javaFileEncoding * Javaファイルのエンコーディング */ public void setJavaFileEncoding(String javaFileEncoding) { this.javaFileEncoding = javaFileEncoding; } /** * 上書きをする場合{@code true}、しない場合{@code false}を返します。 * * @return 上書きをする場合{@code true}、しない場合{@code false} */ public boolean isOverwrite() { return overwrite; } /** * 上書きをする場合{@code true}、しない場合{@code false}を設定します。 * * @param overwrite * 上書きをする場合{@code true}、しない場合{@code false} */ public void setOverwrite(boolean overwrite) { this.overwrite = overwrite; } /** * ルートパッケージ名を返します。 * * @return ルートパッケージ名 */ public String getRootPackageName() { return rootPackageName; } /** * ルートパッケージ名を設定します。 * * @param rootPackageName * ルートパッケージ名 */ public void setRootPackageName(String rootPackageName) { this.rootPackageName = rootPackageName; } /** * テンプレートファイルのエンコーディングを返します。 * * @return テンプレートファイルのエンコーディング */ public String getTemplateFileEncoding() { return templateFileEncoding; } /** * テンプレートファイルのエンコーディングを設定します。 * * @param templateFileEncoding * テンプレートファイルのエンコーディング */ public void setTemplateFileEncoding(String templateFileEncoding) { this.templateFileEncoding = templateFileEncoding; } /** * テンプレートファイルを格納したプライマリディレクトリを返します。 * * @return テンプレートファイルを格納したプライマリディレクトリ */ public File getTemplateFilePrimaryDir() { return templateFilePrimaryDir; } /** * テンプレートファイルを格納したプライマリディレクトリを設定します。 * * @param templateFilePrimaryDir * テンプレートファイルを格納したプライマリディレクトリ */ public void setTemplateFilePrimaryDir(File templateFilePrimaryDir) { this.templateFilePrimaryDir = templateFilePrimaryDir; } @Override protected void doValidate() { if (classpathDir == null) { throw new RequiredPropertyNullRuntimeException("classpathDir"); } } @Override protected void doInit() { entityMetaReader = createEntityMetaReader(); conditionModelFactory = createConditionModelFactory(); generator = createGenerator(); } @Override protected void doExecute() { for (EntityMeta entityMeta : entityMetaReader.read()) { generateCondition(entityMeta); } } @Override protected void doDestroy() { } /** * 条件クラスのJavaファイルを生成します。 * * @param entityMeta * エンティティメタデータ */ protected void generateCondition(EntityMeta entityMeta) { ConditionModel model = conditionModelFactory .getConditionModel(entityMeta); GenerationContext context = createGenerationContext(model, conditionTemplateFileName); generator.generate(context); } /** * {@link EntityMetaReader}の実装を作成します。 * * @return {@link EntityMetaReader}の実装 */ protected EntityMetaReader createEntityMetaReader() { return factory.createEntityMetaReader(this, classpathDir, ClassUtil .concatName(rootPackageName, entityPackageName), jdbcManager .getEntityMetaFactory(), entityClassNamePattern, ignoreEntityClassNamePattern, false, null, null); } /** * {@link ConditionModelFactory}の実装を作成します。 * * @return {@link ConditionModelFactory}の実装 */ protected ConditionModelFactory createConditionModelFactory() { return factory.createConditionModelFactory(this, ClassUtil.concatName( rootPackageName, conditionPackageName), conditionClassNameSuffix); } /** * {@link Generator}の実装を作成します。 * * @return {@link Generator}の実装 */ protected Generator createGenerator() { return factory.createGenerator(this, templateFileEncoding, templateFilePrimaryDir); } /** * {@link GenerationContext}の実装を作成します。 * * @param model * モデル * @param templateName * テンプレート名 * @return {@link GenerationContext}の実装 */ protected GenerationContext createGenerationContext(ClassModel model, String templateName) { File file = FileUtil.createJavaFile(javaFileDestDir, model .getPackageName(), model.getShortClassName()); return factory.createGenerationContext(this, model, file, templateName, javaFileEncoding, overwrite); } @Override protected Logger getLogger() { return logger; } }