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.app.service;
17  
18  import java.util.List;
19  
20  import org.codelibs.core.beans.util.BeanUtil;
21  import org.codelibs.core.lang.StringUtil;
22  import org.codelibs.fess.Constants;
23  import org.codelibs.fess.app.pager.KeyMatchPager;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  import org.codelibs.fess.opensearch.config.cbean.KeyMatchCB;
26  import org.codelibs.fess.opensearch.config.exbhv.KeyMatchBhv;
27  import org.codelibs.fess.opensearch.config.exentity.KeyMatch;
28  import org.dbflute.cbean.result.PagingResultBean;
29  import org.dbflute.optional.OptionalEntity;
30  
31  import jakarta.annotation.Resource;
32  
33  /**
34   * Service class for KeyMatch.
35   */
36  public class KeyMatchService extends FessAppService {
37  
38      /** The KeyMatch behavior. */
39      @Resource
40      protected KeyMatchBhv keyMatchBhv;
41  
42      /**
43       * Default constructor.
44       */
45      public KeyMatchService() {
46          super();
47      }
48  
49      /** The Fess config. */
50      @Resource
51      protected FessConfig fessConfig;
52  
53      /**
54       * Get a list of key matches.
55       *
56       * @param keyMatchPager Pager for key matches.
57       * @return A list of key matches.
58       */
59      public List<KeyMatch> getKeyMatchList(final KeyMatchPager keyMatchPager) {
60  
61          final PagingResultBean<KeyMatch> keyMatchList = keyMatchBhv.selectPage(cb -> {
62              cb.paging(keyMatchPager.getPageSize(), keyMatchPager.getCurrentPageNumber());
63              setupListCondition(cb, keyMatchPager);
64          });
65  
66          // update pager
67          BeanUtil.copyBeanToBean(keyMatchList, keyMatchPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
68          keyMatchPager.setPageNumberList(keyMatchList.pageRange(op -> {
69              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
70          }).createPageNumberList());
71  
72          return keyMatchList;
73      }
74  
75      /**
76       * Get a key match.
77       *
78       * @param id The ID of the key match.
79       * @return An optional entity of the key match.
80       */
81      public OptionalEntity<KeyMatch> getKeyMatch(final String id) {
82          return keyMatchBhv.selectByPK(id);
83      }
84  
85      /**
86       * Store a key match.
87       *
88       * @param keyMatch The key match to store.
89       */
90      public void store(final KeyMatch keyMatch) {
91  
92          keyMatchBhv.insertOrUpdate(keyMatch, op -> {
93              op.setRefreshPolicy(Constants.TRUE);
94          });
95  
96      }
97  
98      /**
99       * Delete a key match.
100      *
101      * @param keyMatch The key match to delete.
102      */
103     public void delete(final KeyMatch keyMatch) {
104 
105         keyMatchBhv.delete(keyMatch, op -> {
106             op.setRefreshPolicy(Constants.TRUE);
107         });
108 
109     }
110 
111     /**
112      * Set up list conditions.
113      *
114      * @param cb The condition bean.
115      * @param keyMatchPager The pager for key matches.
116      */
117     protected void setupListCondition(final KeyMatchCB cb, final KeyMatchPager keyMatchPager) {
118         if (StringUtil.isNotBlank(keyMatchPager.term)) {
119             cb.query().setTerm_Wildcard(wrapQuery(keyMatchPager.term));
120         }
121         if (StringUtil.isNotBlank(keyMatchPager.query)) {
122             cb.query().setQuery_Wildcard(wrapQuery(keyMatchPager.query));
123         }
124         // TODO Long, Integer, String supported only.
125 
126         // setup condition
127         cb.query().addOrderBy_Term_Asc();
128 
129         // search
130 
131     }
132 
133 }