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.RelatedQueryPager;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  import org.codelibs.fess.opensearch.config.cbean.RelatedQueryCB;
26  import org.codelibs.fess.opensearch.config.exbhv.RelatedQueryBhv;
27  import org.codelibs.fess.opensearch.config.exentity.RelatedQuery;
28  import org.codelibs.fess.util.ComponentUtil;
29  import org.dbflute.cbean.result.PagingResultBean;
30  import org.dbflute.optional.OptionalEntity;
31  
32  import jakarta.annotation.Resource;
33  
34  /**
35   * Service class for managing related query entities.
36   * This service provides operations to retrieve, store, and delete related queries,
37   * which are used to suggest alternative search terms to users.
38   */
39  public class RelatedQueryService extends FessAppService {
40  
41      /**
42       * Default constructor for RelatedQueryService.
43       * This constructor is used by the DI container to create an instance of the service.
44       */
45      public RelatedQueryService() {
46          super();
47      }
48  
49      /**
50       * Behavior class for accessing related query data in the database.
51       * This provides database operations for RelatedQuery entities.
52       */
53      @Resource
54      protected RelatedQueryBhv relatedQueryBhv;
55  
56      /**
57       * Configuration properties for Fess application.
58       * Used to access various configuration settings like paging parameters.
59       */
60      @Resource
61      protected FessConfig fessConfig;
62  
63      /**
64       * Retrieves a paginated list of related queries based on the provided pager parameters.
65       * This method performs a database query with pagination and updates the pager with result information.
66       *
67       * @param relatedQueryPager the pager containing pagination parameters and search conditions
68       * @return a list of RelatedQuery entities matching the search criteria
69       */
70      public List<RelatedQuery> getRelatedQueryList(final RelatedQueryPager relatedQueryPager) {
71  
72          final PagingResultBean<RelatedQuery> relatedQueryList = relatedQueryBhv.selectPage(cb -> {
73              cb.paging(relatedQueryPager.getPageSize(), relatedQueryPager.getCurrentPageNumber());
74              setupListCondition(cb, relatedQueryPager);
75          });
76  
77          // update pager
78          BeanUtil.copyBeanToBean(relatedQueryList, relatedQueryPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
79          relatedQueryPager.setPageNumberList(
80                  relatedQueryList.pageRange(op -> op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger())).createPageNumberList());
81  
82          return relatedQueryList;
83      }
84  
85      /**
86       * Retrieves a specific related query by its unique identifier.
87       *
88       * @param id the unique identifier of the related query to retrieve
89       * @return an OptionalEntity containing the RelatedQuery if found, or empty if not found
90       */
91      public OptionalEntity<RelatedQuery> getRelatedQuery(final String id) {
92          return relatedQueryBhv.selectByPK(id);
93      }
94  
95      /**
96       * Stores (inserts or updates) a related query in the database.
97       * After storing, the related query helper is updated to refresh the cache.
98       *
99       * @param relatedQuery the RelatedQuery entity to store
100      */
101     public void store(final RelatedQuery relatedQuery) {
102 
103         relatedQueryBhv.insertOrUpdate(relatedQuery, op -> op.setRefreshPolicy(Constants.TRUE));
104         ComponentUtil.getRelatedQueryHelper().update();
105     }
106 
107     /**
108      * Deletes a related query from the database.
109      * After deletion, the related query helper is updated to refresh the cache.
110      *
111      * @param relatedQuery the RelatedQuery entity to delete
112      */
113     public void delete(final RelatedQuery relatedQuery) {
114 
115         relatedQueryBhv.delete(relatedQuery, op -> op.setRefreshPolicy(Constants.TRUE));
116         ComponentUtil.getRelatedQueryHelper().update();
117     }
118 
119     /**
120      * Sets up the search conditions for the related query list based on the pager parameters.
121      * This method configures wildcard searches for term and queries fields, and sets up ordering.
122      *
123      * @param cb the condition bean for building the query
124      * @param relatedQueryPager the pager containing search parameters
125      */
126     protected void setupListCondition(final RelatedQueryCB cb, final RelatedQueryPager relatedQueryPager) {
127         if (StringUtil.isNotBlank(relatedQueryPager.term)) {
128             cb.query().setTerm_Wildcard(wrapQuery(relatedQueryPager.term));
129         }
130         if (StringUtil.isNotBlank(relatedQueryPager.queries)) {
131             cb.query().setQueries_Wildcard(wrapQuery(relatedQueryPager.queries));
132         }
133         // TODO Long, Integer, String supported only.
134 
135         // setup condition
136         cb.query().addOrderBy_Term_Asc();
137         cb.query().addOrderBy_CreatedTime_Asc();
138 
139         // search
140 
141     }
142 
143 }