1 /*
2 * Copyright 2012-2025 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 org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.codelibs.fess.util.ComponentUtil;
27
28 import jakarta.annotation.PostConstruct;
29
30 /**
31 * View class for managing facet query configurations and their display.
32 * This class handles the setup and organization of query facets for the search interface,
33 * including automatic generation of file type facets and custom query mappings.
34 */
35 public class FacetQueryView {
36 /** Logger instance for this class */
37 private static final Logger logger = LogManager.getLogger(FacetQueryView.class);
38
39 /** Title for this facet query view */
40 protected String title;
41
42 /** Map of display keys to their corresponding query strings */
43 protected Map<String, String> queryMap = new LinkedHashMap<>();
44
45 /**
46 * Default constructor for FacetQueryView.
47 */
48 public FacetQueryView() {
49 // Default constructor
50 }
51
52 /**
53 * Initializes the facet query view with default file type queries.
54 * This method is called after dependency injection to set up file type facets
55 * and register all queries with the FacetInfo component.
56 */
57 @PostConstruct
58 public void init() {
59 final String filetypeField = ComponentUtil.getFessConfig().getIndexFieldFiletype();
60 final Collection<String> values = queryMap.values();
61 if (values.stream().anyMatch(s -> s.startsWith(filetypeField))) {
62 final ResourceBundle resources = ResourceBundle.getBundle("fess_label", Locale.ENGLISH);
63 final String[] fileTypes = ComponentUtil.getFileTypeHelper().getTypes();
64 for (final String fileType : fileTypes) {
65 final String value = filetypeField + ":" + fileType;
66 if (!values.contains(value)) {
67 final String key = "labels.facet_filetype_" + fileType;
68 if (resources.containsKey(key)) {
69 queryMap.put(key, value);
70 } else {
71 queryMap.put(fileType.toUpperCase(Locale.ROOT), value);
72 }
73 }
74 }
75 queryMap.remove("labels.facet_filetype_others");
76 queryMap.put("labels.facet_filetype_others", "filetype:others");
77 if (logger.isDebugEnabled()) {
78 logger.debug("Updated query map: queryMap={}", queryMap);
79 }
80 }
81
82 final FacetInfo facetInfo = ComponentUtil.getComponent("facetInfo");
83 queryMap.values().stream().distinct().forEach(facetInfo::addQuery);
84 }
85
86 /**
87 * Gets the title for this facet query view.
88 *
89 * @return the title string
90 */
91 public String getTitle() {
92 return title;
93 }
94
95 /**
96 * Sets the title for this facet query view.
97 *
98 * @param title the title to set
99 */
100 public void setTitle(final String title) {
101 this.title = title;
102 }
103
104 /**
105 * Gets the map of display keys to query strings.
106 *
107 * @return the query map
108 */
109 public Map<String, String> getQueryMap() {
110 return queryMap;
111 }
112
113 /**
114 * Adds a query to the query map with the specified display key.
115 *
116 * @param key the display key for the query
117 * @param query the query string to add
118 */
119 public void addQuery(final String key, final String query) {
120 queryMap.put(key, query);
121 }
122
123 /**
124 * Returns a string representation of this FacetQueryView object.
125 * Includes the title and query map for debugging purposes.
126 *
127 * @return string representation of this FacetQueryView instance
128 */
129 @Override
130 public String toString() {
131 return "FacetQueryView [title=" + title + ", queryMap=" + queryMap + "]";
132 }
133
134 }