001/*
002 *  Copyright 2016 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.calendars.generators;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.generation.ServiceableGenerator;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.apache.commons.lang3.BooleanUtils;
029import org.apache.commons.lang3.StringUtils;
030import org.xml.sax.SAXException;
031
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.plugins.workspaces.calendars.CalendarWorkspaceModule;
034import org.ametys.plugins.workspaces.project.ProjectsCatalogueManager;
035import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint;
036import org.ametys.plugins.workspaces.project.objects.Project;
037
038/**
039 * Filtered events generator for the simple service.
040 */
041public class FilteredEventsGenerator extends ServiceableGenerator
042{
043    /** The calendar manager */
044    private CalendarWorkspaceModule _calendarModule;
045    private ProjectsCatalogueManager _projectCatalogManager;
046    private AmetysObjectResolver _resolver;
047    
048    @Override
049    public void service(ServiceManager serviceManager) throws ServiceException
050    {
051        WorkspaceModuleExtensionPoint moduleManagerEP = (WorkspaceModuleExtensionPoint) serviceManager.lookup(WorkspaceModuleExtensionPoint.ROLE);
052        _calendarModule = moduleManagerEP.getModule(CalendarWorkspaceModule.CALENDAR_MODULE_ID);
053        
054        _projectCatalogManager = (ProjectsCatalogueManager)  serviceManager.lookup(ProjectsCatalogueManager.ROLE);
055        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
056    }
057
058    @Override
059    public void generate() throws IOException, SAXException, ProcessingException
060    {
061        contentHandler.startDocument();
062        
063        int months = parameters.getParameterAsInteger("search-period", 3);
064        int max = parameters.getParameterAsInteger("max-results", Integer.MAX_VALUE);
065        
066        List<String> calendarIds = null;
067        List<String> tagIds = null;
068        
069        XMLUtils.startElement(contentHandler, "events");
070        
071        _calendarModule.getUpcomingEvents(months, max, calendarIds, tagIds)
072            .stream()
073            .forEach(this::_saxEvent);
074        
075        XMLUtils.endElement(contentHandler, "events");
076
077        contentHandler.endDocument();
078    }
079    
080    /**
081     * SAX necessary event properties for the simple filtered event service.
082     * @param eventData map containing the event data
083     */
084    protected void _saxEvent(Map<String, Object> eventData)
085    {
086        try
087        {
088            boolean calendarHasViewRight = BooleanUtils.isTrue((Boolean) eventData.get("calendarHasViewRight"));
089            boolean calendarIsPublic = BooleanUtils.isTrue((Boolean) eventData.get("calendarIsPublic"));
090            
091            AttributesImpl attrs = new AttributesImpl();
092            
093            attrs.addCDATAAttribute("id", (String) eventData.get("id"));
094            attrs.addCDATAAttribute("calendarHasViewRight", Boolean.toString(calendarHasViewRight));
095            attrs.addCDATAAttribute("calendarIsPublic", Boolean.toString(calendarIsPublic));
096            XMLUtils.startElement(contentHandler, "event", attrs);
097            
098            // project 
099            attrs.clear();
100            String projectId = (String) eventData.get("projectId");
101            attrs.addCDATAAttribute("id", (String) eventData.get(projectId));
102            XMLUtils.createElement(contentHandler, "project", (String) eventData.get("projectTitle"));
103            
104            Project project = _resolver.resolveById(projectId);
105            _projectCatalogManager.saxCategory(contentHandler, project, "projectCategory");
106            
107            XMLUtils.createElement(contentHandler, "calendar", (String) eventData.get("calendar"));
108            
109            XMLUtils.createElement(contentHandler, "title", (String) eventData.get("title"));
110            
111            String startDate = (String) eventData.get("startDate");
112            XMLUtils.createElement(contentHandler, "startDate", startDate);
113            
114            // Link to calendar module is provided only is the user has the right to view the calendar
115            if (calendarHasViewRight)
116            {
117                String eventUrl = (String) eventData.get("eventUrl");
118                if (StringUtils.isNotEmpty(eventUrl))
119                {
120                    XMLUtils.createElement(contentHandler, "eventUrl", eventUrl);
121                }
122            }
123            
124            XMLUtils.endElement(contentHandler, "event");
125        }
126        catch (SAXException e)
127        {
128            throw new RuntimeException("An error occurred while gathering the events' information.");
129        }
130    }
131}