001/*
002 *  Copyright 2017 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.wiki;
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.ametys.cms.transformation.xslt.ResolveURIComponent;
027import org.ametys.plugins.repository.events.EventType;
028
029/**
030 * {@link EventType} implementation for the creation of a new page in the wiki module
031 */
032public class WikiPageUpdatedEventType extends WikiEventType
033{
034    @Override
035    public Map<String, Object> event2JSON(Node eventNode) throws RepositoryException
036    {
037        Map<String, Object> event = super.event2JSON(eventNode);
038        if (eventNode.hasProperty(EVENT_PAGE_ID_PROPERTY))
039        {
040            String pageId = eventNode.getProperty(EVENT_PAGE_ID_PROPERTY).getString();
041            event.put("pageId", pageId);
042            event.put("pageTitle", eventNode.getProperty(EVENT_PAGE_TITLE_PROPERTY).getString());
043            event.put("pageUrl", ResolveURIComponent.resolve("page", pageId));
044        }
045        
046        return event;
047    }
048    
049    @Override
050    public Map<String, Object> mergeEvents(List<Map<String, Object>> events)
051    {
052        Map<String, Object> mergedEvent = super.mergeEvents(events);
053        
054        List<Map<String, Object>> mergedPages = new ArrayList<>();
055        
056        List<String> knownPages = new ArrayList<>();
057        
058        for (Map<String, Object> event : events)
059        {
060            String pageId = (String) event.get("pageId");
061            if (!knownPages.contains(pageId))
062            {
063                knownPages.add(pageId);
064                Map<String, Object> pageInfo = new HashMap<>();
065                
066                pageInfo.put("pageTitle", event.get("pageTitle"));
067                pageInfo.put("pageUrl", event.get("pageUrl"));
068                
069                mergedPages.add(pageInfo);
070            }
071        }
072        mergedEvent.put("pages", mergedPages);
073        mergedEvent.put("amount", mergedPages.size());
074        
075        return mergedEvent;
076    }
077}