package org.sakaiproject.wontovlab.tool.jsf;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.faces.component.UIData;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sakaiproject.wontovlab.logic.ExperimentLogic;
import org.sakaiproject.wontovlab.logic.ExternalLogic;
import org.sakaiproject.wontovlab.logic.MaterialLogic;
import org.sakaiproject.wontovlab.model.Drug;
import org.sakaiproject.wontovlab.model.Equipment;
import org.sakaiproject.wontovlab.model.Material;
import org.sakaiproject.wontovlab.model.Solution;
import org.sakaiproject.wontovlab.model.StepMaterial;
import org.wontovlab.model.Property;
import org.wontovlab.model.Task;

/**
 * This is a backing bean for the JSF app which handles the events and
 * sends the information from the logic layer to the UI
 * @author Sakai App Builder -AZ
 */
public class StepBean 
{

	private static Log log = LogFactory.getLog(StepBean.class);
	
	public Locale locale = new Locale("pt", "BR");
	private ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
	
	private ExternalLogic externalLogic;

	private static final String STEPMATERIAL_DEFAULT_DESCRIPTION = "";
	private static final int STEPMATERIAL_DEFAULT_IDSTEP = 0;
	private static final Float STEPMATERIAL_DEFAULT_QUANTITY = Float.parseFloat("0");

	private List<Property> selectedMaterialProperties = null;
	private HtmlDataTable currentMaterialPropertiesDataTableBind;
	
	private UIData dataTable;
	private UIData dataTableInsert;

	private List<org.wontovlab.model.Material> stepMaterialList = null;
	
	private StepWrapper currentStep;

	private int firstScoreRow;

	private ExperimentLogic experimentLogic;
	private MaterialLogic materialLogic;

	private ExperimentBean experimentBean;
	private MaterialBean materialBean;
	
	private String stepMaterialDescription = STEPMATERIAL_DEFAULT_DESCRIPTION;
	private int stepMaterialIdStep = STEPMATERIAL_DEFAULT_IDSTEP;

	
	private List<SelectItem> somEquipmentList = new ArrayList<SelectItem>(); //Select one menu
	private Long selectedEquipment = new Long(-1);
	
	private List<SelectItem> somDrugList = new ArrayList<SelectItem>(); //Select one menu
	private Long selectedDrug =  new Long(-1);
	
	private List<SelectItem> somSolutionList = new ArrayList<SelectItem>(); //Select one menu
	private Long selectedSolution =  new Long(-1);
	
	private List<SelectItem> domainOntologyMaterialList = new ArrayList<SelectItem>();

	private List<Drug> listDrugs;
	private List<Equipment> listEquipments;
	private List<Solution> listSolutions;
	

	List<StepMaterial> wrappedEquipmentsRunningExperiment;
	List<StepMaterial> wrappedDrugsRunningExperiment;
	List<StepMaterial> wrappedSolutionsRunningExperiment;
	
	private HtmlDataTable bindingDTRunExperimentStepMaterials;
	



	private String imageName = null;

	
	

	public void setDomainOntologySolutionList(
			List<SelectItem> domainOntologyMaterialList) {
		this.domainOntologyMaterialList = domainOntologyMaterialList;
	}

	///////////////////////////// Running Experiment 
	private List<StepMaterial> dtRunningExperimentSelectedMaterials;
	
	private List<RunningExperimentMaterialWrapper> dtRunningExperimentEquipments;
	private List<RunningExperimentMaterialWrapper> dtRunningExperimentDrugs;
	private List<RunningExperimentMaterialWrapper> dtRunningExperimentSolutions;
	
	private UIData bindingDTRunnigExperimentEquipments;
	private UIData bindingDTRunnigExperimentDrugs;
	private UIData bindingDTRunnigExperimentSolutions;
	private String workflowComparisonResult = null;
	///////////////////////////// Running Experiment
	
	
	
	
	
	
	///////////////////////////// Experiment Authorship
	private String selectedResourceKindRadio = null;
	
	private List<org.wontovlab.model.Material> similarMaterials = null;
	private List<SelectItem> somApplicationOntologyTaskList = null; //Select one menu
	private String selectedApplicationOntology = new String("null");
	private org.wontovlab.model.Material currentSelectedMaterial = null;
	private int stepNumber = 1;
	private org.wontovlab.model.Experiment experiment = null; 
	private List<org.wontovlab.model.Step> experimentStepList = null;
	/////////////////////////////

	

	
	public String getWorkflowComparisonResult() {
		return workflowComparisonResult;
	}
	


