001/*
002 *  Copyright 2016 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.core.ui.populations;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022import java.util.stream.Collectors;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032
033import org.ametys.core.cocoon.JSonReader;
034import org.ametys.core.user.directory.ModifiableUserDirectory;
035import org.ametys.core.user.population.PopulationContextHelper;
036import org.ametys.core.user.population.UserPopulation;
037import org.ametys.core.user.population.UserPopulationDAO;
038
039/**
040 * Action for generating the user populations (all of it or by a given context)
041 */
042public class GetUserPopulationsAction extends ServiceableAction
043{
044    /** The user population DAO */
045    private UserPopulationDAO _userPopulationDAO;
046    /** The helper for the associations population/context */
047    private PopulationContextHelper _populationContextHelper;
048
049    @Override
050    public void service(ServiceManager serviceManager) throws ServiceException
051    {
052        super.service(serviceManager);
053        _userPopulationDAO = (UserPopulationDAO) serviceManager.lookup(UserPopulationDAO.ROLE);
054        _populationContextHelper = (PopulationContextHelper) manager.lookup(PopulationContextHelper.ROLE);
055    }
056
057    @Override
058    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
059    {
060        List<UserPopulation> populations;
061        
062        @SuppressWarnings("unchecked")
063        Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
064        
065        @SuppressWarnings("unchecked")
066        List<String> contexts = (List<String>) jsParameters.get("contexts");
067        
068        
069        // GET POPULATIONS
070        Boolean showDisabled = (Boolean) jsParameters.get("showDisabled");
071        
072        if (contexts == null && showDisabled == Boolean.TRUE)
073        {
074            Boolean withAdmin = Boolean.FALSE;
075            if (jsParameters.get("withAdmin") != null)
076            {
077                withAdmin = (Boolean) jsParameters.get("withAdmin");
078            }
079            populations = _userPopulationDAO.getUserPopulations(withAdmin);
080        }
081        else if (contexts == null)
082        {
083            Boolean withAdmin = Boolean.FALSE;
084            if (jsParameters.get("withAdmin") != null)
085            {
086                withAdmin = (Boolean) jsParameters.get("withAdmin");
087            }
088            populations = _userPopulationDAO.getEnabledUserPopulations(withAdmin);
089        }
090        else
091        {
092            Set<String> populationIds = _populationContextHelper.getUserPopulationsOnContexts(contexts, false);
093            populations = populationIds.stream().map(pId -> _userPopulationDAO.getUserPopulation(pId)).collect(Collectors.toList());
094        }
095        
096        // FILTER MODIFIABLE ONLY
097        Boolean modifiable = (Boolean) jsParameters.get("modifiable");
098        if (modifiable == Boolean.TRUE)
099        {
100            populations = populations.stream().filter(up -> containsModifiableUserDirectory(up)).collect(Collectors.toList());
101        }
102        
103        // CONVERT TO JSON
104        Map<String, Object> result = new HashMap<>();
105        List<Object> populationsAsJson = populations.stream().map(_userPopulationDAO::getUserPopulationAsJson).collect(Collectors.toList());
106        result.put("userPopulations", populationsAsJson);
107        
108        Request request = ObjectModelHelper.getRequest(objectModel);
109        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
110        
111        return EMPTY_MAP;
112    }
113
114    private boolean containsModifiableUserDirectory(UserPopulation up)
115    {
116        return up.getUserDirectories().stream().anyMatch(ud -> ud instanceof ModifiableUserDirectory);
117    }
118}