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.plugins.userdirectory.synchronize;
017
018import java.util.Collections;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.collections4.MapUtils;
025import org.slf4j.Logger;
026
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.ModifiableDefaultContent;
029import org.ametys.core.user.UserManager;
030import org.ametys.plugins.contentio.synchronize.impl.SQLSynchronizableContentsCollection;
031import org.ametys.plugins.repository.AmetysObjectIterable;
032import org.ametys.plugins.userdirectory.DeleteUserComponent;
033
034/**
035 * SQL synchronizable collection for users
036 */
037public class SQLSynchronizableUsersCollection extends SQLSynchronizableContentsCollection
038{
039    private static final String __PARAM_SQL_TABLE_LOGIN = "login";
040    private static final String __PARAM_POPULATION_ID = "populationId";
041    
042    /** The user SCC helper */
043    protected UserSCCHelper _userSCCHelper;
044
045    /** The user manager */
046    protected UserManager _userManager;
047    
048    /** The delete user component */
049    protected DeleteUserComponent _deleteUserComponent;
050    
051    @Override
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        super.service(smanager);
055        _userSCCHelper = (UserSCCHelper) smanager.lookup(UserSCCHelper.ROLE);
056        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
057        _deleteUserComponent = (DeleteUserComponent) smanager.lookup(DeleteUserComponent.ROLE);
058    }
059    
060    @Override
061    public String getIdField()
062    {
063        return UserSCCHelper.USER_UNIQUE_ID_METADATA_NAME;
064    }
065    
066    @Override
067    public Map<String, List<String>> getMapping()
068    {
069        Map<String, List<String>> mapping = super.getMapping();
070        mapping.put(getIdField(), Collections.singletonList(getLoginSQLColumnName()));
071        
072        return mapping;
073    }
074    
075    /**
076     * Get the login column name of user SQL table
077     * @return The login column name of user SQL table
078     */
079    public String getLoginSQLColumnName()
080    {
081        return (String) getParameterValues().get(__PARAM_SQL_TABLE_LOGIN);
082    }
083    
084    /**
085     * Get population Id
086     * @return The population Id
087     */
088    public String getPopulationId()
089    {
090        return (String) getParameterValues().get(__PARAM_POPULATION_ID);
091    }
092    
093    @Override
094    protected Map<String, Object> _getSearchParameters(Map<String, Object> parameters, int offset, int limit, List<Object> sort, List<String> columns)
095    {
096        // Add the sql column name for the login. 
097        String loginSQLColumn = getLoginSQLColumnName();
098        if (!columns.contains(loginSQLColumn))
099        {
100            columns.add(loginSQLColumn);
101        }
102        
103        return super._getSearchParameters(parameters, offset, limit, sort, columns);
104    }
105    
106    @Override
107    protected boolean _fillContent(Map<String, List<Object>> remoteValues, ModifiableDefaultContent content, boolean create, Logger logger)
108    {
109        boolean hasChanges = super._fillContent(remoteValues, content, create, logger);
110        
111        String newLogin = remoteValues.get(getIdField()).get(0).toString();
112        hasChanges = _userSCCHelper.synchronizeUserMetadata(newLogin, getPopulationId(), content, logger) || hasChanges;
113        
114        return hasChanges;
115    }
116    
117    
118    @Override
119    protected boolean _checkIdObjectValue(String remoteKey, Object idObjectValue, Logger logger)
120    {
121        if (!super._checkIdObjectValue(remoteKey, idObjectValue, logger))
122        {
123            return false;
124        }
125        
126        String login = idObjectValue.toString();
127        String population = getPopulationId();
128        if (_userManager.getUser(population, login) == null)
129        {
130            logger.warn("The user " + login + " don't belong to population " + population);
131            return false;
132        }
133        
134        return true;
135    }
136    
137    @Override
138    protected void deleteUnexistingContents(Logger logger)
139    {
140        String query = _getContentPathQuery(null, null, null);
141        AmetysObjectIterable<ModifiableDefaultContent> contents = _resolver.query(query);
142        
143        List<Content> contentsToRemove = _getContentsToRemove(contents);
144        if (!contentsToRemove.isEmpty())
145        {
146            _nbDeletedContents += _deleteUserComponent.deleteContentsWithLog(contentsToRemove, MapUtils.EMPTY_SORTED_MAP, MapUtils.EMPTY_SORTED_MAP, logger);
147        }
148    }
149}