Package net.raymanoz.migrate

Source Code of net.raymanoz.migrate.ScriptCreatorImpl

package net.raymanoz.migrate;

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.raymanoz.config.Configuration;
import net.raymanoz.config.ParameteriserImpl;
import net.raymanoz.io.File;
import net.raymanoz.util.Properties;
import net.raymanoz.util.StreamUtil;
import net.raymanoz.util.StreamUtilImpl;

public class ScriptCreatorImpl implements ScriptCreator {
  public static String UMIGRATE_SCRIPT_NAME_PROPERTY = "uMigrate.scriptName";
 
  final private ScriptCreatorAssembler assembler;
  final private Configuration configuration;
  final private Properties properties;
  final private StreamUtil streamUtil;

  public ScriptCreatorImpl(ScriptCreatorAssembler assembler, Configuration configuration, Properties properties) {
    this(assembler, configuration, properties, new StreamUtilImpl())
  }
 
  public ScriptCreatorImpl(ScriptCreatorAssembler assembler, Configuration configuration, Properties properties, StreamUtil streamUtil) {
    this.assembler = assembler;
    this.configuration = configuration;
    this.properties = properties;
    this.streamUtil = streamUtil;
  }

 
  private  ScriptList lastestScriptDirScripts(){
    File latestScriptDir = configuration.getLatestScriptDirectory();
    File[] files = latestScriptDir.listFiles();   
    ScriptList scripts = assembler.newScriptList(files, configuration.getNumberOfDigits(), latestScriptDir.getDBVersion());
    return scripts;
  }
 
  public void validateLatestScriptDir(){
    // will throw a ProblemInScriptListNumbers if invalid;
    lastestScriptDirScripts();
  }
 
 
  public void newScript(String scriptAction, String prefixConditions){
    ScriptList scripts = lastestScriptDirScripts();
    String nextScriptName = scripts.nextScriptName(scriptAction);
    File nextScript = assembler.newFile(configuration.getLatestScriptDirectory(), nextScriptName);
    if(configuration.getTemplateFile().exists(false)) {
      properties.setProperty(UMIGRATE_SCRIPT_NAME_PROPERTY, nextScriptName);
      ParameteriserImpl.transferParameteriserProperties(configuration.uMigrateProperties(), properties);
      assembler.newFileUtil().copyFile(configuration.getTemplateFile(), nextScript, properties, prefixConditions);
    } else {
      try {
        nextScript.createNewFile();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
    System.out.println("***************");
    System.out.println("");
    System.out.println("Created Script: " + nextScript.getCanonicalPath());
    System.out.println("");
  }

  public void newScript(String scriptAction) {
    newScript(scriptAction, "");
  }

  public static final String SQL_PROPERTY_NAME = "sql";
  private static final Pattern SQL_PROPERTY_PATTERN = Pattern.compile("\\$\\{"+ SQL_PROPERTY_NAME +"\\}");
  @Override
  public boolean validateTemplate() {
    File template = configuration.getTemplateFile();
    if (!template.exists(false)){
      System.out.println("***************");
      System.out.println("");
      System.out.println("No Template File found at: " + template.getCanonicalPath());
      System.out.println("");
      return false;
    }
    InputStream stream = streamUtil.newFileInputStream(template);
    String templateFile = assembler.newFileUtil().loadStreamIntoString(stream);
    Matcher matcher = SQL_PROPERTY_PATTERN.matcher(templateFile);
    if (!matcher.find()){
      System.out.println("***************");
      System.out.println("");
      System.out.println("Required ${" + SQL_PROPERTY_NAME +"} not found in template: " + template.getAbsolutePath());
      System.out.println("");
      return false;
    }
    return true;
  }

}
TOP

Related Classes of net.raymanoz.migrate.ScriptCreatorImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.