001/* 002 * Copyright 2022 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.HashMap; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024 025import org.ametys.core.observation.Event; 026import org.ametys.core.observation.Observer; 027import org.ametys.plugins.repository.AmetysObjectResolver; 028import org.ametys.plugins.workspaces.project.objects.Project; 029import org.ametys.web.ObservationConstants; 030import org.ametys.web.repository.page.Page; 031import org.ametys.web.repository.page.PagesContainer; 032import org.ametys.web.repository.site.Site; 033 034/** 035 * This {@link Observer} converts Page events to minisite page events if needed 036 */ 037public class ConvertPageEvent2MinisitePageEventObserver extends AbstractConvertMinisiteEventObserver 038{ 039 /** Map for events id conversion */ 040 protected static final Map<String, String> _EVENTS_ID_CONVERSION = Map.of( 041 org.ametys.web.ObservationConstants.EVENT_PAGE_ADDED, org.ametys.plugins.workspaces.ObservationConstants.EVENT_MINISITE_PAGE_CREATED, 042 org.ametys.web.ObservationConstants.EVENT_PAGE_UPDATED, org.ametys.plugins.workspaces.ObservationConstants.EVENT_MINISITE_PAGE_UPDATED, 043 org.ametys.web.ObservationConstants.EVENT_PAGE_RENAMED, org.ametys.plugins.workspaces.ObservationConstants.EVENT_MINISITE_PAGE_RENAMED, 044 org.ametys.web.ObservationConstants.EVENT_PAGE_DELETING, org.ametys.plugins.workspaces.ObservationConstants.EVENT_MINISITE_PAGE_DELETED); 045 046 private AmetysObjectResolver _resolver; 047 048 @Override 049 public void service(ServiceManager serviceManager) throws ServiceException 050 { 051 super.service(serviceManager); 052 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 053 } 054 055 public boolean supports(Event event) 056 { 057 String eventId = event.getId(); 058 return _EVENTS_ID_CONVERSION.containsKey(eventId); 059 } 060 061 public void observe(Event event, Map<String, Object> transientVars) throws Exception 062 { 063 Page page = (Page) event.getArguments().get(org.ametys.web.ObservationConstants.ARGS_PAGE); 064 if (page == null) 065 { 066 // in case of page deletion 067 String pageId = (String) event.getArguments().get(org.ametys.web.ObservationConstants.ARGS_PAGE_ID); 068 page = _resolver.resolveById(pageId); 069 } 070 071 Project project = _getProject(page); 072 073 if (project != null && isMinisitePage(project, page)) 074 { 075 Map<String, Object> eventParams = new HashMap<>(event.getArguments()); 076 // add project to event parameters 077 eventParams.put(org.ametys.plugins.workspaces.ObservationConstants.ARGS_PROJECT, project); 078 079 if (event.getId().equals(org.ametys.web.ObservationConstants.EVENT_PAGE_DELETING)) 080 { 081 eventParams.remove(ObservationConstants.ARGS_PAGE); 082 eventParams.put(ObservationConstants.ARGS_PAGE_ID, page.getId()); 083 eventParams.put(org.ametys.plugins.workspaces.ObservationConstants.ARGS_PAGE_TITLE, page.getTitle()); 084 } 085 086 _observationManager.notify(new Event(_EVENTS_ID_CONVERSION.get(event.getId()), event.getIssuer(), eventParams)); 087 } 088 } 089 090 /** 091 * Get the project the page belongs 092 * @param page the page 093 * @return the project or null if the page does not belong to a project 094 */ 095 protected Project _getProject(PagesContainer page) 096 { 097 Site site = page.getSite(); 098 List<Project> projectsForSite = _projectManager.getProjectsForSite(site); 099 if (!projectsForSite.isEmpty()) 100 { 101 return projectsForSite.get(0); 102 } 103 104 return null; 105 } 106}