001/*
002 *  Copyright 2017 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.observer;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.List;
022import java.util.Map;
023
024import org.ametys.cms.ObservationConstants;
025import org.ametys.cms.repository.Content;
026import org.ametys.core.observation.Event;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.web.repository.content.WebContent;
029import org.ametys.web.repository.page.Page;
030
031/**
032 * Listen content page and send mail
033 */
034public class PageSubscriptionValidateContentObserver extends AbstractPageSubscriptionObserver
035{
036    @Override
037    public boolean supports(Event event)
038    {
039        String eventType = event.getId();
040        return eventType.equals(ObservationConstants.EVENT_CONTENT_VALIDATED);
041    }
042    
043    @Override
044    protected String getSiteParameterId()
045    {
046        return "page-subscription-content-validation";
047    }
048
049    @Override
050    protected String _getMailSubject(Event event, Page page)
051    {
052        List<String> i18nparam = new ArrayList<>();
053        i18nparam.add(page.getSite().getTitle()); // {0}
054        i18nparam.add(page.getTitle()); // {1}
055
056        String i18nKey = null;
057        i18nKey = "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_UPDATE_CONTENT_TITLE";
058
059        I18nizableText i18nSubject = new I18nizableText("plugin.page-subscription", i18nKey, i18nparam);
060        return _i18nUtils.translate(i18nSubject);
061    }
062
063    @Override
064    protected String _getMailBody(Event event, Page page)
065    {
066        List<String> i18nparam = new ArrayList<>();
067
068        i18nparam.add(_getAbsolutePageUrl(page)); // {0}
069        i18nparam.add(page.getTitle()); // {1}
070        i18nparam.add(page.getSite().getTitle()); // {2}
071        i18nparam.add(_userManager.getUser(event.getIssuer()).getFullName()); // {3}
072
073        I18nizableText i18nTextBody = null;
074        i18nTextBody = new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_UPDATE_CONTENT_TEXT_BODY", i18nparam);
075
076        return _i18nUtils.translate(i18nTextBody);
077    }
078
079    @Override
080    protected Collection<Page> _getPages(Event event)
081    {
082        Map<String, Object> args = event.getArguments();
083        String contentId = (String) args.get(ObservationConstants.ARGS_CONTENT_ID);
084        Content content = _resolver.resolveById(contentId);
085
086        if (content instanceof WebContent)
087        {
088            return ((WebContent) content).getReferencingPages();
089        }
090        
091        return Collections.emptyList();
092    }
093
094}