001/*
002 *  Copyright 2019 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.projects;
017
018import java.util.Map;
019
020import javax.jcr.Node;
021import javax.jcr.RepositoryException;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.cms.ObservationConstants;
027import org.ametys.cms.repository.Content;
028import org.ametys.plugins.repository.RepositoryConstants;
029import org.ametys.plugins.repository.events.EventType;
030import org.ametys.plugins.workspaces.wall.WallContentManager;
031
032/**
033 * {@link EventType} implementation for the addition of a wall content
034 */
035public class WallContentCreatedEventType extends ProjectsEventType
036{
037    /** Constant for content's summary */
038    public static final String EVENT_WALL_CONTENT_SUMMARY_PROPERTY = RepositoryConstants.NAMESPACE_PREFIX + ":contentSummary";
039    /** Constant for content's id */
040    public static final String EVENT_WALL_CONTENT_ID_PROPERTY = RepositoryConstants.NAMESPACE_PREFIX + ":contentId";
041    /** Constant for content's type */
042    public static final String EVENT_WALL_CONTENT_TYPE_PROPERTY = RepositoryConstants.NAMESPACE_PREFIX + ":contentType";
043    
044    private WallContentManager _wallContentManager;
045    
046    @Override
047    public void service(ServiceManager serviceManager) throws ServiceException
048    {
049        super.service(serviceManager);
050        _wallContentManager = (WallContentManager) serviceManager.lookup(WallContentManager.ROLE);
051    }
052    
053    @Override
054    protected void storeAdditionalEventData(Node eventNode, Map<String, Object> parameters) throws RepositoryException
055    {
056        super.storeAdditionalEventData(eventNode, parameters);
057        
058        Content content = (Content) parameters.get(ObservationConstants.ARGS_CONTENT);
059        eventNode.setProperty(EVENT_WALL_CONTENT_ID_PROPERTY, content.getId());
060        eventNode.setProperty(EVENT_WALL_CONTENT_SUMMARY_PROPERTY, _wallContentManager.getExcerpt(content, 100));
061        eventNode.setProperty(EVENT_WALL_CONTENT_TYPE_PROPERTY, content.getTypes()[0]);
062    }
063    
064    @Override
065    public Map<String, Object> event2JSON(Node eventNode) throws RepositoryException
066    {
067        Map<String, Object> event = super.event2JSON(eventNode);
068        
069        event.put("contentId", eventNode.getProperty(EVENT_WALL_CONTENT_ID_PROPERTY).getString());
070        event.put("contentSummary", eventNode.getProperty(EVENT_WALL_CONTENT_SUMMARY_PROPERTY).getString());
071        event.put("contentType", eventNode.getProperty(EVENT_WALL_CONTENT_TYPE_PROPERTY).getString());
072        
073        return event;
074    }
075    
076    @Override
077    public boolean isMergeable(Map<String, Object> event1, Map<String, Object> event2)
078    {
079        return false;
080    }
081}