001/*
002 *  Copyright 2019 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.web.frontoffice.search.metamodel.impl;
017
018import java.util.Objects;
019
020import org.ametys.cms.repository.Content;
021import org.ametys.cms.search.Sort.Order;
022import org.ametys.cms.search.model.SystemProperty;
023import org.ametys.plugins.explorer.resources.Resource;
024import org.ametys.runtime.i18n.I18nizableText;
025import org.ametys.web.frontoffice.search.metamodel.Returnable;
026import org.ametys.web.frontoffice.search.metamodel.SortDefinition;
027import org.ametys.web.repository.page.Page;
028
029/**
030 * This class has the purpose to functionaly have a common {@link SortDefinition} (for all {@link Returnable}s) on
031 * fields such as last validation date, first validation date, last modification date, last major validation date, etc.
032 * <br>And to technically rely on {@link SystemProperty}.
033 * <br>This means that with the {@link SystemProperty} mechanism, the sort field is already indexed for {@link Content}s.
034 * <br><b>BUT</b> for every other kind of document (such as {@link Page} for {@link PageReturnable}, {@link Resource} for {@link ResourceReturnable}, etc.),
035 * the corresponding sort fields must be manually indexed.
036 */
037public class CommonSystemPropertyBasedSortDefinition implements SortDefinition
038{
039    /** The prefix id of the sort definition */
040    protected static final String __PREFIX_ID = "common$";
041    private SystemProperty _systemProperty;
042    private I18nizableText _label;
043    private Order[] _orders;
044    
045    /**
046     * Default constructor with the label of the given {@link SystemProperty}
047     * @param systemProperty The {@link SystemProperty} this {@link SortDefinition} is based on.
048     * @param orders The orders allowed for this {@link SortDefinition}
049     */
050    public CommonSystemPropertyBasedSortDefinition(SystemProperty systemProperty, Order[] orders)
051    {
052        this(systemProperty, systemProperty.getLabel(), orders);
053    }
054    
055    /**
056     * Default constructor
057     * @param systemProperty The {@link SystemProperty} this {@link SortDefinition} is based on.
058     * @param label The label
059     * @param orders The orders allowed for this {@link SortDefinition}
060     */
061    public CommonSystemPropertyBasedSortDefinition(SystemProperty systemProperty, I18nizableText label, Order[] orders)
062    {
063        Objects.nonNull(systemProperty);
064        _systemProperty = systemProperty;
065        _label = label;
066        _orders = orders;
067    }
068
069    @Override
070    public String getId()
071    {
072        return __PREFIX_ID + _systemProperty.getId();
073    }
074
075    @Override
076    public I18nizableText getLabel()
077    {
078        return _label;
079    }
080    
081    @Override
082    public Order[] orders()
083    {
084        return _orders;
085    }
086
087    @Override
088    public String getField()
089    {
090        return _systemProperty.getSearchField().getSortField();
091    }
092}
093