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.runtime.workspace;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.cocoon.ProcessingException;
022import org.apache.cocoon.generation.AbstractGenerator;
023import org.apache.cocoon.xml.AttributesImpl;
024import org.apache.cocoon.xml.XMLUtils;
025import org.xml.sax.SAXException;
026
027import org.ametys.runtime.servlet.RuntimeConfig;
028import org.ametys.runtime.workspace.WorkspaceManager.InactivityCause;
029
030/**
031 * SAXes the data for the workspace tree
032 */
033public class WorkspacesGenerator extends AbstractGenerator
034{
035    public void generate() throws IOException, SAXException, ProcessingException
036    {
037        contentHandler.startDocument();
038        
039        _saxWorkspaces();
040        
041        contentHandler.endDocument();
042    }
043    
044    private void _saxWorkspaces() throws SAXException
045    {
046        String defaultWorkspace = RuntimeConfig.getInstance().getDefaultWorkspace();
047        AttributesImpl attrs2 = new AttributesImpl();
048        attrs2.addCDATAAttribute("default", defaultWorkspace);
049        XMLUtils.startElement(contentHandler, "workspaces", attrs2);
050        
051        for (Workspace workspace : WorkspaceManager.getInstance().getWorkspaces().values())
052        {
053            XMLUtils.startElement(contentHandler, "workspace");
054            XMLUtils.createElement(contentHandler, "name", workspace.getName());
055            XMLUtils.createElement(contentHandler, "theme", workspace.getThemeName());
056            XMLUtils.endElement(contentHandler, "workspace");
057        }
058        
059        Map<String, InactivityCause> inactiveWorkspaces = WorkspaceManager.getInstance().getInactiveWorkspaces();
060        for (String workspace : inactiveWorkspaces.keySet())
061        {
062            InactivityCause inactivityCause = inactiveWorkspaces.get(workspace);
063            
064            AttributesImpl attrs = new AttributesImpl();
065            attrs.addCDATAAttribute("inactive", "true");
066            attrs.addCDATAAttribute("cause", inactivityCause.toString());
067            XMLUtils.createElement(contentHandler, "workspace", attrs, workspace);
068        }
069        
070        XMLUtils.endElement(contentHandler, "workspaces");
071    }
072
073}