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.events.calendars;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import javax.jcr.Node;
024import javax.jcr.RepositoryException;
025
026import org.apache.avalon.framework.context.Context;
027import org.apache.avalon.framework.context.ContextException;
028import org.apache.avalon.framework.context.Contextualizable;
029import org.apache.avalon.framework.service.ServiceException;
030import org.apache.avalon.framework.service.ServiceManager;
031import org.apache.cocoon.components.ContextHelper;
032import org.apache.cocoon.environment.Request;
033
034import org.ametys.plugins.explorer.ObservationConstants;
035import org.ametys.plugins.explorer.calendars.Calendar;
036import org.ametys.plugins.explorer.calendars.CalendarEvent;
037import org.ametys.plugins.repository.RepositoryConstants;
038import org.ametys.plugins.repository.events.EventType;
039import org.ametys.plugins.workspaces.calendars.CalendarWorkspaceModule;
040import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint;
041import org.ametys.plugins.workspaces.project.objects.Project;
042
043/**
044 * {@link EventType} implementation for the creation of a calendar event
045 */
046public class CalendarEventCreatedOrUpdatedEventType extends CalendarsEventType implements Contextualizable
047{
048    /** Constant for calendar event's title */
049    public static final String EVENT_CALENDAR_EVENT_TITLE_PROPERTY = RepositoryConstants.NAMESPACE_PREFIX + ":eventTitle";
050    /** Constant for calendar event's id */
051    public static final String EVENT_CALENDAR_EVENT_ID_PROPERTY = RepositoryConstants.NAMESPACE_PREFIX + ":eventId";
052    
053    private CalendarWorkspaceModule _calendarModule;
054    
055    private Context _context;
056    
057    @Override
058    public void contextualize(Context context) throws ContextException
059    {
060        _context = context;
061    }
062    
063    @Override
064    public void service(ServiceManager serviceManager) throws ServiceException
065    {
066        super.service(serviceManager);
067        WorkspaceModuleExtensionPoint moduleManagerEP = (WorkspaceModuleExtensionPoint) serviceManager.lookup(WorkspaceModuleExtensionPoint.ROLE);
068        _calendarModule = moduleManagerEP.getModule(CalendarWorkspaceModule.CALENDAR_MODULE_ID);
069    }
070    
071    @Override
072    protected void storeAdditionalEventData(Node eventNode, Map<String, Object> parameters) throws RepositoryException
073    {
074        super.storeAdditionalEventData(eventNode, parameters);
075        
076        Calendar calendar = (Calendar) parameters.get(ObservationConstants.ARGS_CALENDAR);
077        CalendarEvent calendarEvent = (CalendarEvent) parameters.get(ObservationConstants.ARGS_CALENDAR_EVENT);
078        
079        // Calendar
080        eventNode.setProperty(EVENT_CALENDAR_TITLE_PROPERTY, calendar.getName());
081        eventNode.setProperty(EVENT_CALENDAR_ID_PROPERTY, calendar.getId());
082        
083        // Event
084        eventNode.setProperty(EVENT_CALENDAR_EVENT_TITLE_PROPERTY, calendarEvent.getTitle());
085        eventNode.setProperty(EVENT_CALENDAR_EVENT_ID_PROPERTY, calendarEvent.getId());
086    }
087    
088    @Override
089    public Map<String, Object> event2JSON(Node eventNode) throws RepositoryException
090    {
091        Map<String, Object> event = super.event2JSON(eventNode);
092        
093        String calendarId = eventNode.getProperty(EVENT_CALENDAR_ID_PROPERTY).getString();
094        event.put("calendarId", calendarId);
095        event.put("calendarTitle", eventNode.getProperty(EVENT_CALENDAR_TITLE_PROPERTY).getString());
096        
097        String eventId = eventNode.getProperty(EVENT_CALENDAR_EVENT_ID_PROPERTY).getString();
098        event.put("eventId", eventId);
099        event.put("eventTitle", eventNode.getProperty(EVENT_CALENDAR_EVENT_TITLE_PROPERTY).getString());
100        
101        Request request = ContextHelper.getRequest(_context);
102        String lang = (String) request.getAttribute("sitemapLanguage");
103        
104        String projectName = eventNode.getProperty(EVENT_PROJECT_NAME_PROPERTY).getString();
105        Project project = _projectManager.getProject(projectName);
106        event.put("eventUrl", _calendarModule.getEventUri(project, calendarId, eventId, lang));
107        
108        return event;
109    }
110    
111    @Override
112    public boolean isMergeable(Map<String, Object> event1, Map<String, Object> event2)
113    {
114        if (!super.isMergeable(event1, event2))
115        {
116            return false;
117        }
118        
119        String calendar1 = (String) event1.get("calendarId");
120        String calendar2 = (String) event1.get("calendarId");
121        
122        return calendar1 != null && calendar2 != null && calendar1.equals(calendar2);
123    }
124    
125    @Override
126    public Map<String, Object> mergeEvents(List<Map<String, Object>> events)
127    {
128        Map<String, Object> mergedEVent = super.mergeEvents(events);
129        
130        List<Map<String, Object>> mergedEvents = new ArrayList<>();
131        
132        for (Map<String, Object> event : events)
133        {
134            Map<String, Object> eventInfo = new HashMap<>();
135            
136            eventInfo.put("eventTitle", event.get("eventTitle"));
137            eventInfo.put("eventId", event.get("eventId"));
138            eventInfo.put("eventUrl", event.get("eventUrl"));
139            
140            mergedEvents.add(eventInfo);
141        }
142        mergedEVent.put("events", mergedEvents);
143        
144        return mergedEVent;
145    }
146}