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.tasks;
017
018import java.util.List;
019import java.util.Map;
020import java.util.stream.Collectors;
021
022import javax.jcr.Node;
023import javax.jcr.RepositoryException;
024
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.plugins.explorer.ObservationConstants;
028import org.ametys.plugins.explorer.tasks.Task;
029import org.ametys.plugins.repository.RepositoryConstants;
030import org.ametys.plugins.repository.events.EventType;
031
032/**
033 * {@link EventType} implementation for the assignment of a task
034 */
035public class TaskAssignedEventType extends TasksEventType
036{
037    /** Constant for task's assignees */
038    public static final String EVENT_TASK_ASSIGNEES_PROPERTY = RepositoryConstants.NAMESPACE_PREFIX + ":assignees";
039    
040    @Override
041    protected void storeAdditionalEventData(Node eventNode, Map<String, Object> parameters) throws RepositoryException
042    {
043        super.storeAdditionalEventData(eventNode, parameters);
044        
045        Task task = (Task) parameters.get(ObservationConstants.ARGS_TASK);
046        
047        List<String> assignees = task.getAssignment().stream().map(identity -> _userHelper.getUserFullName(identity)).collect(Collectors.toList());
048        eventNode.setProperty(EVENT_TASK_ASSIGNEES_PROPERTY, StringUtils.join(assignees, ","));
049    }
050    
051    @Override
052    public Map<String, Object> event2JSON(Node eventNode) throws RepositoryException
053    {
054        Map<String, Object> event = super.event2JSON(eventNode);
055        
056        // Assignees
057        event.put("assignees", eventNode.getProperty(EVENT_TASK_ASSIGNEES_PROPERTY).getString());
058        
059        return event;
060    }
061    
062    @Override
063    public boolean isMergeable(Map<String, Object> event1, Map<String, Object> event2)
064    {
065        return false;
066    }
067}