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.Collection;
019import java.util.Iterator;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.contenttype.MetadataType;
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.search.model.SystemProperty;
028import org.ametys.cms.search.query.Query;
029import org.ametys.cms.search.query.Query.Operator;
030import org.ametys.cms.search.query.UsersQuery;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.core.util.JSONUtils;
033import org.ametys.plugins.core.user.UserHelper;
034
035/**
036 * Abstract class providing base functionality for a user-typed {@link SystemProperty}.
037 */
038public abstract class AbstractUserSystemProperty extends AbstractSystemProperty
039{
040    /** The user helper */
041    protected UserHelper _userHelper;
042    /** The JSON utils */
043    protected JSONUtils _jsonUtils;
044
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE);
050        _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE);
051    }
052    
053    @Override
054    public MetadataType getType()
055    {
056        return MetadataType.USER;
057    }
058    
059    @Override
060    public String getRenderer()
061    {
062        return "Ametys.cms.content.EditContentsGrid.renderUser";
063    }
064    
065    @Override
066    public String getConverter()
067    {
068        return "Ametys.cms.content.EditContentsGrid.convertUser";
069    }
070    
071    @Override
072    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
073    {
074        UserIdentity[] users = parseUserArray(value);
075        return new UsersQuery(_getSolrFieldName(), users);
076    }
077    
078    /**
079     * Get the Solr field name
080     * @return the field name
081     */
082    protected abstract String _getSolrFieldName();
083    
084    @Override
085    public Object getSortValue(Content content)
086    {
087        Object value = getValue(content);
088        if (value != null)
089        {
090            if (value instanceof String)
091            {
092                String sortablename = _userHelper.getUserSortableName(UserIdentity.stringToUserIdentity((String) value));
093                if (sortablename != null)
094                {
095                    return sortablename;
096                }
097            }
098            else if (value instanceof Collection<?>)
099            {
100                Iterator<?> values = ((Collection<?>) value).iterator();
101                if (values.hasNext())
102                {
103                    String sortablename = _userHelper.getUserSortableName(UserIdentity.stringToUserIdentity((String) values.next()));
104                    if (sortablename != null)
105                    {
106                        return sortablename;
107                    }
108                }
109            }
110        }
111        
112        return getValue(content);
113    }
114    
115    /**
116     * Set the user info in the given map.
117     * @param infos The map to fill with the user infos.
118     * @param user The user.
119     */
120    protected void setUserInfos(Map<String, Object> infos, UserIdentity user)
121    {
122        Map<String, Object> user2json = _userHelper.user2json(user, true);
123        infos.putAll(user2json);
124    }
125}