RepositoryImage.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 javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Getter;
import lombok.Setter;
import org.genesys.filerepository.metadata.ImageMetadata;

import com.fasterxml.jackson.annotation.JsonGetter;

/**
 * An {@link RepositoryImage} is an graphics file in one of the supported image
 * formats (PNG and JPG). It extends the {@link RepositoryFile} by including
 * image-specific metadata defined in {@link ImageMetadata}.
 *
 * @author mobreza
 */
@Cacheable
@Entity
@Table(name="repository_image")
@Getter
@Setter
public class RepositoryImage extends RepositoryFile implements ImageMetadata {

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

	/** The width. */
	@Column
	private int width;

	/** The height. */
	@Column
	private int height;

	/** The orientation. */
	@Enumerated(EnumType.ORDINAL)
	private Orientation orientation;

	/** Thumbnail path */
	@Transient
	private String thumbnailPath;

	/*
	 * (non-Javadoc)
	 * @see org.genesys.filerepository.model.RepositoryFile#prePersist()
	 */
	@Override
	@PrePersist
	@PreUpdate
	protected void prePersist() {
		// Don't forget the superclass!
		super.prePersist();
		updateOrientation();
	}

	@PostLoad
	@PostUpdate
	@PostPersist
	protected void postLoad() {
		super.postLoad();
		if (getUuid() != null) {
			this.thumbnailPath = getStorageFolder() + "/" + getUuid();
		}
	}

	/**
	 * Sets the width.
	 *
	 * @param width the new width
	 */
	public void setWidth(final int width) {
		this.width = width;
		updateOrientation();
	}

	/**
	 * Sets the height.
	 *
	 * @param height the new height
	 */
	public void setHeight(final int height) {
		this.height = height;
		updateOrientation();
	}

	/**
	 * Update orientation.
	 */
	private void updateOrientation() {
		orientation = width > height ? Orientation.LANDSCAPE : Orientation.PORTRAIT;
	}

	/**
	 * Get the path where thumbnails of this image are stored.
	 *
	 * @return Thumbnails path
	 */
	@JsonGetter
	public String getThumbnailPath() {
		return this.thumbnailPath;
	}

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