View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.opensearch.config.allcommon;
17  
18  import java.io.Serializable;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import org.dbflute.Entity;
23  import org.dbflute.FunCustodial;
24  import org.dbflute.dbmeta.accessory.EntityModifiedProperties;
25  import org.dbflute.dbmeta.accessory.EntityUniqueDrivenProperties;
26  import org.dbflute.util.DfCollectionUtil;
27  import org.opensearch.action.delete.DeleteRequestBuilder;
28  import org.opensearch.action.index.IndexRequestBuilder;
29  
30  /**
31   * @author ESFlute (using FreeGen)
32   */
33  public abstract class EsAbstractEntity implements Entity, Serializable, Cloneable {
34  
35      // ===================================================================================
36      //                                                                          Definition
37      //                                                                          ==========
38      private static final long serialVersionUID = 1L;
39  
40      // ===================================================================================
41      //                                                                           Attribute
42      //                                                                           =========
43      protected DocMeta docMeta;
44      protected final EntityUniqueDrivenProperties __uniqueDrivenProperties = newUniqueDrivenProperties();
45      protected final EntityModifiedProperties __modifiedProperties = newModifiedProperties();
46      protected EntityModifiedProperties __specifiedProperties;
47      protected boolean __createdBySelect;
48  
49      // ===================================================================================
50      //                                                                            Doc Meta
51      //                                                                            ========
52      public DocMeta asDocMeta() {
53          if (docMeta == null) {
54              docMeta = new DocMeta();
55          }
56          return docMeta;
57      }
58  
59      // ===================================================================================
60      //                                                                 Modified Properties
61      //                                                                 ===================
62      public Set<String> mymodifiedProperties() {
63          return __modifiedProperties.getPropertyNames();
64      }
65  
66      public void mymodifyProperty(String propertyName) {
67          registerModifiedProperty(propertyName);
68      }
69  
70      public void mymodifyPropertyCancel(String propertyName) {
71          __modifiedProperties.remove(propertyName);
72      }
73  
74      public void clearModifiedInfo() {
75          __modifiedProperties.clear();
76      }
77  
78      public boolean hasModification() {
79          return !__modifiedProperties.isEmpty();
80      }
81  
82      protected EntityModifiedProperties newModifiedProperties() {
83          return new EntityModifiedProperties();
84      }
85  
86      protected void registerModifiedProperty(String propertyName) {
87          __modifiedProperties.addPropertyName(propertyName);
88          registerSpecifiedProperty(propertyName); // synchronize if exists, basically for user's manual call
89      }
90  
91      public void modifiedToSpecified() {
92          if (__modifiedProperties.isEmpty()) {
93              return; // basically no way when called in Framework (because called when SpecifyColumn exists)
94          }
95          __specifiedProperties = newModifiedProperties();
96          __specifiedProperties.accept(__modifiedProperties);
97      }
98  
99      // ===================================================================================
100     //                                                                Specified Properties
101     //                                                                ====================
102     public Set<String> myspecifiedProperties() {
103         if (__specifiedProperties != null) {
104             return __specifiedProperties.getPropertyNames();
105         }
106         return DfCollectionUtil.emptySet();
107     }
108 
109     public void myspecifyProperty(String propertyName) {
110         registerSpecifiedProperty(propertyName);
111     }
112 
113     public void myspecifyPropertyCancel(String propertyName) {
114         if (__specifiedProperties != null) {
115             __specifiedProperties.remove(propertyName);
116         }
117     }
118 
119     public void clearSpecifiedInfo() {
120         if (__specifiedProperties != null) {
121             __specifiedProperties.clear();
122         }
123     }
124 
125     protected void checkSpecifiedProperty(String propertyName) {
126         FunCustodial.checkSpecifiedProperty(this, propertyName, __specifiedProperties);
127     }
128 
129     protected void registerSpecifiedProperty(String propertyName) { // basically called by modified property registration
130         if (__specifiedProperties != null) { // normally false, true if e.g. setting after selected
131             __specifiedProperties.addPropertyName(propertyName);
132         }
133     }
134 
135     // ===================================================================================
136     //                                                                          Unique Key
137     //                                                                          ==========
138     @Override
139     public boolean hasPrimaryKeyValue() {
140         return asDocMeta().id() != null;
141     }
142 
143     protected EntityUniqueDrivenProperties newUniqueDrivenProperties() {
144         return new EntityUniqueDrivenProperties();
145     }
146 
147     @Override
148     public Set<String> myuniqueDrivenProperties() {
149         return __uniqueDrivenProperties.getPropertyNames();
150     }
151 
152     @Override
153     public void myuniqueByProperty(String propertyName) {
154         __uniqueDrivenProperties.addPropertyName(propertyName);
155     }
156 
157     @Override
158     public void myuniqueByPropertyCancel(String propertyName) {
159         __uniqueDrivenProperties.remove(propertyName);
160     }
161 
162     @Override
163     public void clearUniqueDrivenInfo() {
164         __uniqueDrivenProperties.clear();
165     }
166 
167     // ===================================================================================
168     //                                                                      Classification
169     //                                                                      ==============
170     @Override
171     public void myunlockUndefinedClassificationAccess() {
172     }
173 
174     @Override
175     public boolean myundefinedClassificationAccessAllowed() {
176         return false;
177     }
178 
179     // ===================================================================================
180     //                                                                     Birthplace Mark
181     //                                                                     ===============
182     @Override
183     public void markAsSelect() {
184         __createdBySelect = true;
185     }
186 
187     @Override
188     public boolean createdBySelect() {
189         return __createdBySelect;
190     }
191 
192     public void clearMarkAsSelect() {
193         __createdBySelect = false;
194     }
195 
196     // ===================================================================================
197     //                                                                        Empty String
198     //                                                                        ============
199     protected String convertEmptyToNull(String value) {
200         return (value != null && value.length() == 0) ? null : value;
201     }
202 
203     // ===================================================================================
204     //                                                                              Source
205     //                                                                              ======
206     public abstract Map<String, Object> toSource();
207 
208     // ===================================================================================
209     //                                                                      Basic Override
210     //                                                                      ==============
211     // #pending hashCode(), equals()
212     @Override
213     public int instanceHash() {
214         return super.hashCode();
215     }
216 
217     @Override
218     public String toString() {
219         return getClass().getSimpleName() + ":" + doBuildColumnString(", ") + "@" + Integer.toHexString(hashCode());
220     }
221 
222     protected abstract String doBuildColumnString(String dm);
223 
224     @Override
225     public String toStringWithRelation() { // #pending
226         return toString();
227     }
228 
229     @Override
230     public String buildDisplayString(String name, boolean column, boolean relation) { // #pending
231         return toString();
232     }
233 
234     // ===================================================================================
235     //                                                                        Assist Class
236     //                                                                        ============
237     public class DocMeta implements Serializable {
238 
239         private static final long serialVersionUID = 1L;
240 
241         protected String id;
242 
243         protected Long version;
244 
245         protected Long seqNo;
246 
247         protected Long primaryTerm;
248 
249         private transient RequestOptionCall<IndexRequestBuilder> indexOption;
250 
251         private transient RequestOptionCall<DeleteRequestBuilder> deleteOption;
252 
253         public DocMeta id(String id) {
254             this.id = id;
255             myuniqueByProperty("_id");
256             return this;
257         }
258 
259         public String id() {
260             return id;
261         }
262 
263         public DocMeta version(Long version) {
264             this.version = version;
265             return this;
266         }
267 
268         public Long version() {
269             return version;
270         }
271 
272         public DocMeta seqNo(Long seqNo) {
273             this.seqNo = seqNo;
274             return this;
275         }
276 
277         public Long seqNo() {
278             return seqNo;
279         }
280 
281         public DocMeta primaryTerm(Long primaryTerm) {
282             this.primaryTerm = primaryTerm;
283             return this;
284         }
285 
286         public Long primaryTerm() {
287             return primaryTerm;
288         }
289 
290         public DocMeta indexOption(RequestOptionCall<IndexRequestBuilder> builder) {
291             this.indexOption = builder;
292             return this;
293         }
294 
295         public RequestOptionCall<IndexRequestBuilder> indexOption() {
296             return indexOption;
297         }
298 
299         public DocMeta deleteOption(RequestOptionCall<DeleteRequestBuilder> builder) {
300             this.deleteOption = builder;
301             return this;
302         }
303 
304         public RequestOptionCall<DeleteRequestBuilder> deleteOption() {
305             return deleteOption;
306         }
307     }
308 
309     @FunctionalInterface
310     public interface RequestOptionCall<OP> {
311         void callback(OP op);
312     }
313 }