ActivityPost.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.FetchType;
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.Copyable;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.Publishable;
import org.genesys.filerepository.model.RepositoryImage;
import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Document;

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

	private static final long serialVersionUID = 8690395020204070378L;

	@Size(max = 500)
	@NotNull
	@Column(name = "title", nullable = false, length = 500)
	private String title;

	/** Post body in HTML */
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	private String body;

	/** Summary in Markdown format */
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	private String summary;
	
	@NotNull
	@JsonView({ JsonViews.Public.class })
	@Column(nullable = false)
	private Instant publishDate;

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

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "imageId", unique = false)
	private RepositoryImage coverImage;

	public Instant getPostDate() {
		return this.getCreatedDate();
	}

	@Override
	public String toString() {
		return "ActivityPost id=" + getId() + " title=" + title;
	}

	@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 ActivityPost;
	}
}