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