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