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