001/* 002 * Copyright 2020 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 018 019import java.util.Map; 020 021import org.apache.avalon.framework.configuration.Configurable; 022import org.apache.avalon.framework.configuration.Configuration; 023import org.apache.avalon.framework.configuration.ConfigurationException; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026 027import org.ametys.core.observation.Event; 028import org.ametys.core.util.I18nUtils; 029import org.ametys.plugins.explorer.calendars.Calendar; 030import org.ametys.plugins.explorer.calendars.Calendar.CalendarVisibility; 031import org.ametys.plugins.explorer.resources.ModifiableResourceCollection; 032import org.ametys.plugins.repository.AmetysObjectIterable; 033import org.ametys.plugins.workspaces.calendars.CalendarWorkspaceModule; 034import org.ametys.plugins.workspaces.calendars.WorkspaceCalendarDAO; 035import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint; 036import org.ametys.plugins.workspaces.project.objects.Project; 037import org.ametys.runtime.i18n.I18nizableText; 038import org.ametys.web.repository.site.Site; 039import org.ametys.web.repository.sitemap.Sitemap; 040 041/** 042 * Initialize the calendar when a project is created 043 */ 044public class InitializeCalendarObserver extends AbstractInitializeProjectObserver implements Configurable 045{ 046 /** Workspace Calendar DAO */ 047 protected WorkspaceCalendarDAO _calendarDAO; 048 049 /** Workspace Module Extension Point */ 050 protected WorkspaceModuleExtensionPoint _moduleEP; 051 052 /** The i18n utils. */ 053 protected I18nUtils _i18nUtils; 054 055 private String _pluginName; 056 057 private I18nizableText _templateDesc; 058 private String _color; 059 private String _visibility; 060 private String _workflowName; 061 private I18nizableText _title; 062 private I18nizableText _description; 063 064 @Override 065 public void service(ServiceManager manager) throws ServiceException 066 { 067 super.service(manager); 068 _calendarDAO = (WorkspaceCalendarDAO) manager.lookup(WorkspaceCalendarDAO.ROLE); 069 _moduleEP = (WorkspaceModuleExtensionPoint) manager.lookup(WorkspaceModuleExtensionPoint.ROLE); 070 _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE); 071 } 072 073 public void configure(Configuration configuration) throws ConfigurationException 074 { 075 _templateDesc = I18nizableText.parseI18nizableText(configuration.getChild("template-desc"), "plugin." + _pluginName, ""); 076 _color = configuration.getChild("color").getValue("col1"); 077 _visibility = configuration.getChild("visibility").getValue(CalendarVisibility.PUBLIC.name()); 078 _workflowName = configuration.getChild("workflow").getValue("calendar-default"); 079 _title = I18nizableText.parseI18nizableText(configuration.getChild("title"), "plugin." + _pluginName); 080 _description = I18nizableText.parseI18nizableText(configuration.getChild("description"), "plugin." + _pluginName, ""); 081 } 082 083 @Override 084 public int getPriority(Event event) 085 { 086 return super.getPriority(event) + 100; 087 } 088 089 @Override 090 protected void doObserve(Event event, Map<String, Object> transientVars, Site site, Project project) throws Exception 091 { 092 CalendarWorkspaceModule module = _moduleEP.getModule(CalendarWorkspaceModule.CALENDAR_MODULE_ID); 093 ModifiableResourceCollection moduleRoot = module.getModuleRoot(project, false); 094 095 if (moduleRoot != null && !_hasCalendar(module, project)) 096 { 097 Boolean renameIfExists = false; 098 Boolean checkRights = false; 099 100 String lang = getLang(project); 101 String description = _i18nUtils.translate(_description, lang); 102 String inputName = _i18nUtils.translate(_title, lang); 103 String templateDesc = _i18nUtils.translate(_templateDesc, lang); 104 try 105 { 106 _calendarDAO.addCalendar(moduleRoot.getId(), inputName, description, templateDesc, _color, _visibility, _workflowName, renameIfExists, checkRights); 107 } 108 catch (Exception e) 109 { 110 getLogger().error("Error while trying to create the first calendar in a newly created project", e); 111 } 112 113 } 114 } 115 116 private boolean _hasCalendar(CalendarWorkspaceModule calendarModule, Project project) 117 { 118 AmetysObjectIterable<Calendar> calendars = calendarModule.getCalendars(project); 119 return calendars != null && calendars.getSize() > 0; 120 } 121 122 /** 123 * Get the lang for this project, or fr if none found 124 * @param project the project to check 125 * @return a lang in this project 126 */ 127 protected String getLang(Project project) 128 { 129 /* 130 * get the sites, then the sitemaps (transformed back to a stream of sitemap) 131 * then get the name 132 * return the 1st one, or "fr" by default if none is available 133 */ 134 return project.getSites() 135 .stream() 136 .sequential() 137 .map(Site::getSitemaps) 138 .flatMap(AmetysObjectIterable::stream) 139 .map(Sitemap::getName) 140 .findFirst() 141 .orElse("fr"); 142 } 143 144 public void setPluginInfo(String pluginName, String featureName, String id) 145 { 146 _pluginName = pluginName; 147 } 148 149}