package com.libreecm.db.jpa;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.*;

import org.apache.chemistry.opencmis.commons.data.CmisExtensionElement;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="extension_feature")
public class ExtensionFeatureImpl extends LibreECMObject implements org.apache.chemistry.opencmis.commons.data.ExtensionFeature {
	@Id
	@GeneratedValue(generator="hilo-gen")
	@GenericGenerator(name="hilo-gen", strategy="hilo")
	private Integer id;
		 
	@Transient
	private static final long serialVersionUID = 1L;
 
    private String url;
    private String commonName;
    private String versionLabel;
    private String description;
    
    @ElementCollection(fetch=FetchType.EAGER)
    @CollectionTable(name="extfeat_featdata")
    @MapKeyColumn(name="feature_type")
    @Column(name="feature_data")
    private Map<String, String> featureData;
 
    @OneToMany
    private List<CmisExtensionElementImpl> extensions;
    
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getCommonName() {
        return commonName;
    }

    public void setCommonName(String commonName) {
        this.commonName = commonName;
    }

    public String getVersionLabel() {
        return versionLabel;
    }

    public void setVersionLabel(String versionLabel) {
        this.versionLabel = versionLabel;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

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

    public void setFeatureData(Map<String, String> featureData) {
        this.featureData = featureData;
    }

	public void setId(String id) {
		this.id = Integer.parseInt(id);
	}

	public ExtensionFeatureImpl(){
    	
    }

	public ExtensionFeatureImpl(String url, String commonName, String versionLabel, String description,
			Map<String, String> featureData, List<CmisExtensionElement> cmisExtensionElement) {
		super();
		this.url = url;
		this.commonName = commonName;
		this.versionLabel = versionLabel;
		this.description = description;
		this.featureData = featureData;
		this.setExtensions(cmisExtensionElement);
	}

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

	@Override
	public void setExtensions(List<CmisExtensionElement> arg0) {
		this.extensions=new ArrayList<CmisExtensionElementImpl>();

		Iterator<CmisExtensionElement> it = arg0.iterator();
		while (it.hasNext()){
			this.extensions.add((CmisExtensionElementImpl) it.next());
		}		 
	}

	@Override
	public String getId() {
		if (id==null){
			return null;
		}
		return id.toString();
	}

    
}
