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.SchemaHDO;
import com.k_int.ciim.data.hdo.SchemaSectionHDO;
import com.sun.jersey.api.view.ImplicitProduces;

@XmlRootElement(name="schema")
@ImplicitProduces("application/json;qs=5")
@XmlAccessorType(XmlAccessType.FIELD)
@Component
public class SchemaJSON 
{
	private String id = null;
	private String name = null;
	private String description = null;
	private List<SchemaSectionJSON> ordered_sections = new ArrayList<SchemaSectionJSON>();
	
	protected SchemaJSON(){;}
	
	public SchemaJSON(SchemaHDO hdo)
	{
		if(hdo != null)
		{
			this.id = hdo.getId().toString();
			this.name = hdo.getName();
			this.description = hdo.getDescription();
			
			//iterate through ordered schema section hdo's... 
			if(hdo.getOrderedSections() != null && hdo.getOrderedSections().size() > 0)
			{
				for(SchemaSectionHDO section_hdo : hdo.getOrderedSections())
				{
					if(section_hdo != null)
					{	
						//...and convert them to json
						ordered_sections.add(new SchemaSectionJSON(section_hdo));
					}
				}
				
				Collections.sort(ordered_sections); 
			}
		}
		else 
		{
			throw new NullPointerException("cannot create SchemaJSON as HDO is null.");
		}
	}

	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<SchemaSectionJSON> getOrderedSections() {
		return ordered_sections;
	}

	public void setOrderedSections(List<SchemaSectionJSON> ordered_sections) {
		this.ordered_sections = ordered_sections;
	}

	public String getDescription() 
	{
		return description;
	}

	public void setDescription(String description) 
	{
		this.description = description;
	}
}
