Package net.raymanoz.migrate

Source Code of net.raymanoz.migrate.UMigrate

package net.raymanoz.migrate;

import java.io.IOException;

import net.raymanoz.command.CheckConfigurationsCommand;
import net.raymanoz.command.Command;
import net.raymanoz.command.CommandAssemblerImpl;
import net.raymanoz.command.CommandList;
import net.raymanoz.command.CommandListImpl;
import net.raymanoz.command.CommandNotFoundException;
import net.raymanoz.command.ConditionHelp;
import net.raymanoz.command.GenerateDropScriptCommand;
import net.raymanoz.command.ListCommand;
import net.raymanoz.command.MigrateCommand;
import net.raymanoz.command.NewScriptCommand;
import net.raymanoz.command.NewScriptFromCommand;
import net.raymanoz.command.UsageCommand;
import net.raymanoz.config.ApplicationProperties;
import net.raymanoz.config.ApplicationPropertiesImpl;
import net.raymanoz.config.ConditionHandlerListAssembler;
import net.raymanoz.config.ConfigurationImpl;
import net.raymanoz.util.CmdLineArgumentsAndSwitches;
import net.raymanoz.util.CmdLineArgumentsAndSwitchesImpl;
import net.raymanoz.util.FileUtilImpl;
import net.raymanoz.util.FileUtil;
import net.raymanoz.util.Properties;
import net.raymanoz.util.StreamUtilImpl;
import net.raymanoz.util.PlainProperties;

import static net.raymanoz.command.MigrateCommand.OVERRIDE_PROP_SWITCH_NAMES;
import static net.raymanoz.command.UsageCommand.USAGE_COMMAND_STRING;

public class UMigrate {
 
  private CommandList commands;

  public UMigrate(CommandList commands) {
    this.commands = commands;
  }
 
  public static void main(String[] args) throws IOException {
    UMigrate migrate = new UMigrate(new CommandListImpl());
    migrate.execute(args);
  }

  final private static String[] SWITCHES_WITH_PARAMS = OVERRIDE_PROP_SWITCH_NAMES;
  public void execute(String[] args) {
    CmdLineArgumentsAndSwitches cmdln = new CmdLineArgumentsAndSwitchesImpl(args, SWITCHES_WITH_PARAMS);
    registerCommands(cmdln);
    if(cmdln.argCount() == 0) {
      showUsage(cmdln);
    } else {
      executeCommand(cmdln);
    }
  }

  private void executeCommand(CmdLineArgumentsAndSwitches cmdln) {
    Command command;
    try {
      command = commands.get(cmdln.arg(0));
      command.execute(cmdln.arguments());
    } catch (ProblemInScriptListNumbers pe){
      System.out.println("************************************");
      System.out.println("");
      System.out.println(pe.getMessage());
      System.out.println("");
      System.out.println("************************************");
    } catch (CommandNotFoundException e) {
      System.out.println("Error Unknown Command: " + cmdln.arg(0));
      System.out.println("");
      showUsage(cmdln);
    }
  }

  private void showUsage(CmdLineArgumentsAndSwitches cmdln) {
    try {
      commands.get(USAGE_COMMAND_STRING).execute();
    } catch (CommandNotFoundException e) {
      new UsageCommand(commands).execute();
    }
  }
 
  private PlainProperties uMigrateProperties(CmdLineArgumentsAndSwitches cmdln){
    if (cmdln.hasSwitch(OVERRIDE_PROP_SWITCH_NAMES)) {
      return PlainProperties.createFromResource(cmdln.switchValue(OVERRIDE_PROP_SWITCH_NAMES)"uMigrate.properties");
    }
    return PlainProperties.createFromResource("uMigrate.properties");
  }

  private void registerCommands(CmdLineArgumentsAndSwitches cmdln) {

    Properties uMigrateProperties = uMigrateProperties(cmdln);
    ApplicationProperties applicationProperties = new ApplicationPropertiesImpl(uMigrateProperties);
    FileUtil fileUtil = new FileUtilImpl(new StreamUtilImpl());
    ConfigurationImpl configuration = new ConfigurationImpl(applicationProperties, fileUtil, uMigrateProperties);
    ConditionHandlerListAssembler.setupConditions(configuration);
    CommandAssemblerImpl commandAssembler = new CommandAssemblerImpl(configuration);
    PlainProperties properties = new PlainProperties();
    ScriptCreator creator = new ScriptCreatorImpl(new ScriptCreatorAssemblerImpl(configuration), configuration, properties);
   
    commands.register(new NewScriptCommand(properties,creator));
    commands.register(new NewScriptFromCommand(properties,creator,fileUtil, configuration));
    commands.register(new GenerateDropScriptCommand(configuration, creator, properties));
    commands.register(new MigrateCommand(new SchemaVersionRepositoryImpl(configuration), configuration, commandAssembler));
    commands.register(new ListCommand(commandAssembler));
    commands.register(new UsageCommand(commands));
    commands.register(new ConditionHelp());
    commands.register(new CheckConfigurationsCommand(configuration));
}

}
TOP

Related Classes of net.raymanoz.migrate.UMigrate

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.