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.project.observers; 017 018import java.io.IOException; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.configuration.ConfigurationException; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.xml.sax.SAXException; 026 027import org.ametys.core.observation.Event; 028import org.ametys.core.observation.ObservationManager; 029import org.ametys.core.user.CurrentUserProvider; 030import org.ametys.plugins.repository.AmetysObjectIterable; 031import org.ametys.plugins.workspaces.PagePopulator; 032import org.ametys.plugins.workspaces.project.objects.Project; 033import org.ametys.web.ObservationConstants; 034import org.ametys.web.repository.page.Page; 035import org.ametys.web.repository.site.Site; 036import org.ametys.web.repository.sitemap.Sitemap; 037 038/** 039 * Initializes all project workspace pages when a sitemap is created. 040 */ 041public class InitializeProjectSitemapObserver extends AbstractInitializeProjectObserver 042{ 043 /** The observation manager. */ 044 protected ObservationManager _observationManager; 045 046 /** Current user provider */ 047 protected CurrentUserProvider _currentUserProvider; 048 /** the page populator */ 049 protected PagePopulator _pagePopulator; 050 /** the plugin name */ 051 protected String _pluginName; 052 053 @Override 054 public void setPluginInfo(String pluginName, String featureName, String id) 055 { 056 _pluginName = pluginName; 057 } 058 059 @Override 060 public void service(ServiceManager manager) throws ServiceException 061 { 062 super.service(manager); 063 _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE); 064 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 065 _pagePopulator = (PagePopulator) manager.lookup(PagePopulator.ROLE); 066 } 067 068 @Override 069 public void doObserve(Event event, Map<String, Object> transientVars, Site site, Project project) throws Exception 070 { 071 if (project != null) 072 { 073 // Initialize each sitemap with a set of predefined and configured pages 074 try (AmetysObjectIterable<Sitemap> sitemaps = site.getSitemaps()) 075 { 076 for (Sitemap sitemap : sitemaps) 077 { 078 try (AmetysObjectIterable< ? extends Page> pages = sitemap.getChildrenPages()) 079 { 080 if (pages.getSize() == 0) 081 { 082 _initializeSitemap(sitemap, project); 083 } 084 } 085 } 086 } 087 } 088 } 089 090 091 /** 092 * Initialize the given sitemap. 093 * @param sitemap the Sitemap object. 094 * @param project the corresponding project 095 */ 096 protected void _initializeSitemap(Sitemap sitemap, Project project) 097 { 098 String path = "skin:" + sitemap.getSite().getSkinId() + "://conf/project-home-model.xml"; 099 try 100 { 101 _pagePopulator.initPage(sitemap, path); 102 } 103 catch (ConfigurationException | IOException | SAXException e) 104 { 105 getLogger().error("An error occurred while trying to create and configure the project home page.", e); 106 } 107 108 _projectManager.initializeModulesSitemap(project, sitemap); 109 110 // Notify of the sitemap change. 111 Map<String, Object> eventParams = new HashMap<>(); 112 eventParams.put(ObservationConstants.ARGS_SITEMAP, sitemap); 113 _observationManager.notify(new Event(ObservationConstants.EVENT_SITEMAP_UPDATED, _currentUserProvider.getUser(), eventParams)); 114 } 115}