View Javadoc
1   /*
2    * Copyright 2012-2019 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.helper;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.regex.Pattern;
25  
26  import javax.annotation.PostConstruct;
27  
28  import org.codelibs.core.lang.StringUtil;
29  import org.codelibs.core.misc.Pair;
30  import org.codelibs.fess.es.config.exbhv.RelatedContentBhv;
31  import org.codelibs.fess.es.config.exentity.RelatedContent;
32  import org.codelibs.fess.util.ComponentUtil;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  public class RelatedContentHelper {
37  
38      private static final Logger logger = LoggerFactory.getLogger(RelatedContentHelper.class);
39  
40      protected volatile Map<String, Pair<Map<String, String>, List<Pair<Pattern, String>>>> relatedContentMap = Collections.emptyMap();
41  
42      protected String regexPrefix = "regex:";
43  
44      protected String queryPlaceHolder = "__QUERY__";
45  
46      @PostConstruct
47      public void init() {
48          reload();
49      }
50  
51      public int update() {
52          return reload();
53      }
54  
55      public List<RelatedContent> getAvailableRelatedContentList() {
56  
57          return ComponentUtil.getComponent(RelatedContentBhv.class).selectList(cb -> {
58              cb.query().matchAll();
59              cb.query().addOrderBy_SortOrder_Asc();
60              cb.query().addOrderBy_Term_Asc();
61              cb.fetchFirst(ComponentUtil.getFessConfig().getPageRelatedcontentMaxFetchSizeAsInteger());
62          });
63      }
64  
65      protected int reload() {
66          final Map<String, Pair<Map<String, String>, List<Pair<Pattern, String>>>> relatedContentMap = new HashMap<>();
67          getAvailableRelatedContentList().stream().forEach(entity -> {
68              final String key = getHostKey(entity);
69              Pair<Map<String, String>, List<Pair<Pattern, String>>> pair = relatedContentMap.get(key);
70              if (pair == null) {
71                  pair = new Pair<>(new HashMap<>(), new ArrayList<>());
72                  relatedContentMap.put(key, pair);
73              }
74              if (entity.getTerm().startsWith(regexPrefix)) {
75                  final String regex = entity.getTerm().substring(regexPrefix.length());
76                  if (StringUtil.isBlank(regex)) {
77                      logger.warn("Unknown regex pattern: " + entity.getTerm());
78                  } else {
79                      pair.getSecond().add(new Pair<>(Pattern.compile(regex), entity.getContent()));
80                  }
81              } else {
82                  pair.getFirst().put(toLowerCase(entity.getTerm()), entity.getContent());
83              }
84          });
85          this.relatedContentMap = relatedContentMap;
86          return relatedContentMap.size();
87      }
88  
89      protected String getHostKey(final RelatedContent entity) {
90          final String key = entity.getVirtualHost();
91          return StringUtil.isBlank(key) ? StringUtil.EMPTY : key;
92      }
93  
94      public String[] getRelatedContents(final String query) {
95          final String key = ComponentUtil.getVirtualHostHelper().getVirtualHostKey();
96          final Pair<Map<String, String>, List<Pair<Pattern, String>>> pair = relatedContentMap.get(key);
97          if (pair != null) {
98              final List<String> contentList = new ArrayList<>();
99              final String content = pair.getFirst().get(toLowerCase(query));
100             if (StringUtil.isNotBlank(content)) {
101                 contentList.add(content);
102             }
103             for (final Pair<Pattern, String> regexData : pair.getSecond()) {
104                 if (regexData.getFirst().matcher(query).matches()) {
105                     contentList.add(regexData.getSecond().replace(queryPlaceHolder, query));
106                 }
107             }
108             return contentList.toArray(new String[contentList.size()]);
109         }
110         return StringUtil.EMPTY_STRINGS;
111     }
112 
113     private String toLowerCase(final String term) {
114         return term != null ? term.toLowerCase(Locale.ROOT) : term;
115     }
116 
117     public void setRegexPrefix(final String regexPrefix) {
118         this.regexPrefix = regexPrefix;
119     }
120 
121     public void setQueryPlaceHolder(final String queryPlaceHolder) {
122         this.queryPlaceHolder = queryPlaceHolder;
123     }
124 
125 }