001/* 002 * Copyright 2017 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.Collection; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Locale; 022import java.util.Map; 023 024import javax.jcr.RepositoryException; 025 026import org.apache.avalon.framework.context.Context; 027import org.apache.avalon.framework.context.ContextException; 028import org.apache.avalon.framework.context.Contextualizable; 029import org.apache.avalon.framework.service.ServiceException; 030import org.apache.avalon.framework.service.ServiceManager; 031import org.apache.cocoon.components.ContextHelper; 032import org.apache.cocoon.environment.Request; 033import org.apache.commons.lang3.ArrayUtils; 034 035import org.ametys.cms.ObservationConstants; 036import org.ametys.cms.repository.Content; 037import org.ametys.core.observation.Event; 038import org.ametys.core.observation.ObservationManager; 039import org.ametys.core.user.CurrentUserProvider; 040import org.ametys.plugins.workspaces.editionfo.EditionFOWorkspaceModule; 041import org.ametys.plugins.workspaces.project.ProjectManager; 042import org.ametys.plugins.workspaces.project.objects.Project; 043import org.ametys.web.repository.content.WebContent; 044import org.ametys.web.repository.page.ModifiablePage; 045import org.ametys.web.repository.page.Page; 046 047/** 048 * Observer for storing event on page updated 049 */ 050public class UpdatePageOnContentValidatedObserver extends AbstractWorkspacesEventsObserver implements Contextualizable 051{ 052 private ProjectManager _projectManager; 053 private Context _context; 054 private ObservationManager _observationManager; 055 private CurrentUserProvider _currentUserProvider; 056 057 @Override 058 public void service(ServiceManager serviceManager) throws ServiceException 059 { 060 super.service(serviceManager); 061 062 _projectManager = (ProjectManager) serviceManager.lookup(ProjectManager.ROLE); 063 _observationManager = (ObservationManager) serviceManager.lookup(ObservationManager.ROLE); 064 _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE); 065 } 066 067 068 public void contextualize(Context context) throws ContextException 069 { 070 _context = context; 071 } 072 073 public boolean supports(Event event) 074 { 075 return event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED); 076 } 077 078 public void observe(Event event, Map<String, Object> transientVars) throws Exception 079 { 080 Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT); 081 String[] cTypes = content.getTypes(); 082 if (ArrayUtils.contains(cTypes, EditionFOWorkspaceModule.EDITIONFO_CONTENT_TYPE) && content instanceof WebContent) 083 { 084 Project project = _getProject(); 085 086 if (project != null) 087 { 088 _updateContentPage(content, project); 089 } 090 } 091 } 092 093 094 private void _updateContentPage(Content content, Project project) 095 { 096 Collection<Page> referencingPages = ((WebContent) content).getReferencingPages(); 097 for (Page page : referencingPages) 098 { 099 Map<String, Object> eventParams = new HashMap<>(); 100 eventParams.put(org.ametys.web.ObservationConstants.ARGS_PAGE, page); 101 eventParams.put(org.ametys.plugins.workspaces.ObservationConstants.ARGS_PROJECT, project); 102 103 String pageTitle = page.getTitle(); 104 String contentTitle = content.getTitle(new Locale(page.getSitemapName())); 105 if (pageTitle != null && pageTitle.equals(contentTitle)) 106 { 107 _observationManager.notify(new Event(org.ametys.plugins.workspaces.ObservationConstants.EVENT_EDITIONFO_UPDATED, _currentUserProvider.getUser(), eventParams)); 108 } 109 else if (page instanceof ModifiablePage) 110 { 111 ((ModifiablePage) page).setTitle(contentTitle); 112 ((ModifiablePage) page).saveChanges(); 113 114 eventParams.put(org.ametys.plugins.workspaces.ObservationConstants.ARGS_PAGE_OLD_NAME, pageTitle); 115 _observationManager.notify(new Event(org.ametys.plugins.workspaces.ObservationConstants.EVENT_EDITIONFO_RENAMED, _currentUserProvider.getUser(), eventParams)); 116 } 117 } 118 } 119 120 private Project _getProject() 121 { 122 Request request = ContextHelper.getRequest(_context); 123 String siteName = (String) request.getAttribute("siteName"); 124 List<String> projects = _projectManager.getProjectsForSite(siteName); 125 if (projects.size() > 0) 126 { 127 return _projectManager.getProject(projects.get(0)); 128 } 129 return null; 130 } 131 132 133 /** 134 * Store the event 135 * @param event The event 136 * @param project The project. Can be null 137 * @throws RepositoryException if failed to store event 138 */ 139 @Override 140 protected void storeEvent(Event event, Project project) throws RepositoryException 141 { 142 if (project != null) 143 { 144 Map<String, Object> eventArguments = getEventParameters(event, project); 145 146 eventArguments.put("projectName", project.getName()); 147 eventArguments.put("projectTitle", project.getTitle()); 148 149 _eventTypeExtensionPoint.addEvent(org.ametys.plugins.workspaces.ObservationConstants.EVENT_EDITIONFO_UPDATED, eventArguments, project); 150 } 151 } 152 153 154}