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.pagesubscription.notifier;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.ametys.core.ui.mail.StandardMailBodyHelper;
023import org.ametys.core.ui.mail.StandardMailBodyHelper.MailBodyBuilder;
024import org.ametys.core.user.User;
025import org.ametys.core.util.URIUtils;
026import org.ametys.plugins.explorer.ObservationConstants;
027import org.ametys.plugins.explorer.resources.Resource;
028import org.ametys.plugins.repository.activities.Activity;
029import org.ametys.plugins.repository.activities.ActivityType;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.web.activities.PageResourcesUpdatedActivityType;
032import org.ametys.web.repository.page.Page;
033
034/**
035 * Notifier for {@link PageResourcesUpdatedActivityType}
036 */
037public class PageSubscriptionResourcesUpdatedNotifier extends AbstractPageSubscriptionNotifier
038{
039    private static final int __MAX_RESOURCE_LINK = 5;
040    
041    @Override
042    public boolean support(ActivityType activityType)
043    {
044        return activityType instanceof PageResourcesUpdatedActivityType;
045    }
046    
047    @Override
048    protected String getSiteParameterId()
049    {
050        return "page-subscription-attachments";
051    }
052    
053    @Override
054    protected I18nizableText _getMailSubject(Activity activity, Page page)
055    {
056        List<String> i18nparam = new ArrayList<>();
057        i18nparam.add(page.getTitle()); // {0}
058        
059        String i18nKey = null;
060        switch (activity.getEventType())
061        {
062            case ObservationConstants.EVENT_RESOURCE_CREATED:
063                String[] resourceIds = activity.getValue(PageResourcesUpdatedActivityType.RESOURCES_IDS);
064                int size = resourceIds.length;
065                if (size > 1)
066                {
067                    i18nKey = "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_ADD_RESOURCES_SUBJECT";
068                    i18nparam.add(String.valueOf(size)); // {1}
069                }
070                else
071                {
072                    i18nKey = "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_ADD_RESOURCE_SUBJECT";
073                }
074                break;
075            case ObservationConstants.EVENT_RESOURCE_UPDATED:
076                i18nKey = "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_UPDATE_RESOURCE_SUBJECT";
077                break;
078            case ObservationConstants.EVENT_RESOURCE_DELETED:
079                i18nKey = "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_DELETE_RESOURCE_SUBJECT";
080                break;
081            default:
082                break;
083        }
084
085        return new I18nizableText("plugin.page-subscription", i18nKey, i18nparam);
086    }
087
088    @Override
089    protected String _getMailHtmlBody(Activity activity, Page page, User author) throws IOException
090    {
091        String pageUrl = _getAbsolutePageUrl(page);
092        String pageTitle = page.getTitle();
093        String siteTitle = page.getSite().getTitle();
094
095        MailBodyBuilder bodyBuilder = StandardMailBodyHelper.newHTMLBody();
096        
097        switch (activity.getEventType())
098        {
099            case ObservationConstants.EVENT_RESOURCE_CREATED:
100                String[] resourceIds = activity.getValue(PageResourcesUpdatedActivityType.RESOURCES_IDS);
101                int size = resourceIds.length;
102                if (size > 1)
103                {
104                    bodyBuilder.withTitle(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_ADD_RESOURCES_BODY_TITLE", List.of(String.valueOf(size))));
105                    bodyBuilder.withMessage(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_ADD_RESOURCES_BODY", List.of(String.valueOf(size), pageTitle, siteTitle)));
106                    bodyBuilder.withDetails(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_ADD_RESOURCES_DETAILS_TITLE"), _getAbsoluteResourcesUrl(resourceIds, page), false);
107                }
108                else
109                {
110                    Resource resource = _resolver.resolveById(resourceIds[0]);
111                    String resourceName = resource.getName();
112                    String resourceUrl = _getAbsoluteResourceUrl(resource, page);
113                    
114                    bodyBuilder.withTitle(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_ADD_RESOURCE_BODY_TITLE", List.of(resourceName)));
115                    bodyBuilder.addMessage(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_ADD_RESOURCE_BODY", List.of(resourceName, pageTitle, siteTitle)));
116                    bodyBuilder.addMessage(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_RESOURCE_DOWNLOAD", List.of(resourceUrl)));
117                }
118                break;
119
120            case ObservationConstants.EVENT_RESOURCE_UPDATED:
121                String[] updatedResourceIds = activity.getValue(PageResourcesUpdatedActivityType.RESOURCES_IDS);
122                Resource resource = _resolver.resolveById(updatedResourceIds[0]);
123                String resourceName = resource.getName();
124                String resourceUrl = _getAbsoluteResourceUrl(resource, page);
125                
126                bodyBuilder.withTitle(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_UPDATE_RESOURCE_BODY_TITLE", List.of(resourceName)));
127                bodyBuilder.addMessage(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_UPDATE_RESOURCE_BODY", List.of(resourceName, pageTitle, siteTitle)));
128                bodyBuilder.addMessage(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_RESOURCE_DOWNLOAD", List.of(resourceUrl)));
129                break;
130
131            case ObservationConstants.EVENT_RESOURCE_DELETED:
132                String deletedResourceName = activity.getValue(PageResourcesUpdatedActivityType.RESOURCE_DELETED_NAME);
133                bodyBuilder.withTitle(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_DELETE_RESOURCE_BODY_TITLE", List.of(deletedResourceName)));
134                bodyBuilder.addMessage(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_DELETE_RESOURCE_BODY", List.of(deletedResourceName, pageTitle, siteTitle)));
135                break;
136            default:
137                break;
138        }
139        
140        return bodyBuilder
141                .withLink(pageUrl, new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_PAGE_LINK_TITLE"))
142                .withLanguage(page.getSitemapName())
143                .build();
144    }
145    
146    private String _getAbsoluteResourcesUrl(String[] resourceIds, Page page)
147    {
148        StringBuilder sb = new StringBuilder();
149
150        int nbResources = resourceIds.length;
151        sb.append("<ul>");
152        for (int i = 0; i < Math.min(nbResources, __MAX_RESOURCE_LINK); i++)
153        {
154            Resource resource = _resolver.resolveById(resourceIds[i]);
155            sb.append("<li>")
156                   .append("<a href=\"" + _getAbsoluteResourceUrl(resource, page) + "\">")
157                   .append(resource.getName())
158                   .append("</a>")
159                   .append("</li>");
160        }
161
162        int nbOthers = nbResources - __MAX_RESOURCE_LINK;
163        if (nbOthers > 0)
164        {
165            I18nizableText moreLinks = new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_NOTIFICATION_MAIL_ADD_RESOURCES_MORE_LINKS", List.of(String.valueOf(nbOthers)));
166            
167            sb.append("<li>")
168                .append(_i18nUtils.translate(moreLinks, page.getSitemapName()))
169                .append("</li>");
170        }
171        
172        sb.append("</ul>");
173
174        return sb.toString();
175    }
176
177    private String _getAbsoluteResourceUrl(Resource resource, Page page)
178    {
179        return new StringBuilder().append(page.getSite().getUrl()).append("/_page-attachment").append("?id=").append(URIUtils.encodeParameter(resource.getId())).append(
180                "&download=true").toString();
181    }
182}