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