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

import gov.nih.nci.cananolab.domain.common.Datum;
import gov.nih.nci.cananolab.dto.common.DataRowBean;
import gov.nih.nci.cananolab.dto.common.DataSetBean;
import gov.nih.nci.cananolab.dto.particle.characterization.CharacterizationBean;
import gov.nih.nci.cananolab.exception.CharacterizationResultException;
import gov.nih.nci.cananolab.service.particle.NanoparticleCharacterizationResultService;
import gov.nih.nci.cananolab.service.particle.impl.NanoparticleCharacterizationResultServiceLocalImpl;

import java.util.List;

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

/**
 * Methods for DWR Ajax
 *
 * @author pansu, tanq
 *
 */
public class DWRCharacterizationResultManager {
	private NanoparticleCharacterizationResultService service = new NanoparticleCharacterizationResultServiceLocalImpl();

	public DataSetBean resetDataSet() {
		DynaValidatorForm charForm = (DynaValidatorForm) (WebContextFactory
				.get().getSession().getAttribute("characterizationForm"));
		CharacterizationBean charBean = (CharacterizationBean) (charForm
				.get("achar"));
		DataSetBean newDataSetBean = new DataSetBean();
		charBean.setTheDataSet(newDataSetBean);
		return newDataSetBean;
	}

	// addColumnHeader includes the function add and edit
	public DataSetBean addColumnHeader(Datum datum) throws CharacterizationResultException {
		DynaValidatorForm charForm = (DynaValidatorForm) (WebContextFactory
				.get().getSession().getAttribute("characterizationForm"));
		CharacterizationBean charBean = (CharacterizationBean) (charForm
				.get("achar"));
		DataSetBean theDataSet = charBean.getTheDataSet();
		theDataSet.getTheDataRow().addDatum(datum);
		return theDataSet;
	}

	// addRow includes the function add and edit
	public DataSetBean addRow(List<Datum> data) throws CharacterizationResultException {
		DynaValidatorForm charForm = (DynaValidatorForm) (WebContextFactory
				.get().getSession().getAttribute("characterizationForm"));
		CharacterizationBean charBean = (CharacterizationBean) (charForm
				.get("achar"));
		DataSetBean theDataSet = charBean.getTheDataSet();
		if (data != null) {
			//TODO:: if rowid!=null, get the row then update
			//DataRowBean dataRowBean = theDataSet.getTheDataRow();
			DataRowBean dataRowBean = null;
			boolean hasData = false;
			int i=0;
			for (Datum datum : data) {
				if (theDataSet.getDomain().getId()!=null) {
					datum.setDataSet(theDataSet.getDomain());
				}
				if (i==0) {
					if (datum.getDataRow()!=null && datum.getDataRow().getId()>0) {
						dataRowBean = theDataSet.getDataRowBean(datum);
					}else {
						dataRowBean = new DataRowBean();
					}
				}

				dataRowBean.addDatum(datum);
				hasData = true;
				i++;
			}
			if (hasData) {
				theDataSet.addDataRow(dataRowBean);
			}
		}
		return theDataSet;
	}

	public DataSetBean deleteDataRow(DataRowBean dataRowBean)
			throws CharacterizationResultException {
		DynaValidatorForm charForm = (DynaValidatorForm) (WebContextFactory
				.get().getSession().getAttribute("characterizationForm"));
		CharacterizationBean charBean = (CharacterizationBean) (charForm
				.get("achar"));
		DataSetBean theDataSet = charBean.getTheDataSet();
		if (dataRowBean != null) {
			// TODO:: remove base on rowID?? or row index??
			theDataSet.removeDataRow(dataRowBean);
		}
		return theDataSet;
	}

	public DataSetBean getDataSetBeanBy(String dataSetId) throws Exception{
		List<Datum> data=service.getDataForDataSet(dataSetId);
		DataSetBean dataSetBean=new DataSetBean(data);
		return dataSetBean;
	}

	public DataSetBean findDataSetById(String dataSetId)
		throws CharacterizationResultException {
		List<Datum> data = service.getDataForDataSet(dataSetId);
		DataSetBean dataSetBean = new DataSetBean(data);
		dataSetBean.setTheDataRow(dataSetBean.getDataRows().get(0));
		DynaValidatorForm charForm = (DynaValidatorForm) (WebContextFactory
				.get().getSession().getAttribute("characterizationForm"));
		CharacterizationBean charBean = (CharacterizationBean) (charForm
				.get("achar"));
		charBean.setTheDataSet(dataSetBean);
		return dataSetBean;
	}
}

@@ -86,16 +86,88 @@
 mon.
+DataRow;%0D%0Aimport gov.nih.nci.cananolab.domain.particle.characterization.
 Datum;%0D%0A
 impo
@@ -158,24 +158,24 @@
 ion.Datum;%0D%0A
-
 import gov.n
@@ -348,24 +348,106 @@
 ationBean;%0D%0A
+import gov.nih.nci.cananolab.dto.particle.characterization.ExperimentConfigBean;%0D%0A
 import gov.n
@@ -503,24 +503,91 @@
 Exception;%0D%0A
+import gov.nih.nci.cananolab.exception.ExperimentConfigException;%0D%0A
 import gov.n
@@ -793,16 +793,45 @@
 l.List;%0D
+%0Aimport java.util.SortedMap;%0D
 %0A%0D%0Aimpor
@@ -2486,24 +2486,27 @@
 a != null) %7B
+%09%09%09
 %0D%0A%09%09%09//TODO:
@@ -2842,79 +2842,8 @@
  %7B%0D%0A
-%09%09%09%09%09if (datum.getDataRow()!=null && datum.getDataRow().getId()%3E0) %7B%0D%0A%09
 %09%09%09%09
@@ -2894,21 +2894,40 @@
 um);
+%09
 %0D%0A%09%09%09%09%09
-%7Delse 
+if (dataRowBean == null)
 %7B%0D%0A%09
@@ -2980,18 +2980,22 @@
 %0A%09%09%09%09%7D%0D%0A
+%09%09%09%09
 %0D%0A
-
 %09%09%09%09data
@@ -3902,24 +3902,25 @@
 tBean;%0D%0A%09%7D%0D%0A
+%09
 %0D%0A%09public Da

fix addRow for editRow