package org.wontovlab.tool;

/* 
 * This Class have methods that will be available when this project is exported as a WAR file to be 
 * used as a web service
 */

import java.util.List;

import org.wontovlab.model.Experiment;
import org.wontovlab.model.Material;
import org.wontovlab.model.Task;
import org.wontovlab.model.TO.ExperimentTO;
import org.wontovlab.model.TO.MaterialTO;
import org.wontovlab.model.TO.TaskTO;

public class Wpfws {

	private OntologyMethodsBean ontologyMethodsCoreBean;
	private XPDLParserBean xpdlParserBean; 
	


	
	
	
////////////////////////////////////////////////////////////////////////////////	

	/*
	 * Return all materials from Domain Ontology 
	 */
	public MaterialTO[] doGetAllMaterials(boolean isOWLFileSavedLocally, String doOwlPath, String doDocURI, String aoOwlPath, String aoDocURI) {
		ontologyMethodsCoreBean = OntologyMethodsBean.getInstance(isOWLFileSavedLocally, doOwlPath, doDocURI, aoOwlPath, aoDocURI);

		List<Material> lm = ontologyMethodsCoreBean.doGetAllMaterials();
		
		MaterialTO[] materialTO = new MaterialTO[lm.size()];
		
		int i = 0;
    	for (Material material: lm) 
    	{
			materialTO[i] = new MaterialTO(material);
			i++;
		}
    	
		return materialTO;
	}

	/*
	 * This method returns a Material object from a domain ontology URI
	 */
	public MaterialTO doGetMaterial(String domainOntologyMaterialReference, boolean isOWLFileSavedLocally, String doOwlPath, String doDocURI, String aoOwlPath, String aoDocURI) {
		
		ontologyMethodsCoreBean = OntologyMethodsBean.getInstance(isOWLFileSavedLocally, doOwlPath, doDocURI, aoOwlPath, aoDocURI);
		
		Material material = ontologyMethodsCoreBean.doGetMaterial(domainOntologyMaterialReference);
		
		MaterialTO materialTO = new MaterialTO(material);
		
		return materialTO;
	}

	/*
	 * @ param material Material object which the user wants to get similar materials
	 * This method returns a Material object list which have materials similar to the material passed as parameter.    
	 */
	public MaterialTO[] doGetInstancesSimilarTo(MaterialTO materialTO, boolean isOWLFileSavedLocally, String doOwlPath, String doDocURI, String aoOwlPath, String aoDocURI) {
		
		ontologyMethodsCoreBean = OntologyMethodsBean.getInstance(isOWLFileSavedLocally, doOwlPath, doDocURI, aoOwlPath, aoDocURI);
		
		Material material = new Material(materialTO);
		
		List<Material> materials = ontologyMethodsCoreBean.doGetInstancesSimilarTo(material);
		
		if (materials == null)
			return null;
		
		MaterialTO[] materialTOList = new MaterialTO[materials.size()];
		
		for (int i=0; i < materials.size(); i++) {
			materialTOList[i] = new MaterialTO(materials.get(i));
		}
		
		return materialTOList; 
	}

////////////////////////////////////////////////////////////////////////////////	
	
	/*
	 * Return all tasks from Application Ontology 
	 */
	public TaskTO[] aoGetAllTasks(boolean isOWLFileSavedLocally, String doOwlPath, String doDocURI, String aoOwlPath, String aoDocURI) {
		
		ontologyMethodsCoreBean = OntologyMethodsBean.getInstance(isOWLFileSavedLocally, doOwlPath, doDocURI, aoOwlPath, aoDocURI);
		
		List<Task> task = ontologyMethodsCoreBean.aoGetAllTasks();
		
		TaskTO[] taskTO = new TaskTO[task.size()];
		
		for (int i=0; i < task.size(); i++) {
			taskTO[i] = new TaskTO(task.get(i));
		}
		
		return taskTO;
	}
	
	/*
	 * @param task The application ontology task. It will be used to get the task's application ontology URI 
	 * This method returns a Material object list of all materials used in the task passed as parameter 
	 */
	protected MaterialTO[] aoGetTaskMaterials(String applicationOntologyTaskURI, boolean isOWLFileSavedLocally, String doOwlPath, String doDocURI, String aoOwlPath, String aoDocURI) {
		
		ontologyMethodsCoreBean = OntologyMethodsBean.getInstance(isOWLFileSavedLocally, doOwlPath, doDocURI, aoOwlPath, aoDocURI);
		
		List<Material> materials = ontologyMethodsCoreBean.aoGetTaskMaterials(applicationOntologyTaskURI);
		
		MaterialTO[] materialTO = new MaterialTO[materials.size()];
		
		for (int i=0; i < materials.size(); i++) {
			materialTO[i] = new MaterialTO(materials.get(i));
		}
		
		return materialTO;
	}
	
