001/* 002 * Copyright 2024 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; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.component.Component; 022 023import org.ametys.cms.contenttype.ContentType; 024import org.ametys.cms.model.ContentElementDefinition; 025import org.ametys.cms.model.properties.Property; 026import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition; 027import org.ametys.runtime.i18n.I18nizableText; 028import org.ametys.runtime.model.ElementDefinition; 029import org.ametys.web.frontoffice.search.metamodel.impl.ContentReferencingSearchServiceCriterionDefinition; 030import org.ametys.web.frontoffice.search.metamodel.impl.ReferencingSearchServiceCriterionDefinition; 031import org.ametys.web.frontoffice.search.metamodel.impl.TagCriterionDefinition; 032 033/** 034 * Helper for {@link SearchServiceCriterionDefinition} 035 */ 036public class SearchServiceCriterionDefinitionHelper implements Component 037{ 038 /** The component role. */ 039 public static final String ROLE = SearchServiceCriterionDefinitionHelper.class.getName(); 040 041 /** 042 * Retrieves a {@link SearchServiceCriterionDefinition} referencing the given definition 043 * @param criterionDefinitionName the name of the criterion definition to create 044 * @param reference the definition of the element to reference 045 * @param referencePath the path of the reference from the model containing the reference 046 * @param searchable the ({@link Searchable}) the criterion belongs to 047 * @param pluginName the name of the plugin declaring the criterion definition 048 * @return the {@link SearchServiceCriterionDefinition} 049 */ 050 public SearchServiceCriterionDefinition createReferencingSearchServiceCriterionDefinition(String criterionDefinitionName, ElementDefinition reference, String referencePath, Searchable searchable, String pluginName) 051 { 052 return createReferencingSearchServiceCriterionDefinition(criterionDefinitionName, reference, referencePath, searchable, null, pluginName); 053 } 054 055 /** 056 * Retrieves a {@link SearchServiceCriterionDefinition} referencing the given definition 057 * @param criterionDefinitionName the name of the criterion definition to create 058 * @param reference the definition of the element to reference 059 * @param referencePath the path of the reference from the model containing the reference 060 * @param searchable the ({@link Searchable}) the criterion belongs to 061 * @param contentType the content type defining the reference 062 * @param pluginName the name of the plugin declaring the criterion definition 063 * @return the {@link SearchServiceCriterionDefinition} 064 */ 065 public SearchServiceCriterionDefinition createReferencingSearchServiceCriterionDefinition(String criterionDefinitionName, ElementDefinition reference, String referencePath, Searchable searchable, ContentType contentType, String pluginName) 066 { 067 if (reference instanceof Property && !(reference instanceof CriterionDefinitionAwareElementDefinition)) 068 { 069 // This item can not be used as a criterion, ignore it 070 return null; 071 } 072 073 SearchServiceCriterionDefinition criterionDefinition = createReferencingSearchServiceCriterionDefinitionInstance(reference, referencePath, contentType); 074 criterionDefinition.setName(criterionDefinitionName); 075 criterionDefinition.setPluginName(pluginName); 076 criterionDefinition.setSearchable(searchable); 077 078 return criterionDefinition; 079 } 080 081 /** 082 * Retrieves a {@link SearchServiceCriterionDefinition} instance for criterion referencing the given definition 083 * @param reference the definition of the element to reference 084 * @param referencePath the path of the reference from the model containing the reference 085 * @param contentType the content type defining the reference 086 * @return the {@link SearchServiceCriterionDefinition} 087 */ 088 public SearchServiceCriterionDefinition createReferencingSearchServiceCriterionDefinitionInstance(ElementDefinition reference, String referencePath, ContentType contentType) 089 { 090 return "tags".equals(referencePath) 091 ? new TagCriterionDefinition(reference, referencePath, contentType) 092 : reference instanceof ContentElementDefinition 093 ? new ContentReferencingSearchServiceCriterionDefinition(reference, referencePath, contentType) 094 : new ReferencingSearchServiceCriterionDefinition<>(reference, referencePath, contentType); 095 } 096 097 /** 098 * Retrieves the default values to put in a Criterion definition JSON 099 * @param <T> Type of the criterion definition 100 * @param criterionDefinition the criterion definition 101 * @param isTooBigForStaticEnumerator <code>true</code> if the criterion definition can contain too much data 102 * @return the default values to put in a Criterion definition JSON 103 */ 104 public <T> Map<String, Object> getDefaultSearchServiceCriterionDefinitionJSON(SearchServiceCriterionDefinition<T> criterionDefinition, boolean isTooBigForStaticEnumerator) 105 { 106 Map<String, Object> result = new HashMap<>(); 107 108 result.put("allowDisplayAllValues", !isTooBigForStaticEnumerator); 109 result.put("canBeRestricted", criterionDefinition.isEnumerated()); 110 result.put("contextPrefixLabel", criterionDefinition.getContextPrefixLabel()); 111 result.put("groupLabel", criterionDefinition.getSearchable() 112 .map(Searchable::getLabel) 113 .orElse(new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_COMMON_CRITERIA_GROUP"))); 114 115 return result; 116 } 117}