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.Model; 032import org.ametys.runtime.model.type.DataContext; 033import org.ametys.runtime.model.type.ModelItemType; 034import org.ametys.runtime.plugin.ExtensionPoint; 035 036/** 037 * Interface for a property 038 * @param <T> type of the property values 039 * @param <X> type of ametys object supported by this property 040 */ 041public interface Property<T, X extends ModelAwareDataAwareAmetysObject> extends ElementDefinition<T> 042{ 043 /** 044 * Called by {@link Model} parsing when the model items of all models have been initialized. 045 * Can be used to do more initialization, checks, ... needing model items (attributes / properties) of distant contents 046 * @throws Exception if an error occurs or if an additional check fails. 047 */ 048 public default void initializeAfterModelItemsInitialization() throws Exception 049 { 050 // Do nothing 051 } 052 053 @Override 054 public IndexableElementType<T> getType(); 055 056 /** 057 * Set the extension point containing the types available for this property 058 * @param availableTypesExtensionPoint the extension point to set 059 */ 060 public void setAvailableTypeExtensionPoint(ExtensionPoint<ModelItemType> availableTypesExtensionPoint); 061 062 /** 063 * Get the property's value 064 * @param ametysObject the ametysObject containing the property 065 * @return the values to index. 066 */ 067 public Object getValue(X ametysObject); 068 069 /** 070 * Generates SAX events for the property 071 * @param contentHandler the {@link ContentHandler} that will receive the SAX events 072 * @param ametysObject the ametysObject containing the property to SAX. 073 * @param context the context of the data to SAX. Can not be null. 074 * @throws SAXException if an error occurs during the SAX events generation 075 */ 076 public default void valueToSAX(ContentHandler contentHandler, X ametysObject, DataContext context) throws SAXException 077 { 078 Object value = getValue(ametysObject); 079 if (value != null) 080 { 081 getType().valueToSAX(contentHandler, getName(), value, context); 082 } 083 } 084 085 /** 086 * Convert the property into a JSON object 087 * @param ametysObject the ametysObject containing the property to convert. 088 * @param context the context of the data to convert. Can not be null. 089 * @return The property as JSON 090 */ 091 public default Object valueToJSON(X ametysObject, DataContext context) 092 { 093 Object value = getValue(ametysObject); 094 return getType().valueToJSONForClient(value, context); 095 } 096 097 /** 098 * Index the property in a solr document. 099 * @param document the solr document to index into. 100 * @param ametysObject the ametysObject containing the property to index. 101 * @param context the context of the data to index. Can not be null. 102 */ 103 public default void indexValue(SolrInputDocument document, X ametysObject, IndexableDataContext context) 104 { 105 Object value = getValue(ametysObject); 106 getType().indexValue(document, document, getName(), value, context); 107 } 108 109 public default String getCriterionWidget() 110 { 111 return null; 112 } 113 114 public default Map<String, I18nizableText> getCriterionWidgetParameters(Configuration configuration) 115 { 116 return new HashMap<>(); 117 } 118}