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