View Javadoc
1   /*
2    * Copyright 2012-2017 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.es.log.bsbhv;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.codelibs.fess.es.log.allcommon.EsAbstractBehavior;
22  import org.codelibs.fess.es.log.allcommon.EsAbstractEntity.RequestOptionCall;
23  import org.codelibs.fess.es.log.bsentity.dbmeta.SearchLogDbm;
24  import org.codelibs.fess.es.log.cbean.SearchLogCB;
25  import org.codelibs.fess.es.log.exentity.SearchLog;
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.elasticsearch.action.bulk.BulkRequestBuilder;
36  import org.elasticsearch.action.delete.DeleteRequestBuilder;
37  import org.elasticsearch.action.index.IndexRequestBuilder;
38  
39  /**
40   * @author ESFlute (using FreeGen)
41   */
42  public abstract class BsSearchLogBhv extends EsAbstractBehavior<SearchLog, SearchLogCB> {
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_log";
55      }
56  
57      @Override
58      public String asEsIndexType() {
59          return "search_log";
60      }
61  
62      @Override
63      public String asEsSearchType() {
64          return "search_log";
65      }
66  
67      @Override
68      public SearchLogDbm asDBMeta() {
69          return SearchLogDbm.getInstance();
70      }
71  
72      @Override
73      protected <RESULT extends SearchLog> RESULT createEntity(Map<String, Object> source, Class<? extends RESULT> entityType) {
74          try {
75              final RESULT result = entityType.newInstance();
76              result.setAccessType(DfTypeUtil.toString(source.get("accessType")));
77              result.setUser(DfTypeUtil.toString(source.get("user")));
78              result.setRoles(toStringArray(source.get("roles")));
79              result.setQueryId(DfTypeUtil.toString(source.get("queryId")));
80              result.setClientIp(DfTypeUtil.toString(source.get("clientIp")));
81              result.setHitCount(DfTypeUtil.toLong(source.get("hitCount")));
82              result.setQueryOffset(DfTypeUtil.toInteger(source.get("queryOffset")));
83              result.setQueryPageSize(DfTypeUtil.toInteger(source.get("queryPageSize")));
84              result.setReferer(DfTypeUtil.toString(source.get("referer")));
85              result.setRequestedAt(toLocalDateTime(source.get("requestedAt")));
86              result.setResponseTime(DfTypeUtil.toLong(source.get("responseTime")));
87              result.setQueryTime(DfTypeUtil.toLong(source.get("queryTime")));
88              result.setSearchWord(DfTypeUtil.toString(source.get("searchWord")));
89              result.setUserAgent(DfTypeUtil.toString(source.get("userAgent")));
90              result.setUserInfoId(DfTypeUtil.toString(source.get("userInfoId")));
91              result.setUserSessionId(DfTypeUtil.toString(source.get("userSessionId")));
92              result.setLanguages(DfTypeUtil.toString(source.get("languages")));
93              result.setVirtualHost(DfTypeUtil.toString(source.get("virtualHost")));
94              return updateEntity(source, result);
95          } catch (InstantiationException | IllegalAccessException e) {
96              final String msg = "Cannot create a new instance: " + entityType.getName();
97              throw new IllegalBehaviorStateException(msg, e);
98          }
99      }
100 
101     protected <RESULT extends SearchLog> RESULT updateEntity(Map<String, Object> source, RESULT result) {
102         return result;
103     }
104 
105     // ===================================================================================
106     //                                                                              Select
107     //                                                                              ======
108     public int selectCount(CBCall<SearchLogCB> cbLambda) {
109         return facadeSelectCount(createCB(cbLambda));
110     }
111 
112     public OptionalEntity<SearchLog> selectEntity(CBCall<SearchLogCB> cbLambda) {
113         return facadeSelectEntity(createCB(cbLambda));
114     }
115 
116     protected OptionalEntity<SearchLog> facadeSelectEntity(SearchLogCB cb) {
117         return doSelectOptionalEntity(cb, typeOfSelectedEntity());
118     }
119 
120     protected <ENTITY extends SearchLog> OptionalEntity<ENTITY> doSelectOptionalEntity(SearchLogCB cb, Class<? extends ENTITY> tp) {
121         return createOptionalEntity(doSelectEntity(cb, tp), cb);
122     }
123 
124     @Override
125     public SearchLogCB newConditionBean() {
126         return new SearchLogCB();
127     }
128 
129     @Override
130     protected Entity doReadEntity(ConditionBean cb) {
131         return facadeSelectEntity(downcast(cb)).orElse(null);
132     }
133 
134     public SearchLog selectEntityWithDeletedCheck(CBCall<SearchLogCB> cbLambda) {
135         return facadeSelectEntityWithDeletedCheck(createCB(cbLambda));
136     }
137 
138     public OptionalEntity<SearchLog> selectByPK(String id) {
139         return facadeSelectByPK(id);
140     }
141 
142     protected OptionalEntity<SearchLog> facadeSelectByPK(String id) {
143         return doSelectOptionalByPK(id, typeOfSelectedEntity());
144     }
145 
146     protected <ENTITY extends SearchLog> ENTITY doSelectByPK(String id, Class<? extends ENTITY> tp) {
147         return doSelectEntity(xprepareCBAsPK(id), tp);
148     }
149 
150     protected SearchLogCB xprepareCBAsPK(String id) {
151         assertObjectNotNull("id", id);
152         return newConditionBean().acceptPK(id);
153     }
154 
155     protected <ENTITY extends SearchLog> OptionalEntity<ENTITY> doSelectOptionalByPK(String id, Class<? extends ENTITY> tp) {
156         return createOptionalEntity(doSelectByPK(id, tp), id);
157     }
158 
159     @Override
160     protected Class<? extends SearchLog> typeOfSelectedEntity() {
161         return SearchLog.class;
162     }
163 
164     @Override
165     protected Class<SearchLog> typeOfHandlingEntity() {
166         return SearchLog.class;
167     }
168 
169     @Override
170     protected Class<SearchLogCB> typeOfHandlingConditionBean() {
171         return SearchLogCB.class;
172     }
173 
174     public ListResultBean<SearchLog> selectList(CBCall<SearchLogCB> cbLambda) {
175         return facadeSelectList(createCB(cbLambda));
176     }
177 
178     public PagingResultBean<SearchLog> selectPage(CBCall<SearchLogCB> cbLambda) {
179         // #pending same?
180         return (PagingResultBean<SearchLog>) facadeSelectList(createCB(cbLambda));
181     }
182 
183     public void selectCursor(CBCall<SearchLogCB> cbLambda, EntityRowHandler<SearchLog> entityLambda) {
184         facadeSelectCursor(createCB(cbLambda), entityLambda);
185     }
186 
187     public void selectBulk(CBCall<SearchLogCB> cbLambda, EntityRowHandler<List<SearchLog>> entityLambda) {
188         delegateSelectBulk(createCB(cbLambda), entityLambda, typeOfSelectedEntity());
189     }
190 
191     // ===================================================================================
192     //                                                                              Update
193     //                                                                              ======
194     public void insert(SearchLog entity) {
195         doInsert(entity, null);
196     }
197 
198     public void insert(SearchLog entity, RequestOptionCall<IndexRequestBuilder> opLambda) {
199         entity.asDocMeta().indexOption(opLambda);
200         doInsert(entity, null);
201     }
202 
203     public void update(SearchLog entity) {
204         doUpdate(entity, null);
205     }
206 
207     public void update(SearchLog entity, RequestOptionCall<IndexRequestBuilder> opLambda) {
208         entity.asDocMeta().indexOption(opLambda);
209         doUpdate(entity, null);
210     }
211 
212     public void insertOrUpdate(SearchLog entity) {
213         doInsertOrUpdate(entity, null, null);
214     }
215 
216     public void insertOrUpdate(SearchLog entity, RequestOptionCall<IndexRequestBuilder> opLambda) {
217         entity.asDocMeta().indexOption(opLambda);
218         doInsertOrUpdate(entity, null, null);
219     }
220 
221     public void delete(SearchLog entity) {
222         doDelete(entity, null);
223     }
224 
225     public void delete(SearchLog entity, RequestOptionCall<DeleteRequestBuilder> opLambda) {
226         entity.asDocMeta().deleteOption(opLambda);
227         doDelete(entity, null);
228     }
229 
230     public int queryDelete(CBCall<SearchLogCB> cbLambda) {
231         return doQueryDelete(createCB(cbLambda), null);
232     }
233 
234     public int[] batchInsert(List<SearchLog> list) {
235         return batchInsert(list, null, null);
236     }
237 
238     public int[] batchInsert(List<SearchLog> list, RequestOptionCall<BulkRequestBuilder> call) {
239         return batchInsert(list, call, null);
240     }
241 
242     public int[] batchInsert(List<SearchLog> list, RequestOptionCall<BulkRequestBuilder> call,
243             RequestOptionCall<IndexRequestBuilder> entityCall) {
244         return doBatchInsert(new BulkList<>(list, call, entityCall), null);
245     }
246 
247     public int[] batchUpdate(List<SearchLog> list) {
248         return batchUpdate(list, null, null);
249     }
250 
251     public int[] batchUpdate(List<SearchLog> list, RequestOptionCall<BulkRequestBuilder> call) {
252         return batchUpdate(list, call, null);
253     }
254 
255     public int[] batchUpdate(List<SearchLog> list, RequestOptionCall<BulkRequestBuilder> call,
256             RequestOptionCall<IndexRequestBuilder> entityCall) {
257         return doBatchUpdate(new BulkList<>(list, call, entityCall), null);
258     }
259 
260     public int[] batchDelete(List<SearchLog> list) {
261         return batchDelete(list, null, null);
262     }
263 
264     public int[] batchDelete(List<SearchLog> list, RequestOptionCall<BulkRequestBuilder> call) {
265         return batchDelete(list, call, null);
266     }
267 
268     public int[] batchDelete(List<SearchLog> list, RequestOptionCall<BulkRequestBuilder> call,
269             RequestOptionCall<IndexRequestBuilder> entityCall) {
270         return doBatchDelete(new BulkList<>(list, call, entityCall), null);
271     }
272 
273     // #pending create, modify, remove
274 }