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.explorer;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.generation.ServiceableGenerator;
029import org.apache.cocoon.xml.AttributesImpl;
030import org.apache.cocoon.xml.XMLUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.right.RightManager;
034import org.ametys.core.user.CurrentUserProvider;
035import org.ametys.core.user.UserIdentity;
036import org.ametys.plugins.explorer.resources.ModifiableResourceCollection;
037import org.ametys.plugins.explorer.resources.ResourceCollection;
038import org.ametys.plugins.repository.AmetysObjectResolver;
039import org.ametys.plugins.repository.ModifiableAmetysObject;
040
041
042/**
043 * SAX the explorer nodes information
044 */
045public class ResourceCollectionInformationGenerator extends ServiceableGenerator
046{
047    private AmetysObjectResolver _resolver;
048
049    private RightManager _rightManager;
050
051    private CurrentUserProvider _userProvider;
052
053    @Override
054    public void service(ServiceManager serviceManager) throws ServiceException
055    {
056        super.service(serviceManager);
057
058        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
059        _rightManager = (RightManager) serviceManager.lookup(RightManager.ROLE);
060        _userProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
061    }
062
063    @SuppressWarnings("unchecked")
064    @Override
065    public void generate() throws IOException, SAXException, ProcessingException
066    {
067        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
068        
069        List<String> ids = null;
070        Object idParam = jsParameters.get("id");
071        if (idParam instanceof String)
072        {
073            ids = new ArrayList<>();
074            ids.add((String) idParam);
075        }
076        else
077        {
078            ids = (List<String>) idParam;
079        }
080
081        contentHandler.startDocument();
082        XMLUtils.startElement(contentHandler, "resources");
083
084        for (String id : ids)
085        {
086            ResourceCollection collection = (ResourceCollection) _resolver.resolveById(id);
087            
088            boolean isShared = ResourceHelper.isShared(collection);
089            String sharedRoot = ResourceHelper.getSharedRootName(collection);
090            
091            AttributesImpl attrs = new AttributesImpl();
092            attrs.addAttribute("", "id", "id", "CDATA", collection.getId());
093            attrs.addAttribute("", "applicationId", "applicationId", "CDATA", collection.getApplicationId());
094            attrs.addAttribute("", "name", "name", "CDATA", collection.getName());
095            attrs.addAttribute("", "path", "path", "CDATA", collection.getExplorerPath());
096            attrs.addAttribute("", "isModifiable", "isModifiable", "CDATA", String.valueOf(collection instanceof ModifiableAmetysObject));
097            attrs.addAttribute("", "canCreateChild", "canCreateChild", "CDATA", String.valueOf(collection instanceof ModifiableResourceCollection));
098            attrs.addAttribute("", "isShared", "isShared", "CDATA", String.valueOf(isShared));
099            if (isShared)
100            {
101                attrs.addCDATAAttribute("sharedRoot", sharedRoot);
102            }
103            
104            XMLUtils.startElement(contentHandler, "resource-collection", attrs);
105            _saxUserRights(collection);
106            XMLUtils.endElement(contentHandler, "resource-collection");
107        }
108
109        XMLUtils.endElement(contentHandler, "resources");
110        contentHandler.endDocument();
111    }
112
113    private void _saxUserRights(ResourceCollection collection) throws SAXException
114    {
115        UserIdentity user = _userProvider.getUser();
116        Set<String> rights = _rightManager.getUserRights(user, collection);
117
118        XMLUtils.startElement(contentHandler, "user-rights");
119
120        for (String rightID : rights)
121        {
122            AttributesImpl rightAttr = new AttributesImpl();
123            rightAttr.addCDATAAttribute("id", rightID);
124            XMLUtils.createElement(contentHandler, "right", rightAttr);
125        }
126        XMLUtils.endElement(contentHandler, "user-rights");
127    }
128
129}