	public List<StepMaterial> getDtRunningExperimentSelectedMaterials() {
		return dtRunningExperimentSelectedMaterials;
	}
	public HtmlDataTable getBindingDTRunExperimentStepMaterials() {
		return bindingDTRunExperimentStepMaterials;
	}
	
	public void setBindingDTRunExperimentStepMaterials(
			HtmlDataTable bindingDTRunExperimentStepMaterials) {
		this.bindingDTRunExperimentStepMaterials = bindingDTRunExperimentStepMaterials;
	}
	public UIData getBindingDTRunnigExperimentEquipments() {
		return bindingDTRunnigExperimentEquipments;
	}
	
	public void setBindingDTRunnigExperimentEquipments(
			UIData bindingDTRunnigExperimentEquipments) {
		this.bindingDTRunnigExperimentEquipments = bindingDTRunnigExperimentEquipments;
	}
	public UIData getBindingDTRunnigExperimentDrugs() {
		return bindingDTRunnigExperimentDrugs;
	}
	
	public void setBindingDTRunnigExperimentDrugs(
			UIData bindingDTRunnigExperimentDrugs) {
		this.bindingDTRunnigExperimentDrugs = bindingDTRunnigExperimentDrugs;
	}
	public UIData getBindingDTRunnigExperimentSolutions() {
		return bindingDTRunnigExperimentSolutions;
	}
	
	public void setBindingDTRunnigExperimentSolutions(
			UIData bindingDTRunnigExperimentSolutions) {
		this.bindingDTRunnigExperimentSolutions = bindingDTRunnigExperimentSolutions;
	}
	public void setDtRunningExperimentSelectedMaterials(
			List<StepMaterial> dtRunningExperimentSelectedMaterials) {
		this.dtRunningExperimentSelectedMaterials = dtRunningExperimentSelectedMaterials;
	}

	public List<RunningExperimentMaterialWrapper> getDtRunningExperimentEquipments() {
		return dtRunningExperimentEquipments;
	}
	public void setDtRunningExperimentEquipments(
			List<RunningExperimentMaterialWrapper> dtRunningExperimentEquipments) {
		this.dtRunningExperimentEquipments = dtRunningExperimentEquipments;
	}

	public List<RunningExperimentMaterialWrapper> getDtRunningExperimentDrugs() {
		return dtRunningExperimentDrugs;
	}
	public void setDtRunningExperimentDrugs(
			List<RunningExperimentMaterialWrapper> dtRunningExperimentDrugs) {
		this.dtRunningExperimentDrugs = dtRunningExperimentDrugs;
	}

	public List<RunningExperimentMaterialWrapper> getDtRunningExperimentSolutions() {
		return dtRunningExperimentSolutions;
	}
	public void setDtRunningExperimentSolutions(
			List<RunningExperimentMaterialWrapper> dtRunningExperimentSolutions) {
		this.dtRunningExperimentSolutions = dtRunningExperimentSolutions;
	}
	
	public int getStepNumber() {
		return stepNumber;
	}
	public org.wontovlab.model.Material getCurrentSelectedMaterial() {
		return currentSelectedMaterial;
	}

	public String getSelectedApplicationOntology() {
		return selectedApplicationOntology;
	}
	public void setSelectedApplicationOntology(String selectedApplicationOntology) {
		this.selectedApplicationOntology = selectedApplicationOntology;
	}

	public void setSomApplicationOntologyTaskList(
			List<SelectItem> somApplicationOntologyTaskList) {
		this.somApplicationOntologyTaskList = somApplicationOntologyTaskList;
	}
	
	public HtmlDataTable getCurrentMaterialPropertiesDataTableBind() {
		return currentMaterialPropertiesDataTableBind;
	}
	public void setCurrentMaterialPropertiesDataTableBind(
			HtmlDataTable currentMaterialPropertiesDataTableBind) {
		this.currentMaterialPropertiesDataTableBind = currentMaterialPropertiesDataTableBind;
	}

	public void setSelectedMaterialProperties(
			List<Property> selectedMaterialProperties) {
		this.selectedMaterialProperties = selectedMaterialProperties;
	}

