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.WebAuthPager;
23  import org.codelibs.fess.mylasta.direction.FessConfig;
24  import org.codelibs.fess.opensearch.config.cbean.WebAuthenticationCB;
25  import org.codelibs.fess.opensearch.config.exbhv.WebAuthenticationBhv;
26  import org.codelibs.fess.opensearch.config.exentity.WebAuthentication;
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 web authentication configurations.
35   * Provides CRUD operations for web authentication settings including
36   * listing, retrieving, storing, and deleting authentication configurations.
37   */
38  public class WebAuthenticationService {
39  
40      /**
41       * Default constructor.
42       */
43      public WebAuthenticationService() {
44          // Default constructor
45      }
46  
47      /**
48       * Behavior class for web authentication operations.
49       */
50      @Resource
51      protected WebAuthenticationBhv webAuthenticationBhv;
52  
53      /**
54       * Fess configuration settings.
55       */
56      @Resource
57      protected FessConfig fessConfig;
58  
59      /**
60       * Gets a paginated list of web authentications based on the provided pager.
61       *
62       * @param webAuthenticationPager The pager containing pagination and search criteria
63       * @return List of web authentication configurations
64       */
65      public List<WebAuthentication> getWebAuthenticationList(final WebAuthPager webAuthenticationPager) {
66  
67          final PagingResultBean<WebAuthentication> webAuthenticationList = webAuthenticationBhv.selectPage(cb -> {
68              cb.paging(webAuthenticationPager.getPageSize(), webAuthenticationPager.getCurrentPageNumber());
69              setupListCondition(cb, webAuthenticationPager);
70          });
71  
72          // update pager
73          BeanUtil.copyBeanToBean(webAuthenticationList, webAuthenticationPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
74          webAuthenticationPager.setPageNumberList(webAuthenticationList.pageRange(op -> {
75              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
76          }).createPageNumberList());
77  
78          return webAuthenticationList;
79      }
80  
81      /**
82       * Gets a web authentication configuration by its ID.
83       *
84       * @param id The ID of the web authentication configuration
85       * @return Optional containing the web authentication if found
86       */
87      public OptionalEntity<WebAuthentication> getWebAuthentication(final String id) {
88          return webAuthenticationBhv.selectByPK(id);
89      }
90  
91      /**
92       * Stores a web authentication configuration.
93       * Parameters are encrypted before storage.
94       *
95       * @param webAuthentication The web authentication configuration to store
96       */
97      public void store(final WebAuthentication webAuthentication) {
98          webAuthentication.setParameters(ParameterUtil.encrypt(webAuthentication.getParameters()));
99          webAuthenticationBhv.insertOrUpdate(webAuthentication, op -> {
100             op.setRefreshPolicy(Constants.TRUE);
101         });
102 
103     }
104 
105     /**
106      * Deletes a web authentication configuration.
107      *
108      * @param webAuthentication The web authentication configuration to delete
109      */
110     public void delete(final WebAuthentication webAuthentication) {
111 
112         webAuthenticationBhv.delete(webAuthentication, op -> {
113             op.setRefreshPolicy(Constants.TRUE);
114         });
115 
116     }
117 
118     /**
119      * Sets up the list condition for querying web authentications.
120      *
121      * @param cb The condition bean for the query
122      * @param webAuthenticationPager The pager containing search criteria
123      */
124     protected void setupListCondition(final WebAuthenticationCB cb, final WebAuthPager webAuthenticationPager) {
125         if (webAuthenticationPager.id != null) {
126             cb.query().docMeta().setId_Equal(webAuthenticationPager.id);
127         }
128         // TODO Long, Integer, String supported only.
129         // setup condition
130         cb.query().addOrderBy_Hostname_Asc();
131         cb.query().addOrderBy_WebConfigId_Asc();
132 
133         // search
134 
135     }
136 
137     /**
138      * Gets a list of web authentications for a specific web configuration.
139      *
140      * @param webConfigId The ID of the web configuration
141      * @return List of web authentication configurations for the specified web config
142      */
143     public List<WebAuthentication> getWebAuthenticationList(final String webConfigId) {
144         return webAuthenticationBhv.selectList(cb -> {
145             cb.query().setWebConfigId_Equal(webConfigId);
146             cb.fetchFirst(fessConfig.getPageWebAuthMaxFetchSizeAsInteger());
147         });
148     }
149 
150 }