View Javadoc
1   /*
2    * Copyright 2012-2020 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.entity;
17  
18  import java.util.Collection;
19  import java.util.LinkedHashMap;
20  import java.util.Locale;
21  import java.util.Map;
22  import java.util.ResourceBundle;
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.fess.util.ComponentUtil;
29  
30  public class FacetQueryView {
31      private static final Logger logger = LogManager.getLogger(FacetQueryView.class);
32  
33      protected String title;
34  
35      protected Map<String, String> queryMap = new LinkedHashMap<>();
36  
37      @PostConstruct
38      public void init() {
39          final String filetypeField = ComponentUtil.getFessConfig().getIndexFieldFiletype();
40          final Collection<String> values = queryMap.values();
41          if (values.stream().anyMatch(s -> s.startsWith(filetypeField))) {
42              final ResourceBundle resources = ResourceBundle.getBundle("fess_label", Locale.ENGLISH);
43              final String[] fileTypes = ComponentUtil.getFileTypeHelper().getTypes();
44              for (final String fileType : fileTypes) {
45                  final String value = filetypeField + ":" + fileType;
46                  if (!values.contains(value)) {
47                      final String key = "labels.facet_filetype_" + fileType;
48                      if (resources.containsKey(key)) {
49                          queryMap.put(key, value);
50                      } else {
51                          queryMap.put(fileType.toUpperCase(Locale.ROOT), value);
52                      }
53                  }
54              }
55              queryMap.remove("labels.facet_filetype_others");
56              queryMap.put("labels.facet_filetype_others", "filetype:others");
57              if (logger.isDebugEnabled()) {
58                  logger.debug("updated query map: {}", queryMap);
59              }
60          }
61  
62          final FacetInfo facetInfo = ComponentUtil.getComponent("facetInfo");
63          queryMap.values().stream().distinct().forEach(facetInfo::addQuery);
64      }
65  
66      public String getTitle() {
67          return title;
68      }
69  
70      public void setTitle(final String title) {
71          this.title = title;
72      }
73  
74      public Map<String, String> getQueryMap() {
75          return queryMap;
76      }
77  
78      public void addQuery(final String key, final String query) {
79          queryMap.put(key, query);
80      }
81  
82      @Override
83      public String toString() {
84          return "FacetQueryView [title=" + title + ", queryMap=" + queryMap + "]";
85      }
86  
87  }