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