package org.jsoftware.command;

import org.jsoftware.config.AbstractConfigurationParser;
import org.jsoftware.config.ConfigurationEntry;
import org.jsoftware.impl.ConsoleDbManagerPasswordCallback;
import org.jsoftware.impl.DbManager;

import java.io.File;
import java.util.Collection;


/**
 * @author szalik
 */
public abstract class AbstractSingleConfDbPatchCommand extends AbstractCommand {
	private String selectedConfiguration;
	protected ConfigurationEntry configurationEntry;
	protected DbManager manager;
	
	
	public void setSelectedConfiguration(String selectedConfiguration) {
		this.selectedConfiguration = selectedConfiguration;
	}
	
	public void execute() throws CommandExecutionException, CommandFailureException {
		try {
			File cfgFile = getConfigFile();
			ConfigurationEntry mavenConfigurationEntry = getConf();
			if (cfgFile != null && mavenConfigurationEntry != null) {
				throw new CommandExecutionException("Ambiguous configuration. Both \"configFile\" and \"conf\" properties are set!");
			}
			if (mavenConfigurationEntry == null && cfgFile == null) {
				throw new CommandExecutionException("Configuration problem. Set one of \"configFile\" or \"conf\" property!");
			}
			
			if (cfgFile != null) {
				Collection<ConfigurationEntry> conf = AbstractConfigurationParser.discoverConfiguration(getConfigFile());
				if (selectedConfiguration == null) {
					selectedConfiguration = System.getProperty("maven.dbpatch.configuration");
				}
				if (selectedConfiguration == null) {
					throw new CommandFailureException(this, "configuration missing - selectedConfiguration not set", "Please set maven.dbpatch.configuration property.");
				}
				ConfigurationEntry confEntry = null;
				for(ConfigurationEntry ce : conf) {
					if (ce.getId().equals(selectedConfiguration)) {
						confEntry = ce; 
						break;
					}
				}
				if (confEntry == null) {
                    throw new CommandFailureException(this, "configuration missing - no configuration for " + selectedConfiguration, "No configuration for " + selectedConfiguration + ".");
                }
				this.configurationEntry = confEntry;
			} 
			
			if (mavenConfigurationEntry != null) {
				mavenConfigurationEntry.validate();
				configurationEntry = mavenConfigurationEntry;
			}
			
			log.debug("Configuration:\n\t" + configurationEntry.toString().replace("\n", "\n\t").trim());
			// do work
			DbManager manager = new DbManager(configurationEntry);
			manager.init(new ConsoleDbManagerPasswordCallback());
			this.manager = manager;
			log.info("Configuration: " + configurationEntry.getId());
			executeInternal();
			manager.dispose();
		} catch (Exception e) {
			throw new CommandExecutionException(e.getMessage(), e);
		}
		
	}
	
    
	protected abstract void executeInternal() throws Exception;

    public String getSelectedConfiguration() {
        return selectedConfiguration;
    }

    public ConfigurationEntry getConfigurationEntry() {
        return configurationEntry;
    }
}

@@ -226,16 +226,53 @@
 Manager;
+%0Aimport org.jsoftware.log.LogFactory;
 %0A%0Aimport
@@ -759,18 +759,381 @@
 ption %7B%0A
-%09%09
+        String configurationProperty = System.getProperty(%22maven.dbpatch.configuration%22);%0A        if (configurationProperty != null && configurationProperty.trim().length() %3E 0) %7B%0A            LogFactory.getInstance().info(%22Using profile %5C%22%22 + configurationProperty.trim() + %22%5C%22.%22);%0A            selectedConfiguration =configurationProperty.trim();%0A        %7D%0A        
 try %7B%0A%09%09
@@ -1619,24 +1619,24 @@
  != null) %7B%0A
+
 %09%09%09%09Collecti
@@ -1778,135 +1778,8 @@
 ) %7B%0A
-%09%09%09%09%09selectedConfiguration = System.getProperty(%22maven.dbpatch.configuration%22);%0A%09%09%09%09%7D%0A%09%09%09%09if (selectedConfiguration == null) %7B%0A
 %09%09%09%09

Allow to override selected profile always.