View Javadoc
1   /*
2    * Copyright 2012-2017 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.ArrayList;
19  import java.util.List;
20  
21  import javax.annotation.Resource;
22  
23  import org.codelibs.core.beans.util.BeanUtil;
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.exbhv.WebConfigToLabelBhv;
31  import org.codelibs.fess.es.config.exentity.WebConfig;
32  import org.codelibs.fess.es.config.exentity.WebConfigToLabel;
33  import org.codelibs.fess.mylasta.direction.FessConfig;
34  import org.dbflute.cbean.result.PagingResultBean;
35  import org.dbflute.optional.OptionalEntity;
36  
37  public class WebConfigService {
38  
39      @Resource
40      protected WebConfigToLabelBhv webConfigToLabelBhv;
41  
42      @Resource
43      protected WebConfigBhv webConfigBhv;
44  
45      @Resource
46      protected WebAuthenticationBhv webAuthenticationBhv;
47  
48      @Resource
49      protected RequestHeaderBhv requestHeaderBhv;
50  
51      @Resource
52      protected FessConfig fessConfig;
53  
54      public List<WebConfig> getWebConfigList(final WebConfigPager webConfigPager) {
55  
56          final PagingResultBean<WebConfig> webConfigList = webConfigBhv.selectPage(cb -> {
57              cb.paging(webConfigPager.getPageSize(), webConfigPager.getCurrentPageNumber());
58              setupListCondition(cb, webConfigPager);
59          });
60  
61          // update pager
62          BeanUtil.copyBeanToBean(webConfigList, webConfigPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
63          webConfigPager.setPageNumberList(webConfigList.pageRange(op -> {
64              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
65          }).createPageNumberList());
66  
67          return webConfigList;
68      }
69  
70      public void delete(final WebConfig webConfig) {
71  
72          final String webConfigId = webConfig.getId();
73  
74          webConfigBhv.delete(webConfig, op -> {
75              op.setRefreshPolicy(Constants.TRUE);
76          });
77  
78          webConfigToLabelBhv.queryDelete(cb -> {
79              cb.query().setWebConfigId_Equal(webConfigId);
80          });
81  
82          webAuthenticationBhv.queryDelete(cb -> {
83              cb.query().setWebConfigId_Equal(webConfigId);
84          });
85  
86          requestHeaderBhv.queryDelete(cb -> {
87              cb.query().setWebConfigId_Equal(webConfigId);
88          });
89      }
90  
91      public List<WebConfig> getAllWebConfigList() {
92          return getAllWebConfigList(true, true, true, null);
93      }
94  
95      public List<WebConfig> getWebConfigListByIds(final List<String> idList) {
96          if (idList == null) {
97              return getAllWebConfigList();
98          } else {
99              return getAllWebConfigList(true, true, false, idList);
100         }
101     }
102 
103     public List<WebConfig> getAllWebConfigList(final boolean withLabelType, final boolean withRoleType, final boolean available,
104             final List<String> idList) {
105         final List<WebConfig> list = webConfigBhv.selectList(cb -> {
106             if (available) {
107                 cb.query().setAvailable_Equal(Constants.T);
108             }
109             if (idList != null) {
110                 cb.query().setId_InScope(idList);
111             }
112             cb.fetchFirst(fessConfig.getPageWebConfigMaxFetchSizeAsInteger());
113         });
114 
115         return list;
116     }
117 
118     public OptionalEntity<WebConfig> getWebConfig(final String id) {
119         return webConfigBhv.selectByPK(id).map(entity -> {
120 
121             final List<WebConfigToLabel> wctltmList = webConfigToLabelBhv.selectList(wctltmCb -> {
122                 wctltmCb.query().setWebConfigId_Equal(entity.getId());
123                 wctltmCb.fetchFirst(fessConfig.getPageLabeltypeMaxFetchSizeAsInteger());
124             });
125             if (!wctltmList.isEmpty()) {
126                 final List<String> labelTypeIds = new ArrayList<>(wctltmList.size());
127                 for (final WebConfigToLabel mapping : wctltmList) {
128                     labelTypeIds.add(mapping.getLabelTypeId());
129                 }
130                 entity.setLabelTypeIds(labelTypeIds.toArray(new String[labelTypeIds.size()]));
131             }
132             return entity;
133         });
134     }
135 
136     public void store(final WebConfig webConfig) {
137         final boolean isNew = webConfig.getId() == null;
138         final String[] labelTypeIds = webConfig.getLabelTypeIds();
139 
140         webConfigBhv.insertOrUpdate(webConfig, op -> {
141             op.setRefreshPolicy(Constants.TRUE);
142         });
143         final String webConfigId = webConfig.getId();
144         if (isNew) {
145             // Insert
146             if (labelTypeIds != null) {
147                 final List<WebConfigToLabel> wctltmList = new ArrayList<>();
148                 for (final String id : labelTypeIds) {
149                     final WebConfigToLabel mapping = new WebConfigToLabel();
150                     mapping.setWebConfigId(webConfigId);
151                     mapping.setLabelTypeId(id);
152                     wctltmList.add(mapping);
153                 }
154                 webConfigToLabelBhv.batchInsert(wctltmList, op -> {
155                     op.setRefreshPolicy(Constants.TRUE);
156                 });
157             }
158         } else {
159             // Update
160             if (labelTypeIds != null) {
161                 final List<WebConfigToLabel> list = webConfigToLabelBhv.selectList(wctltmCb -> {
162                     wctltmCb.query().setWebConfigId_Equal(webConfigId);
163                     wctltmCb.fetchFirst(fessConfig.getPageLabeltypeMaxFetchSizeAsInteger());
164                 });
165                 final List<WebConfigToLabel> newList = new ArrayList<>();
166                 final List<WebConfigToLabel> matchedList = new ArrayList<>();
167                 for (final String id : labelTypeIds) {
168                     boolean exist = false;
169                     for (final WebConfigToLabel mapping : list) {
170                         if (mapping.getLabelTypeId().equals(id)) {
171                             exist = true;
172                             matchedList.add(mapping);
173                             break;
174                         }
175                     }
176                     if (!exist) {
177                         // new
178                         final WebConfigToLabel mapping = new WebConfigToLabel();
179                         mapping.setWebConfigId(webConfigId);
180                         mapping.setLabelTypeId(id);
181                         newList.add(mapping);
182                     }
183                 }
184                 list.removeAll(matchedList);
185                 webConfigToLabelBhv.batchInsert(newList, op -> {
186                     op.setRefreshPolicy(Constants.TRUE);
187                 });
188                 webConfigToLabelBhv.batchDelete(list, op -> {
189                     op.setRefreshPolicy(Constants.TRUE);
190                 });
191             }
192         }
193     }
194 
195     protected void setupListCondition(final WebConfigCB cb, final WebConfigPager webConfigPager) {
196         if (webConfigPager.id != null) {
197             cb.query().docMeta().setId_Equal(webConfigPager.id);
198         }
199         // TODO Long, Integer, String supported only.
200 
201         // setup condition
202         cb.query().addOrderBy_SortOrder_Asc();
203         cb.query().addOrderBy_Name_Asc();
204 
205         // search
206 
207     }
208 
209 }