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.bsbhv;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.codelibs.fess.opensearch.config.allcommon.EsAbstractBehavior;
22  import org.codelibs.fess.opensearch.config.allcommon.EsAbstractEntity.RequestOptionCall;
23  import org.codelibs.fess.opensearch.config.bsentity.dbmeta.KeyMatchDbm;
24  import org.codelibs.fess.opensearch.config.cbean.KeyMatchCB;
25  import org.codelibs.fess.opensearch.config.exentity.KeyMatch;
26  import org.dbflute.Entity;
27  import org.dbflute.bhv.readable.CBCall;
28  import org.dbflute.bhv.readable.EntityRowHandler;
29  import org.dbflute.cbean.ConditionBean;
30  import org.dbflute.cbean.result.ListResultBean;
31  import org.dbflute.cbean.result.PagingResultBean;
32  import org.dbflute.exception.IllegalBehaviorStateException;
33  import org.dbflute.optional.OptionalEntity;
34  import org.dbflute.util.DfTypeUtil;
35  import org.opensearch.action.bulk.BulkRequestBuilder;
36  import org.opensearch.action.delete.DeleteRequestBuilder;
37  import org.opensearch.action.index.IndexRequestBuilder;
38  
39  /**
40   * @author ESFlute (using FreeGen)
41   */
42  public abstract class BsKeyMatchBhv extends EsAbstractBehavior<KeyMatch, KeyMatchCB> {
43  
44      // ===================================================================================
45      //                                                                    Control Override
46      //                                                                    ================
47      @Override
48      public String asTableDbName() {
49          return asEsIndexType();
50      }
51  
52      @Override
53      protected String asEsIndex() {
54          return "fess_config.key_match";
55      }
56  
57      @Override
58      public String asEsIndexType() {
59          return "key_match";
60      }
61  
62      @Override
63      public String asEsSearchType() {
64          return "key_match";
65      }
66  
67      @Override
68      public KeyMatchDbm asDBMeta() {
69          return KeyMatchDbm.getInstance();
70      }
71  
72      @Override
73      protected <RESULT extends KeyMatch> RESULT createEntity(Map<String, Object> source, Class<? extends RESULT> entityType) {
74          try {
75              final RESULT result = entityType.newInstance();
76              result.setBoost(DfTypeUtil.toFloat(source.get("boost")));
77              result.setCreatedBy(DfTypeUtil.toString(source.get("createdBy")));
78              result.setCreatedTime(DfTypeUtil.toLong(source.get("createdTime")));
79              result.setMaxSize(DfTypeUtil.toInteger(source.get("maxSize")));
80              result.setQuery(DfTypeUtil.toString(source.get("query")));
81              result.setTerm(DfTypeUtil.toString(source.get("term")));
82              result.setUpdatedBy(DfTypeUtil.toString(source.get("updatedBy")));
83              result.setUpdatedTime(DfTypeUtil.toLong(source.get("updatedTime")));
84              result.setVirtualHost(DfTypeUtil.toString(source.get("virtualHost")));
85              return updateEntity(source, result);
86          } catch (InstantiationException | IllegalAccessException e) {
87              final String msg = "Cannot create a new instance: " + entityType.getName();
88              throw new IllegalBehaviorStateException(msg, e);
89          }
90      }
91  
92      protected <RESULT extends KeyMatch> RESULT updateEntity(Map<String, Object> source, RESULT result) {
93          return result;
94      }
95  
96      // ===================================================================================
97      //                                                                              Select
98      //                                                                              ======
99      public int selectCount(CBCall<KeyMatchCB> cbLambda) {
100         return facadeSelectCount(createCB(cbLambda));
101     }
102 
103     public OptionalEntity<KeyMatch> selectEntity(CBCall<KeyMatchCB> cbLambda) {
104         return facadeSelectEntity(createCB(cbLambda));
105     }
106 
107     protected OptionalEntity<KeyMatch> facadeSelectEntity(KeyMatchCB cb) {
108         return doSelectOptionalEntity(cb, typeOfSelectedEntity());
109     }
110 
111     protected <ENTITY extends KeyMatch> OptionalEntity<ENTITY> doSelectOptionalEntity(KeyMatchCB cb, Class<? extends ENTITY> tp) {
112         return createOptionalEntity(doSelectEntity(cb, tp), cb);
113     }
114 
115     @Override
116     public KeyMatchCB newConditionBean() {
117         return new KeyMatchCB();
118     }
119 
120     @Override
121     protected Entity doReadEntity(ConditionBean cb) {
122         return facadeSelectEntity(downcast(cb)).orElse(null);
123     }
124 
125     public KeyMatch selectEntityWithDeletedCheck(CBCall<KeyMatchCB> cbLambda) {
126         return facadeSelectEntityWithDeletedCheck(createCB(cbLambda));
127     }
128 
129     public OptionalEntity<KeyMatch> selectByPK(String id) {
130         return facadeSelectByPK(id);
131     }
132 
133     protected OptionalEntity<KeyMatch> facadeSelectByPK(String id) {
134         return doSelectOptionalByPK(id, typeOfSelectedEntity());
135     }
136 
137     protected <ENTITY extends KeyMatch> ENTITY doSelectByPK(String id, Class<? extends ENTITY> tp) {
138         return doSelectEntity(xprepareCBAsPK(id), tp);
139     }
140 
141     protected KeyMatchCB xprepareCBAsPK(String id) {
142         assertObjectNotNull("id", id);
143         return newConditionBean().acceptPK(id);
144     }
145 
146     protected <ENTITY extends KeyMatch> OptionalEntity<ENTITY> doSelectOptionalByPK(String id, Class<? extends ENTITY> tp) {
147         return createOptionalEntity(doSelectByPK(id, tp), id);
148     }
149 
150     @Override
151     protected Class<? extends KeyMatch> typeOfSelectedEntity() {
152         return KeyMatch.class;
153     }
154 
155     @Override
156     protected Class<KeyMatch> typeOfHandlingEntity() {
157         return KeyMatch.class;
158     }
159 
160     @Override
161     protected Class<KeyMatchCB> typeOfHandlingConditionBean() {
162         return KeyMatchCB.class;
163     }
164 
165     public ListResultBean<KeyMatch> selectList(CBCall<KeyMatchCB> cbLambda) {
166         return facadeSelectList(createCB(cbLambda));
167     }
168 
169     public PagingResultBean<KeyMatch> selectPage(CBCall<KeyMatchCB> cbLambda) {
170         // #pending same?
171         return (PagingResultBean<KeyMatch>) facadeSelectList(createCB(cbLambda));
172     }
173 
174     public void selectCursor(CBCall<KeyMatchCB> cbLambda, EntityRowHandler<KeyMatch> entityLambda) {
175         facadeSelectCursor(createCB(cbLambda), entityLambda);
176     }
177 
178     public void selectBulk(CBCall<KeyMatchCB> cbLambda, EntityRowHandler<List<KeyMatch>> entityLambda) {
179         delegateSelectBulk(createCB(cbLambda), entityLambda, typeOfSelectedEntity());
180     }
181 
182     // ===================================================================================
183     //                                                                              Update
184     //                                                                              ======
185     public void insert(KeyMatch entity) {
186         doInsert(entity, null);
187     }
188 
189     public void insert(KeyMatch entity, RequestOptionCall<IndexRequestBuilder> opLambda) {
190         entity.asDocMeta().indexOption(opLambda);
191         doInsert(entity, null);
192     }
193 
194     public void update(KeyMatch entity) {
195         doUpdate(entity, null);
196     }
197 
198     public void update(KeyMatch entity, RequestOptionCall<IndexRequestBuilder> opLambda) {
199         entity.asDocMeta().indexOption(opLambda);
200         doUpdate(entity, null);
201     }
202 
203     public void insertOrUpdate(KeyMatch entity) {
204         doInsertOrUpdate(entity, null, null);
205     }
206 
207     public void insertOrUpdate(KeyMatch entity, RequestOptionCall<IndexRequestBuilder> opLambda) {
208         entity.asDocMeta().indexOption(opLambda);
209         doInsertOrUpdate(entity, null, null);
210     }
211 
212     public void delete(KeyMatch entity) {
213         doDelete(entity, null);
214     }
215 
216     public void delete(KeyMatch entity, RequestOptionCall<DeleteRequestBuilder> opLambda) {
217         entity.asDocMeta().deleteOption(opLambda);
218         doDelete(entity, null);
219     }
220 
221     public int queryDelete(CBCall<KeyMatchCB> cbLambda) {
222         return doQueryDelete(createCB(cbLambda), null);
223     }
224 
225     public int[] batchInsert(List<KeyMatch> list) {
226         return batchInsert(list, null, null);
227     }
228 
229     public int[] batchInsert(List<KeyMatch> list, RequestOptionCall<BulkRequestBuilder> call) {
230         return batchInsert(list, call, null);
231     }
232 
233     public int[] batchInsert(List<KeyMatch> list, RequestOptionCall<BulkRequestBuilder> call,
234             RequestOptionCall<IndexRequestBuilder> entityCall) {
235         return doBatchInsert(new BulkList<>(list, call, entityCall), null);
236     }
237 
238     public int[] batchUpdate(List<KeyMatch> list) {
239         return batchUpdate(list, null, null);
240     }
241 
242     public int[] batchUpdate(List<KeyMatch> list, RequestOptionCall<BulkRequestBuilder> call) {
243         return batchUpdate(list, call, null);
244     }
245 
246     public int[] batchUpdate(List<KeyMatch> list, RequestOptionCall<BulkRequestBuilder> call,
247             RequestOptionCall<IndexRequestBuilder> entityCall) {
248         return doBatchUpdate(new BulkList<>(list, call, entityCall), null);
249     }
250 
251     public int[] batchDelete(List<KeyMatch> list) {
252         return batchDelete(list, null, null);
253     }
254 
255     public int[] batchDelete(List<KeyMatch> list, RequestOptionCall<BulkRequestBuilder> call) {
256         return batchDelete(list, call, null);
257     }
258 
259     public int[] batchDelete(List<KeyMatch> list, RequestOptionCall<BulkRequestBuilder> call,
260             RequestOptionCall<IndexRequestBuilder> entityCall) {
261         return doBatchDelete(new BulkList<>(list, call, entityCall), null);
262     }
263 
264     // #pending create, modify, remove
265 }