	public String getSelectedResourceKindRadio() {
		return selectedResourceKindRadio;
	}
	public void setSelectedResourceKindRadio(String selectedResourceKindRadio) {
		this.selectedResourceKindRadio = selectedResourceKindRadio;
	}

	public void setSomSolutionList(List<SelectItem> somSolutionList) {
		this.somSolutionList = somSolutionList;
	}
	
	public Long getSelectedSolution() {
		return selectedSolution;
	}
	public void setSelectedSolution(Long selectedSolution) {
		this.selectedSolution = selectedSolution;
	}
	
	public List<Solution> getListSolutions() {
		return listSolutions;
	}
	public void setListSolutions(List<Solution> listSolutions) {
		this.listSolutions = listSolutions;
	}
	
	
	public List<StepMaterial> getWrappedSolutionsRunningExperiment() {
		return wrappedSolutionsRunningExperiment;
	}
	public void setWrappedSolutionsRunningExperiment(
			List<StepMaterial> wrappedSolutionsRunningExperiment) {
		this.wrappedSolutionsRunningExperiment = wrappedSolutionsRunningExperiment;
	}

	public List<Equipment> getListEquipments() {
		return listEquipments;
	}
	public void setListEquipments(List<Equipment> listEquipments) {
		this.listEquipments = listEquipments;
	}
	
	public List<Drug> getListDrugs() {
		return listDrugs;
	}
	public void setListDrugs(List<Drug> listDrugs) {
		this.listDrugs = listDrugs;
	}
	
	public String getImageName() {
		return imageName;
	}
	public void setImageName(String imageName) {
		this.imageName = imageName;
	}

	public List<StepMaterial> getWrappedEquipmentsRunningExperiment() {
		return wrappedEquipmentsRunningExperiment;
	}
	public void setWrappedEquipmentsRunningExperiment(
			List<StepMaterial> wrappedEquipmentsRunningExperiment) {
		this.wrappedEquipmentsRunningExperiment = wrappedEquipmentsRunningExperiment;
	}
	
	public List<StepMaterial> getWrappedDrugsRunningExperiment() {
		return wrappedDrugsRunningExperiment;
	}
	public void setWrappedDrugsRunningExperiment(
			List<StepMaterial> wrappedDrugsRunningExperiment) {
		this.wrappedDrugsRunningExperiment = wrappedDrugsRunningExperiment;
	}

	public MaterialLogic getMaterialLogic() {
		return materialLogic;
	}
	public void setMaterialLogic(MaterialLogic materialLogic) {
		this.materialLogic = materialLogic;
	}

	public void setSomEquipmentList(List<SelectItem> somEquipmentList) {
		this.somEquipmentList = somEquipmentList;
	}
	public void setSomDrugList(List<SelectItem> somDrugList) {
		this.somDrugList = somDrugList;
	}

	public Long getSelectedDrug() {
		return selectedDrug;
	}
	public void setSelectedDrug(Long selectedDrug) {
		this.selectedDrug = selectedDrug;
	}

	public MaterialBean getMaterialBean() {
		return materialBean;
	}
	public void setMaterialBean(MaterialBean materialBean) {
		this.materialBean = materialBean;
	}
	
	public Long getSelectedEquipment() {
		return selectedEquipment;
	}
	public void setSelectedEquipment(Long selectedEquipment) {
		this.selectedEquipment = selectedEquipment;
	}
	
	public String getStepMaterialDescription() {
		return stepMaterialDescription;
	}
	public void setStepMaterialDescription(String stepMaterialDescription) {
		this.stepMaterialDescription = stepMaterialDescription;
	}
	
	public int getStepMaterialIdStep() {
		return stepMaterialIdStep;
	}
	public void setStepMaterialIdStep(int stepMaterialIdStep) {
		this.stepMaterialIdStep = stepMaterialIdStep;
	}
	
	public ExperimentBean getExperimentBean() {
		return experimentBean;
	}
	public void setExperimentBean(ExperimentBean experimentBean) {
		this.experimentBean = experimentBean;
	}
	
	public UIData getDataTableInsert() {
		return dataTableInsert;
	}
	public void setDataTableInsert(UIData dataTableInsert) {
		this.dataTableInsert = dataTableInsert;
	}

	public UIData getDataTable()
	{
		return dataTable;
	}
	public void setDataTable(UIData dataTable)
	{
		this.dataTable = dataTable;
	}

