Article.java

/*
 * Copyright 2019 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.genesys.server.model.impl;

import java.text.SimpleDateFormat;
import java.time.Instant;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.ClassPK;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.Publishable;
import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Document;

@Entity
@Table(name = "article")
@Document(indexName = "article")
@Audited
@Getter
@Setter
public class Article extends TranslatedAuditedVersionedModel<ArticleLang, Article> implements Publishable, Copyable<Article> {

	private static final long serialVersionUID = 8690395020204070378L;

	@ManyToOne(cascade = {}, optional = false)
	@JoinColumn(name = "classPk")
	private ClassPK classPk;

	@Column(nullable = false)
	@NotNull
	private long targetId;

	/**
	 * A descriptive unique URL to the article
	 */
	@Size(max = 150)
	@NotNull
	@Column(nullable = false, length = 150)
	private String slug;

	@NotNull
	@Column(nullable = false, columnDefinition = "boolean default false")
	private boolean template;

	@Lob
	@Type(type = "org.hibernate.type.TextType")
	@Column(name = "title")
	private String title;

	@Lob
	@Type(type = "org.hibernate.type.TextType")
	private String summary;

	@Size(max = 100000)
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	@Column(length = 100000)
	private String body;

	@NotNull
	@JsonView({ JsonViews.Public.class })
	@Column(nullable = false)
	private Instant publishDate;

	@JsonView({ JsonViews.Protected.class })
	private Instant expirationDate;

	@Override
	@JsonView({ JsonViews.Protected.class })
	public boolean isPublished() {
		var inst = Instant.now();
		if (publishDate != null && publishDate.isBefore(inst)) {
			return expirationDate == null || expirationDate.isAfter(inst);
		}
		return false;
	}

	@Override
	@JsonIgnore
	public String getVersionTag() {
		return publishDate != null ? new SimpleDateFormat("yyyy-MM-dd").format(publishDate) : null;
	}
	
	@Override
	public boolean canEqual(Object other) {
		return other instanceof Article;
	}
}