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

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;

/**
 * Entity that defines a parameter that may be attributed to
 * OrganizationEntities. An instance of this entity does not represent an
 * instance of a parameter. Instead, it's the parameter's definition. It's a
 * metadata, if you prefer.
 * 
 * @author tiago
 * 
 */
@Entity
public class OrganizationEntityParam extends BaseEntity {
	private static final long serialVersionUID = 3906255000714264348L;
	
	public static enum DataType {
		STRING('S', String.class), LONG('L', Long.class), DOUBLE('D', Double.class), DATE('T', Date.class);
		
		private static Map<Character, DataType> values;
		private char code;
		private Class<?> classValue;
		
		private DataType(char code, Class<?> classValue) {
			this.classValue = classValue;
			addValue(this.code = code, this);
		}
		
		private static void addValue(Character code, DataType enumInstance) {
			if (values == null) {values = new HashMap<Character, DataType>();}
			values.put(code, enumInstance);
		}
		
		public char toValue() {
			return code;
		}
		
		public static DataType fromValue(char code) {
			return values.get(code);
		}

		public Class<?> getClassValue() {
			return classValue;
		}
	}
	
	@NotNull @NaturalId private String name;
	@NotNull @NaturalId @ManyToOne private OrganizationEntityType organizationEntityType;
	private boolean mandatory;
	
	@Type(type = "country.company.project.daoimpl.GenericEnumUserType", 
		    parameters = { @Parameter(name = "enumClass", value = "country.company.project.entities.OrganizationEntityParam$DataType") })
	@NotNull
    private DataType dataType;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public OrganizationEntityType getOrganizationEntityType() {
		return organizationEntityType;
	}
	public void setOrganizationEntityType(OrganizationEntityType organizationEntityType) {
		this.organizationEntityType = organizationEntityType;
	}
	public DataType getDataType() {
		return dataType;
	}
	public void setDataType(DataType dataType) {
		this.dataType = dataType;
	}
	public boolean isMandatory() {
		return mandatory;
	}
	public void setMandatory(boolean mandatory) {
		this.mandatory = mandatory;
	}
}
