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