View Javadoc
1   /*
2    * Copyright 2012-2021 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.core.lang.StringUtil;
24  import org.codelibs.fess.Constants;
25  import org.codelibs.fess.app.pager.KeyMatchPager;
26  import org.codelibs.fess.es.config.cbean.KeyMatchCB;
27  import org.codelibs.fess.es.config.exbhv.KeyMatchBhv;
28  import org.codelibs.fess.es.config.exentity.KeyMatch;
29  import org.codelibs.fess.mylasta.direction.FessConfig;
30  import org.dbflute.cbean.result.PagingResultBean;
31  import org.dbflute.optional.OptionalEntity;
32  
33  public class KeyMatchService extends FessAppService {
34  
35      @Resource
36      protected KeyMatchBhv keyMatchBhv;
37  
38      @Resource
39      protected FessConfig fessConfig;
40  
41      public List<KeyMatch> getKeyMatchList(final KeyMatchPager keyMatchPager) {
42  
43          final PagingResultBean<KeyMatch> keyMatchList = keyMatchBhv.selectPage(cb -> {
44              cb.paging(keyMatchPager.getPageSize(), keyMatchPager.getCurrentPageNumber());
45              setupListCondition(cb, keyMatchPager);
46          });
47  
48          // update pager
49          BeanUtil.copyBeanToBean(keyMatchList, keyMatchPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
50          keyMatchPager.setPageNumberList(keyMatchList.pageRange(op -> {
51              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
52          }).createPageNumberList());
53  
54          return keyMatchList;
55      }
56  
57      public OptionalEntity<KeyMatch> getKeyMatch(final String id) {
58          return keyMatchBhv.selectByPK(id);
59      }
60  
61      public void store(final KeyMatch keyMatch) {
62  
63          keyMatchBhv.insertOrUpdate(keyMatch, op -> {
64              op.setRefreshPolicy(Constants.TRUE);
65          });
66  
67      }
68  
69      public void delete(final KeyMatch keyMatch) {
70  
71          keyMatchBhv.delete(keyMatch, op -> {
72              op.setRefreshPolicy(Constants.TRUE);
73          });
74  
75      }
76  
77      protected void setupListCondition(final KeyMatchCB cb, final KeyMatchPager keyMatchPager) {
78          if (StringUtil.isNotBlank(keyMatchPager.term)) {
79              cb.query().setTerm_Wildcard(wrapQuery(keyMatchPager.term));
80          }
81          if (StringUtil.isNotBlank(keyMatchPager.query)) {
82              cb.query().setQuery_Wildcard(wrapQuery(keyMatchPager.query));
83          }
84          // TODO Long, Integer, String supported only.
85  
86          // setup condition
87          cb.query().addOrderBy_Term_Asc();
88  
89          // search
90  
91      }
92  
93  }