AccessionCollect.java

  1. /*
  2.  * Copyright 2019 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 java.util.Set;
  18. import java.util.stream.Collectors;

  19. import javax.persistence.CollectionTable;
  20. import javax.persistence.Column;
  21. import javax.persistence.ElementCollection;
  22. import javax.persistence.Entity;
  23. import javax.persistence.FetchType;
  24. import javax.persistence.JoinColumn;
  25. import javax.persistence.Lob;
  26. import javax.persistence.OneToOne;
  27. import javax.persistence.PrePersist;
  28. import javax.persistence.PreUpdate;
  29. import javax.persistence.Table;
  30. import javax.persistence.Transient;
  31. import javax.persistence.Version;
  32. import javax.validation.constraints.Pattern;
  33. import javax.validation.constraints.Size;

  34. import lombok.Getter;
  35. import lombok.Setter;
  36. import org.apache.commons.lang3.StringUtils;
  37. import org.genesys.blocks.auditlog.annotations.Audited;
  38. import org.genesys.blocks.model.BasicModel;
  39. import org.genesys.blocks.model.SelfCleaning;
  40. import org.hibernate.annotations.Type;
  41. import org.springframework.data.elasticsearch.annotations.Field;
  42. import org.springframework.data.elasticsearch.annotations.FieldType;

  43. import com.fasterxml.jackson.annotation.JsonBackReference;

  44. /**
  45.  * Collecting data
  46.  */
  47. @Entity
  48. @Table(name = "accession_collect")
  49. @Audited
  50. @Getter
  51. @Setter
  52. public class AccessionCollect extends BasicModel implements AccessionRelated, SelfCleaning {

  53.     private static final long serialVersionUID = 6848317825287346724L;

  54.     @Version
  55.     private long version = 0;

  56.     @OneToOne(mappedBy = "coll", optional = false, fetch = FetchType.LAZY, cascade = {})
  57.     @JsonBackReference
  58.     private AccessionId accession;

  59.     @Size(max = 8)
  60.     @Column(name = "collDate", length = 8)
  61.     private String collDate;

  62.     @Size(max = 64)
  63.     @Column(name = "collNumb", length = 64)
  64.     private String collNumb;

  65.     @Size(max = 128)
  66.     @Column(name = "collMissId", length = 128)
  67.     private String collMissId;

  68.     @Column(name = "collCode", nullable = false, length = 128)
  69.     @ElementCollection(fetch = FetchType.LAZY)
  70.     @CollectionTable(name = "accession_collect_code", joinColumns = @JoinColumn(name = "collectId", referencedColumnName = "id"))
  71.     @Field(type = FieldType.Keyword)
  72.     private Set<@Pattern(regexp = "[A-Z]{3}\\d{3,4}") @Size(max = 128) String> collCode;

  73.     @Column(name = "collName", nullable = false, length = 250)
  74.     @ElementCollection(fetch = FetchType.LAZY)
  75.     @CollectionTable(name = "accession_collect_name", joinColumns = @JoinColumn(name = "collectId", referencedColumnName = "id"))
  76.     private Set<@Size(max = 250) String> collName;

  77.     @Column(name = "collInstAddress", nullable = false, length = 128)
  78.     @ElementCollection(fetch = FetchType.LAZY)
  79.     @CollectionTable(name = "accession_collect_instaddr", joinColumns = @JoinColumn(name = "collectId", referencedColumnName = "id"))
  80.     private Set<@Size(max = 128) String> collInstAddress;

  81.     @Column(name = "collSite")
  82.     @Lob
  83.     @Type(type = "org.hibernate.type.TextType")
  84.     private String collSite;

  85.     @Column
  86.     private Integer collSrc;

  87.     @PrePersist
  88.     @PreUpdate
  89.     private void prePersist() {
  90.         trimStringsToNull();

  91.         // remove blank collCode
  92.         if (collCode != null && !collCode.isEmpty()) {
  93.             Set<String> notBlank = collCode.stream().map(str -> StringUtils.trimToNull(str)).filter(str -> str != null).collect(Collectors.toSet());
  94.             collCode.clear();
  95.             collCode.addAll(notBlank);
  96.         }
  97.         // remove blank collName
  98.         if (collName != null && !collName.isEmpty()) {
  99.             Set<String> notBlank = collName.stream().map(str -> StringUtils.trimToNull(str)).filter(str -> str != null).map(str -> StringUtils.abbreviate(str, "...", 250)).collect(Collectors.toSet());
  100.             collName.clear();
  101.             collName.addAll(notBlank);
  102.         }
  103.     }

  104.     @Transient
  105.     public boolean isEmpty() {
  106.         if (StringUtils.isNotBlank(collDate)) {
  107.             return false;
  108.         }

  109.         if (StringUtils.isNotBlank(collNumb)) {
  110.             return false;
  111.         }

  112.         if (StringUtils.isNotBlank(collMissId)) {
  113.             return false;
  114.         }

  115.         if (collName != null && collName.stream().filter(StringUtils::isNotBlank).count() > 0) {
  116.             return false;
  117.         }

  118.         if (collCode != null && collCode.stream().filter(StringUtils::isNotBlank).count() > 0) {
  119.             return false;
  120.         }

  121.         if (collInstAddress != null && collInstAddress.stream().filter(StringUtils::isNotBlank).count() > 0) {
  122.             return false;
  123.         }

  124.         if (StringUtils.isNotBlank(collSite)) {
  125.             return false;
  126.         }

  127.         if (collSrc != null) {
  128.             return false;
  129.         }

  130.         return true;
  131.     }

  132.     @Override
  133.     public boolean canEqual(Object other) {
  134.         return other instanceof AccessionCollect;
  135.     }
  136. }