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.WebConfigPager;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  import org.codelibs.fess.opensearch.config.cbean.WebConfigCB;
26  import org.codelibs.fess.opensearch.config.exbhv.RequestHeaderBhv;
27  import org.codelibs.fess.opensearch.config.exbhv.WebAuthenticationBhv;
28  import org.codelibs.fess.opensearch.config.exbhv.WebConfigBhv;
29  import org.codelibs.fess.opensearch.config.exentity.WebConfig;
30  import org.codelibs.fess.util.ParameterUtil;
31  import org.dbflute.cbean.result.ListResultBean;
32  import org.dbflute.cbean.result.PagingResultBean;
33  import org.dbflute.optional.OptionalEntity;
34  
35  import jakarta.annotation.Resource;
36  
37  /**
38   * Service class for managing web crawling configurations.
39   * Provides CRUD operations for web configuration settings including
40   * listing, retrieving, storing, and deleting web crawling configurations.
41   */
42  public class WebConfigService extends FessAppService {
43  
44      /**
45       * Default constructor.
46       */
47      public WebConfigService() {
48          super();
49      }
50  
51      /**
52       * Behavior class for web configuration operations.
53       */
54      @Resource
55      protected WebConfigBhv webConfigBhv;
56  
57      /**
58       * Behavior class for web authentication operations.
59       */
60      @Resource
61      protected WebAuthenticationBhv webAuthenticationBhv;
62  
63      /**
64       * Behavior class for request header operations.
65       */
66      @Resource
67      protected RequestHeaderBhv requestHeaderBhv;
68  
69      /**
70       * Fess configuration settings.
71       */
72      @Resource
73      protected FessConfig fessConfig;
74  
75      /**
76       * Gets a paginated list of web configurations based on the provided pager.
77       *
78       * @param webConfigPager The pager containing pagination and search criteria
79       * @return List of web configuration objects
80       */
81      public List<WebConfig> getWebConfigList(final WebConfigPager webConfigPager) {
82  
83          final PagingResultBean<WebConfig> webConfigList = webConfigBhv.selectPage(cb -> {
84              cb.paging(webConfigPager.getPageSize(), webConfigPager.getCurrentPageNumber());
85              setupListCondition(cb, webConfigPager);
86          });
87  
88          // update pager
89          BeanUtil.copyBeanToBean(webConfigList, webConfigPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
90          webConfigPager.setPageNumberList(webConfigList.pageRange(op -> {
91              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
92          }).createPageNumberList());
93  
94          return webConfigList;
95      }
96  
97      /**
98       * Deletes a web configuration and all its related data.
99       * This includes removing associated web authentications and request headers.
100      *
101      * @param webConfig The web configuration to delete
102      */
103     public void delete(final WebConfig webConfig) {
104 
105         final String webConfigId = webConfig.getId();
106 
107         webConfigBhv.delete(webConfig, op -> {
108             op.setRefreshPolicy(Constants.TRUE);
109         });
110 
111         webAuthenticationBhv.queryDelete(cb -> {
112             cb.query().setWebConfigId_Equal(webConfigId);
113         });
114 
115         requestHeaderBhv.queryDelete(cb -> {
116             cb.query().setWebConfigId_Equal(webConfigId);
117         });
118     }
119 
120     /**
121      * Gets a web configuration by its ID.
122      *
123      * @param id The ID of the web configuration
124      * @return Optional containing the web configuration if found
125      */
126     public OptionalEntity<WebConfig> getWebConfig(final String id) {
127         return webConfigBhv.selectByPK(id);
128     }
129 
130     /**
131      * Gets a web configuration by its name.
132      * If multiple configurations have the same name, returns the first one ordered by sort order.
133      *
134      * @param name The name of the web configuration
135      * @return Optional containing the web configuration if found
136      */
137     public OptionalEntity<WebConfig> getWebConfigByName(final String name) {
138         final ListResultBean<WebConfig> list = webConfigBhv.selectList(cb -> {
139             cb.query().setName_Equal(name);
140             cb.query().addOrderBy_SortOrder_Asc();
141         });
142         if (list.isEmpty()) {
143             return OptionalEntity.empty();
144         }
145         return OptionalEntity.of(list.get(0));
146     }
147 
148     /**
149      * Stores a web configuration.
150      * Configuration parameters are encrypted before storage.
151      *
152      * @param webConfig The web configuration to store
153      */
154     public void store(final WebConfig webConfig) {
155         webConfig.setConfigParameter(ParameterUtil.encrypt(webConfig.getConfigParameter()));
156         webConfigBhv.insertOrUpdate(webConfig, op -> {
157             op.setRefreshPolicy(Constants.TRUE);
158         });
159     }
160 
161     /**
162      * Sets up the list condition for querying web configurations.
163      * Applies search filters based on pager criteria such as name, URLs, and description.
164      *
165      * @param cb The condition bean for the query
166      * @param webConfigPager The pager containing search criteria
167      */
168     protected void setupListCondition(final WebConfigCB cb, final WebConfigPager webConfigPager) {
169         if (StringUtil.isNotBlank(webConfigPager.name)) {
170             cb.query().setName_Wildcard(wrapQuery(webConfigPager.name));
171         }
172         if (StringUtil.isNotBlank(webConfigPager.urls)) {
173             cb.query().setUrls_Wildcard(wrapQuery(webConfigPager.urls));
174         }
175         if (StringUtil.isNotBlank(webConfigPager.description)) {
176             if (webConfigPager.description.startsWith("*")) {
177                 cb.query().setDescription_Wildcard(webConfigPager.description);
178             } else if (webConfigPager.description.endsWith("*")) {
179                 cb.query().setDescription_Prefix(webConfigPager.description.replaceAll("\\*$", StringUtil.EMPTY));
180             } else {
181                 cb.query().setDescription_MatchPhrase(webConfigPager.description);
182             }
183         }
184         // TODO Long, Integer, String supported only.
185 
186         // setup condition
187         cb.query().addOrderBy_SortOrder_Asc();
188         cb.query().addOrderBy_Name_Asc();
189 
190         // search
191 
192     }
193 
194 }