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.chat;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.logger.AbstractLogEnabled;
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.AsyncObserver;
027import org.ametys.core.observation.Event;
028import org.ametys.plugins.workspaces.ObservationConstants;
029import org.ametys.plugins.workspaces.project.objects.Project;
030import org.ametys.runtime.config.Config;
031
032/**
033 * Create a Rocket.Chat room when a project is created
034 */
035public class ProjectAddedObserver extends AbstractLogEnabled implements Serviceable, AsyncObserver
036{
037    /** Rocket.Chat helper */
038    protected ChatHelper _chatHelper;
039
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        _chatHelper = (ChatHelper) manager.lookup(ChatHelper.ROLE);
043    }
044
045    public boolean supports(Event event)
046    {
047        return ObservationConstants.EVENT_PROJECT_ADDED.equals(event.getId()) && Config.getInstance().<Boolean>getValue("workspaces.chat.active");
048    }
049
050    public int getPriority(Event event)
051    {
052        return MAX_PRIORITY; // Yes, "MAX PRIORITY" is the lowest priority...
053    }
054
055    public void observe(Event event, Map<String, Object> transientVars) throws Exception
056    {
057        Project project = (Project) event.getArguments().get(ObservationConstants.ARGS_PROJECT);
058        try
059        {
060            _chatHelper.getRoom(project.getName(), true);
061        }
062        catch (IOException e)
063        {
064            getLogger().error("An error occurred that prevented the room creation", e);
065        }
066    }
067
068}