package info.bukova.isspst.data;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "FILE_METAINFO")
@Indexed
public class FileMetainfo extends BaseData {

	@Column(name = "FILE_NAME")
	@Field(index = Index.YES, analyze = Analyze.YES)
	private String fileName;

	@Column(name = "MODULE_ID")
	private String moduleId;

	@Column(name = "RECORD_ID")
	private int recordId;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "CONTENT_ID")
	@Cascade(CascadeType.SAVE_UPDATE)
	@IndexedEmbedded
	private FileContent content;

	@Column(name = "MD5")
	private String md5;

	@Column(name = "DESCRIPTION")
	@Field(index = Index.YES, analyze = Analyze.YES)
	private String description;

	private void ensureContentExists() {
		if (content == null) {
			content = new FileContent();
		}
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getPathInFilesystem() {
		if (content != null) {
			return content.getPathInFilesystem();
		}
		return null;
	}

	public void setPathInFilesystem(String pathInFilesystem) {
		ensureContentExists();
		content.setPathInFilesystem(pathInFilesystem);
	}

	public String getModuleId() {
		return moduleId;
	}

	public void setModuleId(String moduleId) {
		this.moduleId = moduleId;
	}

	public int getRecordId() {
		return recordId;
	}

	public void setRecordId(int recordId) {
		this.recordId = recordId;
	}

	public FileContent getContent() {
		return content;
	}

	public void setContent(FileContent content) {
		this.content = content;
	}

	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		this.md5 = md5;
	}

	public String getDescription() {
		return description;
	}

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

	public String getContentType() {
		if (content != null) {
			return content.getContentType();
		}

		return null;
	}

	public void setContentType(String contentType) {
		ensureContentExists();
		content.setContentType(contentType);
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof FileMetainfo)) return false;

		FileMetainfo that = (FileMetainfo) o;

		if (recordId != that.recordId) return false;
		if (content != null ? !content.equals(that.content) : that.content != null) return false;
		if (description != null ? !description.equals(that.description) : that.description != null) return false;
		if (fileName != null ? !fileName.equals(that.fileName) : that.fileName != null) return false;
		if (md5 != null ? !md5.equals(that.md5) : that.md5 != null) return false;
		if (moduleId != null ? !moduleId.equals(that.moduleId) : that.moduleId != null) return false;

		return true;
	}

	@Override
	public int hashCode() {
		int result = fileName != null ? fileName.hashCode() : 0;
		result = 31 * result + (moduleId != null ? moduleId.hashCode() : 0);
		result = 31 * result + recordId;
		result = 31 * result + (content != null ? content.hashCode() : 0);
		result = 31 * result + (md5 != null ? md5.hashCode() : 0);
		result = 31 * result + (description != null ? description.hashCode() : 0);
		return result;
	}
}
