package org.codehaus.mojo.dbunit;

/*
 * The MIT License
 *
 * Copyright (c) 2006, The Codehaus
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
*/

import java.io.File;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.dbunit.ant.Operation;
import org.dbunit.database.IDatabaseConnection;

/**
 * Execute DbUnit's Database Operation with an external dataset file.
 * 
 * @goal operation
 * @author <a href="mailto:dantran@gmail.com">Dan Tran</a>
 * @author <a href="mailto:topping@codehaus.org">Brian Topping</a>
 * @version $Id$
 * 
 */
public class OperationMojo
    extends AbstractDbUnitMojo
{
    /**
     * Type of Database operation to perform. Supported types are UPDATE, 
     * INSERT, DELETE, DELETE_ALL, REFRESH, CLEAN_INSERT, MSSQL_INSERT, 
     * MSSQL_REFRESH, MSSQL_CLEAN_INSERT
     * 
     * @parameter expression="${type}" 
     * @required
     */
    protected String type;

    /**
     * When true, place the entired operation in one transaction
     * @parameter expression="${transaction}" default-value="false"
     */
    protected boolean transaction;

    /**
     * DataSet file
     * 
     * @parameter expression="${src}"
     * @required
     */
    protected File src;

    /**
     * Dataset file format type. Valid types are: flat, xml, csv, and dtd
     * 
     * @parameter expression="${format}" default-value="xml";
     * @required
     */
    protected String format;

    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        if ( skip )
        {
            this.getLog().info( "Skip operation: " + type + " execution" );
            
            return;
        }

        super.execute();
        
        try
        {
            IDatabaseConnection connection = createConnection();
            try
            {
                Operation op = new Operation();
                op.setFormat( format );
                op.setSrc( src );
                op.setTransaction( transaction );
                op.setType( type );
                op.execute( connection );
            }
            finally
            {
                connection.close();
            }
        }
        catch ( Exception e )
        {
            throw new MojoExecutionException( "Error executing database operation: " + type, e );
        }

    }
}

@@ -1191,16 +1191,174 @@
 o.File;%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0A%0Aimport org.apache.commons.collections.CollectionUtils;
 %0Aimport 
@@ -2358,32 +2358,59 @@
 Set file%0A     * 
+Please use sources instead.
 %0A     * @paramet
@@ -2436,32 +2436,38 @@
 c%7D%22%0A     * @
-required
+deprecated 1.0
 %0A     */%0A   
@@ -2486,16 +2486,108 @@
 ile src;
+%0A    %0A    /**%0A     * DataSet files.%0A     * @parameter %0A     */%0A    protected File%5B%5D sources;
 %0A%0A    /*
@@ -3055,32 +3055,271 @@
 ute();%0A        %0A
+        List concatenatedSources = new ArrayList();%0A        CollectionUtils.addIgnoreNull( concatenatedSources, src );%0A        if ( sources != null ) %7B%0A            concatenatedSources.addAll( Arrays.asList( sources ) );%0A        %7D%0A        %0A
         try%0A    
@@ -3381,32 +3381,45 @@
 teConnection();%0A
+            %0A
             try%0A
@@ -3424,32 +3424,171 @@
 y%0A            %7B%0A
+                for ( Iterator i = concatenatedSources.iterator(); i.hasNext(); ) %7B%0A                    File source = (File) i.next();%0A    
                 
@@ -3631,24 +3631,28 @@
             
+    
 op.setFormat
@@ -3659,24 +3659,28 @@
 ( format );%0A
+    
             
@@ -3695,22 +3695,29 @@
 etSrc( s
+ou
 rc
+e
  );%0A
+    
         
@@ -3750,32 +3750,36 @@
  transaction );%0A
+    
                 
@@ -3814,16 +3814,20 @@
         
+    
 op.execu
@@ -3836,32 +3836,50 @@
 ( connection );%0A
+                %7D%0A
             %7D%0A  
@@ -4114,21 +4114,20 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%7D%0A

allow dbunit maven plugin to pick up data from multiple files