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.Calendar;
019import java.util.Date;
020import java.util.GregorianCalendar;
021import java.util.Map;
022
023import org.ametys.cms.contenttype.MetadataType;
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.search.SearchField;
026import org.ametys.cms.search.model.SystemProperty;
027import org.ametys.cms.search.query.LastMajorValidationDateQuery;
028import org.ametys.cms.search.query.Query;
029import org.ametys.cms.search.query.Query.Operator;
030import org.ametys.cms.search.solr.field.LastMajorValidationSearchField;
031import org.ametys.runtime.parameter.ParameterHelper;
032
033/**
034 * {@link SystemProperty} which represents the last major validation date of a content.
035 */
036public class LastMajorValidationSystemProperty extends AbstractSystemProperty
037{
038
039    @Override
040    public MetadataType getType()
041    {
042        return MetadataType.DATETIME;
043    }
044    
045    @Override
046    public boolean isMultiple()
047    {
048        return false;
049    }
050    
051    @Override
052    public boolean isSortable()
053    {
054        return true;
055    }
056    
057    @Override
058    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
059    {
060        Date dateValue = parseDate(value);
061        
062        GregorianCalendar calendar = new GregorianCalendar();
063        calendar.setTime(dateValue);
064        calendar.set(Calendar.HOUR, 0);
065        calendar.set(Calendar.MINUTE, 0);
066        calendar.set(Calendar.MILLISECOND, 0);
067        
068        if (operator.equals(Operator.LE))
069        {
070            calendar.add(Calendar.DAY_OF_YEAR, 1);
071            return new LastMajorValidationDateQuery(Operator.LT, calendar.getTime());
072        }
073        
074        return new LastMajorValidationDateQuery(operator, calendar.getTime());
075    }
076    
077    @Override
078    public SearchField getSearchField()
079    {
080        return new LastMajorValidationSearchField();
081    }
082    
083    @Override
084    public String getWidget()
085    {
086        return "edition.date";
087    }
088    
089    @Override
090    public Object getValue(Content content)
091    {
092        return content.getLastMajorValidationDate();
093    }
094    
095    @Override
096    public Object getJsonValue(Content content, boolean full)
097    {
098        return ParameterHelper.valueToString(getValue(content));
099    }
100
101}