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.web.workspace;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.Parameters;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Redirector;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.environment.SourceResolver;
028
029import org.ametys.core.cocoon.ActionResultGenerator;
030import org.ametys.web.repository.site.GetSitesAction;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteType;
033
034/**
035 * Get all the available sites for log-in user
036 *
037 */
038public class UserSitesAction extends GetSitesAction
039{
040    @Override
041    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
042    {
043        Request request = ObjectModelHelper.getRequest(objectModel);
044        
045        List<Map<String, Object>> sites = new ArrayList<>();
046        
047        for (String siteName : _siteManager.getGrantedSites())
048        {
049            Site site = _siteManager.getSite(siteName);
050            
051            SiteType siteType = _siteTypeExtensionPoint.getExtension(site.getType());
052            if (siteType != null)
053            {
054                sites.add(_site2json(site));
055            }
056        }
057        
058        Map<String, Object> result = new HashMap<>();
059        result.put("sites", sites);
060        request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
061        
062        return EMPTY_MAP;
063    }
064}