ImageGallery.java

/*
 * Copyright 2018 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.filerepository.model;

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

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderColumn;
import javax.persistence.Table;

import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.security.model.AclAwareModel;
import org.hibernate.annotations.Type;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

/**
 * {@link ImageGallery} is a collection of ordered {@link RepositoryImage}
 * instances.
 *
 * Gallery has a dedicated "path" in the file repository, where new images are
 * posted. When an image is removed from the Gallery, it may be removed if it is
 * hosted in the Gallery path, but must be kept as-is if it does not share the
 * path.
 *
 * @author mobreza
 */
@Cacheable
@Entity
@Table(name = "repository_gallery")
@Getter
@Setter
public class ImageGallery extends AuditedVersionedModel implements AclAwareModel, Copyable<ImageGallery> {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 6811344310828880973L;

	/**
	 * Each Gallery has a dedicated "path" in the file repository. A gallery "owns"
	 * a unique path in the repository.
	 */
	@Column(nullable = false, unique = true)
	@JsonIgnore
	private String path;

	/**
	 * Gallery title in English.
	 */
	@Column(length = 200)
	private String title;

	/**
	 * Gallery description in English.
	 */
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	private String description;

	/** The images. */
	@ManyToMany(cascade = { CascadeType.REFRESH })
	@JoinTable(name = "repository_gallery_image", joinColumns = @JoinColumn(name = "galleryId"), inverseJoinColumns = @JoinColumn(name = "imageId"))
	@OrderColumn(name = "position")
	@JsonView({ JsonViews.Root.class })
	private List<RepositoryImage> images;

	/** The folder. */
	@OneToOne(cascade = {}, fetch = FetchType.LAZY, optional = false)
	@JoinColumn(unique = true)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "path")
	@JsonIdentityReference(alwaysAsId = true)
	@JsonProperty(access = Access.READ_ONLY)
	private RepositoryFolder folder;


	@Override
	public AclAwareModel aclParentObject() {
		return folder;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.model.Copyable#apply(java.lang.Object)
	 */
	@Override
	public ImageGallery apply(final ImageGallery source) {
		this.active = source.active;
		this.description = source.description;
		this.path = source.path;
		this.title = source.title;

		return this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.model.Copyable#copy()
	 */
	@Override
	public ImageGallery copy() {
		final ImageGallery copy = new ImageGallery();
		copy.apply(this);
		copy.images = new ArrayList<>(this.images);
		return copy;
	}

	@Override
	public boolean canEqual(Object other) {
		return other instanceof ImageGallery;
	}
}