	public StepWrapper getCurrentStep() {
		return currentStep;
	}
	public void setCurrentStep(StepWrapper currentStep) {
		this.currentStep = currentStep;
	}

	public ExperimentLogic getExperimentLogic()
	{
		return experimentLogic;
	}
	public void setExperimentLogic(ExperimentLogic experimentLogic)
	{
		this.experimentLogic = experimentLogic;
	}
	
	public void setExternalLogic(ExternalLogic externalLogic) {
		this.externalLogic = externalLogic;
	}
	
	
	
	public StepBean() 
	{
        //listDTSelectedMaterialRunExperiment = new ArrayList<StepMaterial>();
		wrappedDrugsRunningExperiment = new ArrayList<StepMaterial>(); // will be used in showsetvalue [drug] | [equipment]
		wrappedEquipmentsRunningExperiment = new ArrayList<StepMaterial>(); // will be used in showsetvalue [drug] | [equipment]
		wrappedSolutionsRunningExperiment = new ArrayList<StepMaterial>();
		//listaOrdenada = new ArrayList<Step>();
	}
	
	
	
	
	/// O atributo experimentBean estava dando erro ao ser acessado no contrutor, entao, no facesconfig foi adicionado 
	/// o atributo init por ultimo, assim o experimentbean ja foi instanciado. Posso dizer que o init virou outro construtor 
	public void setInit(boolean x) {
		
		
	}
	
	public String processActionBack () {
		return "experimentDetail";
	}
	
	public boolean getCanCreate()
	{
		return experimentLogic.canCreateExperiments(externalLogic.getCurrentLocationId(), externalLogic.getCurrentUserId());
	}
	
	


	public String processActionNew() 
	{
		stepNumber = 1;
		dataTableInsert = null;
		stepMaterialList = null;
		currentMaterialPropertiesDataTableBind = null;
		selectedMaterialProperties = null;
		
		return "newStep";
	}
    
	
	public String processActionList() 
	{
		log.debug("in process action list weblabs...");
		
		return "listStep";
	}
	
	public String processActionReturnToExperimentDetail() {
		return "experimentDetail";
	}
	
	

	



	public List<SelectItem> getSomDrugList() {
		somDrugList.clear();
		somDrugList.add(new SelectItem(new Long(-1), " "));
		
		List l = materialBean.getMaterialLogic().listDrug(externalLogic.getCurrentLocationId(), externalLogic.getCurrentUserId());
		
		for (int i = 0; i < l.size(); i++) {
			somDrugList.add(new SelectItem(((Drug) l.get(i)).getMaterialId(), ((Drug) l.get(i)).getDescription() ));
		}		

		return somDrugList;
	}	
	
	public List<SelectItem> getSomEquipmentList() {
		somEquipmentList.clear();
		somEquipmentList.add(new SelectItem(new Long(-1), " "));
		
		List l = materialBean.getMaterialLogic().listEquipment(externalLogic.getCurrentLocationId(), externalLogic.getCurrentUserId());
		
		for (int i = 0; i < l.size(); i++) {
			somEquipmentList.add(new SelectItem(((Equipment) l.get(i)).getMaterialId() , ((Equipment) l.get(i)).getDescription() ));
		}
		
		return somEquipmentList;
	}
	
	public List<SelectItem> getSomSolutionList() {
		somSolutionList.clear();
		somSolutionList.add(new SelectItem(new Long(-1), " "));
		
		List l = materialBean.getMaterialLogic().listSolution(externalLogic.getCurrentLocationId(), externalLogic.getCurrentUserId());
		
		for (int i = 0; i < l.size(); i++) {
			somSolutionList.add(new SelectItem(((Solution) l.get(i)).getMaterialId() , ((Solution) l.get(i)).getDescription() ));
		}
		return somSolutionList;
	}
	
	public List<SelectItem> getDomainOntologyMaterialList() {
		domainOntologyMaterialList.clear();
		domainOntologyMaterialList.add(new SelectItem(" ", " "));
		
		List<org.wontovlab.model.Material> materials = experimentBean.wpf.doGetAllMaterials();
		
		for (int i = 0; i < materials.size(); i++) {
			domainOntologyMaterialList.add(new SelectItem(materials.get(i).getOntologyReference() , materials.get(i).getName() ));
		}
		
		
		return domainOntologyMaterialList;
	}
	
	
	public List<org.wontovlab.model.Material> getStepMaterialList() {
		return stepMaterialList;
	}
	

    
    
