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.runtime.config.Config;
026import org.ametys.web.repository.content.WebContent;
027import org.ametys.web.repository.page.Page;
028
029/**
030 * OS workflow function to send mail after an action is triggered.
031 */
032public class SendMailFunction extends org.ametys.cms.workflow.SendMailFunction
033{
034    @Override
035    protected List<String> getSubjectI18nParams (User user, WorkflowAwareContent content)
036    {
037        List<String> params = super.getSubjectI18nParams(user, content);
038        if (content instanceof WebContent)
039        {
040            WebContent webContent = (WebContent) content;
041            params.add(webContent.getSite().getTitle());
042        }
043        return params;
044    }
045    
046    @Override
047    protected String getMailBody(String bodyI18nKey, User user, WorkflowAwareContent content, Map transientVars)
048    {
049        String i18nKey = bodyI18nKey;
050        
051        if (content instanceof WebContent)
052        {
053            WebContent webContent = (WebContent) content;
054            if (webContent.getReferencingPages().size() == 0)
055            {
056                // Orphan content
057                i18nKey += "_ORPHAN";
058            }
059        }
060        else
061        {
062            i18nKey += "_NOSITE";
063        }
064        
065        return super.getMailBody(i18nKey, user, content, transientVars);
066    }
067    
068    @Override
069    protected List<String> getBodyI18nParams(User user, WorkflowAwareContent content)
070    {
071        String requestUri = Config.getInstance().getValueAsString("cms.url");
072        
073        List<String> params = new ArrayList<>();
074        
075        params.add(user.getFullName()); // {0}
076        params.add(content.getTitle()); // {1}
077        
078        if (content instanceof WebContent)
079        {
080            WebContent webContent = (WebContent) content;
081            
082            params.add(webContent.getSite().getTitle());  // {2}
083            
084            Iterator<Page> pages = webContent.getReferencingPages().iterator();
085            if (pages.hasNext())
086            {
087                Page page = pages.next();
088                params.add(page.getTitle()); // {3}
089                params.add(requestUri + "/" + webContent.getSite().getName() + "/index.html?uitool=uitool-page,id:%27" + page.getId() + "%27"); // {4}
090            }
091            else
092            {
093                // no referencing page
094                params.add(requestUri + "/" + webContent.getSite().getName() + "/index.html?uitool=uitool-content,id:%27" + content.getId() + "%27"); // {3}
095            }
096        }
097        else
098        {
099            params.add(requestUri); // {2}
100        }
101        return params;
102    }
103}