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