    // Paging.
    public void changePagingState(ValueChangeEvent valueChange) 
    {
    	log.error("changePagingState: old=" + valueChange.getOldValue() + ", new=" + valueChange.getNewValue());
    	FacesContext.getCurrentInstance().renderResponse();
    }
        
    public int getFirstRow() 
    {
    	if (log.isDebugEnabled()) log.debug("getFirstRow " + firstScoreRow);
    	return firstScoreRow;
    }
    
    public void setFirstRow(int firstRow) 
    {
    	if (log.isDebugEnabled()) log.debug("setFirstRow from " + firstScoreRow + " to " + firstRow);
    	firstScoreRow = firstRow;
    }

    
  
    
    
    
    
    ///////////////////////////// Running Experiment 
	
	public String processActionRunExperiment() {

		dtRunningExperimentSelectedMaterials = new ArrayList<StepMaterial>();
		dtRunningExperimentEquipments = new ArrayList();
		dtRunningExperimentDrugs = new ArrayList();
		dtRunningExperimentSolutions = new ArrayList();
		
		bindingDTRunnigExperimentDrugs=null;
		bindingDTRunnigExperimentEquipments=null;
		bindingDTRunnigExperimentSolutions=null;
		bindingDTRunExperimentStepMaterials = null;
		
		stepMaterialList = null;
		dataTableInsert = null;
		experiment = null;
		
		stepNumber=1;
		
		listDrugs = materialLogic.listDrug(externalLogic.getCurrentLocationId(), externalLogic.getCurrentUserId());
		listEquipments = materialLogic.listEquipment(externalLogic.getCurrentLocationId(), externalLogic.getCurrentUserId());
		listSolutions = materialLogic.listSolution(externalLogic.getCurrentLocationId(), externalLogic.getCurrentUserId());
	

		// Filling out all data tables with its respective materials
		for (int i=0; i<listEquipments.size(); i++) {
			RunningExperimentMaterialWrapper runningExperimentEquipmentWrapper = new RunningExperimentMaterialWrapper();
    		
			runningExperimentEquipmentWrapper.setMaterial(((Material) materialBean.getAllMaterialsMap().get(listEquipments.get(i).getMaterialId())));
			runningExperimentEquipmentWrapper.setImage(materialLogic.getMaterialImageName(listEquipments.get(i).getMaterialId()));
			
			dtRunningExperimentEquipments.add(runningExperimentEquipmentWrapper);
		}
		for (int i=0; i<listDrugs.size(); i++) {
			RunningExperimentMaterialWrapper runningExperimentDrugWrapper = new RunningExperimentMaterialWrapper();

			runningExperimentDrugWrapper.setMaterial(((Material) materialBean.getAllMaterialsMap().get(listDrugs.get(i).getMaterialId())));
			runningExperimentDrugWrapper.setImage(materialLogic.getMaterialImageName(listDrugs.get(i).getMaterialId()));
			
			dtRunningExperimentDrugs.add(runningExperimentDrugWrapper);
		}
		for (int i=0; i<listSolutions.size(); i++) {
			RunningExperimentMaterialWrapper runningExperimentSolutionWrapper = new RunningExperimentMaterialWrapper();

			runningExperimentSolutionWrapper.setMaterial(((Material) materialBean.getAllMaterialsMap().get(listSolutions.get(i).getMaterialId())));
			runningExperimentSolutionWrapper.setImage(materialLogic.getMaterialImageName(listSolutions.get(i).getMaterialId()));
			
			dtRunningExperimentSolutions.add(runningExperimentSolutionWrapper);
		}		

		
		return "runExperiment";
	}
	
	public void runningExperimentAddSelectedMaterial() {

		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String selectedMaterialId = (String)requestMap.get("SELECTED_MATERIAL_PARAM"); 
		System.out.println("selectedMaterialId--------> " + selectedMaterialId);
		
		currentSelectedMaterial = experimentBean.getWpf().doGetMaterial(((Material) materialBean.getAllMaterialsMap().get(new Long(selectedMaterialId))).getDomainOntologyReference());
		
		if (stepMaterialList == null) {
			stepMaterialList = new ArrayList<org.wontovlab.model.Material>();
			stepMaterialList.add(currentSelectedMaterial);
		} else {
			stepMaterialList.add(currentSelectedMaterial);
		}
	
	}
	
