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.ModelAwareDataAwareAmetysObject; 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 {@link ElementDefinition} that have specific behaviors for indexation 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 ModelAwareDataAwareAmetysObject> 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 getType().indexValue(document, document, getName(), value, context); 045 } 046 047 /** 048 * Get if the element can be sorted on. 049 * @return <code>true</code> if the element can be sorted on, <code>false</code> otherwise. 050 */ 051 public default boolean isSortable() 052 { 053 return Optional.ofNullable(getSolrSortFieldName()) 054 .isPresent(); 055 } 056 057 /** 058 * Gets if the element can be facetable 059 * @return <code>true</code> if the element can be facetable, <code>false</code> otherwise. 060 */ 061 public default boolean isFacetable() 062 { 063 return Optional.ofNullable(getSolrFacetFieldName()) 064 .isPresent(); 065 } 066 067 /** 068 * Retrieves the name of the element's solr field 069 * @return the name of the element's solr field 070 */ 071 public default String getSolrFieldName() 072 { 073 DataContext context = DataContext.newInstance() 074 .withModelItem(this); 075 return getName() + getType().getIndexingFieldSuffix(context); 076 } 077 078 /** 079 * Retrieves the name of the element's solr sort field 080 * @return the name of the element's solr sort field 081 */ 082 public String getSolrSortFieldName(); 083 084 /** 085 * Retrieves the name of the element's solr facet field 086 * @return the name of the element's solr facet field 087 */ 088 public default String getSolrFacetFieldName() 089 { 090 DataContext context = DataContext.newInstance() 091 .withModelItem(this); 092 IndexableElementType<T> type = getType(); 093 094 return type.isFacetable(context) 095 ? getName() + type.getFacetFieldSuffix(context) 096 : null; 097 } 098 099 /** 100 * Get the element's value 101 * @param ametysObject the ametysObject containing the element 102 * @return the values to index. 103 */ 104 public Object getValue(X ametysObject); 105 106 /** 107 * Get the sort value represented by this field in the given result ametys object. 108 * @param ametysObject the result ametys object. 109 * @return the content sort value, must be scalar (String, long, double, Date). 110 */ 111 public default Object getSortValue(X ametysObject) 112 { 113 Object value = getValue(ametysObject); 114 if (value == null) 115 { 116 return null; 117 } 118 119 IndexableElementType<T> type = getType(); 120 if (type.getManagedClassArray().isInstance(value)) 121 { 122 T[] values = type.getManagedClassArray().cast(value); 123 return values.length > 0 ? values[0] : value; 124 } 125 else 126 { 127 return value; 128 } 129 } 130 131 @Override 132 public IndexableElementType<T> getType(); 133}