	public TaskTO aoGetTask(String ontoInstanceReference, boolean isOWLFileSavedLocally, String doOwlPath, String doDocURI, String aoOwlPath, String aoDocURI) {
		
		ontologyMethodsCoreBean = OntologyMethodsBean.getInstance(isOWLFileSavedLocally, doOwlPath, doDocURI, aoOwlPath, aoDocURI);
		
		Task task = ontologyMethodsCoreBean.aoGetTask(ontoInstanceReference);
		
		TaskTO taskTO = new TaskTO(task);
		
		return taskTO;
	}
	
	/*
	 * @param applicationOntologyTaskURI The application ontology step URI
	 * This method get the param applicationOntologyTaskURI and verify over the application ontology if there are alternative
	 * steps for this step. If there is, it will return a list of Step.
	 */
	protected TaskTO[] aoGetTasksSimilarTo(String applicationOntologyTaskReference, boolean isOWLFileSavedLocally, String doOwlPath, String doDocURI, String aoOwlPath, String aoDocURI) {
		
		ontologyMethodsCoreBean = OntologyMethodsBean.getInstance(isOWLFileSavedLocally, doOwlPath, doDocURI, aoOwlPath, aoDocURI);
		
		List<Task> task = ontologyMethodsCoreBean.aoGetTasksSimilarTo(applicationOntologyTaskReference);
		
		TaskTO[] taskTO = new TaskTO[task.size()];
		
		for (int i=0; i < task.size(); i++) {
			taskTO[i] = new TaskTO(task.get(i));
		}
		
		return taskTO;
	}
	
	
	

	
	/*
	 * Read the authorship XPDL workflow from a file. Before use any method to handle the workflow, it is necessary to read it through this method.
	 */
	public ExperimentTO readAuthorshipWorkflow(String xpdlPath) throws Exception {
		
		return (new ExperimentTO(xpdlParserBean.readAuthorshipXPDL(xpdlPath)));
	}
	
	/*
	 * Read the execution XPDL workflow from a file. Before use any method to handle the workflow, it is necessary to read it through this method.
	 */
	public ExperimentTO readExecutionWorkflow(String xpdlPath) throws Exception {
		
		return (new ExperimentTO(xpdlParserBean.readExecutionXPDL(xpdlPath)));
		
	}
	
	
	




	/*
	 * Comparing the authorship and execution workflows 
	 */
	public String  comparingWorkflows(String authorshipXpdlPath, String executionXpdlPath) throws Exception{
		xpdlParserBean = new XPDLParserBean();
		
		xpdlParserBean.readAuthorshipXPDL(authorshipXpdlPath);
		
		xpdlParserBean.readExecutionXPDL(executionXpdlPath);
		
		return xpdlParserBean.comparingWorkflows();
	}

	
	/*
	 * It creates the XPDL authorship workflow string and save it on the hard disk
	 * @param experiment The experiment object which will be read and written in a XPDL file 
	 * @param xpdlFilePath Path where the XPDL file will be created (on the file system)
	 */
	public Boolean creatingAuthorshipWorkflow(ExperimentTO experimentTO, String xpdlFilePath) {

		xpdlParserBean = new XPDLParserBean();
		
		Experiment experiment = new Experiment(experimentTO);
		
		xpdlParserBean.creatingAuthorshipWorkflow(experiment, xpdlFilePath);
		
		return true;
		
		
	}
	
	/*
	 * It creates the XPDL execution workflow string and save it on the hard disk
	 * @param experiment The experiment object which will be read and written in a XPDL file 
	 * @param xpdlFilePath Path where the XPDL file will be created (on the file system)
	 */
	public Boolean creatingExecutionWorkflow(ExperimentTO experimentTO, String xpdlFilePath) {
		
		xpdlParserBean = new XPDLParserBean();
		
		Experiment experiment = new Experiment(experimentTO);
		
		xpdlParserBean.creatingExecutionWorkflow(experiment, xpdlFilePath);
		
		return true;
		
	}
	
	



}
