MaterialSubRequest.java

/**
 * Copyright 2014 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.server.model.genesys;

import static org.genesys.server.model.genesys.MaterialSubRequest.State.DRAFT;

import java.text.MessageFormat;
import java.time.Instant;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.hibernate.annotations.Type;

import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonRawValue;

/**
 * {@link MaterialRequest} is broken down into individual requests to
 * institutes.
 *
 * @author matijaobreza
 *
 */
@Entity
@Table(name = "requestsub")
@Getter
@Setter
@NoArgsConstructor
public class MaterialSubRequest extends AuditedVersionedModel {
	
	private static final long serialVersionUID = 718290325614791811L;

	public enum State {
		DRAFT(-1), NOTCONFIRMED(0), CONFIRMED(1), SENT(2), PROCESSING(3), REJECTED(4);

		private final int value;

		State(int state) {
			this.value = state;
		}

		@JsonValue
		public int getValue() {
			return value;
		}

		@JsonCreator
		public static State fromValue(int value) {
			for (State state : values()) {
				if (state.value == value) {
					return state;
				}
			}
			throw new IllegalArgumentException("Unexpected value '" + value + "'");
		}
	}

	@Column
	private int state = DRAFT.getValue();

	@Column(length = 36, unique = true)
	private String uuid;

	@Column(length = 8, nullable = false)
	private String instCode;

	@Column(length = 200, nullable = true)
	private String instEmail;

	@Column(length = 100000)
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	@JsonRawValue
	private String body;

	@Column(length = 50)
	private String providerId;

	@Column
	@Lob
	private String providerNote;

	@ManyToOne(cascade = {}, optional = false)
	@JsonIdentityReference(alwaysAsId = true)
	private MaterialRequest sourceRequest;

	private Instant lastReminderDate;

	@PrePersist
	void prepersist() {
		if (this.uuid == null) {
			this.uuid = UUID.randomUUID().toString();
		}
	}

	@Override
	public String toString() {
		return MessageFormat.format("SubRequest uuid={0} inst={1} body={2}", uuid, instCode, body);
	}

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