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.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.exbhv.FileConfigToLabelBhv;
30  import org.codelibs.fess.es.config.exentity.FileConfig;
31  import org.codelibs.fess.es.config.exentity.FileConfigToLabel;
32  import org.codelibs.fess.mylasta.direction.FessConfig;
33  import org.dbflute.cbean.result.PagingResultBean;
34  import org.dbflute.optional.OptionalEntity;
35  
36  public class FileConfigService {
37  
38      @Resource
39      protected FileConfigToLabelBhv fileConfigToLabelBhv;
40  
41      @Resource
42      protected FileConfigBhv fileConfigBhv;
43  
44      @Resource
45      protected FileAuthenticationBhv fileAuthenticationBhv;
46  
47      @Resource
48      protected FessConfig fessConfig;
49  
50      public List<FileConfig> getFileConfigList(final FileConfigPager fileConfigPager) {
51  
52          final PagingResultBean<FileConfig> fileConfigList = fileConfigBhv.selectPage(cb -> {
53              cb.paging(fileConfigPager.getPageSize(), fileConfigPager.getCurrentPageNumber());
54              setupListCondition(cb, fileConfigPager);
55          });
56  
57          // update pager
58          BeanUtil.copyBeanToBean(fileConfigList, fileConfigPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
59          fileConfigPager.setPageNumberList(fileConfigList.pageRange(op -> {
60              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
61          }).createPageNumberList());
62  
63          return fileConfigList;
64      }
65  
66      public void delete(final FileConfig fileConfig) {
67  
68          final String fileConfigId = fileConfig.getId();
69  
70          fileConfigBhv.delete(fileConfig, op -> {
71              op.setRefreshPolicy(Constants.TRUE);
72          });
73  
74          fileConfigToLabelBhv.queryDelete(cb -> {
75              cb.query().setFileConfigId_Equal(fileConfigId);
76          });
77  
78          fileAuthenticationBhv.queryDelete(cb -> {
79              cb.query().setFileConfigId_Equal(fileConfigId);
80          });
81      }
82  
83      public List<FileConfig> getAllFileConfigList() {
84          return getAllFileConfigList(true, true, true, null);
85      }
86  
87      public List<FileConfig> getFileConfigListByIds(final List<String> idList) {
88          if (idList == null) {
89              return getAllFileConfigList();
90          } else {
91              return getAllFileConfigList(true, true, false, idList);
92          }
93      }
94  
95      public List<FileConfig> getAllFileConfigList(final boolean withLabelType, final boolean withRoleType, final boolean available,
96              final List<String> idList) {
97          final List<FileConfig> list = fileConfigBhv.selectList(cb -> {
98              if (available) {
99                  cb.query().setAvailable_Equal(Constants.T);
100             }
101             if (idList != null) {
102                 cb.query().setId_InScope(idList);
103             }
104             cb.fetchFirst(fessConfig.getPageFileConfigMaxFetchSizeAsInteger());
105         });
106         return list;
107     }
108 
109     public OptionalEntity<FileConfig> getFileConfig(final String id) {
110         return fileConfigBhv.selectByPK(id).map(entity -> {
111 
112             final List<FileConfigToLabel> fctltmList = fileConfigToLabelBhv.selectList(fctltmCb -> {
113                 fctltmCb.query().setFileConfigId_Equal(entity.getId());
114                 fctltmCb.fetchFirst(fessConfig.getPageLabeltypeMaxFetchSizeAsInteger());
115             });
116             if (!fctltmList.isEmpty()) {
117                 final List<String> labelTypeIds = new ArrayList<>(fctltmList.size());
118                 for (final FileConfigToLabel mapping : fctltmList) {
119                     labelTypeIds.add(mapping.getLabelTypeId());
120                 }
121                 entity.setLabelTypeIds(labelTypeIds.toArray(new String[labelTypeIds.size()]));
122             }
123             return entity;
124         });
125     }
126 
127     public void store(final FileConfig fileConfig) {
128         final boolean isNew = fileConfig.getId() == null;
129         final String[] labelTypeIds = fileConfig.getLabelTypeIds();
130 
131         fileConfigBhv.insertOrUpdate(fileConfig, op -> {
132             op.setRefreshPolicy(Constants.TRUE);
133         });
134         final String fileConfigId = fileConfig.getId();
135         if (isNew) {
136             // Insert
137             if (labelTypeIds != null) {
138                 final List<FileConfigToLabel> fctltmList = new ArrayList<>();
139                 for (final String labelTypeId : labelTypeIds) {
140                     final FileConfigToLabel mapping = new FileConfigToLabel();
141                     mapping.setFileConfigId(fileConfigId);
142                     mapping.setLabelTypeId(labelTypeId);
143                     fctltmList.add(mapping);
144                 }
145                 fileConfigToLabelBhv.batchInsert(fctltmList, op -> {
146                     op.setRefreshPolicy(Constants.TRUE);
147                 });
148             }
149         } else {
150             // Update
151             if (labelTypeIds != null) {
152                 final List<FileConfigToLabel> fctltmList = fileConfigToLabelBhv.selectList(fctltmCb -> {
153                     fctltmCb.query().setFileConfigId_Equal(fileConfigId);
154                     fctltmCb.fetchFirst(fessConfig.getPageLabeltypeMaxFetchSizeAsInteger());
155                 });
156                 final List<FileConfigToLabel> newList = new ArrayList<>();
157                 final List<FileConfigToLabel> matchedList = new ArrayList<>();
158                 for (final String id : labelTypeIds) {
159                     boolean exist = false;
160                     for (final FileConfigToLabel mapping : fctltmList) {
161                         if (mapping.getLabelTypeId().equals(id)) {
162                             exist = true;
163                             matchedList.add(mapping);
164                             break;
165                         }
166                     }
167                     if (!exist) {
168                         // new
169                         final FileConfigToLabel mapping = new FileConfigToLabel();
170                         mapping.setFileConfigId(fileConfigId);
171                         mapping.setLabelTypeId(id);
172                         newList.add(mapping);
173                     }
174                 }
175                 fctltmList.removeAll(matchedList);
176                 fileConfigToLabelBhv.batchInsert(newList, op -> {
177                     op.setRefreshPolicy(Constants.TRUE);
178                 });
179                 fileConfigToLabelBhv.batchDelete(fctltmList, op -> {
180                     op.setRefreshPolicy(Constants.TRUE);
181                 });
182             }
183         }
184     }
185 
186     protected void setupListCondition(final FileConfigCB cb, final FileConfigPager fileConfigPager) {
187         if (fileConfigPager.id != null) {
188             cb.query().docMeta().setId_Equal(fileConfigPager.id);
189         }
190         // TODO Long, Integer, String supported only.
191 
192         // setup condition
193         cb.query().addOrderBy_SortOrder_Asc();
194         cb.query().addOrderBy_Name_Asc();
195 
196         // search
197 
198     }
199 
200 }