/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.applet.components;

import org.bodington.applet.components.*;

import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;


public class TransferFormDataDialog extends org.bodington.applet.components.TransferDataDialog
implements Runnable
{
    String[] names, values;
    URL url;
    Thread worker=null;
    boolean success;
    
    public TransferFormDataDialog( JDesktopPane desktop )
    {
	super( desktop, "Submitting request", "Preparing to Transfer Request", true );
    }
    
    public void transfer( String[] names, String[] values, URL url )
    throws IOException
    {
	success = false;
	
	if ( worker!=null )
	    throw new IOException( "Transfer already in progress." );
	
	this.names = names;
	this.values = values;
	this.url = url;
	
	worker = new Thread( this );
	worker.start();
	
    }
    
    public void run()
    {
	try
	{
	    startPost( url );
	    
	    for ( int i=0; i<names.length; i++ )
		this.postField( names[i], values[i] );
	    
	    InputStreamReader inr = new InputStreamReader( getResponse() );
	    BufferedReader reader = new BufferedReader( inr );
	    
	    String confirm = reader.readLine();
	    
	    System.err.println( "Confirmation: " + confirm );
	    
	    String line = null;
	    StringBuffer message = null;
	    if ( !confirm.equalsIgnoreCase( "OK" ) )
	    {
		message = new StringBuffer();
		line = reader.readLine();
		message.append( line );
		/*
		while ( (line = reader.readLine()) != null )
		    {
		    message.append( line );
		    message.append( "\n" );
		    }
		 */
	    }
	    
	    
	    if ( message != null )
	    {
		setErrorMessage( message.toString() );
		setMessage( "Server error message." );
	    }
	    else
	    {
		setMessage( "Transfer/command complete" );
		setProgress( 100 );
		success = true;
	    }
	    
	    inr.close();
	    if ( listener != null )
		listener.progressCompleted( this, false );
	}
	catch ( Exception ex )
	{
	    setMessage( "Transfer/command failed." );
	    setErrorMessage( ex.getMessage() );
	    ex.printStackTrace();
	    if ( listener != null )
		listener.progressCompleted( this, true );
	}
	finally
	{
	    worker = null;
	    setBusy( false );
	    panel.setCloseEnabled( true );
	    panel.setCancelEnabled( false );
	}
    }
    
}
