package gov.nih.nci.cananolab.dto.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.util.Comparators;
import gov.nih.nci.cananolab.util.DateUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.apache.axis.utils.StringUtils;

/**
 * View bean for technique and associated instruments;
 * 
 * @author pansu, tanq
 * 
 */
public class ExperimentConfigBean {
	private ExperimentConfig domain = new ExperimentConfig();
	private List<Instrument> instruments = new ArrayList<Instrument>(20);

	public ExperimentConfigBean() {
		domain = new ExperimentConfig();
		domain.setTechnique(new Technique());
	}

	public ExperimentConfigBean(ExperimentConfig config) {
		domain = config;
		for (Instrument instrument : config.getInstrumentCollection()) {
			instruments.add(instrument);
		}
		Collections.sort(instruments,
				new Comparators.InstrumentDateComparator());
	}

	public ExperimentConfig getDomain() {
		return domain;
	}

	public void setDomain(ExperimentConfig domain) {
		this.domain = domain;
	}

	public String getTechniqueDisplayName() {
		String techniqueDisplayName = "";
		if (domain.getTechnique() == null) {
			return techniqueDisplayName;
		}
		if (!StringUtils.isEmpty(domain.getTechnique().getAbbreviation())) {
			techniqueDisplayName = domain.getTechnique().getType() + "("
					+ domain.getTechnique().getAbbreviation() + ")";
		} else {
			techniqueDisplayName = domain.getTechnique().getType();
		}
		return techniqueDisplayName;
	}

	public void addInstrument(Instrument instrument) {
		int index = instruments.indexOf(instrument);
		if (index != -1) {
			instruments.remove(instrument);
			// retain the original order
			instruments.add(index, instrument);
		} else {
			instruments.add(instrument);
		}
	}

	public void removeInstrument(Instrument instrument) {
		instruments.remove(instrument);
	}

	/**
	 * @return the instruments
	 */
	public List<Instrument> getInstruments() {
		return instruments;
	}

	/**
	 * @param instruments
	 *            the instruments to set
	 */
	public void setInstruments(List<Instrument> instruments) {
		this.instruments = instruments;
	}

	public void setupDomain(String createdBy) throws Exception {
		if (domain.getId() != null && domain.getId() == 0) {
			domain.setId(null);
		}
		if (domain.getId() == null) {
			domain.setCreatedBy(createdBy);
			domain.setCreatedDate(new Date());
		}
		if (domain.getInstrumentCollection() != null) {
			domain.getInstrumentCollection().clear();
		} else {
			domain.setInstrumentCollection(new HashSet<Instrument>());
		}
		int i = 0;
		for (Instrument instrument : instruments) {
			if (!StringUtils.isEmpty(instrument.getType())
					|| !StringUtils.isEmpty(instrument.getManufacturer())
					|| !StringUtils.isEmpty(instrument.getModelName())) {
				instrument.setCreatedBy(createdBy);
				instrument.setCreatedDate(DateUtils.addSecondsToCurrentDate(i));
				domain.getInstrumentCollection().add(instrument);
				i++;
			}
		}
	}

	public boolean equals(Object obj) {
		boolean eq = false;
		if (obj instanceof ExperimentConfigBean) {
			ExperimentConfigBean c = (ExperimentConfigBean) obj;
			Long thisId = this.domain.getId();
			if (thisId != null && thisId.equals(c.getDomain().getId())) {
				eq = true;
			}
		}
		return eq;
	}

	public int hashCode() {
		return domain.hashCode();
	}

	private String getInstrumentDisplayName(Instrument instrument) {
		StringBuffer sb = new StringBuffer();
		if (!StringUtils.isEmpty(instrument.getType())) {
			sb.append(instrument.getType());
			sb.append(" ");
		}
		if (!StringUtils.isEmpty(instrument.getManufacturer())) {
			sb.append("(");
			sb.append(instrument.getManufacturer());
			if (!StringUtils.isEmpty(instrument.getModelName())) {
				sb.append(", ");
				sb.append(instrument.getModelName());

			}
			sb.append(")");
		}
		return sb.toString();
	}

	public String[] getInstrumentDisplayNames() {
		List<String> displayNames = new ArrayList<String>();
		for (Instrument instrument : instruments) {
			displayNames.add(getInstrumentDisplayName(instrument));
		}
		if (displayNames.isEmpty()) {
			return null;
		}
		return displayNames.toArray(new String[displayNames.size()]);
	}

}

@@ -255,24 +255,70 @@
 mparators;%0D%0A
+import gov.nih.nci.cananolab.util.Constants;%0D%0A
 import gov.n
@@ -351,16 +351,16 @@
 Utils;%0D%0A
-
 %0D%0Aimport
@@ -594,25 +594,24 @@
 ruments;%0D%0A *
- 
 %0D%0A * @author
@@ -626,17 +626,16 @@
 tanq%0D%0A *
- 
 %0D%0A */%0D%0Ap
@@ -998,16 +998,68 @@
 onfig;%0D%0A
+%09%09if (config.getInstrumentCollection() != null) %7B%0D%0A%09
 %09%09for (I
@@ -1113,32 +1113,33 @@
 ection()) %7B%0D%0A%09%09%09
+%09
 instruments.add(
@@ -1152,23 +1152,25 @@
 ment);%0D%0A
+%09
 %09%09%7D%0D%0A%09%09
+%09
 Collecti
@@ -1192,16 +1192,17 @@
 ments,%0D%0A
+%09
 %09%09%09%09new 
@@ -1239,24 +1239,29 @@
 arator());%0D%0A
+%09%09%7D%0D%0A
 %09%7D%0D%0A%0D%0A%09publi
@@ -2711,32 +2711,168 @@
 .getId() == null
+%0D%0A%09%09%09%09%7C%7C !StringUtils.isEmpty(domain.getCreatedBy())%0D%0A%09%09%09%09&& domain.getCreatedBy().equals(%0D%0A%09%09%09%09%09%09Constants.AUTO_COPY_ANNOTATION_PREFIX)
 ) %7B%0D%0A%09%09%09domain.s

update copy created by