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.Map;
019
020import org.ametys.cms.data.type.ModelItemTypeConstants;
021import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject;
022import org.ametys.cms.search.model.SystemProperty;
023import org.ametys.cms.search.query.Query;
024import org.ametys.cms.search.query.Query.Operator;
025import org.ametys.cms.search.query.UsersQuery;
026import org.ametys.core.user.UserIdentity;
027
028/**
029 * Abstract class providing base functionality for a user-typed {@link SystemProperty}.
030 * @param <X> type of ametys object supported by this property
031 */
032public abstract class AbstractUserSystemProperty<X extends ModelAwareDataAwareAmetysObject> extends AbstractSystemProperty<UserIdentity, X>
033{
034    @Override
035    public String getRenderer()
036    {
037        return "Ametys.plugins.cms.search.SearchGridHelper.renderUser";
038    }
039    
040    @Override
041    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
042    {
043        return new UsersQuery(_getSolrFieldName(), operator, operator != Operator.EXISTS ? parseUserArray(value) : new UserIdentity[] {});
044    }
045    
046    /**
047     * Get the Solr field name
048     * @return the field name
049     */
050    protected abstract String _getSolrFieldName();
051    
052    /**
053     * Get the value as {@link UserIdentity}
054     * @param ametysObject the ametys object
055     * @return the user identity
056     */
057    protected abstract UserIdentity _getUserIdentityValue(X ametysObject);
058    
059    @Override
060    public Object getValue(X ametysObject)
061    {
062        return _getUserIdentityValue(ametysObject);
063    }
064    
065    @Override
066    public Object getSortValue(X content)
067    {
068        UserIdentity user = (UserIdentity) getValue(content);
069        if (user != null)
070        {
071            return _userHelper.getUserSortableName(user);
072        }
073        
074        return user;
075    }
076    
077    @Override
078    protected String _getTypeId()
079    {
080        return ModelItemTypeConstants.USER_ELEMENT_TYPE_ID;
081    }
082}