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