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