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.impl;
017
018import java.time.ZoneOffset;
019import java.time.ZonedDateTime;
020
021import org.apache.solr.common.SolrInputDocument;
022
023import org.ametys.cms.data.type.indexing.SortableIndexableElementType;
024import org.ametys.core.model.type.AbstractDateTimeElementType;
025import org.ametys.core.util.DateUtils;
026import org.ametys.runtime.model.type.DataContext;
027
028/**
029 * Class for date time type of elements that can be indexed
030 */
031public class DateTimeIndexableElementType extends AbstractDateTimeElementType implements SortableIndexableElementType<ZonedDateTime>
032{
033    public void indexSingleValue(SolrInputDocument document, SolrInputDocument rootObjectDocument, String fieldName, ZonedDateTime value, DataContext context)
034    {
035        String valueToIndex = getSingleValueToIndex(value);
036        document.addField(fieldName + getIndexingFieldSuffix(context), valueToIndex);
037    }
038    
039    @SuppressWarnings("unchecked")
040    public <X> X getSingleValueToIndex(ZonedDateTime value)
041    {
042        return (X) DateUtils.zonedDateTimeToString(value, ZoneOffset.UTC);
043    }
044    
045    public String getSortFieldSuffix()
046    {
047        return "_dt_sort";
048    }
049
050    public String getSchemaType()
051    {
052        return "pdate";
053    }
054    
055    public String getIndexingFieldSuffix(DataContext context)
056    {
057        return "_dt";
058    }
059}