MaterialRequest.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 java.text.MessageFormat;
import java.time.Instant;
import java.util.List;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

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.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@Entity
@Table(name = "request")
@JsonIdentityInfo(scope = MaterialRequest.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
@Getter
@Setter
@NoArgsConstructor
public class MaterialRequest extends AuditedVersionedModel {
	
	private static final long serialVersionUID = 4552573253693001837L;
	public static final int DRAFT = -1;
	public static final int NOTVALIDATED = 0;
	public static final int VALIDATED = 1;
	public static final int DISPATCHED = 2;

	@Column
	private int state = DRAFT;

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

	@Size(max = 200)
	@NotNull
	@Column(length = 200, nullable = false)
	private String email;

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

	@Size(max = 32)
	@Column(length = 32, nullable = true)
	private String pid;

	@OneToMany(cascade = {}, mappedBy = "sourceRequest")
	@JsonProperty(value = "subrequests")
	private List<MaterialSubRequest> subRequests;

	private Instant lastReminderDate;

	@Column(length = 100)
	@Size(max = 100)
	private String origin;

	@Column(length = 100)
	@Size(max = 100)
	private String sid;

	@Column(length = 10)
	@Size(min = 2, max = 10)
	private String language;

	@Column(name = "internal")
	private boolean internalRequest = false;

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

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

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