    public void runningExperimentAddStep() {
    	
		if (stepNumber == 1) {
			experiment = new org.wontovlab.model.Experiment();
			experimentStepList = new ArrayList<org.wontovlab.model.Step>();
			
			experiment.setName(experimentBean.getCurrentExperiment().getExperiment().getName());
			experiment.setExperimentId(experimentBean.getCurrentExperiment().getExperiment().getExperimentId());
			
			org.wontovlab.model.Step currentStep = new org.wontovlab.model.Step();
			currentStep.setDescription(stepMaterialDescription);
			currentStep.setId("Step_" + stepNumber);
			currentStep.setMaterials(stepMaterialList);
			
			experimentStepList.add(currentStep);
			
			experiment.setStepList(experimentStepList);
		} else {
			org.wontovlab.model.Step currentStep = new org.wontovlab.model.Step();
			currentStep.setDescription(stepMaterialDescription);
			currentStep.setId("Step_" + stepNumber);
			currentStep.setMaterials(stepMaterialList);
			
			experimentStepList.add(currentStep);
			
			experiment.setStepList(experimentStepList);	
		}
		
		System.out.println("CHAMOU");
		for (int i=0; i<experiment.getStepList().size(); i++ ) {
			System.out.println("====================================");
			for (int j=0; j < experiment.getStepList().get(i).getMaterials().size(); j++) {
				System.out.println("Material......: " + experiment.getStepList().get(i).getMaterials().get(j).getName());
				System.out.println("MatOntRef.....: " + experiment.getStepList().get(i).getMaterials().get(j).getOntologyReference());
				if (experiment.getStepList().get(i).getMaterials().get(j).getProperties() != null) {
					for (int z=0; z < experiment.getStepList().get(i).getMaterials().get(j).getProperties().size(); z++ ) {
						System.out.println("Property...: " + experiment.getStepList().get(i).getMaterials().get(j).getProperties().get(z).getName());
						System.out.println("Value......: " + experiment.getStepList().get(i).getMaterials().get(j).getProperties().get(z).getValue());
					}
				}
			}
		}
		
		stepNumber++;
		stepMaterialList = null;
		
    }
    
    public String runningExperimentFinishExperiment() {
    	
    	experimentBean.wpf.creatingExecutionWorkflow(experiment, externalLogic.getSakaiHomePath() + "/executionExperiment_" + experimentBean.getCurrentExperiment().getExperiment().getExperimentId().toString() + ".xpdl");
    	workflowComparisonResult = "";
    	try {
			experimentBean.wpf.readAuthorshipXPDL(externalLogic.getSakaiHomePath() + "/authorshipExperiment_" + experimentBean.getCurrentExperiment().getExperiment().getExperimentId().toString() + ".xpdl");
			experimentBean.wpf.readExecutionXPDL(externalLogic.getSakaiHomePath() + "/executionExperiment_" + experimentBean.getCurrentExperiment().getExperiment().getExperimentId().toString() + ".xpdl");
			workflowComparisonResult = experimentBean.wpf.comparingWorkflows();
    	} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
    	
    	return "finishExperiment";
    }
    
    public String processActionExperimentList() {
    	System.out.println("CHAMOU O processActionExperimentList");
    	return "experimentList";
    }
    
    
	///////////////////////////// Experiment Authorship
    
