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