package com.k_int.npdb.datamodel;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.k_int.aggregator.util.SOLRHelper;
import com.k_int.aggregator.util.SOLRHelper.NVPair;

/**
 * Class Contributor
 */

@Entity
@Table(name = "CONTRIBUTOR")
public class Contributor {

	private Long id;
	private String externalId;
	private Source source;
	private String name;
	private String description;
	private String type;
	private Person person;
	private List<Role> roles = new ArrayList<Role>();

	public static final String PERSON = "Person";
	public static final String GROUP = "Group";
	public static final String COMPANY = "Company";
	
	private static Log log = LogFactory.getLog(Contributor.class);
	
	
	public Contributor() {
	};

	/**
	 * Get the value of id
	 * 
	 * @return the value of id
	 */
	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	/**
	 * Get the value of externalId
	 * 
	 * @return the value of externalId
	 */
	@Column(name = "EXTERNAL_ID")
	public String getExternalId() {
		return externalId;
	}

	/**
	 * Get the value of source
	 * 
	 * @return the value of source
	 */
	@ManyToOne
	@JoinColumn(name = "SOURCE_FK")
	public Source getSource() {
		return source;
	}

	/**
	 * Get the value of name
	 * 
	 * @return the value of name
	 */
	@Column(name = "NAME")
	public String getName() {
		return name;
	}

	/**
	 * Get the value of description
	 * 
	 * @return the value of description
	 */
	@Column(name = "DESCRIPTION")
	@Lob
	public String getDescription() {
		return description;
	}

	@ManyToOne
	@JoinColumn(name = "PERSON_FK")
	public Person getPerson() {
		return person;
	}
	
	@Column(name = "TYPE")
	public String getType() {
		return type;
	}

	@OneToMany(mappedBy = "contributor")
	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

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

	public void setExternalId(String externalId) {
		this.externalId = externalId;
	}

	public void setId(Long id) {
		this.id = id;
	}

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

	public void setPerson(Person person) {
		this.person = person;
	}

	public void setSource(Source source) {
		this.source = source;
	}
	
	public List<NVPair> toSOLRIndex() {
		List<NVPair> indexProperties = new ArrayList<NVPair>();
		indexProperties.add(new SOLRHelper.NVPair("internalId", "" + id));
		indexProperties.add(new SOLRHelper.NVPair("source", source.getName()));
		indexProperties.add(new SOLRHelper.NVPair("identifier", "person_" + id));
		indexProperties.add(new SOLRHelper.NVPair("name", name));
		indexProperties.add(new SOLRHelper.NVPair("type", "person"));
		return indexProperties;
	}
	

	public static Contributor lookupOrCreate(org.hibernate.Session session, String name, String lastName)
			throws org.hibernate.HibernateException {
		Contributor result = null;
		org.hibernate.Query q = session
				.createQuery("select x from com.k_int.npdb.datamodel.Contributor x where x.name = ? or x.name = ?");
		q.setString(0, name);
		q.setString(1, lastName);
		List<Contributor> list =  (List<Contributor>)q.list();
		if (list.size() == 1)
			result = (Contributor)list.get(0);
		else if (list.size() > 1)
		{	
			log.debug("!!!!!Contributors non unique: name: " + name + ", last name " + name);
			for (Contributor c: list)
			{
				log.debug("c id " + c.getId());
			}
		}
		if (result == null) {
			result = new Contributor();
			result.setName(name);
			session.save(result);
		}
		return result;
	}
}
