package cz.muni.fi.jboss.migration.actions;

import cz.muni.fi.jboss.migration.ex.MigrationException;
import org.jboss.as.cli.batch.BatchedCommand;
import org.jboss.as.cli.batch.impl.DefaultBatchedCommand;
import org.jboss.dmr.ModelNode;

/**
 * @author Ondrej Zizka, ozizka at redhat.com
 */
public class CliCommandAction extends AbstractStatefulAction {

    //private ModelNode cliCommand;
    //private String script;

    // Better approach
    private BatchedCommand command;

    
    // script parameter is created text script and cliCommand is script representation in CLI API
    public CliCommandAction(String script, ModelNode cliCommand) {
        //this.cliCommand = scriptAPI;
        //this.script = script;
        this.command = new DefaultBatchedCommand(script, cliCommand);
    }


    @Override
    public String toDescription() {
        return "Perform CLI command: " + this.command;
    }
    
    
    


    @Override
    public void preValidate() throws MigrationException {
        if ((this.command.getCommand() == null) || (this.command.getCommand().isEmpty()))
            throw new MigrationException("CLI script for CliCommandAction doesn't exist");
        if (this.command.getRequest() == null) {
            throw new MigrationException("ModelNode for CliCommandAction cannot be null");
        }
    }


    @Override
    public void perform() throws MigrationException {
        getMigrationContext().getBatch().add(this.command);
        setState(State.DONE);
    }


    @Override
    public void rollback() throws MigrationException {
        // Batch provides rollback. Probably empty method.
        setState(State.ROLLED_BACK);

    }


    @Override
    public void postValidate() throws MigrationException {
        // Empty?
    }


    @Override
    public void backup() throws MigrationException {
        // Will be empty. Batch do everything
        setState(State.BACKED_UP);
    }


    @Override
    public void cleanBackup() {
        // Empty?
        setState(State.FINISHED);
    }

}// class

@@ -38,16 +38,70 @@
 tions;%0A%0A
+import cz.muni.fi.jboss.migration.ex.ActionException;%0A
 import c
@@ -1153,37 +1153,34 @@
       throw new 
-Migra
+Ac
 tionException(%22C
@@ -1177,16 +1177,22 @@
 ception(
+this, 
 %22CLI scr
@@ -1304,21 +1304,18 @@
 row new 
-Migra
+Ac
 tionExce
@@ -1320,16 +1320,22 @@
 ception(
+this, 
 %22ModelNo

Improve exceptions: Add action reference to the ActionException, and use it where appropriate.