001/*
002 *  Copyright 2010 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.notification;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang.StringUtils;
025
026import org.ametys.core.observation.Event;
027import org.ametys.plugins.explorer.ObservationConstants;
028import org.ametys.plugins.explorer.resources.Resource;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.workspaces.project.objects.Project;
031import org.ametys.runtime.i18n.I18nizableText;
032
033/**
034 * This observer observes event of resources to notify concerned users
035 */
036public class ResourcesMailNotifierObserver extends AbstractSendNotificationObserver
037{
038    /** resource Notify Helper */
039    protected ResourceNotifierHelper _resourceNotifierHelper;
040    
041    @Override
042    public void service(ServiceManager smanager) throws ServiceException
043    {
044        super.service(smanager);
045        _resourceNotifierHelper = (ResourceNotifierHelper) smanager.lookup(ResourceNotifierHelper.ROLE);
046    }
047    
048    @Override
049    public boolean supports(Event event)
050    {
051        String resourceId = (String) event.getArguments().getOrDefault(ObservationConstants.ARGS_ID, null);
052        if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_UPDATED) && _resourceNotifierHelper.resourceRecentlyModified(resourceId))
053        {
054            return false;
055        }
056        
057        return event.getId().equals(ObservationConstants.EVENT_RESOURCE_CREATED)
058                || event.getId().equals(ObservationConstants.EVENT_RESOURCE_UPDATED)
059                || event.getId().equals(ObservationConstants.EVENT_RESOURCE_DELETED);
060    }
061    
062    @Override
063    protected AmetysObject getEventAmetysObject(Event event)
064    {
065        String parentId = (String) event.getArguments().get(ObservationConstants.ARGS_PARENT_ID);
066        return _resolver.resolveById(parentId);
067    }
068
069    @Override
070    protected String getMailBodyURI(Event event, Project project)
071    {
072        return "cocoon://_plugins/workspaces/notification-mail-resource";
073    }
074
075    @Override
076    protected I18nizableText getI18nSubject(Event event, Project project)
077    {
078        String eventId = event.getId();
079        Map<String, Object> eventArgs = event.getArguments();
080
081        String i18nKey = "PROJECT_MAIL_NOTIFICATION_SUBJECT_" + StringUtils.replaceChars(eventId.toUpperCase(), '.', '_');
082        List<String> i18nParams = new ArrayList<>();
083        i18nParams.add(project.getTitle()); // {0}
084        if (eventId.equals(ObservationConstants.EVENT_RESOURCE_CREATED))
085        {
086            @SuppressWarnings("unchecked")
087            Map<String, Resource> resources = (Map<String, Resource>) eventArgs.get(ObservationConstants.ARGS_RESOURCES);
088            if (resources.size() > 1)
089            {
090                i18nKey += "_MULTIPLE";
091                i18nParams.add(String.valueOf(resources.size())); // {1}
092            }
093            else
094            {
095                i18nParams.add(resources.values().iterator().next().getName()); // {1}
096            }
097        }
098        else
099        {
100            i18nParams.add(eventArgs.get("oldName") != null ? (String) eventArgs.get("oldName") : (String) eventArgs.get(ObservationConstants.ARGS_NAME)); // {1}
101        }
102        return new I18nizableText("plugin." + _pluginName, i18nKey, i18nParams);
103    }
104    
105    @Override
106    protected String getRightIdForNotify()
107    {
108        return "Plugins_Projects_Document_Mail_Notification";
109    }
110}