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.repository.activities;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
024
025/**
026 * This class is in charge of loading the various {@link ActivityType}.
027 * It also allows to communicate with them for the retrieval of activity in JSON
028 */
029public class ActivityTypeExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<ActivityType>
030{
031    /** The Avalon Role */
032    public static final String ROLE = ActivityTypeExtensionPoint.class.getName();
033    
034    /**
035     * Merge activities
036     * @param initialActivities The activities to merge
037     * @return The merged activities
038     */
039    public List<Map<String, Object>> mergeActivities(List<Activity> initialActivities)
040    {
041        List<Map<String, Object>> mergedActivities = new ArrayList<>();
042        
043        List<Activity> activitiesToMerge = new ArrayList<>(initialActivities);
044        Map<String, List<Activity>> mergeableActivities = new HashMap<>();
045        
046        for (Activity activity : initialActivities)
047        {
048            String activityId = activity.getId();
049            
050            List<Activity> clonedActivities = new ArrayList<>(activitiesToMerge);
051            for (Activity clonedActivity : clonedActivities)
052            {
053                String clonedActivityId = clonedActivity.getId();
054                
055                if (!activityId.equals(clonedActivityId))
056                {
057                    ActivityType activityType = activity.getActivityType();
058                    ActivityType clonedActivityType = activity.getActivityType();
059                    
060                    if (activityType == clonedActivityType && activityType.isMergeable(activity, clonedActivity))
061                    {
062                        mergeableActivities.computeIfAbsent(activityId, str -> new ArrayList<>()).add(clonedActivity);
063                        
064                        // The events were merged : no need to re-process them
065                        activitiesToMerge.remove(activity);
066                        activitiesToMerge.remove(clonedActivity);
067                    }
068                }
069                else
070                {
071                    mergeableActivities.computeIfAbsent(activityId, str -> new ArrayList<>()).add(activity);
072                }
073            }
074        }
075        
076        for (List<Activity> acts : mergeableActivities.values())
077        {
078            // FIXME GG not sure it's fine to take the first one. Quite random
079            ActivityType eventType = acts.get(0).getActivityType();
080            try
081            {
082                mergedActivities.add(eventType.mergeActivities(acts));
083            }
084            catch (Exception e)
085            {
086                getLogger().error("An error occurred while trying to merge activities. The original activities are kept", e);
087                for (Activity act : acts)
088                {
089                    try
090                    {
091                        mergedActivities.add(act.toJSONForClient());
092                    }
093                    catch (Exception e2)
094                    {
095                        getLogger().error("An error occured while trying to serialize activity '{}'. The activity will be ignored.", act.getId(), e2);
096                    }
097                }
098            }
099        }
100        
101        return mergedActivities;
102    }
103    
104    /**
105     * Returns the first {@link ActivityType} matching the provided event id 
106     * @param eventId the id of the event
107     * @return The event type
108     */
109    public ActivityType getActivityType(String eventId)
110    {
111        for (String extensionId : getExtensionsIds())
112        {
113            ActivityType eventType = getExtension(extensionId);
114            if (eventType.getSupportedEventTypes().keySet().contains(eventId))
115            {
116                return eventType;
117            }
118        }
119        
120        throw new ActivityTypeProcessingException("There is no ActivityType corresponding with the id '" + eventId + "'.");
121    }
122}