RepositoryFolder.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.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

import javax.persistence.Cacheable;
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.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.UuidModel;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.blocks.security.model.AclObjectIdentity;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.service.impl.PathValidator;
import org.hibernate.annotations.Type;
import org.springframework.data.domain.Sort;

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

/**
 * <b>Repository Folder</b> represents a location (path) of files in the
 * repository. It is immutable and it's <code>path</code> cannot be modified.
 * 
 * @since 1.1-SNAPSHOT
 */
@Cacheable
@Entity
@Table(name = "repository_folder")
@Getter
@Setter
public class RepositoryFolder extends UuidModel implements AclAwareModel {

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

	/** Default sort order */
	public static final Sort DEFAULT_SORT = Sort.by("name");

	/**
	 * Reference to parent Folder. Root folders have this set to null. This
	 * establishes hierarchical structure of folders.
	 * 
	 * Because paths are immutable, this cannot be updated.
	 */
	@ManyToOne(cascade = {}, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(updatable = false)
	@JsonIgnore
	private RepositoryFolder parent;

	/**
	 * List of sub-folders.
	 */
	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "parent")
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "path")
	@JsonIdentityReference(alwaysAsId = true)
	@JsonView({ JsonViews.Root.class })
	private List<RepositoryFolder> children;

	/** List of files in this folder. */
	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "folder")
	@JsonIgnore
	private List<RepositoryFile> files;

	/** {@code ImageGallery} for this folder */
	@OneToOne(cascade = {}, fetch = FetchType.LAZY, optional = true, mappedBy = "folder")
	@JsonIgnore
	private ImageGallery gallery;
	
	/**
	 * The name of the folder within parent Folder. It is immutable and must not be
	 * updated.
	 */
	@Column(name = "name", nullable = false, updatable = false)
	private String name;

	/**
	 * Unique path in the repository. It is immutable and must not be updated. It is
	 * based on the parent Folder path + name.
	 */
	@Column(name = "path", nullable = false, unique = true, updatable = false)
	private String path;

	/**
	 * A human-friendly folder title, not to be confused with {@link #name} above.
	 */
	@Column
	private String title;

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

	/** The folder path. */
	@Transient
	private volatile Path folderPath;

	/** The parent ACL OID */
	@JsonIgnore
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	private AclObjectIdentity parentOid;

	/**
	 * Pre persist.
	 */
	@PrePersist
	protected void prePersist() {
		if (uuid == null) {
			uuid = UUID.randomUUID();
		}
		this.path = this.parent == null ? "/" + this.name : this.parent.getFolderPath().resolve(this.name).normalize().toAbsolutePath().toString();
	}

	/**
	 * The parent folder is generally the parent object for any folder.
	 *
	 * @return the ACL parent object
	 */
	@Override
	public AclAwareModel aclParentObject() {
		return this.parent;
	}
	
	@Override
	public AclObjectIdentity aclParentObjectIdentity() {
		return this.parentOid;
	}
	
	/**
	 * Sets the folder name.
	 *
	 * @param name the new name
	 * @throws InvalidRepositoryPathException if name contains illegal characters
	 */
	public void setName(String name) throws InvalidRepositoryPathException {
		PathValidator.checkValidFolderName(name);
		this.name = name;
	}

	/**
	 * Gets the folder path.
	 *
	 * @return the folder path
	 */
	public Path getFolderPath() {
		synchronized (this) {
			if (folderPath == null) {
				this.folderPath = Paths.get(this.path);
			}
		}
		return this.folderPath;
	}

	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.model.VersionedModel#toString()
	 */
	@Override
	public String toString() {
		return "Folder id=" + getId() + " path=" + this.path;
	}

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