package org.apache.airavata.migrator.registry;

import org.apache.airavata.commons.gfac.type.ApplicationDeploymentDescription;
import org.apache.airavata.commons.gfac.type.HostDescription;
import org.apache.airavata.commons.gfac.type.ServiceDescription;
import org.apache.airavata.schemas.gfac.*;
import org.ogce.schemas.gfac.beans.ApplicationBean;
import org.ogce.schemas.gfac.beans.HostBean;
import org.ogce.schemas.gfac.beans.ServiceBean;
import org.ogce.schemas.gfac.beans.utils.ParamObject;

import java.util.ArrayList;
import java.util.List;

public class MigrationUtil {
    /**
     * Creates a HostDescription from HostBean
     *
     * @param hostBean HostBean
     * @return HostDescription
     */
    public static HostDescription createHostDescription(HostBean hostBean) {
        HostDescription host = new HostDescription();
        if(hostBean.getGateKeeperendPointReference()!=null ||
                hostBean.getGridFtpendPointReference()!=null) {
            host.getType().changeType(GlobusHostType.type);
            host.getType().setHostName(hostBean.getHostName());
            host.getType().setHostAddress(hostBean.getHostName());
            ((GlobusHostType) host.getType()).
                    setGridFTPEndPointArray(new String[]{hostBean.getGridFtpendPointReference()});
            ((GlobusHostType) host.getType()).
                    setGlobusGateKeeperEndPointArray(new String[]{hostBean.getGateKeeperendPointReference()});
        } else {
            host.getType().setHostName(hostBean.getHostName());
            host.getType().setHostAddress(hostBean.getHostName());
        }
        return host;
    }

    /**
     * Creates ServiceDescription from ServiceBean
     * @param serviceBean ServiceBean
     * @return ServiceDescription
     */
    public static ServiceDescription createServiceDescription(ServiceBean serviceBean) {
        ServiceDescription serv = new ServiceDescription();
        serv.getType().setName(serviceBean.getServiceName());

        ArrayList<ParamObject> inputParameterTypes = serviceBean.getMethodBean().getInputParms();
        List<InputParameterType> inputList = new ArrayList<InputParameterType>();
        if (inputParameterTypes != null) {
            for (ParamObject inputParameterType : inputParameterTypes) {
                InputParameterType input = InputParameterType.Factory.newInstance();
                String inputParamName = inputParameterType.getName();
                input.setParameterName(inputParamName);

                //TODO properly set the type
                String type = inputParameterType.getType();
                input.setParameterType(StringParameterType.Factory.newInstance());
                inputList.add(input);
            }
            InputParameterType[] inputParamList = inputList.toArray(new InputParameterType[inputList.size()]);
            serv.getType().setInputParametersArray(inputParamList);
        }

        ArrayList<ParamObject> outputParameterTypes = serviceBean.getMethodBean().getOutputParms();
        List<OutputParameterType> outputList = new ArrayList<OutputParameterType>();
        if (outputParameterTypes != null){
            for (ParamObject outputParameterType : outputParameterTypes) {
                String outputParamName = outputParameterType.getName();
                OutputParameterType output = OutputParameterType.Factory.newInstance();
                output.setParameterName(outputParamName);

                //TODO properly set the type
                String en = outputParameterType.getType();
                output.setParameterType(StringParameterType.Factory.newInstance());
                outputList.add(output);

            }
            OutputParameterType[] outputParamList = outputList.toArray(new OutputParameterType[outputList.size()]);
            serv.getType().setOutputParametersArray(outputParamList);
        }

        return serv;
    }

    /**
     * Creates ApplicationDeploymentDescription from ApplicationBean
     * @param appBean ApplicationBean
     * @return ApplicationDeploymentDescription
     */
    public static ApplicationDeploymentDescription createAppDeploymentDescription(ApplicationBean appBean) {
        ApplicationDeploymentDescription appDesc = new ApplicationDeploymentDescription();
        ApplicationDeploymentDescriptionType app = appDesc.getType();
        ApplicationDeploymentDescriptionType.ApplicationName name =
                ApplicationDeploymentDescriptionType.ApplicationName.Factory.newInstance();
        name.setStringValue(appBean.getApplicationName());

        app.setApplicationName(name);
        app.setExecutableLocation(appBean.getExecutable());
        app.setScratchWorkingDirectory(appBean.getTmpDir());
//      TODO : following are not there in the OGCE schema
//        app.setInputDataDirectory("/tmp/input");
//        app.setOutputDataDirectory("/tmp/output");
//        app.setStandardOutput("/tmp/echo.stdout");
//        app.setStandardError("/tmp/echo.stdout");
        return appDesc;

    }
}

@@ -2492,33 +2492,32 @@
 nputParamName);%0A
-%0A
                 
@@ -2520,122 +2520,37 @@
     
-//TODO properly set the type%0A                String type = inputParameterType.getType();%0A                input.set
+input.setParameterType(create
 Para
@@ -2551,38 +2551,37 @@
 teParameterType(
-String
+input
 ParameterType.Fa
@@ -2577,37 +2577,16 @@
 eterType
-.Factory.newInstance(
 ));%0A    
@@ -3344,17 +3344,16 @@
 mName);%0A
-%0A
         
@@ -3363,72 +3363,19 @@
     
- //TODO properly set the type%0A                String en =
  output
+.set
 Para
@@ -3387,46 +3387,15 @@
 Type
-.getType();%0A                output.set
+(create
 Para
@@ -3404,22 +3404,22 @@
 terType(
-String
+output
 Paramete
@@ -3423,37 +3423,16 @@
 eterType
-.Factory.newInstance(
 ));%0A    
@@ -3671,32 +3671,32 @@
 st);%0A        %7D%0A%0A
-
         return s
@@ -3699,32 +3699,537 @@
 rn serv;%0A    %7D%0A%0A
+    private static ParameterType createParameterType(ParamObject outputParameterType) %7B%0A        String en = outputParameterType.getType();%0A%0A        //DataType dt;%0A        if(%22String%22.equalsIgnoreCase(en)) %7B%0A            return StringParameterType.Factory.newInstance();%0A        %7D else if (%22double%22.equalsIgnoreCase(en)) %7B%0A            return DoubleParameterType.Factory.newInstance();%0A        %7D else %7B%0A            // TODO check%0A            return StringParameterType.Factory.newInstance();%0A        %7D%0A    %7D%0A%0A
     /**%0A     * C

Setting parameter type to the service description.