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.Map; 021import java.util.Optional; 022 023import org.ametys.cms.contenttype.ContentType; 024import org.ametys.cms.search.query.Query; 025import org.ametys.cms.search.query.Query.Operator; 026import org.ametys.cms.search.ui.model.SearchUICriterion; 027import org.ametys.runtime.i18n.I18nizableText; 028import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition; 029import org.ametys.web.frontoffice.search.metamodel.Searchable; 030 031/** 032 * {@link SearchCriterionDefinition} for {@link ContentSearchable} proposing a search criterion (based on a {@link SearchUICriterion}). 033 */ 034public class ContentSearchCriterionDefinition extends AbstractDefaultSearchCriterionDefinition 035{ 036 /** The SearchUICriterion */ 037 protected SearchUICriterion _searchUICriterion; 038 /** The content type on which this criterion definition applies. Can be empty if it applies to all types of contents. */ 039 protected Optional<ContentType> _contentType; 040 041 /** 042 * Default constructor 043 * @param id The id 044 * @param pluginName The plugin name 045 * @param searchable the {@link Searchable} 046 * @param criterion The linked {@link SearchUICriterion} 047 * @param contentType The content type on which this criterion definition applies. Can be empty if it applies to all types of contents. 048 */ 049 public ContentSearchCriterionDefinition( 050 String id, 051 String pluginName, 052 Optional<Searchable> searchable, 053 SearchUICriterion criterion, 054 Optional<ContentType> contentType) 055 { 056 super( 057 id, 058 pluginName, 059 criterion.getLabel(), 060 criterion.getType(), 061 Optional.ofNullable(criterion.getWidget()), 062 Optional.ofNullable(criterion.getWidgetParameters()), 063 Optional.ofNullable(criterion.getEnumerator()), 064 Optional.ofNullable(criterion.getValidator()), 065 Optional.ofNullable(criterion.getDefaultValue()), 066 searchable 067 ); 068 _searchUICriterion = criterion; 069 _contentType = contentType; 070 } 071 072 /** 073 * Gets the {@link SearchUICriterion} associated to this definition 074 * @return the {@link SearchUICriterion} associated to this definition 075 */ 076 public SearchUICriterion getSearchUICriterion() 077 { 078 return _searchUICriterion; 079 } 080 081 @Override 082 public List<I18nizableText> getContextPrefixLabels() 083 { 084 return Collections.singletonList( 085 _contentType 086 .map(ContentType::getLabel) 087 .orElse(new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_SEARCHABLE_CONTENT_ALL_PREFIX_LABEL"))); 088 } 089 090 @Override 091 public Map<String, Object> toJSON() throws Exception 092 { 093 Map<String, Object> json = super.toJSON(); 094 json.put("multiple", _searchUICriterion.isMultiple()); 095 return json; 096 } 097 098 @Override 099 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 100 { 101 return _searchUICriterion.getQuery(value, operator, Collections.EMPTY_MAP, language, contextualParameters); 102 } 103}