001/*
002 *  Copyright 2023 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.activities.documents;
017
018import java.util.List;
019import java.util.stream.Stream;
020
021import org.ametys.plugins.repository.AmetysObject;
022import org.ametys.plugins.repository.activities.Activity;
023import org.ametys.plugins.repository.activities.ActivityType;
024import org.ametys.plugins.workspaces.activities.AbstractWorkspacesActivityNotifier;
025import org.ametys.plugins.workspaces.activities.documents.ResourceReferenceElementType.ResourceReference;
026
027/**
028 * Class representing a activity notifier for documents workspaces
029 */
030public class DocumentsActivityNotifier extends AbstractWorkspacesActivityNotifier
031{
032    public boolean supports(ActivityType activityType)
033    {
034        return activityType instanceof DocumentCommentedActivityType
035                || activityType instanceof ResourceCreatedOrUpdatedActivityType
036                || activityType instanceof ResourceRenamedActivityType;
037    }
038    
039    @Override
040    public String getMailBodyURI(Activity activity)
041    {
042        return "cocoon://_plugins/workspaces/notification-mail-resource";
043    }
044    
045    @Override
046    protected String _getSubjectI18nKey(Activity activity)
047    {
048        String i18nKey = super._getSubjectI18nKey(activity);
049        if (activity.getActivityType() instanceof AbstractDocumentsActivityType && activity.hasValue(AbstractDocumentsActivityType.FILES_DATA_NAME))
050        {
051            ResourceReference[] refs = activity.getValue(AbstractDocumentsActivityType.FILES_DATA_NAME);
052            if (refs.length > 1)
053            {
054                i18nKey += "_MULTIPLE";
055            }
056        }
057        
058        return i18nKey;
059    }
060    
061    @Override
062    public List<String> getSubjectI18nParams(Activity activity)
063    {
064        List<String> i18nParams = super.getSubjectI18nParams(activity);
065        
066        if (activity.hasValue(AbstractDocumentsActivityType.FILE_DATA_NAME))
067        {
068            ResourceReference ref = activity.getValue(AbstractDocumentsActivityType.FILE_DATA_NAME);
069            i18nParams.add(ref.oldName() != null ? ref.oldName() : ref.name()); // {1}
070        }
071        else if (activity.hasValue(AbstractDocumentsActivityType.FILES_DATA_NAME))
072        {
073            ResourceReference[] refs = activity.getValue(AbstractDocumentsActivityType.FILES_DATA_NAME);
074            if (refs.length > 1)
075            {
076                i18nParams.add(String.valueOf(refs.length)); // {1}
077            }
078            else
079            {
080                i18nParams.add(Stream.of(refs).findFirst().map(ResourceReference::name).orElse("")); // {1}
081            }
082        }
083        return i18nParams;
084    }
085    
086    @Override
087    public AmetysObject getTargetAmetysObject(Activity activity)
088    {
089        return _resolver.resolveById(activity.getValue(AbstractDocumentsActivityType.PARENT_FOLDER_ID));
090    }
091}