package ch.elexis.core.jpa.entities;

import java.time.LocalDate;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.eclipse.persistence.annotations.Cache;

import ch.elexis.core.jpa.entities.converter.BooleanCharacterConverterSafe;
import ch.elexis.core.jpa.entities.id.ElexisIdGenerator;
import ch.elexis.core.jpa.entities.listener.EntityWithIdListener;

@Entity
@Table(name = "CH_ELEXIS_OMNIVORE_DATA")
@Cache(expiry = 15000)
@EntityListeners(EntityWithIdListener.class)
@NamedQuery(name = "DocHandle.select.category.names", query = "SELECT DISTINCT dh.title FROM DocHandle dh WHERE dh.deleted = false AND dh.mimetype='text/category' ORDER BY dh.title")
public class DocHandle extends AbstractEntityWithId implements EntityWithId, EntityWithDeleted {

	public static final String CATEGORY_CATEGORY = "text/category";

	// Transparently updated by the EntityListener
	protected Long lastupdate;
	
	@Id
	@GeneratedValue(generator = "system-uuid")
	@Column(unique = true, nullable = false, length = 25)
	private String id = ElexisIdGenerator.generateId();
	
	@Column
	@Convert(converter = BooleanCharacterConverterSafe.class)
	protected boolean deleted = false;
	
	@OneToOne
	@JoinColumn(name = "PatID")
	protected Kontakt kontakt;

	@Column(length = 8)
	protected LocalDate datum;

	@Column(length = 8)
	protected LocalDate creationDate;

	@Column(length = 80)
	protected String category;

	@Column(length = 255)
	protected String title;

	@Column(length = 255)
	protected String mimetype;

	@Column(length = 512)
	protected String keywords;

	@Column(length = 255)
	protected String path;

	@Column(name = "DOCUMENT_STATUS")
	protected int status;
	
	@Column
	@Basic(fetch = FetchType.LAZY)
	@Lob
	protected byte[] doc;

	@Override
	public String getId(){
		return id;
	}

	@Override
	public void setId(String id){
		this.id = id;
	}

	@Override
	public Long getLastupdate(){
		return lastupdate;
	}

	@Override
	public void setLastupdate(Long lastupdate){
		this.lastupdate = lastupdate;
	}

	@Override
	public boolean isDeleted(){
		return deleted;
	}

	@Override
	public void setDeleted(boolean deleted){
		this.deleted = deleted;
	}

	public Kontakt getKontakt() {
		return kontakt;
	}

	public void setKontakt(Kontakt kontakt) {
		this.kontakt = kontakt;
	}

	public LocalDate getDatum() {
		return datum;
	}

	public void setDatum(LocalDate datum) {
		this.datum = datum;
	}

	public LocalDate getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(LocalDate creationDate) {
		this.creationDate = creationDate;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMimetype() {
		return mimetype;
	}

	public void setMimetype(String mimetype) {
		this.mimetype = mimetype;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getPath() {
		return path;
	}
	
	public void setPath(String path) {
		this.path = path;
	}

	public int getStatus(){
		return status;
	}
	
	public void setStatus(int status){
		this.status = status;
	}

	public byte[] getDoc() {
		return doc;
	}

	public void setDoc(byte[] doc) {
		this.doc = doc;
	}
}

@@ -1063,16 +1063,180 @@
 title%22)%0A
+@NamedQuery(name = %22DocHandle.select.categories%22, query = %22SELECT dh FROM DocHandle dh WHERE dh.deleted = false AND dh.mimetype='text/category' ORDER BY dh.title%22)%0A
 public c

add DocHandle categories named query