package gov.nih.nci.cananolab.ui.common;

import gov.nih.nci.cananolab.domain.common.ExperimentConfig;
import gov.nih.nci.cananolab.domain.common.Instrument;
import gov.nih.nci.cananolab.domain.common.Technique;
import gov.nih.nci.cananolab.dto.common.ExperimentConfigBean;
import gov.nih.nci.cananolab.dto.particle.characterization.CharacterizationBean;
import gov.nih.nci.cananolab.exception.CaNanoLabException;
import gov.nih.nci.cananolab.exception.ExperimentConfigException;
import gov.nih.nci.cananolab.service.common.ExperimentConfigService;
import gov.nih.nci.cananolab.service.common.LookupService;
import gov.nih.nci.cananolab.service.common.impl.ExperimentConfigServiceLocalImpl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.SortedSet;

import org.apache.struts.validator.DynaValidatorForm;
import org.directwebremoting.WebContextFactory;

/**
 * Methods for DWR Ajax
 *
 * @author pansu, tanq
 *
 */
public class DWRExperimentConfigManager {
	private ExperimentConfigService service = new ExperimentConfigServiceLocalImpl();

	public ExperimentConfig resetExperimentConfig() {
		DynaValidatorForm charForm = (DynaValidatorForm) (WebContextFactory
				.get().getSession().getAttribute("characterizationForm"));
		CharacterizationBean charBean = (CharacterizationBean) (charForm
				.get("achar"));
		ExperimentConfigBean newExperimentConfigBean = new ExperimentConfigBean();
		charBean.setTheExperimentConfig(newExperimentConfigBean);
		charBean.setTheInstrument(new Instrument());
		return newExperimentConfigBean.getDomain();
	}

	public ExperimentConfig findExperimentConfigById(String id)
			throws ExperimentConfigException {
		return service.findExperimentConfigById(id);
	}

	public Technique findTechniqueByType(String type)
			throws ExperimentConfigException {
		return service.findTechniqueByType(type);
	}

	public String[] findInstrumentTypesByTechniqueType(String techniqueType)
			throws ExperimentConfigException, CaNanoLabException {
		SortedSet<String> types = null;
		types = LookupService.getDefaultAndOtherLookupTypes(techniqueType,
				"instrument", "otherInstrument");
		if (types != null && types.size() > 0) {
			String[] typeArray = new String[types.size()];
			types.toArray(typeArray);
			return typeArray;
		} else {
			return null;
		}
	}

	public String[] findInstrumentTypesByConfigId(String configId)
			throws ExperimentConfigException, CaNanoLabException {
		String techniqueType = null;
		SortedSet<String> types = null;
		ExperimentConfig config = findExperimentConfigById(configId);

		if (config != null && config.getTechnique() != null
				&& config.getTechnique().getType() != null) {
			techniqueType = config.getTechnique().getType();
			types = LookupService.getDefaultAndOtherLookupTypes(techniqueType,
					"instrument", "otherInstrument");
		}
		if (types != null && types.size() > 0) {
			String[] typeArray = new String[types.size()];
			types.toArray(typeArray);
			return typeArray;
		} else {
			return null;
		}
	}

	public ExperimentConfig getANewExperimentConfig()
			throws ExperimentConfigException {
		// add by Qina tempory
		ExperimentConfig config = new ExperimentConfig();
		config.setTechnique(new Technique());
		config.setInstrumentCollection(new ArrayList<Instrument>());
		return config;
	}
	
	public ExperimentConfig addInstrument(ExperimentConfig theExperimentConfig,
			Instrument instrument)
		throws ExperimentConfigException {
		if (theExperimentConfig==null) {
			return null;
		}else if (theExperimentConfig.getInstrumentCollection()!=null) {
			theExperimentConfig.getInstrumentCollection().add(instrument);
		}else {
			Collection<Instrument> instrumentCollection
				= new ArrayList<Instrument>();
			instrumentCollection.add(instrument);
			theExperimentConfig.setInstrumentCollection(instrumentCollection);
		}
		DynaValidatorForm charForm = (DynaValidatorForm) (WebContextFactory
				.get().getSession().getAttribute("characterizationForm"));
		CharacterizationBean charBean = (CharacterizationBean) (charForm
				.get("achar"));
		charBean.setTheExperimentConfig(new ExperimentConfigBean(theExperimentConfig));
		return theExperimentConfig;
	}
}

@@ -753,24 +753,51 @@
 ollection;%0D%0A
+import java.util.HashSet;%0D%0A
 import java.
@@ -3811,32 +3811,30 @@
 %0D%0A%09%09%09%09= new 
-ArrayLis
+HashSe
 t%3CInstrument
@@ -4299,11 +4299,721 @@
 g;%0D%0A%09%7D%0D%0A
-%7D
+%09%0D%0A%09public ExperimentConfig deleteInstrument(ExperimentConfig theExperimentConfig,%0D%0A%09%09%09Instrument instrument)%0D%0A%09%09throws ExperimentConfigException %7B%0D%0A%09%09if (theExperimentConfig==null %7C%7C %0D%0A%09%09%09%09theExperimentConfig.getInstrumentCollection()==null) %7B%0D%0A%09%09%09return theExperimentConfig;%0D%0A%09%09%7Delse %7B%0D%0A%09%09%09theExperimentConfig.getInstrumentCollection().remove(instrument);%0D%0A%09%09%7D%0D%0A%09%09DynaValidatorForm charForm = (DynaValidatorForm) (WebContextFactory%0D%0A%09%09%09%09.get().getSession().getAttribute(%22characterizationForm%22));%0D%0A%09%09CharacterizationBean charBean = (CharacterizationBean) (charForm%0D%0A%09%09%09%09.get(%22achar%22));%0D%0A%09%09charBean.setTheExperimentConfig(new ExperimentConfigBean(theExperimentConfig));%0D%0A%09%09return theExperimentConfig;%0D%0A%09%7D%0D%0A%7D%0D%0A%0D%0A
 %0D%0A

add deleteInstrument