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.RelatedContentPager;
26  import org.codelibs.fess.es.config.cbean.RelatedContentCB;
27  import org.codelibs.fess.es.config.exbhv.RelatedContentBhv;
28  import org.codelibs.fess.es.config.exentity.RelatedContent;
29  import org.codelibs.fess.mylasta.direction.FessConfig;
30  import org.codelibs.fess.util.ComponentUtil;
31  import org.dbflute.cbean.result.PagingResultBean;
32  import org.dbflute.optional.OptionalEntity;
33  
34  public class RelatedContentService extends FessAppService {
35  
36      @Resource
37      protected RelatedContentBhv relatedContentBhv;
38  
39      @Resource
40      protected FessConfig fessConfig;
41  
42      public List<RelatedContent> getRelatedContentList(final RelatedContentPager relatedContentPager) {
43  
44          final PagingResultBean<RelatedContent> relatedContentList = relatedContentBhv.selectPage(cb -> {
45              cb.paging(relatedContentPager.getPageSize(), relatedContentPager.getCurrentPageNumber());
46              setupListCondition(cb, relatedContentPager);
47          });
48  
49          // update pager
50          BeanUtil.copyBeanToBean(relatedContentList, relatedContentPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
51          relatedContentPager.setPageNumberList(
52                  relatedContentList.pageRange(op -> op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger())).createPageNumberList());
53  
54          return relatedContentList;
55      }
56  
57      public OptionalEntity<RelatedContent> getRelatedContent(final String id) {
58          return relatedContentBhv.selectByPK(id);
59      }
60  
61      public void store(final RelatedContent relatedContent) {
62  
63          relatedContentBhv.insertOrUpdate(relatedContent, op -> op.setRefreshPolicy(Constants.TRUE));
64          ComponentUtil.getRelatedContentHelper().update();
65      }
66  
67      public void delete(final RelatedContent relatedContent) {
68  
69          relatedContentBhv.delete(relatedContent, op -> op.setRefreshPolicy(Constants.TRUE));
70          ComponentUtil.getRelatedContentHelper().update();
71      }
72  
73      protected void setupListCondition(final RelatedContentCB cb, final RelatedContentPager relatedContentPager) {
74          if (StringUtil.isNotBlank(relatedContentPager.term)) {
75              cb.query().setTerm_Wildcard(wrapQuery(relatedContentPager.term));
76          }
77          if (StringUtil.isNotBlank(relatedContentPager.content)) {
78              cb.query().setContent_Wildcard(wrapQuery(relatedContentPager.content));
79          }
80          // TODO Long, Integer, String supported only.
81  
82          // setup condition
83          cb.query().addOrderBy_Term_Asc();
84          cb.query().addOrderBy_CreatedTime_Asc();
85  
86          // search
87  
88      }
89  
90      public List<RelatedContent> getAvailableRelatedContentList() {
91          return relatedContentBhv.selectList(cb -> {
92              cb.query().matchAll();
93              cb.query().addOrderBy_Term_Asc();
94              cb.fetchFirst(fessConfig.getPageDocboostMaxFetchSizeAsInteger());
95          });
96      }
97  
98  }