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.web.workflow;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.components.ContextHelper;
027
028import org.ametys.cms.repository.WorkflowAwareContent;
029import org.ametys.core.ui.mail.StandardMailBodyHelper.MailBodyBuilder;
030import org.ametys.core.user.User;
031import org.ametys.core.util.HttpUtils;
032import org.ametys.core.util.URIUtils;
033import org.ametys.runtime.config.Config;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.web.WebHelper;
036import org.ametys.web.repository.content.WebContent;
037import org.ametys.web.repository.page.Page;
038import org.ametys.web.repository.site.Site;
039import org.ametys.web.repository.site.SiteManager;
040
041/**
042 * OS workflow function to send mail after an action is triggered.
043 */
044public class SendMailFunction extends org.ametys.cms.workflow.SendMailFunction
045{
046    private SiteManager _siteManager;
047
048    @Override
049    public void service(ServiceManager smanager) throws ServiceException
050    {
051        super.service(smanager);
052        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
053    }
054    @Override
055    protected List<String> getSubjectI18nParams (User user, WorkflowAwareContent content)
056    {
057        List<String> params = super.getSubjectI18nParams(user, content); // {0}
058        Site site = _getSite(content);
059        if (site != null)
060        {
061            params.add(site.getTitle());  // {1}
062        }
063        else
064        {
065            // fallback to the current site if no site is provided
066            String currentSiteName = WebHelper.getSiteName(ContextHelper.getRequest(_context));
067            params.add(_siteManager.getSite(currentSiteName).getTitle()); // {1}
068        }
069        return params;
070    }
071    
072    @Override
073    protected MailBodyBuilder getMailBody(String subjectI18nKey, String bodyI18nKey, User user, WorkflowAwareContent content, Map transientVars) throws IOException
074    {
075        String i18nKey = bodyI18nKey;
076        
077        Site site = _getSite(content);
078        if (site == null)
079        {
080            i18nKey += "_NOSITE";
081        }
082        else if (_getPage(content, site) == null)
083        {
084            // Orphan content
085            i18nKey += "_ORPHAN";
086        }
087        
088        return super.getMailBody(subjectI18nKey, i18nKey, user, content, transientVars);
089    }
090    
091    @Override
092    protected List<String> getBodyI18nParams(User user, WorkflowAwareContent content)
093    {
094        List<String> params = new ArrayList<>();
095        
096        params.add(user.getFullName()); // {0}
097        params.add(_contentHelper.getTitle(content)); // {1}
098        
099        Site site = _getSite(content);
100        if (site != null)
101        {
102            params.add(site.getTitle());  // {2}
103        }
104        
105        Page page = _getPage(content, site);
106        if (page != null)
107        {
108            params.add(page.getTitle());  // {3}
109        }
110        
111        params.add(_getContentUri(content, page, site));  // {2}, {3} or {4} depending on page and site value
112        
113        return params;
114    }
115    
116    /**
117     * Get the site name
118     * @param content The content
119     * @return the site name
120     */
121    protected Site _getSite(WorkflowAwareContent content)
122    {
123        if (content instanceof WebContent)
124        {
125            return ((WebContent) content).getSite();
126        }
127        
128        return null;
129    }
130
131    /**
132     * Get the page referenced by this content
133     * @param content The content
134     * @param site the site
135     * @return the page or null.
136     */
137    protected Page _getPage(WorkflowAwareContent content, Site site)
138    {
139        if (content instanceof WebContent)
140        {
141            Iterator<Page> pages = ((WebContent) content).getReferencingPages().iterator();
142            if (pages.hasNext())
143            {
144                return pages.next();
145            }
146        }
147        return null;
148    }
149    
150    /**
151     * Get the content uri
152     * @param content the content
153     * @param page the referencing page. Can be null.
154     * @param site The site
155     * @return the content uri
156     */
157    protected String _getContentUri(WorkflowAwareContent content, Page page, Site site)
158    {
159        String siteName;
160        if (site != null)
161        {
162            siteName = site.getName();
163        }
164        else
165        {
166            // fallback to the current site if no site is provided
167            siteName = WebHelper.getSiteName(ContextHelper.getRequest(_context));
168        }
169        
170        if (page != null)
171        {
172            return _getRequestUri() + "/" + siteName + "/index.html?uitool=uitool-page,id:%27" + URIUtils.encodeParameter(page.getId()) + "%27";
173        }
174        else
175        {
176            return _contentHelper.getContentBOUrl(content, Map.of("siteName", siteName));
177        }
178    }
179    
180    /**
181     * Get the request URI.
182     * @return the full request URI.
183     */
184    protected String _getRequestUri()
185    {
186        return HttpUtils.sanitize(Config.getInstance().getValue("cms.url"));
187    }
188    
189    @Override
190    public I18nizableText getLabel()
191    {
192        return new I18nizableText("plugin.web", "PLUGINS_WEB_SEND_MAIL_FUNCTION_LABEL");
193    }
194    
195}