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;
031
032/**
033 * {@link SystemProperty} which represents the last major validation date of a content.
034 */
035public class LastMajorValidationSystemProperty extends AbstractSystemProperty
036{
037
038    @Override
039    public MetadataType getType()
040    {
041        return MetadataType.DATETIME;
042    }
043    
044    @Override
045    public boolean isMultiple()
046    {
047        return false;
048    }
049    
050    @Override
051    public boolean isSortable()
052    {
053        return true;
054    }
055    
056    @Override
057    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
058    {
059        Date dateValue = parseDate(value);
060        
061        GregorianCalendar calendar = new GregorianCalendar();
062        calendar.setTime(dateValue);
063        calendar.set(Calendar.HOUR, 0);
064        calendar.set(Calendar.MINUTE, 0);
065        calendar.set(Calendar.MILLISECOND, 0);
066        
067        if (operator.equals(Operator.LE))
068        {
069            calendar.add(Calendar.DAY_OF_YEAR, 1);
070            return new LastMajorValidationDateQuery(Operator.LT, calendar.getTime());
071        }
072        
073        return new LastMajorValidationDateQuery(operator, calendar.getTime());
074    }
075    
076    @Override
077    public SearchField getSearchField()
078    {
079        return new LastMajorValidationSearchField();
080    }
081    
082    @Override
083    public String getWidget()
084    {
085        return "edition.date";
086    }
087    
088    @Override
089    public Object getValue(Content content)
090    {
091        return content.getLastMajorValidationDate();
092    }
093
094}