	public void processActionInsertItem() {

		String selectedMaterialOntologyReference = new String();
		
		
    	if (selectedResourceKindRadio.equals("0")) {
    		selectedMaterialOntologyReference = ((Equipment) materialBean.getAllMaterialsMap().get(selectedEquipment)).getDomainOntologyReference();
    	}
    	
    	if (selectedResourceKindRadio.equals("1")) {
    		selectedMaterialOntologyReference = ((Drug) materialBean.getAllMaterialsMap().get(selectedDrug)).getDomainOntologyReference();
    	}
    	
    	if (selectedResourceKindRadio.equals("2")) {
    		selectedMaterialOntologyReference = ((Solution) materialBean.getAllMaterialsMap().get(selectedSolution)).getDomainOntologyReference();
    	}
    	
		if (selectedMaterialOntologyReference == null) {
    		System.out.println("Não está cadastrada qual eh a referencia correspondente na ontologia de domínio para este material");
			return;
    	}
    	
		org.wontovlab.model.Material material = experimentBean.getWpf().doGetMaterial(selectedMaterialOntologyReference);

		
		
		
		//Getting all properties from current material and its respective values that were filled out
		List<Property> materialProperties = (List<Property>) currentMaterialPropertiesDataTableBind.getValue();

		System.out.println();
		
		if (materialProperties != null) {
			material.setProperties(materialProperties);
		}
		
		if (stepMaterialList == null) {
			stepMaterialList = new ArrayList<org.wontovlab.model.Material>();
			stepMaterialList.add(material);
		} else {
			stepMaterialList.add(material);
		}
		
		cleanSelectOneMenu();
	}
    
	public void processActionAddStep() {
		
		if (stepNumber == 1) {
			experiment = new org.wontovlab.model.Experiment();
			experimentStepList = new ArrayList<org.wontovlab.model.Step>();
			
			experiment.setDescription(experimentBean.getCurrentExperiment().getExperiment().getDescription());
			experiment.setName(experimentBean.getCurrentExperiment().getExperiment().getName());
			experiment.setExperimentId(experimentBean.getCurrentExperiment().getExperiment().getExperimentId());
			
			org.wontovlab.model.Step currentStep = new org.wontovlab.model.Step();
			currentStep.setApplicationOntologyReference(selectedApplicationOntology);
			currentStep.setDescription(stepMaterialDescription);
			currentStep.setId("Step_" + stepNumber);
			currentStep.setMaterials(stepMaterialList);
			
			experimentStepList.add(currentStep);
			
			experiment.setStepList(experimentStepList);
		} else {
			org.wontovlab.model.Step currentStep = new org.wontovlab.model.Step();
			currentStep.setApplicationOntologyReference(selectedApplicationOntology);
			currentStep.setDescription(stepMaterialDescription);
			currentStep.setId("Step_" + stepNumber);
			currentStep.setMaterials(stepMaterialList);
			
			experimentStepList.add(currentStep);
			
			experiment.setStepList(experimentStepList);			
		}
		
		stepNumber++;
		stepMaterialList = null;
    	selectedApplicationOntology = "null";
		cleanSelectOneMenu();
	}
    
	public String processActionFinishExperimentCreation() {
		System.out.println("FINALIZANDO EXPERIMENTO......");
		
/*		System.out.println(experiment.getName());
		System.out.println(experiment.getDescription());

		for (int i=0; i<experiment.getStepList().size(); i++ ) {
			System.out.println("====================================");
			System.out.println(experiment.getStepList().get(i).getApplicationOntologyReference());
			for (int j=0; j < experiment.getStepList().get(i).getMaterials().size(); j++) {
				System.out.println("Material......: " + experiment.getStepList().get(i).getMaterials().get(j).getName());
				System.out.println("MatOntRef.....: " + experiment.getStepList().get(i).getMaterials().get(j).getOntologyReference());
				if (experiment.getStepList().get(i).getMaterials().get(j).getProperties() != null) {
					for (int z=0; z < experiment.getStepList().get(i).getMaterials().get(j).getProperties().size(); z++ ) {
						System.out.println("Property...: " + experiment.getStepList().get(i).getMaterials().get(j).getProperties().get(z).getName());
						System.out.println("Value......: " + experiment.getStepList().get(i).getMaterials().get(j).getProperties().get(z).getValue());
					}
				}
			}
		}*/
		
		experimentBean.getWpf().creatingAuthorshipWorkflow(experiment, externalLogic.getSakaiHomePath() + "/authorshipExperiment_" + experimentBean.getCurrentExperiment().getExperiment().getExperimentId().toString() + ".xpdl");
		
		return "experimentDetail";
	}
	

	
	public List<org.wontovlab.model.Material> getSimilarMaterials() {

		if (selectedResourceKindRadio == null)
			return null;
		
		String selectedMaterialOntologyReference = new String();

    	//Getting selected material domain ontology reference
    	if (selectedResourceKindRadio.equals("0")) {
    		if (selectedEquipment == -1) {
    			cleanSelectOneMenu();
    			return null;
    		}
    		selectedMaterialOntologyReference = ((Equipment) materialBean.getAllMaterialsMap().get(selectedEquipment)).getDomainOntologyReference();
    	}
    	if (selectedResourceKindRadio.equals("1")) {
    		if (selectedDrug == -1) {
    			cleanSelectOneMenu();
    			return null;
    		}
    		selectedMaterialOntologyReference = ((Drug) materialBean.getAllMaterialsMap().get(selectedDrug)).getDomainOntologyReference();
    	}
    	if (selectedResourceKindRadio.equals("2")) {
    		if (selectedSolution == -1) {
    			cleanSelectOneMenu();
    			return null;
    		}
    		selectedMaterialOntologyReference = ((Solution) materialBean.getAllMaterialsMap().get(selectedSolution)).getDomainOntologyReference();
    	}
    	
		if (selectedMaterialOntologyReference == null) {
			System.out.println("Não está cadastrada qual eh a referencia correspondente na ontologia de domínio para este material");
    		return null;
    	}	
    	
    	
		currentSelectedMaterial = experimentBean.getWpf().doGetMaterial(selectedMaterialOntologyReference);
		similarMaterials = experimentBean.getWpf().doGetInstancesSimilarTo(currentSelectedMaterial);
		
		return similarMaterials;
	}
	
