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