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.helper;
17  
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Locale;
22  import java.util.Map;
23  
24  import javax.annotation.PostConstruct;
25  
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  import org.codelibs.core.lang.StringUtil;
29  import org.codelibs.fess.es.config.exbhv.RelatedQueryBhv;
30  import org.codelibs.fess.es.config.exentity.RelatedQuery;
31  import org.codelibs.fess.util.ComponentUtil;
32  
33  public class RelatedQueryHelper extends AbstractConfigHelper {
34      private static final Logger logger = LogManager.getLogger(RelatedQueryHelper.class);
35  
36      protected volatile Map<String, Map<String, String[]>> relatedQueryMap = Collections.emptyMap();
37  
38      @PostConstruct
39      public void init() {
40          if (logger.isDebugEnabled()) {
41              logger.debug("Initialize {}", this.getClass().getSimpleName());
42          }
43          load();
44      }
45  
46      public List<RelatedQuery> getAvailableRelatedQueryList() {
47  
48          return ComponentUtil.getComponent(RelatedQueryBhv.class).selectList(cb -> {
49              cb.query().matchAll();
50              cb.query().addOrderBy_Term_Asc();
51              cb.fetchFirst(ComponentUtil.getFessConfig().getPageRelatedqueryMaxFetchSizeAsInteger());
52          });
53      }
54  
55      @Override
56      public int load() {
57          final Map<String, Map<String, String[]>> relatedQueryMap = new HashMap<>();
58          getAvailableRelatedQueryList().stream().forEach(entity -> {
59              final String key = getHostKey(entity);
60              Map<String, String[]> map = relatedQueryMap.get(key);
61              if (map == null) {
62                  map = new HashMap<>();
63                  relatedQueryMap.put(key, map);
64              }
65              map.put(toLowerCase(entity.getTerm()), entity.getQueries());
66          });
67          this.relatedQueryMap = relatedQueryMap;
68          return relatedQueryMap.size();
69      }
70  
71      protected String getHostKey(final RelatedQuery entity) {
72          final String key = entity.getVirtualHost();
73          return StringUtil.isBlank(key) ? StringUtil.EMPTY : key;
74      }
75  
76      public String[] getRelatedQueries(final String query) {
77          final String key = ComponentUtil.getVirtualHostHelper().getVirtualHostKey();
78          final Map<String, String[]> map = relatedQueryMap.get(key);
79          if (map != null) {
80              final String[] queries = map.get(toLowerCase(query));
81              if (queries != null) {
82                  return queries;
83              }
84          }
85          return StringUtil.EMPTY_STRINGS;
86      }
87  
88      private String toLowerCase(final String term) {
89          return term != null ? term.toLowerCase(Locale.ROOT) : term;
90      }
91  
92  }