001/* 002 * Copyright 2025 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.systemprop; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.List; 021 022import org.apache.solr.common.SolrInputDocument; 023 024import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject; 025import org.ametys.cms.data.type.indexing.IndexableElementType; 026import org.ametys.cms.model.CMSDataContext; 027import org.ametys.cms.search.model.IndexationAwareElementDefinition; 028import org.ametys.cms.search.model.SystemProperty; 029import org.ametys.cms.search.solr.schema.CopyFieldDefinition; 030import org.ametys.cms.search.solr.schema.FieldDefinition; 031import org.ametys.cms.search.solr.schema.SchemaDefinition; 032 033/** 034 * Interface for {@link SystemProperty} that have specific behaviors for indexation 035 * @param <T> Type of the property value 036 * @param <X> type of ametys object supported by this system property 037 */ 038public interface IndexationAwareSystemProperty<T, X extends ModelAwareDataAwareAmetysObject> extends IndexationAwareElementDefinition<T, X>, SystemProperty<T, X> 039{ 040 public default void indexValue(SolrInputDocument document, X ametysObject, CMSDataContext context) 041 { 042 Object value = getValue(ametysObject); 043 if (value == null) 044 { 045 // Nothing to index 046 return; 047 } 048 049 String solrFieldName = getSolrFieldName(); 050 if (solrFieldName == null) 051 { 052 // Nothing to index 053 return; 054 } 055 056 IndexableElementType<T> type = getType(); 057 if (type.getManagedClass().isInstance(value)) 058 { 059 Object valueToIndex = type.getSingleValueToIndex(type.getManagedClass().cast(value)); 060 document.addField(solrFieldName, valueToIndex); 061 } 062 else if (type.getManagedClassArray().isInstance(value)) 063 { 064 T[] values = type.getManagedClassArray().cast(value); 065 for (T singleValue : values) 066 { 067 Object valueToIndex = type.getSingleValueToIndex(singleValue); 068 document.addField(solrFieldName, valueToIndex); 069 } 070 } 071 072 // Index sort field 073 Object sortValue = getSortValue(ametysObject); 074 String solrSortFieldName = getSolrSortFieldName(); 075 if (isSortable() && sortValue != null && solrSortFieldName != null && !solrFieldName.equals(solrSortFieldName)) 076 { 077 document.setField(solrSortFieldName, sortValue); 078 } 079 } 080 081 /** 082 * Get the schema definitions brought by this element. 083 * @return The schema definitions used by this element. 084 */ 085 public default Collection<SchemaDefinition> getSchemaDefinitions() 086 { 087 List<SchemaDefinition> definitions = new ArrayList<>(); 088 089 String solrFieldName = getSolrFieldName(); 090 String sortFieldName = getSolrSortFieldName(); 091 String facetFieldName = getSolrFacetFieldName(); 092 093 boolean multiple = isMultiple(); 094 String type = getType().getSchemaType(); 095 if (solrFieldName != null && type != null) 096 { 097 definitions.add(new FieldDefinition(solrFieldName, type, multiple, false)); 098 099 if (sortFieldName != null && !sortFieldName.equals(solrFieldName)) 100 { 101 definitions.add(new FieldDefinition(sortFieldName, type, false, false)); 102 } 103 104 if (facetFieldName != null && !facetFieldName.equals(solrFieldName)) 105 { 106 // By default the index value in field name will be automatically copy in facet field 107 // So we do not need the index facet field manually 108 definitions.add(new FieldDefinition(facetFieldName, type, multiple, true)); 109 definitions.add(new CopyFieldDefinition(solrFieldName, facetFieldName)); 110 } 111 } 112 113 return definitions; 114 } 115}