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.data.type.indexing;
017
018import java.util.Arrays;
019
020import org.apache.solr.common.SolrInputDocument;
021
022import org.ametys.runtime.model.type.DataContext;
023
024/**
025 * Interface for element types that can be indexed and with sort value
026 * @param <T> Type of the element value
027 */
028public interface SortableIndexableElementType<T> extends IndexableElementType<T>
029{
030    @SuppressWarnings("unchecked")
031    public default void indexValue(SolrInputDocument document, SolrInputDocument rootObjectDocument, String fieldName, Object value, DataContext context)
032    {
033        IndexableElementType.super.indexValue(document, rootObjectDocument, fieldName, value, context);
034
035        if (value != null)
036        {
037            if (getManagedClass().isInstance(value))
038            {
039                indexSingleSortValue(document, fieldName, (T) value, context);
040            }
041            else if (getManagedClassArray().isInstance(value))
042            {
043                // index sort value only for the first occurrence
044                Arrays.stream((T[]) value)
045                      .findFirst()
046                      .ifPresent(firstValue -> indexSingleSortValue(document, fieldName, firstValue, context));
047            }
048            else
049            {
050                throw new IllegalArgumentException("Try to convert the non " + getId() + " value '" + value + "' to JSON");
051            }
052        }
053    }
054    
055    /**
056     * Index the given single value in sort field in a solr document.
057     * @param document the solr document to index into.
058     * @param fieldName the name of the indexing field
059     * @param value the single value to index
060     * @param context the context of the data to index. Can not be null.
061     */
062    public default void indexSingleSortValue(SolrInputDocument document, String fieldName, T value, DataContext context)
063    {
064        String sortField = fieldName + getSortFieldSuffix();
065        Object sortValue = getSingleSortValueToIndex(value);
066        if (sortValue != null && !document.containsKey(sortField))
067        {
068            // FIXME If it is an enumerated date, shouldn't we index the label? In which language?
069            document.addField(sortField, sortValue);
070        }
071    }
072    
073    /**
074     * Get the suffix for sorting
075     * @return The sort field suffix.
076     */
077    public String getSortFieldSuffix();
078    
079    /**
080     * Retrieves the formatted value to index
081     * @param <X> Type of the value to retrieve
082     * @param value the value to index
083     * @return the formatted value
084     */
085    public default <X> X getSingleSortValueToIndex(T value)
086    {
087        return getSingleValueToIndex(value);
088    }
089}