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.UserDbm;
24  import org.codelibs.fess.opensearch.user.cbean.UserCB;
25  import org.codelibs.fess.opensearch.user.exentity.User;
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 BsUserBhv extends EsAbstractBehavior<User, UserCB> {
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.user";
55      }
56  
57      @Override
58      public String asEsIndexType() {
59          return "user";
60      }
61  
62      @Override
63      public String asEsSearchType() {
64          return "user";
65      }
66  
67      @Override
68      public UserDbm asDBMeta() {
69          return UserDbm.getInstance();
70      }
71  
72      @Override
73      protected <RESULT extends User> RESULT createEntity(Map<String, Object> source, Class<? extends RESULT> entityType) {
74          try {
75              final RESULT result = entityType.newInstance();
76              result.setBusinessCategory(DfTypeUtil.toString(source.get("businessCategory")));
77              result.setCarLicense(DfTypeUtil.toString(source.get("carLicense")));
78              result.setCity(DfTypeUtil.toString(source.get("city")));
79              result.setDepartmentNumber(DfTypeUtil.toString(source.get("departmentNumber")));
80              result.setDescription(DfTypeUtil.toString(source.get("description")));
81              result.setDestinationIndicator(DfTypeUtil.toString(source.get("destinationIndicator")));
82              result.setDisplayName(DfTypeUtil.toString(source.get("displayName")));
83              result.setEmployeeNumber(DfTypeUtil.toString(source.get("employeeNumber")));
84              result.setEmployeeType(DfTypeUtil.toString(source.get("employeeType")));
85              result.setFacsimileTelephoneNumber(DfTypeUtil.toString(source.get("facsimileTelephoneNumber")));
86              result.setGidNumber(DfTypeUtil.toLong(source.get("gidNumber")));
87              result.setGivenName(DfTypeUtil.toString(source.get("givenName")));
88              result.setGroups(toStringArray(source.get("groups")));
89              result.setHomeDirectory(DfTypeUtil.toString(source.get("homeDirectory")));
90              result.setHomePhone(DfTypeUtil.toString(source.get("homePhone")));
91              result.setHomePostalAddress(DfTypeUtil.toString(source.get("homePostalAddress")));
92              result.setInitials(DfTypeUtil.toString(source.get("initials")));
93              result.setInternationaliSDNNumber(DfTypeUtil.toString(source.get("internationaliSDNNumber")));
94              result.setLabeledURI(DfTypeUtil.toString(source.get("labeledURI")));
95              result.setMail(DfTypeUtil.toString(source.get("mail")));
96              result.setMobile(DfTypeUtil.toString(source.get("mobile")));
97              result.setName(DfTypeUtil.toString(source.get("name")));
98              result.setPager(DfTypeUtil.toString(source.get("pager")));
99              result.setPassword(DfTypeUtil.toString(source.get("password")));
100             result.setPhysicalDeliveryOfficeName(DfTypeUtil.toString(source.get("physicalDeliveryOfficeName")));
101             result.setPostOfficeBox(DfTypeUtil.toString(source.get("postOfficeBox")));
102             result.setPostalAddress(DfTypeUtil.toString(source.get("postalAddress")));
103             result.setPostalCode(DfTypeUtil.toString(source.get("postalCode")));
104             result.setPreferredLanguage(DfTypeUtil.toString(source.get("preferredLanguage")));
105             result.setRegisteredAddress(DfTypeUtil.toString(source.get("registeredAddress")));
106             result.setRoles(toStringArray(source.get("roles")));
107             result.setRoomNumber(DfTypeUtil.toString(source.get("roomNumber")));
108             result.setState(DfTypeUtil.toString(source.get("state")));
109             result.setStreet(DfTypeUtil.toString(source.get("street")));
110             result.setSurname(DfTypeUtil.toString(source.get("surname")));
111             result.setTelephoneNumber(DfTypeUtil.toString(source.get("telephoneNumber")));
112             result.setTeletexTerminalIdentifier(DfTypeUtil.toString(source.get("teletexTerminalIdentifier")));
113             result.setTitle(DfTypeUtil.toString(source.get("title")));
114             result.setUidNumber(DfTypeUtil.toLong(source.get("uidNumber")));
115             result.setX121Address(DfTypeUtil.toString(source.get("x121Address")));
116             return updateEntity(source, result);
117         } catch (InstantiationException | IllegalAccessException e) {
118             final String msg = "Cannot create a new instance: " + entityType.getName();
119             throw new IllegalBehaviorStateException(msg, e);
120         }
121     }
122 
123     protected <RESULT extends User> RESULT updateEntity(Map<String, Object> source, RESULT result) {
124         return result;
125     }
126 
127     // ===================================================================================
128     //                                                                              Select
129     //                                                                              ======
130     public int selectCount(CBCall<UserCB> cbLambda) {
131         return facadeSelectCount(createCB(cbLambda));
132     }
133 
134     public OptionalEntity<User> selectEntity(CBCall<UserCB> cbLambda) {
135         return facadeSelectEntity(createCB(cbLambda));
136     }
137 
138     protected OptionalEntity<User> facadeSelectEntity(UserCB cb) {
139         return doSelectOptionalEntity(cb, typeOfSelectedEntity());
140     }
141 
142     protected <ENTITY extends User> OptionalEntity<ENTITY> doSelectOptionalEntity(UserCB cb, Class<? extends ENTITY> tp) {
143         return createOptionalEntity(doSelectEntity(cb, tp), cb);
144     }
145 
146     @Override
147     public UserCB newConditionBean() {
148         return new UserCB();
149     }
150 
151     @Override
152     protected Entity doReadEntity(ConditionBean cb) {
153         return facadeSelectEntity(downcast(cb)).orElse(null);
154     }
155 
156     public User selectEntityWithDeletedCheck(CBCall<UserCB> cbLambda) {
157         return facadeSelectEntityWithDeletedCheck(createCB(cbLambda));
158     }
159 
160     public OptionalEntity<User> selectByPK(String id) {
161         return facadeSelectByPK(id);
162     }
163 
164     protected OptionalEntity<User> facadeSelectByPK(String id) {
165         return doSelectOptionalByPK(id, typeOfSelectedEntity());
166     }
167 
168     protected <ENTITY extends User> ENTITY doSelectByPK(String id, Class<? extends ENTITY> tp) {
169         return doSelectEntity(xprepareCBAsPK(id), tp);
170     }
171 
172     protected UserCB xprepareCBAsPK(String id) {
173         assertObjectNotNull("id", id);
174         return newConditionBean().acceptPK(id);
175     }
176 
177     protected <ENTITY extends User> OptionalEntity<ENTITY> doSelectOptionalByPK(String id, Class<? extends ENTITY> tp) {
178         return createOptionalEntity(doSelectByPK(id, tp), id);
179     }
180 
181     @Override
182     protected Class<? extends User> typeOfSelectedEntity() {
183         return User.class;
184     }
185 
186     @Override
187     protected Class<User> typeOfHandlingEntity() {
188         return User.class;
189     }
190 
191     @Override
192     protected Class<UserCB> typeOfHandlingConditionBean() {
193         return UserCB.class;
194     }
195 
196     public ListResultBean<User> selectList(CBCall<UserCB> cbLambda) {
197         return facadeSelectList(createCB(cbLambda));
198     }
199 
200     public PagingResultBean<User> selectPage(CBCall<UserCB> cbLambda) {
201         // #pending same?
202         return (PagingResultBean<User>) facadeSelectList(createCB(cbLambda));
203     }
204 
205     public void selectCursor(CBCall<UserCB> cbLambda, EntityRowHandler<User> entityLambda) {
206         facadeSelectCursor(createCB(cbLambda), entityLambda);
207     }
208 
209     public void selectBulk(CBCall<UserCB> cbLambda, EntityRowHandler<List<User>> entityLambda) {
210         delegateSelectBulk(createCB(cbLambda), entityLambda, typeOfSelectedEntity());
211     }
212 
213     // ===================================================================================
214     //                                                                              Update
215     //                                                                              ======
216     public void insert(User entity) {
217         doInsert(entity, null);
218     }
219 
220     public void insert(User entity, RequestOptionCall<IndexRequestBuilder> opLambda) {
221         entity.asDocMeta().indexOption(opLambda);
222         doInsert(entity, null);
223     }
224 
225     public void update(User entity) {
226         doUpdate(entity, null);
227     }
228 
229     public void update(User entity, RequestOptionCall<IndexRequestBuilder> opLambda) {
230         entity.asDocMeta().indexOption(opLambda);
231         doUpdate(entity, null);
232     }
233 
234     public void insertOrUpdate(User entity) {
235         doInsertOrUpdate(entity, null, null);
236     }
237 
238     public void insertOrUpdate(User entity, RequestOptionCall<IndexRequestBuilder> opLambda) {
239         entity.asDocMeta().indexOption(opLambda);
240         doInsertOrUpdate(entity, null, null);
241     }
242 
243     public void delete(User entity) {
244         doDelete(entity, null);
245     }
246 
247     public void delete(User entity, RequestOptionCall<DeleteRequestBuilder> opLambda) {
248         entity.asDocMeta().deleteOption(opLambda);
249         doDelete(entity, null);
250     }
251 
252     public int queryDelete(CBCall<UserCB> cbLambda) {
253         return doQueryDelete(createCB(cbLambda), null);
254     }
255 
256     public int[] batchInsert(List<User> list) {
257         return batchInsert(list, null, null);
258     }
259 
260     public int[] batchInsert(List<User> list, RequestOptionCall<BulkRequestBuilder> call) {
261         return batchInsert(list, call, null);
262     }
263 
264     public int[] batchInsert(List<User> list, RequestOptionCall<BulkRequestBuilder> call,
265             RequestOptionCall<IndexRequestBuilder> entityCall) {
266         return doBatchInsert(new BulkList<>(list, call, entityCall), null);
267     }
268 
269     public int[] batchUpdate(List<User> list) {
270         return batchUpdate(list, null, null);
271     }
272 
273     public int[] batchUpdate(List<User> list, RequestOptionCall<BulkRequestBuilder> call) {
274         return batchUpdate(list, call, null);
275     }
276 
277     public int[] batchUpdate(List<User> list, RequestOptionCall<BulkRequestBuilder> call,
278             RequestOptionCall<IndexRequestBuilder> entityCall) {
279         return doBatchUpdate(new BulkList<>(list, call, entityCall), null);
280     }
281 
282     public int[] batchDelete(List<User> list) {
283         return batchDelete(list, null, null);
284     }
285 
286     public int[] batchDelete(List<User> list, RequestOptionCall<BulkRequestBuilder> call) {
287         return batchDelete(list, call, null);
288     }
289 
290     public int[] batchDelete(List<User> list, RequestOptionCall<BulkRequestBuilder> call,
291             RequestOptionCall<IndexRequestBuilder> entityCall) {
292         return doBatchDelete(new BulkList<>(list, call, entityCall), null);
293     }
294 
295     // #pending create, modify, remove
296 }