001/* 002 * Copyright 2024 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.search.model; 017 018import java.util.Optional; 019 020import org.apache.solr.common.SolrInputDocument; 021 022import org.ametys.cms.data.ametysobject.IndexableAmetysObject; 023import org.ametys.cms.data.type.indexing.IndexableElementType; 024import org.ametys.cms.model.CMSDataContext; 025import org.ametys.runtime.model.ElementDefinition; 026import org.ametys.runtime.model.type.DataContext; 027 028/** 029 * Interface for indexable {@link ElementDefinition} 030 * @param <T> Type of the element value 031 * @param <X> type of ametys object supported by this definition 032 */ 033public interface IndexationAwareElementDefinition<T, X extends IndexableAmetysObject> extends ElementDefinition<T> 034{ 035 /** 036 * Index the element in a solr document. 037 * @param document the solr document to index into. 038 * @param ametysObject the ametysObject containing the element to index. 039 * @param context the context of the data to index. Can not be null. 040 */ 041 public default void indexValue(SolrInputDocument document, X ametysObject, CMSDataContext context) 042 { 043 Object value = getValue(ametysObject); 044 045 IndexableElementType<T> type = getType(); 046 if (value == null || type.getManagedClassArray().isInstance(value) && type.getManagedClassArray().cast(value).length == 0) 047 { 048 String solrFieldName = getSolrFieldName(); 049 if (solrFieldName == null) 050 { 051 // Nothing to index 052 return; 053 } 054 055 // Add empty value in document in order to remove the value during partial indexing 056 document.addField(solrFieldName, null); 057 } 058 else 059 { 060 type.indexValue(document, document, getName(), value, context); 061 } 062 } 063 064 /** 065 * Get if the element can be sorted on. 066 * @return <code>true</code> if the element can be sorted on, <code>false</code> otherwise. 067 */ 068 public default boolean isSortable() 069 { 070 return Optional.ofNullable(getSolrSortFieldName()) 071 .isPresent(); 072 } 073 074 /** 075 * Gets if the element can be facetable 076 * @return <code>true</code> if the element can be facetable, <code>false</code> otherwise. 077 */ 078 public default boolean isFacetable() 079 { 080 return Optional.ofNullable(getSolrFacetFieldName()) 081 .isPresent(); 082 } 083 084 /** 085 * Retrieves the name of the element's solr field 086 * @return the name of the element's solr field 087 */ 088 public default String getSolrFieldName() 089 { 090 DataContext context = DataContext.newInstance() 091 .withModelItem(this); 092 return getName() + getType().getIndexingFieldSuffix(context); 093 } 094 095 /** 096 * Retrieves the name of the element's solr sort field 097 * @return the name of the element's solr sort field 098 */ 099 public String getSolrSortFieldName(); 100 101 /** 102 * Retrieves the name of the element's solr facet field 103 * @return the name of the element's solr facet field 104 */ 105 public default String getSolrFacetFieldName() 106 { 107 DataContext context = DataContext.newInstance() 108 .withModelItem(this); 109 IndexableElementType<T> type = getType(); 110 111 return type.isFacetable(context) 112 ? getName() + type.getFacetFieldSuffix(context) 113 : null; 114 } 115 116 /** 117 * Get the element's value 118 * @param ametysObject the ametysObject containing the element 119 * @return the values to index. 120 */ 121 public Object getValue(X ametysObject); 122 123 @Override 124 public IndexableElementType<T> getType(); 125}