001/*
002 *  Copyright 2015 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.Date;
019import java.util.Map;
020
021import org.ametys.cms.contenttype.MetadataType;
022import org.ametys.cms.repository.Content;
023import org.ametys.cms.search.SearchField;
024import org.ametys.cms.search.model.SystemProperty;
025import org.ametys.cms.search.query.LastMajorValidationDateQuery;
026import org.ametys.cms.search.query.Query;
027import org.ametys.cms.search.query.Query.Operator;
028import org.ametys.cms.search.solr.field.LastMajorValidationSearchField;
029import org.ametys.core.util.DateUtils;
030
031/**
032 * {@link SystemProperty} which represents the last major validation date of a content.
033 */
034public class LastMajorValidationSystemProperty extends AbstractSystemProperty
035{
036    @Override
037    public MetadataType getType()
038    {
039        return MetadataType.DATETIME;
040    }
041    
042    @Override
043    public boolean isMultiple()
044    {
045        return false;
046    }
047    
048    @Override
049    public boolean isSortable()
050    {
051        return true;
052    }
053    
054    @Override
055    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
056    {
057        return new LastMajorValidationDateQuery(operator, operator != Operator.EXISTS ? parseDate(value) : null);
058    }
059    
060    @Override
061    public SearchField getSearchField()
062    {
063        return new LastMajorValidationSearchField();
064    }
065    
066    @Override
067    public String getWidget()
068    {
069        return "edition.date";
070    }
071    
072    @Override
073    public Object getValue(Content content)
074    {
075        return content.getLastMajorValidationDate();
076    }
077    
078    @Override
079    public Object getJsonValue(Content content, boolean full)
080    {
081        return DateUtils.dateToString((Date) getValue(content));
082    }
083}