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