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.FileConfigPager;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  import org.codelibs.fess.opensearch.config.cbean.FileConfigCB;
26  import org.codelibs.fess.opensearch.config.exbhv.FileAuthenticationBhv;
27  import org.codelibs.fess.opensearch.config.exbhv.FileConfigBhv;
28  import org.codelibs.fess.opensearch.config.exentity.FileConfig;
29  import org.codelibs.fess.util.ParameterUtil;
30  import org.dbflute.cbean.result.ListResultBean;
31  import org.dbflute.cbean.result.PagingResultBean;
32  import org.dbflute.optional.OptionalEntity;
33  
34  import jakarta.annotation.Resource;
35  
36  /**
37   * Service class for managing file configuration operations.
38   * This service provides CRUD operations for file crawler configurations,
39   * including retrieval, storage, deletion, and search functionality.
40   * It handles pagination and integrates with the file authentication system.
41   */
42  public class FileConfigService extends FessAppService {
43  
44      /**
45       * Default constructor for file configuration service.
46       * Creates a new instance with default values.
47       */
48      public FileConfigService() {
49          super();
50      }
51  
52      /**
53       * Behavior class for file configuration database operations.
54       * Provides access to the file configuration entity operations.
55       */
56      @Resource
57      protected FileConfigBhv fileConfigBhv;
58  
59      /**
60       * Behavior class for file authentication database operations.
61       * Manages authentication configurations associated with file configurations.
62       */
63      @Resource
64      protected FileAuthenticationBhv fileAuthenticationBhv;
65  
66      /**
67       * Fess configuration object providing access to application settings.
68       * Used for retrieving pagination and other configuration parameters.
69       */
70      @Resource
71      protected FessConfig fessConfig;
72  
73      /**
74       * Retrieves a paginated list of file configurations based on the provided pager criteria.
75       * This method applies search conditions from the pager and updates the pager with
76       * pagination information including page numbers and result counts.
77       *
78       * @param fileConfigPager the pager containing search criteria and pagination settings
79       * @return a list of file configurations matching the criteria
80       */
81      public List<FileConfig> getFileConfigList(final FileConfigPager fileConfigPager) {
82  
83          final PagingResultBean<FileConfig> fileConfigList = fileConfigBhv.selectPage(cb -> {
84              cb.paging(fileConfigPager.getPageSize(), fileConfigPager.getCurrentPageNumber());
85              setupListCondition(cb, fileConfigPager);
86          });
87  
88          // update pager
89          BeanUtil.copyBeanToBean(fileConfigList, fileConfigPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
90          fileConfigPager.setPageNumberList(fileConfigList.pageRange(op -> {
91              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
92          }).createPageNumberList());
93  
94          return fileConfigList;
95      }
96  
97      /**
98       * Deletes a file configuration and its associated authentication records.
99       * This method removes the file configuration from the database and also
100      * deletes all related file authentication entries.
101      *
102      * @param fileConfig the file configuration to be deleted
103      */
104     public void delete(final FileConfig fileConfig) {
105 
106         final String fileConfigId = fileConfig.getId();
107 
108         fileConfigBhv.delete(fileConfig, op -> {
109             op.setRefreshPolicy(Constants.TRUE);
110         });
111 
112         fileAuthenticationBhv.queryDelete(cb -> {
113             cb.query().setFileConfigId_Equal(fileConfigId);
114         });
115     }
116 
117     /**
118      * Retrieves a file configuration by its unique identifier.
119      *
120      * @param id the unique identifier of the file configuration
121      * @return an OptionalEntity containing the file configuration if found, empty otherwise
122      */
123     public OptionalEntity<FileConfig> getFileConfig(final String id) {
124         return fileConfigBhv.selectByPK(id);
125     }
126 
127     /**
128      * Retrieves a file configuration by its name.
129      * If multiple configurations exist with the same name, returns the first one
130      * ordered by sort order.
131      *
132      * @param name the name of the file configuration to retrieve
133      * @return an OptionalEntity containing the file configuration if found, empty otherwise
134      */
135     public OptionalEntity<FileConfig> getFileConfigByName(final String name) {
136         final ListResultBean<FileConfig> list = fileConfigBhv.selectList(cb -> {
137             cb.query().setName_Equal(name);
138             cb.query().addOrderBy_SortOrder_Asc();
139         });
140         if (list.isEmpty()) {
141             return OptionalEntity.empty();
142         }
143         return OptionalEntity.of(list.get(0));
144     }
145 
146     /**
147      * Stores a file configuration in the database.
148      * This method encrypts the configuration parameters before saving and
149      * performs an insert or update operation based on whether the configuration exists.
150      *
151      * @param fileConfig the file configuration to be stored
152      */
153     public void store(final FileConfig fileConfig) {
154         fileConfig.setConfigParameter(ParameterUtil.encrypt(fileConfig.getConfigParameter()));
155         fileConfigBhv.insertOrUpdate(fileConfig, op -> {
156             op.setRefreshPolicy(Constants.TRUE);
157         });
158     }
159 
160     /**
161      * Sets up search conditions for the file configuration list query.
162      * This method applies various filter conditions based on the pager parameters
163      * including name, paths, and description filters with wildcard and phrase matching.
164      *
165      * @param cb the condition bean for building the database query
166      * @param fileConfigPager the pager containing search filter criteria
167      */
168     protected void setupListCondition(final FileConfigCB cb, final FileConfigPager fileConfigPager) {
169         if (StringUtil.isNotBlank(fileConfigPager.name)) {
170             cb.query().setName_Wildcard(wrapQuery(fileConfigPager.name));
171         }
172         if (StringUtil.isNotBlank(fileConfigPager.paths)) {
173             cb.query().setPaths_Wildcard(wrapQuery(fileConfigPager.paths));
174         }
175         if (StringUtil.isNotBlank(fileConfigPager.description)) {
176             if (fileConfigPager.description.startsWith("*")) {
177                 cb.query().setDescription_Wildcard(fileConfigPager.description);
178             } else if (fileConfigPager.description.endsWith("*")) {
179                 cb.query().setDescription_Prefix(fileConfigPager.description.replaceAll("\\*$", StringUtil.EMPTY));
180             } else {
181                 cb.query().setDescription_MatchPhrase(fileConfigPager.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 }