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.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;

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

/**
 * 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
 */
@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;

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

	/*
	 * (non-Javadoc)
	 * @see org.genesys.filerepository.metadata.ImageMetadata#getWidth()
	 */
	@Override
	public Integer getWidth() {
		return width;
	}

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

	/*
	 * (non-Javadoc)
	 * @see org.genesys.filerepository.metadata.ImageMetadata#getHeight()
	 */
	@Override
	public Integer getHeight() {
		return height;
	}

	/**
	 * 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
	 */
	public String getThumbnailPath() {
		if (getUuid() == null) {
			return null;
		}
		return getStorageFolder() + "/" + getUuid();
	}

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