View Javadoc
1   /*
2    * Copyright (C) 2024 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.persistence;
7   
8   import com.fasterxml.jackson.annotation.JsonProperty;
9   import jakarta.persistence.Column;
10  import jakarta.persistence.Entity;
11  import jakarta.persistence.EntityListeners;
12  import jakarta.persistence.EnumType;
13  import jakarta.persistence.Enumerated;
14  import jakarta.persistence.GeneratedValue;
15  import jakarta.persistence.GenerationType;
16  import jakarta.persistence.Id;
17  import jakarta.persistence.Table;
18  import jakarta.validation.Valid;
19  import jakarta.validation.constraints.NotNull;
20  import java.io.Serializable;
21  import java.time.OffsetDateTime;
22  import java.util.ArrayList;
23  import java.util.List;
24  import org.hibernate.annotations.Type;
25  import org.springframework.format.annotation.DateTimeFormat;
26  import org.tailormap.api.admin.model.TaskSchedule;
27  import org.tailormap.api.persistence.listener.EntityEventPublisher;
28  
29  /** SearchIndex is a table that stores the metadata for search indexes for a feature type. */
30  @Entity
31  @Table(name = "search_index")
32  @EntityListeners(EntityEventPublisher.class)
33  public class SearchIndex implements Serializable {
34    @Id
35    @GeneratedValue(strategy = GenerationType.IDENTITY)
36    private Long id;
37  
38    @NotNull private String name;
39  
40    private Long featureTypeId;
41  
42    /** List of attribute names that were used when building the search index. */
43    @JsonProperty("searchFieldsUsed")
44    @Type(value = io.hypersistence.utils.hibernate.type.json.JsonBinaryType.class)
45    @Column(columnDefinition = "jsonb")
46    @Valid
47    private List<String> searchFieldsUsed = new ArrayList<>();
48  
49    /** List of attribute names for display that were used when building the search index. */
50    @JsonProperty("searchDisplayFieldsUsed")
51    @Type(value = io.hypersistence.utils.hibernate.type.json.JsonBinaryType.class)
52    @Column(columnDefinition = "jsonb")
53    @Valid
54    private List<String> searchDisplayFieldsUsed = new ArrayList<>();
55  
56    @Column(columnDefinition = "text")
57    private String comment;
58  
59    /** Date and time of last index creation. */
60    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
61    @Valid
62    @JsonProperty("lastIndexed")
63    private OffsetDateTime lastIndexed;
64  
65    @Valid
66    @JsonProperty("schedule")
67    @Type(value = io.hypersistence.utils.hibernate.type.json.JsonBinaryType.class)
68    @Column(columnDefinition = "jsonb")
69    private TaskSchedule schedule;
70  
71    @Enumerated(EnumType.STRING)
72    @NotNull
73    @Column(columnDefinition = "varchar(8) default 'INITIAL'")
74    private SearchIndex.Status status = SearchIndex.Status.INITIAL;
75  
76    public Long getId() {
77      return id;
78    }
79  
80    public SearchIndex setId(Long id) {
81      this.id = id;
82      return this;
83    }
84  
85    public String getName() {
86      return name;
87    }
88  
89    public SearchIndex setName(String name) {
90      this.name = name;
91      return this;
92    }
93  
94    public Long getFeatureTypeId() {
95      return featureTypeId;
96    }
97  
98    public SearchIndex setFeatureTypeId(Long featureTypeId) {
99      this.featureTypeId = featureTypeId;
100     return this;
101   }
102 
103   public List<String> getSearchFieldsUsed() {
104     return searchFieldsUsed;
105   }
106 
107   public SearchIndex setSearchFieldsUsed(List<String> searchFieldsUsed) {
108     this.searchFieldsUsed = searchFieldsUsed;
109     return this;
110   }
111 
112   public List<String> getSearchDisplayFieldsUsed() {
113     return searchDisplayFieldsUsed;
114   }
115 
116   public SearchIndex setSearchDisplayFieldsUsed(List<String> searchDisplayFieldsUsed) {
117     this.searchDisplayFieldsUsed = searchDisplayFieldsUsed;
118     return this;
119   }
120 
121   public String getComment() {
122     return comment;
123   }
124 
125   public SearchIndex setComment(String comment) {
126     this.comment = comment;
127     return this;
128   }
129 
130   public OffsetDateTime getLastIndexed() {
131     return lastIndexed;
132   }
133 
134   public SearchIndex setLastIndexed(OffsetDateTime lastIndexed) {
135     this.lastIndexed = lastIndexed;
136     return this;
137   }
138 
139   public Status getStatus() {
140     return status;
141   }
142 
143   public SearchIndex setStatus(Status status) {
144     this.status = status;
145     return this;
146   }
147 
148   public @Valid TaskSchedule getSchedule() {
149     return schedule;
150   }
151 
152   public SearchIndex setSchedule(@Valid TaskSchedule schedule) {
153     this.schedule = schedule;
154     return this;
155   }
156 
157   public enum Status {
158     INITIAL("initial"),
159     INDEXING("indexing"),
160     INDEXED("indexed"),
161     ERROR("error");
162     private final String value;
163 
164     Status(String value) {
165       this.value = value;
166     }
167 
168     public static SearchIndex.Status fromValue(String value) {
169       for (SearchIndex.Status status : SearchIndex.Status.values()) {
170         if (status.value.equals(value)) {
171           return status;
172         }
173       }
174       throw new IllegalArgumentException("Unexpected value '%s'".formatted(value));
175     }
176 
177     public String getValue() {
178       return value;
179     }
180 
181     @Override
182     public String toString() {
183       return String.valueOf(value);
184     }
185   }
186 }