package com.k_int.ciim.ui.json;

import java.util.ArrayList;
import java.util.Collections;
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.k_int.ciim.data.hdo.SchemaSectionHDO;
import com.sun.jersey.api.view.ImplicitProduces;

@XmlRootElement(name="schema_section") 
@ImplicitProduces("application/json;qs=5")
@XmlAccessorType(XmlAccessType.FIELD)
@Component
public class SchemaSectionJSON implements Comparable<SchemaSectionJSON>
{
	private String id = null;
	private String name = null;
	private List<SchemaElementJSON> ordered_fields = new ArrayList<SchemaElementJSON>();
	private Integer position = null;
	
	protected SchemaSectionJSON(){;}
	
	public SchemaSectionJSON(SchemaSectionHDO hdo)
	{
		if(hdo != null)
		{
			this.id = hdo.getId().toString();
			this.name = hdo.getName();
			this.position = hdo.getPosition();
			//iterate through ordered schema element hdo's... 
			if(hdo.getOrderedFields() != null && hdo.getOrderedFields().size() > 0)
			{
				for(SchemaElementHDO element_hdo : hdo.getOrderedFields())
				{
					if(element_hdo != null)
					{	
						//...and convert them to json
						ordered_fields.add(new SchemaElementJSON(element_hdo));
					}
				}
				
				Collections.sort(ordered_fields); 
			}
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<SchemaElementJSON> getOrderedFields() {
		return ordered_fields;
	}

	public void setOrderedFields(List<SchemaElementJSON> ordered_fields) {
		this.ordered_fields = ordered_fields;
	}
	
	public Integer getPosition()
	{
		return position;
	}

	public void setPosition(Integer position) 
	{
		this.position = position;
	}
	
	/* Comparator method used for sorting */
	public int compareTo(SchemaSectionJSON o) 
	{
        return this.position - o.position;
    }
}
