/**
 * 
 */
package gov.nih.nci.cananolab.dto.common;

import gov.nih.nci.cananolab.domain.common.Author;
import gov.nih.nci.cananolab.domain.common.Publication;
import gov.nih.nci.cananolab.domain.particle.NanoparticleSample;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Publication view bean
 * 
 * @author tanq
 * 
 */
public class PublicationBean extends LabFileBean {
	private static final String delimiter = ";";
	
	private String[] particleNames;
	private String[] researchAreas;
	private List<Author> authors = new ArrayList<Author>(20);

	private boolean foundPubMedArticle = false;

	public PublicationBean() {
		super();
		domainFile = new Publication();
		domainFile.setUriExternal(false);
		authors.add(new Author());
	}

	public PublicationBean(Publication publication) {
		this(publication, true);
	}

	public PublicationBean(Publication publication, boolean loadSamples) {
		super(publication);
		this.domainFile = publication;
		Collection<Author> authorCollection =
			publication.getAuthorCollection();
		if (authorCollection!=null &&
				authorCollection.size()>0) {
			List<Author> authorslist = new ArrayList<Author>(authorCollection);
			Collections.sort(authorslist, 
					new Comparator<Author>() {
			    public int compare(Author o1, Author o2) {
			        return (int)(o2.getCreatedDate().compareTo(o1.getCreatedDate()));
			    }});
			authors = authorslist;
		}
		String researchAreasStr = 
			publication.getResearchArea();
		if (researchAreasStr!=null && researchAreasStr.length()>0) {
			researchAreas = researchAreasStr.split(delimiter);
		}else {
			researchAreas = null;
		}		

		if (loadSamples) {
			particleNames = new String[publication.getNanoparticleSampleCollection()
					.size()];
			int i = 0;
			for (NanoparticleSample particle : publication
					.getNanoparticleSampleCollection()) {
				particleNames[i] = particle.getName();
				i++;
			}
		}
		
	}

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

	public String[] getParticleNames() {
		return particleNames;
	}

	public void setParticleNames(String[] particleNames) {
		this.particleNames = particleNames;
	}
	/**
	 * @return the researchAreas
	 */
	public String[] getResearchAreas() {
		return researchAreas;
	}

	/**
	 * @param researchAreas the researchAreas to set
	 */
	public void setResearchAreas(String[] researchAreas) {
		this.researchAreas = researchAreas;
	}

	/**
	 * @return the authors
	 */
	public List<Author> getAuthors() {
		return authors;
	}

	/**
	 * @param authors the authors to set
	 */
	public void setAuthors(List<Author> authors) {
		this.authors = authors;
	}

//	/**
//	 * @return the authorsStr
//	 */
//	public String getAuthorsStr() {
//		return authorsStr;
//	}
//
//	/**
//	 * @param authorsStr the authorsStr to set
//	 */
//	public void setAuthorsStr(String authorsStr) {
//		this.authorsStr = authorsStr;
//	}
	
	
	public void addAuthor() {
		authors.add(new Author());
	}

	public void removeAuthor(int ind) {
		authors.remove(ind);
	}

	public boolean isFoundPubMedArticle() {
		return foundPubMedArticle;
	}

	public void setFoundPubMedArticle(boolean foundPubMedArticle) {
		this.foundPubMedArticle = foundPubMedArticle;
	}

	
	
}

@@ -906,18 +906,27 @@
 on, 
-tru
+false, fals
 e);%0A%09%7D%0A
+%09
 %0A%09pu
@@ -994,515 +994,93 @@
 ples
-) %7B%0A%09%09super(publication);%0A%09%09this.domainFile = publication;%0A%09%09Collection%3CAuthor%3E authorCollection =%0A%09%09%09publication.getAuthorCollection();%0A%09%09if (authorCollection!=null &&%0A%09%09%09%09authorCollection.size()%3E0
+,%0A%09%09%09boolean loadAuthors
 ) %7B%0A%09%09
-%09
-List%3CAuthor%3E authorslist = new ArrayList%3CAuthor%3E(authorCollection);%0A%09%09%09Collections.sort(authorslist, %0A%09%09%09%09%09new Comparator%3CAuthor%3E() %7B%0A%09%09%09    public int compare(Author o1, Author o2) %7B%0A%09%09%09        return (int)(o2.getCreatedDate().compareTo(o1.getCreatedDate()));%0A%09%09%09    %7D%7D);%0A%09%09%09authors = authorslist;%0A%09%09%7D
+super(publication);%0A%09%09this.domainFile = publication;%0A%09%09
 %0A%09%09S
@@ -1297,17 +1297,16 @@
 ;%0A%09%09%7D%09%09%0A
-%0A
 %09%09if (lo
@@ -1577,16 +1577,502 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%09if (loadAuthors) %7B%0A%09%09%09Collection%3CAuthor%3E authorCollection =%0A%09%09%09%09publication.getAuthorCollection();%0A%09%09%09if (authorCollection!=null &&%0A%09%09%09%09%09authorCollection.size()%3E0) %7B%0A%09%09%09%09List%3CAuthor%3E authorslist = new ArrayList%3CAuthor%3E(authorCollection);%0A%09%09%09%09Collections.sort(authorslist, %0A%09%09%09%09%09%09new Comparator%3CAuthor%3E() %7B%0A%09%09%09%09    public int compare(Author o1, Author o2) %7B%0A%09%09%09%09        return (int)(o2.getCreatedDate().compareTo(o1.getCreatedDate()));%0A%09%09%09%09    %7D%7D);%0A%09%09%09%09authors = authorslist;%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09%0A%09%7D%0A%0A%09

load nanoparticle and authors only when necessary