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.ametys.cms.repository.WorkflowAwareContent;
024import org.ametys.core.user.User;
025import org.ametys.core.util.URLEncoder;
026import org.ametys.web.repository.content.WebContent;
027import org.ametys.web.repository.page.Page;
028import org.ametys.web.repository.site.Site;
029
030/**
031 * OS workflow function to send mail after an action is triggered.
032 */
033public class SendMailFunction extends org.ametys.cms.workflow.SendMailFunction
034{
035    @Override
036    protected List<String> getSubjectI18nParams (User user, WorkflowAwareContent content)
037    {
038        List<String> params = super.getSubjectI18nParams(user, content);
039        Site site = _getSite(content);
040        if (site != null)
041        {
042            params.add(site.getTitle());  // {2}
043        }
044        return params;
045    }
046    
047    @Override
048    protected String getMailBody(String bodyI18nKey, User user, WorkflowAwareContent content, Map transientVars)
049    {
050        String i18nKey = bodyI18nKey;
051        
052        Site site = _getSite(content);
053        if (site == null)
054        {
055            i18nKey += "_NOSITE";
056        }
057        else if (_getPage(content, site) == null)
058        {
059            // Orphan content
060            i18nKey += "_ORPHAN";
061        }
062        
063        return super.getMailBody(i18nKey, user, content, transientVars);
064    }
065    
066    @Override
067    protected List<String> getBodyI18nParams(User user, WorkflowAwareContent content)
068    {
069        List<String> params = new ArrayList<>();
070        
071        params.add(user.getFullName()); // {0}
072        params.add(_contentHelper.getTitle(content)); // {1}
073        
074        Site site = _getSite(content);
075        if (site != null)
076        {
077            params.add(site.getTitle());  // {2}
078        }
079        
080        Page page = _getPage(content, site);
081        if (page != null)
082        {
083            params.add(page.getTitle());  // {3}
084            params.add(_getContentUri(content, page, site));  // {4}
085        }
086        else
087        {
088            params.add(_getContentUri(content, null, site));  // {3}
089        }
090        
091        return params;
092    }
093    
094    /**
095     * Get the site name
096     * @param content The content
097     * @return the site name
098     */
099    protected Site _getSite(WorkflowAwareContent content)
100    {
101        if (content instanceof WebContent)
102        {
103            return ((WebContent) content).getSite();
104        }
105        
106        return null;
107    }
108
109    /**
110     * Get the page referenced by this content
111     * @param content The content
112     * @param site the site
113     * @return the page or null.
114     */
115    protected Page _getPage(WorkflowAwareContent content, Site site)
116    {
117        if (content instanceof WebContent)
118        {
119            Iterator<Page> pages = ((WebContent) content).getReferencingPages().iterator();
120            if (pages.hasNext())
121            {
122                return pages.next();
123            }
124        }
125        return null;
126    }
127    
128    /**
129     * Get the content uri
130     * @param content the content
131     * @param page the referencing page. Can be null.
132     * @param site The site
133     * @return the content uri
134     */
135    protected String _getContentUri(WorkflowAwareContent content, Page page, Site site)
136    {
137        if (page != null)
138        {
139            return _getRequestUri() + "/" + site.getName() + "/index.html?uitool=uitool-page,id:%27" + URLEncoder.encodeParameter(page.getId()) + "%27";
140        }
141        else
142        {
143            return _getRequestUri() + "/" + site.getName() + "/index.html?uitool=uitool-content,id:%27" + URLEncoder.encodeParameter(content.getId()) + "%27";
144        }
145    }
146}