001/*
002 *  Copyright 2024 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.mobileapp.action;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.environment.Request;
026
027import org.ametys.core.user.population.PopulationContextHelper;
028import org.ametys.core.user.population.UserPopulation;
029import org.ametys.core.user.population.UserPopulationDAO;
030import org.ametys.plugins.core.impl.authentication.FormCredentialProvider;
031import org.ametys.plugins.mobileapp.PostConstants;
032
033/**
034 * Returns the latest version compatible between client and server
035 */
036public class GetPopulationsAction extends AbstractPostAction
037{
038    /** The helper for the associations population/context */
039    protected PopulationContextHelper _populationContextHelper;
040    /** The user population DAO */
041    protected UserPopulationDAO _userPopulationDAO;
042
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _populationContextHelper = (PopulationContextHelper) smanager.lookup(PopulationContextHelper.ROLE);
048        _userPopulationDAO = (UserPopulationDAO) smanager.lookup(UserPopulationDAO.ROLE);
049    }
050
051    @Override
052    protected Map<String, Object> doAction(Request request, Map<String, Object> jsonParams)
053    {
054        Map<String, Object> result = new HashMap<>();
055        String siteName = (String) getParameter(PostConstants.SITE_NAME, jsonParams, request);
056
057        if (siteName != null)
058        {
059            List<String> populationContexts = List.of("/sites/" + siteName, "/sites-fo/" + siteName);
060            List<Map<String, Object>> userPopulationsList = new ArrayList<>();
061            for (String context : populationContexts)
062            {
063                userPopulationsList.addAll(_populationContextHelper.getUserPopulationsOnContexts(List.of(context), false, false)
064                        .stream()
065                        .map(_userPopulationDAO::getUserPopulation)
066                        .filter(population -> isCorrectPopulation(population))
067                        .map(userPopulation -> Map.of("key", userPopulation.getId(), "value", userPopulation.getLabel()))
068                        .toList());
069            }
070            result.put("userPopulations", userPopulationsList);
071        }
072
073        result.put("code", 200);
074        return result;
075    }
076
077    private boolean isCorrectPopulation(UserPopulation population)
078    {
079        return population.getCredentialProviders().stream().anyMatch(FormCredentialProvider.class::isInstance);
080    }
081
082}