	public List<Property> getSelectedMaterialProperties() {

		//If the current material has properties, so it is necessary to fill out the dataTable with them
		if (currentSelectedMaterial != null) { 
		
			if (currentSelectedMaterial.getProperties() != null) {
				selectedMaterialProperties = currentSelectedMaterial.getProperties();
			} else {
				selectedMaterialProperties = null;
			}
				
		} 
		
		System.out.println("Chamou selectedMaterialProperties(): " + selectedMaterialProperties);
		
		return selectedMaterialProperties;
	}
   
	public List<SelectItem> getSomApplicationOntologyTaskList() {
		
		somApplicationOntologyTaskList = new ArrayList<SelectItem>();
		
		somApplicationOntologyTaskList.add(new SelectItem(new String("null"), ""));
		
		List<Task> allTasks = experimentBean.getWpf().aoGetAllTasks();
		
		//Removing duplicated task descriptions
		for (int i=0; i < allTasks.size(); i++) {
			for (int j=0; j < allTasks.size(); j++) {
				if (allTasks.get(j) != allTasks.get(i)) {
					if (allTasks.get(i).getDescription().equals(allTasks.get(j).getDescription())) {
						allTasks.get(j).setDescription("DELETAR");
					}
				}
			}
		}
		for (int i=allTasks.size()-1; i > -1; i--) {
			if (allTasks.get(i).getDescription().equals("DELETAR"))
				allTasks.remove(i);
		}
			
		
		for (int i=0; i < allTasks.size(); i++) {

			//if the applicationontology task has no label, so it is filled out with the applicationOntologyReference to avoid nullPointerException error
			if (allTasks.get(i).getDescription() == null)
				allTasks.get(i).setDescription(allTasks.get(i).getApplicationOntologyReference());

			//adding all application ontology tasksin the combobox 
			somApplicationOntologyTaskList.add(new SelectItem(allTasks.get(i).getApplicationOntologyReference(), allTasks.get(i).getDescription()));
		}
		

		
		
		return somApplicationOntologyTaskList;
	}
	
	
    public void cleanSelectOneMenu() {
    	selectedDrug = new Long(-1);
    	selectedEquipment = new Long(-1);
    	selectedSolution = new Long(-1);
    	selectedMaterialProperties = null;
    	similarMaterials = null;
    	currentSelectedMaterial = null;
    	selectedMaterialProperties = null;
    	stepMaterialDescription = STEPMATERIAL_DEFAULT_DESCRIPTION;
    }

    
    
    
    /////////////////////////////Generic Methods for RunningExperiment.jsp and StepNew.jsp
    
	public void processActionDeleteMaterial() {
		org.wontovlab.model.Material materialToDelete = (org.wontovlab.model.Material) dataTableInsert.getRowData();  //to get the user selected item

		System.out.println("Deleting material... " + materialToDelete.getName());
		
		stepMaterialList.remove(materialToDelete);
		
		if (stepMaterialList.size() == 0)
			stepMaterialList = null;
	}
    
    /////////////////////////////
    
    
    
    
    
    
    
    
    
    
    
    
}
