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.app.service;
17  
18  import java.util.List;
19  
20  import javax.annotation.Resource;
21  
22  import org.codelibs.core.beans.util.BeanUtil;
23  import org.codelibs.fess.Constants;
24  import org.codelibs.fess.app.pager.RelatedQueryPager;
25  import org.codelibs.fess.es.config.cbean.RelatedQueryCB;
26  import org.codelibs.fess.es.config.exbhv.RelatedQueryBhv;
27  import org.codelibs.fess.es.config.exentity.RelatedQuery;
28  import org.codelibs.fess.mylasta.direction.FessConfig;
29  import org.codelibs.fess.util.ComponentUtil;
30  import org.dbflute.cbean.result.PagingResultBean;
31  import org.dbflute.optional.OptionalEntity;
32  
33  public class RelatedQueryService {
34  
35      @Resource
36      protected RelatedQueryBhv relatedQueryBhv;
37  
38      @Resource
39      protected FessConfig fessConfig;
40  
41      public List<RelatedQuery> getRelatedQueryList(final RelatedQueryPager relatedQueryPager) {
42  
43          final PagingResultBean<RelatedQuery> relatedQueryList = relatedQueryBhv.selectPage(cb -> {
44              cb.paging(relatedQueryPager.getPageSize(), relatedQueryPager.getCurrentPageNumber());
45              setupListCondition(cb, relatedQueryPager);
46          });
47  
48          // update pager
49          BeanUtil.copyBeanToBean(relatedQueryList, relatedQueryPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
50          relatedQueryPager.setPageNumberList(relatedQueryList.pageRange(op -> op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger()))
51                  .createPageNumberList());
52  
53          return relatedQueryList;
54      }
55  
56      public OptionalEntity<RelatedQuery> getRelatedQuery(final String id) {
57          return relatedQueryBhv.selectByPK(id);
58      }
59  
60      public void store(final RelatedQuery relatedQuery) {
61  
62          relatedQueryBhv.insertOrUpdate(relatedQuery, op -> op.setRefreshPolicy(Constants.TRUE));
63          ComponentUtil.getRelatedQueryHelper().update();
64      }
65  
66      public void delete(final RelatedQuery relatedQuery) {
67  
68          relatedQueryBhv.delete(relatedQuery, op -> op.setRefreshPolicy(Constants.TRUE));
69          ComponentUtil.getRelatedQueryHelper().update();
70      }
71  
72      protected void setupListCondition(final RelatedQueryCB cb, final RelatedQueryPager relatedQueryPager) {
73          if (relatedQueryPager.id != null) {
74              cb.query().docMeta().setId_Equal(relatedQueryPager.id);
75          }
76          // TODO Long, Integer, String supported only.
77  
78          // setup condition
79          cb.query().addOrderBy_Term_Asc();
80          cb.query().addOrderBy_CreatedTime_Asc();
81  
82          // search
83  
84      }
85  
86  }