package com.libreecm.db.jpa;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.chemistry.opencmis.commons.data.CmisExtensionElement;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="cmis_extension_element")
public class CmisExtensionElementImpl extends LibreECMObject implements
org.apache.chemistry.opencmis.commons.data.CmisExtensionElement {

	/**  
	 * 
	 */
	private static final long serialVersionUID = -6926969535277403084L;
	@Id
	@GeneratedValue(generator="hilo-gen")
	@GenericGenerator(name="hilo-gen", strategy="hilo")
	private Integer id; 

	public String getId(){
		if (id==null){
			return null;
		}
		return this.id.toString();
	}
	
	public void setId(String id){
		this.id=Integer.parseInt(id);
	}
	
	public String getName() {
		return this.name;
	}

	private String name;
	private String namespace;
	private String value;

	@ElementCollection(fetch=FetchType.EAGER)
	@CollectionTable(name="cmisattribute")
	@MapKeyColumn(name="attr_id")
	@Column(name="attributes")
	private Map<String, String> attributes;
	
    /**
     * A collection of types changed by this repository
    */
    @OneToMany
	private List<CmisExtensionElementImpl> children;
	
	public CmisExtensionElementImpl(){
		
	}	
	
	public CmisExtensionElementImpl(String name, String namespace, String value, Map<String, String> attributes,
			List<CmisExtensionElementImpl> children) {
		super();
		this.name = name;
		this.namespace = namespace;
		this.value = value;
		this.attributes = attributes;
		this.setChildren(children);
	}
 
	
	public String getNamespace() {
		return this.namespace;
	}

	public String getValue() {
		return value;
	}

	public Map<String, String> getAttributes() {
		if (attributes==null){
			attributes = new HashMap<String, String>();
		}
		return attributes;
	}

	@Override
	public List<CmisExtensionElement> getChildren() {
		if (this.children==null){
			this.children = new ArrayList<>();
		}
		ArrayList<CmisExtensionElement> array= new ArrayList<CmisExtensionElement>();
		array.addAll(this.children);
		return array;
	} 

	public void setName(String name) {
		this.name = name;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setAttributes(Map<String, String> attributes) {
		this.attributes=attributes;
	}

	public void setChildren(List<CmisExtensionElementImpl> arg0) {
	//this.children=new ArrayList<CmisExtensionElementImpl>();
		//Iterator<CmisExtensionElement> it = arg0.iterator();
		//while (it.hasNext()){
		//this.children.add((CmisExtensionElementImpl) it.next());
		//}		
		//}
		this.children=arg0;
	}
}
