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