GenesysAttachment.java

/*
 * Copyright 2025 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.gringlobal.model.integration;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.genesys.blocks.model.EmptyModel;
import org.gringlobal.model.Inventory;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import java.util.Objects;
import java.util.UUID;

/**
 * An attachment in Genesys.
 */
@Entity
@Table(name = "util_genesys_attachment")
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class GenesysAttachment extends EmptyModel {

	private static final long serialVersionUID = 1084529673075679745L;

	public enum ImageSyncList {
		/** GGCE attachment to check */
		LOCAL,
		/** Genesys attachment to check */
		REMOTE,
		/** Does not exist in Genesys */
		imageUpload,
		/** Does not exist locally */
		imageDownload,
		/** Local bytes have changed */
		imageUpdate,
		/** Requires metadata update */
		metadataUpdate,
		/** Synchronized between GGCE and Genesys */
		uploaded,
	}

	public enum ImageSyncStatus {
		PENDING,
		SYNCHRONIZED,
		FAILED
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", columnDefinition = "int")
	private Long id;

	@ManyToOne(cascade = {}, optional = false)
	@JoinColumn(name = "inventory_id", updatable = false)
	private Inventory inventory;

	private UUID uuid;

	@Column(name = "version")
	private Integer attachVersion;

	@Column(name = "content_type")
	private String contentType;

	private String path;

	private String extension;

	@Column(name = "original_filename")
	private String originalFilename;

	private String title;

	@Lob
	private String subject;

	@Lob
	private String description;

	private String creator;

	private String created;

	@Column(name = "rights_holder")
	private String rightsHolder;

	@Column(name = "access_rights")
	private String accessRights;

	private String license;

	@Lob
	@Column(name = "bibliographic_citation")
	private String bibliographicCitation;

	private String md5;

	@Enumerated(EnumType.STRING)
	private ImageSyncList type;

	@Enumerated(EnumType.STRING)
	private ImageSyncStatus status;

	@Lob
	@Column(name = "error_message")
	private String errorMessage;

	@PrePersist
	protected void prePersist() {
		if (status == ImageSyncStatus.SYNCHRONIZED) {
			errorMessage = null;
		}
	}

	@Override
	public Long getId() {
		return id;
	}

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

	// Exclude uuid and version for comparison with Genesys image metadata
	public boolean equalTo(Object o) {
		if (this == o) return true;
		if (o == null || !canEqual(o)) return false;
		GenesysAttachment dto = (GenesysAttachment) o;
		return Objects.equals(contentType, dto.contentType) && Objects.equals(path, dto.path) && Objects.equals(extension, dto.extension) 
			&& Objects.equals(originalFilename, dto.originalFilename) && Objects.equals(title, dto.title) && Objects.equals(subject, dto.subject) 
			&& Objects.equals(description, dto.description) && Objects.equals(creator, dto.creator) && Objects.equals(created, dto.created) 
			&& Objects.equals(rightsHolder, dto.rightsHolder) && Objects.equals(accessRights, dto.accessRights) && Objects.equals(license, dto.license) 
			&& Objects.equals(bibliographicCitation, dto.bibliographicCitation) && Objects.equals(md5, dto.md5);
	}

}