DataConfigService.java 7.3 KB

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