package com.k_int.ciim.ui.json;


import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.springframework.stereotype.Component;

import com.k_int.ciim.data.hdo.SchemaElementHDO;
import com.sun.jersey.api.view.ImplicitProduces;

@XmlRootElement(name="schema_element") 
@ImplicitProduces("application/json;qs=5")
@XmlAccessorType(XmlAccessType.FIELD)
@Component
public class SchemaElementJSON implements Comparable<SchemaElementJSON>
{
	private String id = null;	
	private String minOccurs = "0";
	private String maxOccurs = null;
	private String lowerBound = null;
	private String upperBound = null;
	private String label = null;
	private Integer position = null;
	private PropertyJSON linked_property = null;
	private List<String> values = new ArrayList<String>();
		
	protected SchemaElementJSON(){;}
	
	public SchemaElementJSON(SchemaElementHDO hdo)
	{
		if(hdo != null)
		{
			this.id = hdo.getId().toString();
			
			if(hdo.getMinOccurs() != null)
			{
				this.minOccurs = hdo.getMinOccurs().toString();
			}
			if(hdo.getMaxOccurs() != null)
			{
				this.maxOccurs = hdo.getMaxOccurs().toString();
			}
			if(hdo.getLowerBound() != null)
			{
				this.lowerBound = hdo.getLowerBound().toString();
			}
			if(hdo.getUpperBound() != null)
			{
				this.upperBound = hdo.getUpperBound().toString();
			}
			
			this.label = hdo.getLabel();
						
			if(hdo.getProperty() != null)
			{
				this.linked_property = new PropertyJSON(hdo.getProperty());
				
				//if no label is specified on the schema element then override with the linked property name
				if(label == null || label.trim().length() == 0)
				{
					this.label = hdo.getProperty().getName();
				}
			}
			
			this.position = hdo.getPosition();
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getMinOccurs() {
		return minOccurs;
	}

	public void setMinOccurs(String minOccurs) {
		this.minOccurs = minOccurs;
	}

	public String getMaxOccurs() {
		return maxOccurs;
	}

	public void setMaxOccurs(String maxOccurs) {
		this.maxOccurs = maxOccurs;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public PropertyJSON getLinkedProperty() {
		return linked_property;
	}

	public void setLinkedProperty(PropertyJSON linked_property) {
		this.linked_property = linked_property;
	}

	public List<String> getValues() {
		return values;
	}

	public void setValues(List<String> values) {
		this.values = values;
	}
	
	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}
	
	/* Comparator method used for sorting */
	public int compareTo(SchemaElementJSON o) 
	{
        return this.position - o.position;
    }

	public String getLowerBound() 
	{
		return lowerBound;
	}

	public void setLowerBound(String lowerBound) 
	{
		this.lowerBound = lowerBound;
	}

	public String getUpperBound() 
	{
		return upperBound;
	}

	public void setUpperBound(String upperBound) 
	{
		this.upperBound = upperBound;
	}
}
