View Javadoc
1   /*
2    * Copyright 2012-2021 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 javax.annotation.Resource;
21  
22  import org.codelibs.core.beans.util.BeanUtil;
23  import org.codelibs.fess.Constants;
24  import org.codelibs.fess.app.pager.FileAuthPager;
25  import org.codelibs.fess.es.config.cbean.FileAuthenticationCB;
26  import org.codelibs.fess.es.config.exbhv.FileAuthenticationBhv;
27  import org.codelibs.fess.es.config.exentity.FileAuthentication;
28  import org.codelibs.fess.mylasta.direction.FessConfig;
29  import org.codelibs.fess.util.ParameterUtil;
30  import org.dbflute.cbean.result.PagingResultBean;
31  import org.dbflute.optional.OptionalEntity;
32  
33  public class FileAuthenticationService {
34  
35      @Resource
36      protected FileAuthenticationBhv fileAuthenticationBhv;
37  
38      @Resource
39      protected FessConfig fessConfig;
40  
41      public List<FileAuthentication> getFileAuthenticationList(final FileAuthPager fileAuthenticationPager) {
42  
43          final PagingResultBean<FileAuthentication> fileAuthenticationList = fileAuthenticationBhv.selectPage(cb -> {
44              cb.paging(fileAuthenticationPager.getPageSize(), fileAuthenticationPager.getCurrentPageNumber());
45              setupListCondition(cb, fileAuthenticationPager);
46          });
47  
48          // update pager
49          BeanUtil.copyBeanToBean(fileAuthenticationList, fileAuthenticationPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
50          fileAuthenticationPager.setPageNumberList(fileAuthenticationList.pageRange(op -> {
51              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
52          }).createPageNumberList());
53  
54          return fileAuthenticationList;
55      }
56  
57      public OptionalEntity<FileAuthentication> getFileAuthentication(final String id) {
58          return fileAuthenticationBhv.selectByPK(id);
59      }
60  
61      public void store(final FileAuthentication fileAuthentication) {
62          fileAuthentication.setParameters(ParameterUtil.encrypt(fileAuthentication.getParameters()));
63          fileAuthenticationBhv.insertOrUpdate(fileAuthentication, op -> {
64              op.setRefreshPolicy(Constants.TRUE);
65          });
66  
67      }
68  
69      public void delete(final FileAuthentication fileAuthentication) {
70  
71          fileAuthenticationBhv.delete(fileAuthentication, op -> {
72              op.setRefreshPolicy(Constants.TRUE);
73          });
74  
75      }
76  
77      protected void setupListCondition(final FileAuthenticationCB cb, final FileAuthPager fileAuthenticationPager) {
78          if (fileAuthenticationPager.id != null) {
79              cb.query().docMeta().setId_Equal(fileAuthenticationPager.id);
80          }
81          // TODO Long, Integer, String supported only.
82  
83          // setup condition
84          cb.query().addOrderBy_Hostname_Asc();
85  
86          // search
87  
88      }
89  
90      public List<FileAuthentication> getFileAuthenticationList(final String fileConfigId) {
91          return fileAuthenticationBhv.selectList(cb -> {
92              cb.query().setFileConfigId_Equal(fileConfigId);
93              cb.fetchFirst(fessConfig.getPageFileAuthMaxFetchSizeAsInteger());
94          });
95      }
96  }