001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.frontoffice.search.metamodel.impl; 017 018import java.util.Collections; 019import java.util.List; 020import java.util.Locale; 021import java.util.Map; 022import java.util.Optional; 023 024import org.ametys.cms.contenttype.ContentType; 025import org.ametys.cms.search.query.NotQuery; 026import org.ametys.cms.search.query.Query; 027import org.ametys.cms.search.query.Query.Operator; 028import org.ametys.cms.search.ui.model.SearchUICriterion; 029import org.ametys.runtime.i18n.I18nizableText; 030import org.ametys.runtime.parameter.Validator; 031import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition; 032import org.ametys.web.frontoffice.search.metamodel.Searchable; 033 034/** 035 * {@link SearchCriterionDefinition} for {@link ContentSearchable} proposing a search criterion (based on a {@link SearchUICriterion}). 036 */ 037public class ContentSearchCriterionDefinition extends AbstractDefaultSearchCriterionDefinition 038{ 039 /** The SearchUICriterion */ 040 protected SearchUICriterion _searchUICriterion; 041 /** The content type on which this criterion definition applies. Can be empty if it applies to all types of contents. */ 042 protected Optional<ContentType> _contentType; 043 044 /** 045 * Default constructor 046 * @param id The id 047 * @param pluginName The plugin name 048 * @param searchable the {@link Searchable} 049 * @param criterion The linked {@link SearchUICriterion} 050 * @param contentType The content type on which this criterion definition applies. Can be empty if it applies to all types of contents. 051 * @param validator The validator 052 */ 053 public ContentSearchCriterionDefinition( 054 String id, 055 String pluginName, 056 Optional<Searchable> searchable, 057 SearchUICriterion criterion, 058 Optional<ContentType> contentType, 059 Optional<Validator> validator) 060 { 061 super( 062 id, 063 pluginName, 064 criterion.getLabel(), 065 criterion.getType(), 066 Optional.ofNullable(criterion.getWidget()), 067 Optional.ofNullable(criterion.getWidgetParameters()), 068 Optional.ofNullable(criterion.getEnumerator()), 069 validator.isPresent() ? validator : Optional.ofNullable(criterion.getValidator()), 070 Optional.ofNullable(criterion.getDefaultValue()), 071 searchable 072 ); 073 _searchUICriterion = criterion; 074 _contentType = contentType; 075 } 076 077 /** 078 * Get the label of a facet value. 079 * @param value the facet value. 080 * @param contextualParameters the contextual parameters 081 * @return the label, or null if the value does not exist. 082 */ 083 public Optional<I18nizableText> getFacetLabel(String value, Map<String, Object> contextualParameters) 084 { 085 Locale currentLocale = new Locale((String) contextualParameters.get("lang")); 086 return Optional.of(getSearchUICriterion()) 087 .map(criterion -> criterion.getFacetLabel(value, currentLocale)) 088 .or(() -> Optional.of(new I18nizableText(value))); 089 } 090 091 /** 092 * Gets the {@link SearchUICriterion} associated to this definition 093 * @return the {@link SearchUICriterion} associated to this definition 094 */ 095 public SearchUICriterion getSearchUICriterion() 096 { 097 return _searchUICriterion; 098 } 099 100 @Override 101 public List<I18nizableText> getContextPrefixLabels() 102 { 103 return Collections.singletonList( 104 _contentType 105 .map(ContentType::getLabel) 106 .orElse(new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_SEARCHABLE_CONTENT_ALL_PREFIX_LABEL"))); 107 } 108 109 @Override 110 public Map<String, Object> toJSON() throws Exception 111 { 112 Map<String, Object> json = super.toJSON(); 113 json.put("multiple", _searchUICriterion.isMultiple()); 114 return json; 115 } 116 117 @Override 118 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 119 { 120 return _searchUICriterion.getQuery(value, operator, Collections.EMPTY_MAP, language, contextualParameters); 121 } 122 123 @Override 124 public Query getEmptyValueQuery(String language, Map<String, Object> contextualParameters) 125 { 126 Query existQuery = _searchUICriterion.getQuery(null, Operator.EXISTS, Collections.EMPTY_MAP, language, contextualParameters); 127 return new NotQuery(existQuery); 128 } 129}