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 org.xml.sax.ContentHandler; 019import org.xml.sax.SAXException; 020 021import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject; 022import org.ametys.cms.data.type.indexing.IndexableElementType; 023import org.ametys.runtime.model.ElementDefinition; 024import org.ametys.runtime.model.Model; 025import org.ametys.runtime.model.type.DataContext; 026 027/** 028 * Interface for a property 029 * @param <T> type of the property values 030 * @param <X> type of ametys object supported by this property 031 */ 032public interface Property<T, X extends ModelAwareDataAwareAmetysObject> extends ElementDefinition<T> 033{ 034 /** 035 * Used to do more initialization, checks... needing the role of extension point containing the types available for this property 036 * Called by {@link Model} parsing when the model items of all models have been initialized. 037 * @param availableTypesRole the extension point's role for available types 038 * @throws Exception if an error occurs or if an additional check fails. 039 */ 040 public void init(String availableTypesRole) throws Exception; 041 042 @Override 043 public IndexableElementType<T> getType(); 044 045 /** 046 * Get the property's value 047 * @param ametysObject the ametysObject containing the property 048 * @return the values to index. 049 */ 050 public Object getValue(X ametysObject); 051 052 /** 053 * Generates SAX events for the property 054 * @param contentHandler the {@link ContentHandler} that will receive the SAX events 055 * @param ametysObject the ametysObject containing the property to SAX. 056 * @param context the context of the data to SAX. Can not be null. 057 * @throws SAXException if an error occurs during the SAX events generation 058 */ 059 public default void valueToSAX(ContentHandler contentHandler, X ametysObject, DataContext context) throws SAXException 060 { 061 Object value = getValue(ametysObject); 062 if (value != null) 063 { 064 getType().valueToSAX(contentHandler, getName(), value, context); 065 } 066 } 067 068 /** 069 * Convert the property into a JSON object 070 * @param ametysObject the ametysObject containing the property to convert. 071 * @param context the context of the data to convert. Can not be null. 072 * @return The property as JSON 073 */ 074 public default Object valueToJSON(X ametysObject, DataContext context) 075 { 076 Object value = getValue(ametysObject); 077 return getType().valueToJSONForClient(value, context); 078 } 079}