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.plugins.explorer;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.plugins.explorer.applications.ExplorerApplication;
032import org.ametys.plugins.explorer.applications.ExplorerApplicationExtensionPoint;
033import org.ametys.plugins.explorer.resources.ResourceCollection;
034import org.ametys.plugins.explorer.resources.generators.ResourcesExplorerGenerator;
035import org.ametys.plugins.repository.AmetysObject;
036import org.ametys.plugins.repository.AmetysObjectIterable;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.plugins.repository.ModifiableAmetysObject;
039import org.ametys.plugins.repository.TraversableAmetysObject;
040
041/**
042 * Generates a subtree of the resources explorer.
043 */
044public class ExplorerNodeGenerator extends ServiceableGenerator
045{
046    /** The resolver for ametys object */
047    protected AmetysObjectResolver _resolver;
048    
049    /** The explorer application EP */
050    protected ExplorerApplicationExtensionPoint _explorerApplicationEP;
051    
052    @Override
053    public void service(ServiceManager sManager) throws ServiceException
054    {
055        super.service(sManager);
056        _resolver = (AmetysObjectResolver) sManager.lookup(AmetysObjectResolver.ROLE);
057        _explorerApplicationEP = (ExplorerApplicationExtensionPoint) sManager.lookup(ExplorerApplicationExtensionPoint.ROLE);
058    }
059    
060    @Override
061    public void generate() throws IOException, SAXException, ProcessingException
062    {
063        Request request = ObjectModelHelper.getRequest(objectModel);
064        
065        String id = request.getParameter("node");
066        ExplorerNode node = _resolver.resolveById(id);
067        
068        _saxExplorerNode(node);
069    }
070    
071    private void _saxExplorerNode (ExplorerNode node) throws SAXException
072    {
073        contentHandler.startDocument();
074        
075        AttributesImpl atts = new AttributesImpl();
076        atts.addCDATAAttribute("id", node.getId());
077        
078        XMLUtils.startElement(contentHandler, "Nodes", atts);
079        
080        if (node instanceof TraversableAmetysObject)
081        {
082            TraversableAmetysObject traversableObject = (TraversableAmetysObject) node;
083            AmetysObjectIterable<AmetysObject> children = traversableObject.getChildren();
084            
085            for (AmetysObject child : children)
086            {
087                if (child instanceof ExplorerNode)
088                {
089                    saxExplorerNode((ExplorerNode) child);
090                }
091            }
092        }
093        
094        XMLUtils.endElement(contentHandler, "Nodes");
095        
096        contentHandler.endDocument();
097    }
098    
099    /**
100     * Sax a {@link ExplorerNode}
101     * @param node The node to sax
102     * @throws SAXException If an error occurred while saxing
103     */
104    protected void saxExplorerNode (ExplorerNode node) throws SAXException
105    {
106        AttributesImpl childAtts = new AttributesImpl();
107        childAtts.addCDATAAttribute("id", node.getId());
108        childAtts.addCDATAAttribute("name", node.getName());
109        childAtts.addCDATAAttribute("iconCls", node.getIconCls());
110        childAtts.addCDATAAttribute("applicationId", node.getApplicationId());
111        
112        String relPath = node.getExplorerPath();
113        childAtts.addCDATAAttribute("path", relPath.startsWith("/") ? relPath.substring(1) : relPath);
114        childAtts.addCDATAAttribute("type", ResourcesExplorerGenerator.RESOURCE_COLLECTION);
115        
116        boolean hasResources = false;
117        if (node instanceof ResourceCollection)
118        {
119            hasResources = ((ResourceCollection) node).hasChildResources();
120        }
121        boolean hasChildNodes = node.hasChildExplorerNodes();
122        
123        if (hasChildNodes)
124        {
125            childAtts.addCDATAAttribute("hasChildNodes", "true");
126        }
127        
128        if (hasResources)
129        {
130            childAtts.addCDATAAttribute("hasResources", "true");
131        }
132        
133        if (node instanceof ModifiableAmetysObject)
134        {
135            childAtts.addCDATAAttribute("isModifiable", "true");
136        }
137        
138        if (node instanceof ModifiableExplorerNode)
139        {
140            childAtts.addCDATAAttribute("canCreateChild", "true");
141        }
142        
143        // Adding attributs specific to applications
144        String applicationId = node.getApplicationId();
145        ExplorerApplication explorerApplication = _explorerApplicationEP.hasExtension(applicationId) ? _explorerApplicationEP.getExtension(applicationId) : null;
146        if (explorerApplication != null)
147        {
148            Map<String, String> additionalNodeParameters = explorerApplication.getAdditionalNodeParameters(node);
149            if (additionalNodeParameters != null)
150            {
151                additionalNodeParameters.entrySet().stream()
152                    .forEach(entry -> childAtts.addCDATAAttribute(entry.getKey(), entry.getValue()));
153            }
154        }
155        
156        XMLUtils.createElement(contentHandler, "Node", childAtts);
157    }
158}