View Javadoc
1   /*
2    * Copyright 2012-2017 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.es.user.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.elasticsearch.action.delete.DeleteRequestBuilder;
28  import org.elasticsearch.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  
48      // ===================================================================================
49      //                                                                            Doc Meta
50      //                                                                            ========
51      public DocMeta asDocMeta() {
52          if (docMeta == null) {
53              docMeta = new DocMeta();
54          }
55          return docMeta;
56      }
57  
58      // ===================================================================================
59      //                                                                 Modified Properties
60      //                                                                 ===================
61      public Set<String> mymodifiedProperties() {
62          return __modifiedProperties.getPropertyNames();
63      }
64  
65      public void mymodifyProperty(String propertyName) {
66          registerModifiedProperty(propertyName);
67      }
68  
69      public void mymodifyPropertyCancel(String propertyName) {
70          __modifiedProperties.remove(propertyName);
71      }
72  
73      public void clearModifiedInfo() {
74          __modifiedProperties.clear();
75      }
76  
77      public boolean hasModification() {
78          return !__modifiedProperties.isEmpty();
79      }
80  
81      protected EntityModifiedProperties newModifiedProperties() {
82          return new EntityModifiedProperties();
83      }
84  
85      protected void registerModifiedProperty(String propertyName) {
86          __modifiedProperties.addPropertyName(propertyName);
87          registerSpecifiedProperty(propertyName); // synchronize if exists, basically for user's manual call
88      }
89  
90      public void modifiedToSpecified() {
91          if (__modifiedProperties.isEmpty()) {
92              return; // basically no way when called in Framework (because called when SpecifyColumn exists)
93          }
94          __specifiedProperties = newModifiedProperties();
95          __specifiedProperties.accept(__modifiedProperties);
96      }
97  
98      // ===================================================================================
99      //                                                                Specified Properties
100     //                                                                ====================
101     public Set<String> myspecifiedProperties() {
102         if (__specifiedProperties != null) {
103             return __specifiedProperties.getPropertyNames();
104         }
105         return DfCollectionUtil.emptySet();
106     }
107 
108     public void myspecifyProperty(String propertyName) {
109         registerSpecifiedProperty(propertyName);
110     }
111 
112     public void myspecifyPropertyCancel(String propertyName) {
113         if (__specifiedProperties != null) {
114             __specifiedProperties.remove(propertyName);
115         }
116     }
117 
118     public void clearSpecifiedInfo() {
119         if (__specifiedProperties != null) {
120             __specifiedProperties.clear();
121         }
122     }
123 
124     protected void checkSpecifiedProperty(String propertyName) {
125         FunCustodial.checkSpecifiedProperty(this, propertyName, __specifiedProperties);
126     }
127 
128     protected void registerSpecifiedProperty(String propertyName) { // basically called by modified property registration
129         if (__specifiedProperties != null) { // normally false, true if e.g. setting after selected
130             __specifiedProperties.addPropertyName(propertyName);
131         }
132     }
133 
134     // ===================================================================================
135     //                                                                          Unique Key
136     //                                                                          ==========
137     @Override
138     public boolean hasPrimaryKeyValue() {
139         return asDocMeta().id() != null;
140     }
141 
142     protected EntityUniqueDrivenProperties newUniqueDrivenProperties() {
143         return new EntityUniqueDrivenProperties();
144     }
145 
146     @Override
147     public Set<String> myuniqueDrivenProperties() {
148         return __uniqueDrivenProperties.getPropertyNames();
149     }
150 
151     @Override
152     public void myuniqueByProperty(String propertyName) {
153         __uniqueDrivenProperties.addPropertyName(propertyName);
154     }
155 
156     @Override
157     public void myuniqueByPropertyCancel(String propertyName) {
158         __uniqueDrivenProperties.remove(propertyName);
159     }
160 
161     @Override
162     public void clearUniqueDrivenInfo() {
163         __uniqueDrivenProperties.clear();
164     }
165 
166     // ===================================================================================
167     //                                                                     Birthplace Mark
168     //                                                                     ===============
169     @Override
170     public void markAsSelect() {
171     }
172 
173     @Override
174     public boolean createdBySelect() {
175         return false;
176     }
177 
178     // ===================================================================================
179     //                                                                      Classification
180     //                                                                      ==============
181     @Override
182     public void myunlockUndefinedClassificationAccess() {
183     }
184 
185     @Override
186     public boolean myundefinedClassificationAccessAllowed() {
187         return false;
188     }
189 
190     // ===================================================================================
191     //                                                                        Empty String
192     //                                                                        ============
193     protected String convertEmptyToNull(String value) {
194         return (value != null && value.length() == 0) ? null : value;
195     }
196 
197     // ===================================================================================
198     //                                                                              Source
199     //                                                                              ======
200     public abstract Map<String, Object> toSource();
201 
202     // ===================================================================================
203     //                                                                      Basic Override
204     //                                                                      ==============
205     // #pending hashCode(), equals()
206     @Override
207     public int instanceHash() {
208         return super.hashCode();
209     }
210 
211     @Override
212     public String toString() {
213         return getClass().getSimpleName() + ":" + doBuildColumnString(", ") + "@" + Integer.toHexString(hashCode());
214     }
215 
216     protected abstract String doBuildColumnString(String dm);
217 
218     @Override
219     public String toStringWithRelation() { // #pending
220         return toString();
221     }
222 
223     @Override
224     public String buildDisplayString(String name, boolean column, boolean relation) { // #pending
225         return toString();
226     }
227 
228     // ===================================================================================
229     //                                                                        Assist Class
230     //                                                                        ============
231     public class DocMeta implements Serializable {
232 
233         private static final long serialVersionUID = 1L;
234 
235         protected String id;
236 
237         protected Long version;
238 
239         private transient RequestOptionCall<IndexRequestBuilder> indexOption;
240 
241         private transient RequestOptionCall<DeleteRequestBuilder> deleteOption;
242 
243         public DocMeta id(String id) {
244             this.id = id;
245             myuniqueByProperty("_id");
246             return this;
247         }
248 
249         public String id() {
250             return id;
251         }
252 
253         public DocMeta version(Long version) {
254             this.version = version;
255             return this;
256         }
257 
258         public Long version() {
259             return version;
260         }
261 
262         public DocMeta indexOption(RequestOptionCall<IndexRequestBuilder> builder) {
263             this.indexOption = builder;
264             return this;
265         }
266 
267         public RequestOptionCall<IndexRequestBuilder> indexOption() {
268             return indexOption;
269         }
270 
271         public DocMeta deleteOption(RequestOptionCall<DeleteRequestBuilder> builder) {
272             this.deleteOption = builder;
273             return this;
274         }
275 
276         public RequestOptionCall<DeleteRequestBuilder> deleteOption() {
277             return deleteOption;
278         }
279     }
280 
281     @FunctionalInterface
282     public interface RequestOptionCall<OP> {
283         void callback(OP op);
284     }
285 }