MaterialSubRequest.java

  1. /**
  2.  * Copyright 2014 Global Crop Diversity Trust
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  **/

  16. package org.genesys.server.model.genesys;

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

  18. import java.text.MessageFormat;
  19. import java.time.Instant;
  20. import java.util.UUID;

  21. import javax.persistence.Column;
  22. import javax.persistence.Entity;
  23. import javax.persistence.Lob;
  24. import javax.persistence.ManyToOne;
  25. import javax.persistence.PrePersist;
  26. import javax.persistence.Table;

  27. import com.fasterxml.jackson.annotation.JsonCreator;
  28. import com.fasterxml.jackson.annotation.JsonValue;
  29. import lombok.Getter;
  30. import lombok.NoArgsConstructor;
  31. import lombok.Setter;
  32. import org.genesys.blocks.model.AuditedVersionedModel;
  33. import org.hibernate.annotations.Type;

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

  36. /**
  37.  * {@link MaterialRequest} is broken down into individual requests to
  38.  * institutes.
  39.  *
  40.  * @author matijaobreza
  41.  *
  42.  */
  43. @Entity
  44. @Table(name = "requestsub")
  45. @Getter
  46. @Setter
  47. @NoArgsConstructor
  48. public class MaterialSubRequest extends AuditedVersionedModel {
  49.    
  50.     private static final long serialVersionUID = 718290325614791811L;

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

  53.         private final int value;

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

  57.         @JsonValue
  58.         public int getValue() {
  59.             return value;
  60.         }

  61.         @JsonCreator
  62.         public static State fromValue(int value) {
  63.             for (State state : values()) {
  64.                 if (state.value == value) {
  65.                     return state;
  66.                 }
  67.             }
  68.             throw new IllegalArgumentException("Unexpected value '" + value + "'");
  69.         }
  70.     }

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

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

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

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

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

  84.     @Column(length = 50)
  85.     private String providerId;

  86.     @Column
  87.     @Lob
  88.     private String providerNote;

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

  92.     private Instant lastReminderDate;

  93.     @PrePersist
  94.     void prepersist() {
  95.         if (this.uuid == null) {
  96.             this.uuid = UUID.randomUUID().toString();
  97.         }
  98.     }

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

  103.     @Override
  104.     public boolean canEqual(Object other) {
  105.         return other instanceof MaterialSubRequest;
  106.     }
  107. }