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