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.core.lang.StringUtil;
24  import org.codelibs.fess.Constants;
25  import org.codelibs.fess.app.pager.FileConfigPager;
26  import org.codelibs.fess.es.config.cbean.FileConfigCB;
27  import org.codelibs.fess.es.config.exbhv.FileAuthenticationBhv;
28  import org.codelibs.fess.es.config.exbhv.FileConfigBhv;
29  import org.codelibs.fess.es.config.exentity.FileConfig;
30  import org.codelibs.fess.mylasta.direction.FessConfig;
31  import org.codelibs.fess.util.ParameterUtil;
32  import org.dbflute.cbean.result.ListResultBean;
33  import org.dbflute.cbean.result.PagingResultBean;
34  import org.dbflute.optional.OptionalEntity;
35  
36  public class FileConfigService extends FessAppService {
37  
38      @Resource
39      protected FileConfigBhv fileConfigBhv;
40  
41      @Resource
42      protected FileAuthenticationBhv fileAuthenticationBhv;
43  
44      @Resource
45      protected FessConfig fessConfig;
46  
47      public List<FileConfig> getFileConfigList(final FileConfigPager fileConfigPager) {
48  
49          final PagingResultBean<FileConfig> fileConfigList = fileConfigBhv.selectPage(cb -> {
50              cb.paging(fileConfigPager.getPageSize(), fileConfigPager.getCurrentPageNumber());
51              setupListCondition(cb, fileConfigPager);
52          });
53  
54          // update pager
55          BeanUtil.copyBeanToBean(fileConfigList, fileConfigPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
56          fileConfigPager.setPageNumberList(fileConfigList.pageRange(op -> {
57              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
58          }).createPageNumberList());
59  
60          return fileConfigList;
61      }
62  
63      public void delete(final FileConfig fileConfig) {
64  
65          final String fileConfigId = fileConfig.getId();
66  
67          fileConfigBhv.delete(fileConfig, op -> {
68              op.setRefreshPolicy(Constants.TRUE);
69          });
70  
71          fileAuthenticationBhv.queryDelete(cb -> {
72              cb.query().setFileConfigId_Equal(fileConfigId);
73          });
74      }
75  
76      public OptionalEntity<FileConfig> getFileConfig(final String id) {
77          return fileConfigBhv.selectByPK(id);
78      }
79  
80      public OptionalEntity<FileConfig> getFileConfigByName(final String name) {
81          final ListResultBean<FileConfig> list = fileConfigBhv.selectList(cb -> {
82              cb.query().setName_Equal(name);
83              cb.query().addOrderBy_SortOrder_Asc();
84          });
85          if (list.isEmpty()) {
86              return OptionalEntity.empty();
87          }
88          return OptionalEntity.of(list.get(0));
89      }
90  
91      public void store(final FileConfig fileConfig) {
92          fileConfig.setConfigParameter(ParameterUtil.encrypt(fileConfig.getConfigParameter()));
93          fileConfigBhv.insertOrUpdate(fileConfig, op -> {
94              op.setRefreshPolicy(Constants.TRUE);
95          });
96      }
97  
98      protected void setupListCondition(final FileConfigCB cb, final FileConfigPager fileConfigPager) {
99          if (StringUtil.isNotBlank(fileConfigPager.name)) {
100             cb.query().setName_Wildcard(wrapQuery(fileConfigPager.name));
101         }
102         if (StringUtil.isNotBlank(fileConfigPager.paths)) {
103             cb.query().setPaths_Wildcard(wrapQuery(fileConfigPager.paths));
104         }
105         if (StringUtil.isNotBlank(fileConfigPager.description)) {
106             if (fileConfigPager.description.startsWith("*")) {
107                 cb.query().setDescription_Wildcard(fileConfigPager.description);
108             } else if (fileConfigPager.description.endsWith("*")) {
109                 cb.query().setDescription_Prefix(fileConfigPager.description.replaceAll("\\*$", StringUtil.EMPTY));
110             } else {
111                 cb.query().setDescription_MatchPhrase(fileConfigPager.description);
112             }
113         }
114         // TODO Long, Integer, String supported only.
115 
116         // setup condition
117         cb.query().addOrderBy_SortOrder_Asc();
118         cb.query().addOrderBy_Name_Asc();
119 
120         // search
121 
122     }
123 
124 }