001/* 002 * Copyright 2022 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.cms.model.properties; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.configuration.Configuration; 022import org.apache.solr.common.SolrInputDocument; 023import org.xml.sax.ContentHandler; 024import org.xml.sax.SAXException; 025 026import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject; 027import org.ametys.cms.data.type.indexing.IndexableDataContext; 028import org.ametys.cms.data.type.indexing.IndexableElementType; 029import org.ametys.runtime.i18n.I18nizableText; 030import org.ametys.runtime.model.ElementDefinition; 031import org.ametys.runtime.model.type.DataContext; 032import org.ametys.runtime.model.type.ModelItemType; 033import org.ametys.runtime.plugin.ExtensionPoint; 034 035/** 036 * Interface for a property 037 * @param <T> type of the property values 038 * @param <X> type of ametys object supported by this property 039 */ 040public interface Property<T, X extends ModelAwareDataAwareAmetysObject> extends ElementDefinition<T> 041{ 042 @Override 043 public IndexableElementType<T> getType(); 044 045 /** 046 * Set the extension point containing the types available for this property 047 * @param availableTypesExtensionPoint the extension point to set 048 */ 049 public void setAvailableTypeExtensionPoint(ExtensionPoint<ModelItemType> availableTypesExtensionPoint); 050 051 /** 052 * Get the property's value 053 * @param ametysObject the ametysObject containing the property 054 * @return the values to index. 055 */ 056 public Object getValue(X ametysObject); 057 058 /** 059 * Generates SAX events for the property 060 * @param contentHandler the {@link ContentHandler} that will receive the SAX events 061 * @param ametysObject the ametysObject containing the property to SAX. 062 * @param context the context of the data to SAX. Can not be null. 063 * @throws SAXException if an error occurs during the SAX events generation 064 */ 065 public default void valueToSAX(ContentHandler contentHandler, X ametysObject, DataContext context) throws SAXException 066 { 067 Object value = getValue(ametysObject); 068 if (value != null) 069 { 070 getType().valueToSAX(contentHandler, getName(), value, context); 071 } 072 } 073 074 /** 075 * Convert the property into a JSON object 076 * @param ametysObject the ametysObject containing the property to convert. 077 * @param context the context of the data to convert. Can not be null. 078 * @return The property as JSON 079 */ 080 public default Object valueToJSON(X ametysObject, DataContext context) 081 { 082 Object value = getValue(ametysObject); 083 return getType().valueToJSONForClient(value, context); 084 } 085 086 /** 087 * Index the property in a solr document. 088 * @param document the solr document to index into. 089 * @param ametysObject the ametysObject containing the property to index. 090 * @param context the context of the data to index. Can not be null. 091 */ 092 public default void indexValue(SolrInputDocument document, X ametysObject, IndexableDataContext context) 093 { 094 Object value = getValue(ametysObject); 095 getType().indexValue(document, document, getName(), value, context); 096 } 097 098 public default String getCriterionWidget() 099 { 100 return null; 101 } 102 103 public default Map<String, I18nizableText> getCriterionWidgetParameters(Configuration configuration) 104 { 105 return new HashMap<>(); 106 } 107}