001/*
002 *  Copyright 2020 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.workspaces.report;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.ProcessingException;
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.workspaces.categories.Category;
032import org.ametys.plugins.workspaces.categories.CategoryHelper;
033import org.ametys.plugins.workspaces.project.objects.Project;
034
035/**
036 * Generator for the CSV report service.
037 * Sax data required to select which report to use, and its context
038 */
039public class ReportServiceGenerator extends ServiceableGenerator
040{
041    private ReportHelper _reportHelper;
042    private CategoryHelper _categoryHelper;
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        _reportHelper = (ReportHelper) smanager.lookup(ReportHelper.ROLE);
048        _categoryHelper = (CategoryHelper) smanager.lookup(CategoryHelper.ROLE);
049    }
050    
051    public void generate() throws IOException, SAXException, ProcessingException
052    {
053        contentHandler.startDocument();
054        XMLUtils.startElement(contentHandler, "reports");
055        
056        List<Project> availableProjects = _reportHelper.getAvailableProjects();
057        // Get the root categories from the available projects
058        Set<Category> rootCategories = _reportHelper.getAvailableCategories(availableProjects);
059        
060        saxCategories(rootCategories);
061        saxProjects(availableProjects);
062        
063        XMLUtils.endElement(contentHandler, "reports");
064        contentHandler.endDocument();
065    }
066    
067    /**
068     * SAX the available projects
069     * @param projects the projects to sax
070     * @throws SAXException if an error occurred while saxing
071     */
072    protected void saxProjects(List<Project> projects) throws SAXException
073    {
074        XMLUtils.startElement(contentHandler, "projects");
075        for (Project project : projects)
076        {
077            AttributesImpl attrs = new AttributesImpl();
078            attrs.addCDATAAttribute("id", project.getId());
079            attrs.addCDATAAttribute("name", project.getName());
080            XMLUtils.createElement(contentHandler, "project", attrs, project.getTitle());
081        }
082        XMLUtils.endElement(contentHandler, "projects");
083    }
084    
085    /**
086     * SAX the categories
087     * @param rootCategories the root categories to sax
088     * @throws SAXException if an error occurred while saxing
089     */
090    protected void saxCategories(Set<Category> rootCategories) throws SAXException
091    {
092        XMLUtils.startElement(contentHandler, "categories");
093        for (Category category : rootCategories)
094        {
095            AttributesImpl attrs = new AttributesImpl();
096            attrs.addCDATAAttribute("id", category.getId());
097            attrs.addCDATAAttribute("name", category.getName());
098            XMLUtils.startElement(contentHandler, "category", attrs);
099            
100            Map<String, String> colors = _categoryHelper.getCategoryColor(category);
101            XMLUtils.createElement(contentHandler, "color", colors.get("main"));
102            category.getTitle().toSAX(contentHandler, "title");
103            XMLUtils.endElement(contentHandler, "category");
104        }
105        XMLUtils.endElement(contentHandler, "categories");
106    }
107    
108}