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