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.groupdirectories;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.avalon.framework.parameters.Parameters;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.cocoon.acting.ServiceableAction;
029import org.apache.cocoon.environment.ObjectModelHelper;
030import org.apache.cocoon.environment.Redirector;
031import org.apache.cocoon.environment.Request;
032import org.apache.cocoon.environment.SourceResolver;
033
034import org.ametys.core.cocoon.JSonReader;
035import org.ametys.core.group.GroupDirectoryContextHelper;
036import org.ametys.core.group.GroupDirectoryDAO;
037import org.ametys.core.group.directory.GroupDirectory;
038
039/**
040 * Action for generating the group directories (all of it or by a given context)
041 */
042public class GetGroupDirectoriesAction extends ServiceableAction
043{
044    /** The helper for the associations group directory/context */
045    protected GroupDirectoryContextHelper _directoryContextHelper;
046    /** The DAO for group directories */
047    private GroupDirectoryDAO _groupDirectoryDAO;
048
049    @Override
050    public void service(ServiceManager smanager) throws ServiceException
051    {
052        super.service(smanager);
053        _groupDirectoryDAO = (GroupDirectoryDAO) smanager.lookup(GroupDirectoryDAO.ROLE);
054        _directoryContextHelper = (GroupDirectoryContextHelper) smanager.lookup(GroupDirectoryContextHelper.ROLE);
055    }
056    
057    @Override
058    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
059    {
060        List<Object> groupDirectories;
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        if (contexts == null)
068        {
069            groupDirectories = _groupDirectoryDAO.getGroupDirectories2Json();
070        }
071        else
072        {
073            groupDirectories = new ArrayList<>();
074            
075            Set<String> groupDirectoryIds = _directoryContextHelper.getGroupDirectoriesOnContexts(new HashSet<>(contexts));
076            for (String gdId : groupDirectoryIds)
077            {
078                GroupDirectory gd = _groupDirectoryDAO.getGroupDirectory(gdId);
079                groupDirectories.add(_groupDirectoryDAO.getGroupDirectory2Json(gd));
080            }
081        }
082        
083        Map<String, Object> result = new HashMap<>();
084        result.put("groupDirectories", groupDirectories);
085        
086        Request request = ObjectModelHelper.getRequest(objectModel);
087        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
088        
089        return EMPTY_MAP;
090    }
091
092}