package org.spbu.benchmark.app;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Date;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;

import org.spbu.benchmark.Action;

import org.spbu.benchmark.BenchmarkException;
import org.spbu.benchmark.LoggingSupport;

import com.progress.open4gl.Open4GLException;
import com.progress.open4gl.javaproxy.OpenAppObject;


/**
 * <p>Java class for AppAction complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="AppAction">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="procInvocation" type="{}ProcInvocation"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "AppAction", propOrder = {
    "procInvocation"
})
@XmlSeeAlso({
    PersistentAppAction.class
})
public class AppAction extends LoggingSupport implements Action, Serializable {
	protected OpenAppObject appObject;
	@XmlElement(required = true)
	protected ProcInvocation procInvocation;
	
	protected Map<String, Object> context;
	public AppAction(){
		
	}
	public AppAction(ProcInvocation proc){
		this.procInvocation = proc;
	}
	public AppActionResult invoke() throws BenchmarkException{
		AppActionResult result = new AppActionResult();
		try {
			Date start = new Date();
			try{
			procInvocation.run(appObject);
			}catch(Open4GLException oe){
				if(!oe.getMessage().contains("Object is no longer available for use.")) throw new Exception(oe);
			}
			/*try{
				if(paramArray.getOutputParameter(0) instanceof ResultSet){
					ResultSet rs = (ResultSet)paramArray.getOutputParameter(0);
					ResultSetMetaData m = rs.getMetaData();
					while(rs.next()){
						String str = "";
						for(int i = 0; i <m.getColumnCount();i++) str += rs.getObject(i+1).toString()+"; ";
						log.trace(str);
					}
				
				}
			}catch(Exception e){log.error("error",e);}*/
			long execTime = (new Date()).getTime() - start.getTime();
			result.setSuccessful(true);
			result.setTime(execTime);
		} catch (Exception e) {
			throw new BenchmarkException(
					"Error invoking procedure: " + procInvocation.getProcName(), e);
		}
		return result;
	}

	public OpenAppObject getAppObject() {
		return appObject;
	}
	public void setAppObject(OpenAppObject appObject) {
		this.appObject = appObject;
	}

    /**
     * Gets the value of the procInvocation property.
     * 
     * @return
     *     possible object is
     *     {@link ProcInvocation }
     *     
     */
    public ProcInvocation getProcInvocation() {
        return procInvocation;
    }

    /**
     * Sets the value of the procInvocation property.
     * 
     * @param value
     *     allowed object is
     *     {@link ProcInvocation }
     *     
     */
    public void setProcInvocation(ProcInvocation value) {
        this.procInvocation = value;
    }
	public Map<String, Object> getContext() {
		return context;
	}
	public void setContext(Map<String, Object> context) {
		this.context = context;
		this.getProcInvocation().setContext(context);
	}

}
