001/*
002 *  Copyright 2010 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.cocoon;
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.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.acting.ServiceableAction;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031
032import org.ametys.core.cocoon.JSonReader;
033import org.ametys.core.right.RightManager;
034import org.ametys.core.user.CurrentUserProvider;
035import org.ametys.plugins.repository.AmetysObject;
036import org.ametys.plugins.repository.AmetysObjectIterable;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.plugins.repository.TraversableAmetysObject;
039import org.ametys.web.repository.page.Page;
040import org.ametys.web.repository.page.PagesContainer;
041import org.ametys.web.repository.page.SitemapDAO;
042import org.ametys.web.repository.site.Site;
043import org.ametys.web.repository.sitemap.Sitemap;
044
045/**
046 * Get children of a {@link Site}, {@link Sitemap} or {@link PagesContainer}
047 * Waits for the id of the parent ametys object in request parameters.
048 */
049public class SitemapAction extends ServiceableAction
050{
051    /** The {@link AmetysObjectResolver} */
052    protected AmetysObjectResolver _resolver;
053    /** The sitemap DAO */
054    protected SitemapDAO _sitemapDAO;
055    /** The right manager */
056    protected RightManager _rightManager;
057    /** The user provider */
058    protected CurrentUserProvider _currentUserProvider;
059    
060    @Override
061    public void service(ServiceManager smanager) throws ServiceException
062    {
063        super.service(smanager);
064        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
065        _sitemapDAO = (SitemapDAO) smanager.lookup(SitemapDAO.ROLE);
066        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
067        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
068    }
069    
070    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
071    {
072        Map<String, Object> result = new HashMap<>();
073        
074        Request request = ObjectModelHelper.getRequest(objectModel);
075        String id = request.getParameter("id");
076        
077        @SuppressWarnings("unchecked")
078        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
079        @SuppressWarnings("unchecked")
080        List<String> rights = (List<String>) jsParameters.get("rights");
081        
082        AmetysObject ao = _resolver.resolveById(id);
083        if (ao instanceof Site)
084        {
085            result.put("sitemaps", sitemaps2json((Site) ao, rights));
086        }
087        else if (ao instanceof PagesContainer)
088        {
089            result.put("pages", pagescontainer2json((PagesContainer) ao, rights));
090        }
091        else
092        {
093            result.put("sites", sites2json((TraversableAmetysObject) ao));
094        }
095        
096        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
097        return EMPTY_MAP;
098    }
099    
100    /**
101     * Get the JSON object representing all sites
102     * @param ao The parent ametys object
103     * @return the JSON object
104     */
105    protected List<Map<String, Object>> sites2json (TraversableAmetysObject ao)
106    {
107        List<Map<String, Object>> sites = new ArrayList<>();
108        
109        AmetysObjectIterable< ? extends Site> sitesAO = ao.getChildren();
110        
111        for (Site site : sitesAO)
112        {
113            sites.add(site2json(site));
114        }
115        
116        return sites;
117    }
118    
119    /**
120     * Get the JSON object representing a site
121     * @param site the site
122     * @return the JSON object
123     */
124    protected Map<String, Object> site2json (Site site)
125    {
126        Map<String, Object> jsonObject = new HashMap<>();
127        
128        jsonObject.put("id", site.getId());
129        jsonObject.put("name", site.getName());
130        jsonObject.put("title", site.getTitle());
131        jsonObject.put("type", site.getType());
132        jsonObject.put("url", site.getUrl());
133        jsonObject.put("description", site.getDescription());
134        
135        return jsonObject;
136    }
137
138    /**
139     * Get the JSON object representing the sitemaps of a site
140     * @param site the site
141     * @param rights The rights to check. Can be null or empty to ignore user rights.
142     * @return the JSON object
143     */
144    protected List<Map<String, Object>> sitemaps2json (Site site, List<String> rights)
145    {
146        List<Map<String, Object>> sitemaps = new ArrayList<>();
147        
148        for (Sitemap sitemap : site.getSitemaps())
149        {
150            sitemaps.add(_sitemapDAO.getSitemapProperties(sitemap, rights));
151        }
152        
153        return sitemaps;
154    }
155    
156    /**
157     * Get the JSON object representing child pages of a page container.
158     * @param pageContainer The page container
159     * @param rights The rights to check. Can be null or empty to ignore user rights.
160     * @return the JSON object
161     */
162    protected List<Map<String, Object>> pagescontainer2json (PagesContainer pageContainer, List<String> rights)
163    {
164        List<Map<String, Object>> pages = new ArrayList<>();
165        
166        for (Page page : pageContainer.getChildrenPages())
167        {
168            pages.add(_sitemapDAO.getPageProperties(page, rights));
169        }
170        
171        return pages;
172    }
173}