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.fess.Constants;
22  import org.codelibs.fess.app.pager.FileAuthPager;
23  import org.codelibs.fess.mylasta.direction.FessConfig;
24  import org.codelibs.fess.opensearch.config.cbean.FileAuthenticationCB;
25  import org.codelibs.fess.opensearch.config.exbhv.FileAuthenticationBhv;
26  import org.codelibs.fess.opensearch.config.exentity.FileAuthentication;
27  import org.codelibs.fess.util.ParameterUtil;
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 managing file authentication configurations.
35   * This service provides operations for retrieving, storing, and deleting
36   * file authentication settings used by the Fess search engine.
37   */
38  public class FileAuthenticationService {
39  
40      /**
41       * Default constructor for file authentication service.
42       * Creates a new instance with default values.
43       */
44      public FileAuthenticationService() {
45          // Default constructor
46      }
47  
48      /**
49       * Behavior class for file authentication database operations.
50       */
51      @Resource
52      protected FileAuthenticationBhv fileAuthenticationBhv;
53  
54      /**
55       * Configuration settings for the Fess application.
56       */
57      @Resource
58      protected FessConfig fessConfig;
59  
60      /**
61       * Retrieves a paginated list of file authentication configurations.
62       *
63       * @param fileAuthenticationPager the pager containing pagination settings and search criteria
64       * @return a list of file authentication configurations for the current page
65       */
66      public List<FileAuthentication> getFileAuthenticationList(final FileAuthPager fileAuthenticationPager) {
67  
68          final PagingResultBean<FileAuthentication> fileAuthenticationList = fileAuthenticationBhv.selectPage(cb -> {
69              cb.paging(fileAuthenticationPager.getPageSize(), fileAuthenticationPager.getCurrentPageNumber());
70              setupListCondition(cb, fileAuthenticationPager);
71          });
72  
73          // update pager
74          BeanUtil.copyBeanToBean(fileAuthenticationList, fileAuthenticationPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
75          fileAuthenticationPager.setPageNumberList(fileAuthenticationList.pageRange(op -> {
76              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
77          }).createPageNumberList());
78  
79          return fileAuthenticationList;
80      }
81  
82      /**
83       * Retrieves a specific file authentication configuration by its ID.
84       *
85       * @param id the unique identifier of the file authentication configuration
86       * @return an OptionalEntity containing the file authentication if found, empty otherwise
87       */
88      public OptionalEntity<FileAuthentication> getFileAuthentication(final String id) {
89          return fileAuthenticationBhv.selectByPK(id);
90      }
91  
92      /**
93       * Stores a file authentication configuration.
94       * The parameters are encrypted before storage for security.
95       *
96       * @param fileAuthentication the file authentication configuration to store
97       */
98      public void store(final FileAuthentication fileAuthentication) {
99          fileAuthentication.setParameters(ParameterUtil.encrypt(fileAuthentication.getParameters()));
100         fileAuthenticationBhv.insertOrUpdate(fileAuthentication, op -> {
101             op.setRefreshPolicy(Constants.TRUE);
102         });
103 
104     }
105 
106     /**
107      * Deletes a file authentication configuration from the system.
108      *
109      * @param fileAuthentication the file authentication configuration to delete
110      */
111     public void delete(final FileAuthentication fileAuthentication) {
112 
113         fileAuthenticationBhv.delete(fileAuthentication, op -> {
114             op.setRefreshPolicy(Constants.TRUE);
115         });
116 
117     }
118 
119     /**
120      * Sets up the search conditions for retrieving file authentication configurations.
121      * This method configures the query conditions and ordering for the database query.
122      *
123      * @param cb the condition bean for building the query
124      * @param fileAuthenticationPager the pager containing search criteria
125      */
126     protected void setupListCondition(final FileAuthenticationCB cb, final FileAuthPager fileAuthenticationPager) {
127         if (fileAuthenticationPager.id != null) {
128             cb.query().docMeta().setId_Equal(fileAuthenticationPager.id);
129         }
130         // TODO Long, Integer, String supported only.
131 
132         // setup condition
133         cb.query().addOrderBy_Hostname_Asc();
134 
135         // search
136 
137     }
138 
139     /**
140      * Retrieves all file authentication configurations associated with a specific file configuration.
141      *
142      * @param fileConfigId the ID of the file configuration to retrieve authentications for
143      * @return a list of file authentication configurations for the specified file configuration
144      */
145     public List<FileAuthentication> getFileAuthenticationList(final String fileConfigId) {
146         return fileAuthenticationBhv.selectList(cb -> {
147             cb.query().setFileConfigId_Equal(fileConfigId);
148             cb.fetchFirst(fessConfig.getPageFileAuthMaxFetchSizeAsInteger());
149         });
150     }
151 }