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