package org.openforis.idm.model;

import java.util.HashMap;
import java.util.Map;

import org.openforis.idm.metamodel.TaxonAttributeDefinition;
import org.openforis.idm.model.species.Taxon;
import org.openforis.idm.model.species.TaxonVernacularName;

/**
 * @author G. Miceli
 * @author M. Togna
 * @author S. Ricci
 */
public final class TaxonOccurrence extends AbstractValue {

	private Integer taxonId;
	private String code;
	private String scientificName;
	private String vernacularName;
	private String languageCode;
	private String languageVariety;

	public TaxonOccurrence() {
		super();
	}

	public TaxonOccurrence(String code, String scientificName) {
		super();
		this.code = code;
		this.scientificName = scientificName;
	}

	public TaxonOccurrence(Integer taxonId, String code, String scientificName) {
		super();
		this.taxonId = taxonId;
		this.code = code;
		this.scientificName = scientificName;
	}
	
	public TaxonOccurrence(String code, String scientificName, String vernacularName, 
			String languageCode, String languageVariety) {
		super();
		this.code = code;
		this.scientificName = scientificName;
		this.vernacularName = vernacularName;
		this.languageCode = languageCode;
		this.languageVariety = languageVariety;
	}
	
	public TaxonOccurrence(Integer taxonId, String code, String scientificName, String vernacularName, 
			String languageCode, String languageVariety) {
		super();
		this.taxonId = taxonId;
		this.code = code;
		this.scientificName = scientificName;
		this.vernacularName = vernacularName;
		this.languageCode = languageCode;
		this.languageVariety = languageVariety;
	}

	public TaxonOccurrence(Taxon taxon, TaxonVernacularName vernacularName) {
		this(taxon.getTaxonId(), taxon.getCode(), taxon.getScientificName(), 
					vernacularName.getVernacularName(), vernacularName.getLanguageCode(),
					vernacularName.getLanguageVariety());
	}
	
	@Override
	@SuppressWarnings("serial")
	public Map<String, Object> toMap() {
		return new HashMap<String, Object>() {{
			put(TaxonAttributeDefinition.CODE_FIELD_NAME, code);
			put(TaxonAttributeDefinition.SCIENTIFIC_NAME_FIELD_NAME, scientificName);
			put(TaxonAttributeDefinition.VERNACULAR_NAME_FIELD_NAME, vernacularName);
			put(TaxonAttributeDefinition.LANGUAGE_CODE_FIELD_NAME, languageCode);
			put(TaxonAttributeDefinition.LANGUAGE_VARIETY_FIELD_NAME, languageVariety);
		}};
	}
	
	public Integer getTaxonId() {
		return taxonId;
	}

	public void setTaxonId(Integer taxonId) {
		this.taxonId = taxonId;
	}

	public String getCode() {
		return code;
	}

	public String getScientificName() {
		return scientificName;
	}

	public String getVernacularName() {
		return vernacularName;
	}

	public String getLanguageCode() {
		return languageCode;
	}

	public String getLanguageVariety() {
		return languageVariety;
	}

	@Override
	public String toPrettyFormatString() {
		return code;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result
				+ ((languageCode == null) ? 0 : languageCode.hashCode());
		result = prime * result
				+ ((languageVariety == null) ? 0 : languageVariety.hashCode());
		result = prime * result
				+ ((scientificName == null) ? 0 : scientificName.hashCode());
		result = prime * result + ((taxonId == null) ? 0 : taxonId.hashCode());
		result = prime * result
				+ ((vernacularName == null) ? 0 : vernacularName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TaxonOccurrence other = (TaxonOccurrence) obj;
		if (code == null) {
			if (other.code != null)
				return false;
		} else if (!code.equals(other.code))
			return false;
		if (languageCode == null) {
			if (other.languageCode != null)
				return false;
		} else if (!languageCode.equals(other.languageCode))
			return false;
		if (languageVariety == null) {
			if (other.languageVariety != null)
				return false;
		} else if (!languageVariety.equals(other.languageVariety))
			return false;
		if (scientificName == null) {
			if (other.scientificName != null)
				return false;
		} else if (!scientificName.equals(other.scientificName))
			return false;
		if (taxonId == null) {
			if (other.taxonId != null)
				return false;
		} else if (!taxonId.equals(other.taxonId))
			return false;
		if (vernacularName == null) {
			if (other.vernacularName != null)
				return false;
		} else if (!vernacularName.equals(other.vernacularName))
			return false;
		return true;
	}

}

@@ -77,24 +77,70 @@
 til.Map;%0D%0A%0D%0A
+import org.apache.commons.lang3.StringUtils;%0D%0A
 import org.o
@@ -3009,36 +3009,243 @@
 ring() %7B%0D%0A%09%09
-return code;
+if (StringUtils.isNotBlank(vernacularName)) %7B%0D%0A%09%09%09return String.format(%22%25s - %25s - %25s%5B%25s%5D%22, code, scientificName, vernacularName, languageCode);%0D%0A%09%09%7D else %7B%0D%0A%09%09%09return String.format(%22%25s - %25s%22, code, scientificName);%0D%0A%09%09%7D
 %0D%0A%09%7D%0D%0A%09%0D%0A%09@O

Use better format for TaxonOccurrence to string method