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;
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;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.events.EventTypeExtensionPoint;
031import org.ametys.plugins.workspaces.project.objects.Project;
032import org.ametys.runtime.plugin.component.AbstractLogEnabled;
033
034/**
035 * {@link Observer} implementation for workspaces' events
036 */
037public abstract class AbstractWorkspacesEventsObserver extends AbstractLogEnabled implements Observer, Serviceable
038{
039    /** Node id stored as a transient var */
040    public static final String NODE_ID_EVENT_TRANSIENT_VAR = "workspaces.event.node.id";
041    
042    /** Event type extension point */
043    protected EventTypeExtensionPoint _eventTypeExtensionPoint;
044    
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        _eventTypeExtensionPoint = (EventTypeExtensionPoint) serviceManager.lookup(EventTypeExtensionPoint.ROLE);
049    }
050
051    @Override
052    public int getPriority(Event event)
053    {
054        return MAX_PRIORITY;
055    }
056
057    /**
058     * Store the event
059     * @param event The event
060     * @param project The project. Can be null
061     * @param transientVars If future events needs the created node id, it is stored in the transientVars
062     * @throws RepositoryException if failed to store event
063     */
064    protected void storeEvent(Event event, Project project, Map<String, Object> transientVars) throws RepositoryException
065    {
066        if (project != null)
067        {
068            Map<String, Object> eventArguments = getEventParameters(event, project);
069            
070            eventArguments.put("projectName", project.getName());
071            eventArguments.put("projectTitle", project.getTitle());
072            
073            Node eventNode = _eventTypeExtensionPoint.addEvent(event.getId(), eventArguments, project);
074            if (transientVars != null)
075            {
076                transientVars.put(NODE_ID_EVENT_TRANSIENT_VAR, eventNode.getIdentifier());
077            }
078        }
079        
080    }
081    
082    /**
083     * Get the event's parameters
084     * @param event The event
085     * @param project The project. Can not be null
086     * @return The event's parameters
087     */
088    protected Map<String, Object> getEventParameters(Event event, Project project)
089    {
090        Map<String, Object> params = event.getArguments();
091        
092        params.put("projectName", project.getName());
093        params.put("projectTitle", project.getTitle());
094        
095        return params;
096    }
097    
098    /**
099     * Get the parent project
100     * @param ao The ametys object
101     * @return The parent project or <code>null</code> if not found
102     */
103    protected Project getProject(AmetysObject ao)
104    {
105        AmetysObject parentAO = ao;
106        while (parentAO != null)
107        {
108            if (parentAO instanceof Project)
109            {
110                return (Project) parentAO;
111            } 
112            parentAO = parentAO.getParent();
113        }
114        
115        return null;
116    }
117}