001/*
002 *  Copyright 2018 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 org.ametys.cms.search.SearchField;
019import org.ametys.runtime.i18n.I18nizableText;
020import org.ametys.web.frontoffice.search.metamodel.SortDefinition;
021
022/**
023 * Default implementation of {@link SortDefinition}.
024 */
025public class DefaultSortDefinition implements SortDefinition
026{
027    String _id;
028    I18nizableText _label;
029    String _searchFieldStr;
030    SearchField _searchField;
031
032    /**
033     * Default constructor with a {@link SearchField}
034     * @param id The id
035     * @param label The label
036     * @param searchField The search field
037     */
038    public DefaultSortDefinition(String id, I18nizableText label, SearchField searchField)
039    {
040        this(id, label);
041        _searchField = searchField;
042    }
043    
044    /**
045     * Default constructor with a {@link String}
046     * @param id The id
047     * @param label The label
048     * @param searchFieldString The search field as a string
049     */
050    public DefaultSortDefinition(String id, I18nizableText label, String searchFieldString)
051    {
052        this(id, label);
053        _searchFieldStr = searchFieldString;
054    }
055    
056    private DefaultSortDefinition(String id, I18nizableText label)
057    {
058        _id = id;
059        _label = label;
060    }
061    
062    @Override
063    public String getId()
064    {
065        return _id;
066    }
067
068    @Override
069    public I18nizableText getLabel()
070    {
071        return _label;
072    }
073
074    @Override
075    public String getField()
076    {
077        return _searchFieldStr == null ? _searchField.getSortField() : _searchFieldStr;
078    }
079}