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.content.consistency;
017
018import java.io.File;
019import java.util.ArrayList;
020import java.util.HashMap;
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;
027import org.apache.cocoon.environment.Request;
028import org.quartz.JobDataMap;
029import org.quartz.JobExecutionContext;
030
031import org.ametys.core.util.JSONUtils;
032import org.ametys.plugins.core.schedule.Scheduler;
033import org.ametys.plugins.repository.AmetysObjectIterable;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.web.repository.site.Site;
036import org.ametys.web.repository.site.SiteManager;
037
038/**
039 * Content consistency engine: generate consistency information for all contents.
040 * Sends a report e-mail if there are inconsistencies.
041 */
042public class CheckContentConsistencySchedulable extends org.ametys.cms.content.consistency.CheckContentConsistencySchedulable
043{
044    /** The key for the name of the site containing the contents to check */
045    public static final String SITE_NAME_KEY = "siteName";
046    
047    private static final String __JOBDATAMAP_SITE_NAME_KEY = Scheduler.PARAM_VALUES_PREFIX + SITE_NAME_KEY;
048    
049    /** The utils for JSON */
050    protected JSONUtils _jsonUtils;
051    /** The site manager. */
052    protected SiteManager _siteManager;
053    
054    @Override
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        super.service(manager);
058        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
059        _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE);
060    }
061    
062    @Override
063    public void execute(JobExecutionContext context) throws Exception
064    {
065        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
066        String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_NAME_KEY);
067        List<String> siteNames = _getSiteName(siteAsMap);
068        
069        // Generate the report.
070        AmetysObjectIterable<Site> sites = _siteManager.getSites();
071        for (Site currentSite : sites)
072        {
073            String currentSiteName = currentSite.getName();
074            
075            if (siteNames.isEmpty() || siteNames.contains(currentSiteName))
076            {
077                Request request = ContextHelper.getRequest(_context);
078                request.setAttribute("siteName", currentSiteName);
079                _generateReport();
080            }
081        }
082    }
083    
084    @Override
085    protected File _getReportDirectory()
086    {
087        File rootDirectory = super._getReportDirectory();
088        return new File(rootDirectory, _getCurrentSiteName());
089    }
090    
091    @Override
092    protected String _getReportURL()
093    {
094        return "cocoon://_plugins/web/consistency/" + _getCurrentSiteName() + "/inconsistent-contents-report.xml";
095    }
096    
097    @Override
098    protected I18nizableText _getMailSubject(Map<String, String> parameters)
099    {
100        List<String> subjectParams = new ArrayList<>();
101        subjectParams.add(parameters.get("siteTitle"));
102        
103        return new I18nizableText("plugin.web", "PLUGINS_WEB_GLOBAL_CONTENT_CONSISTENCY_MAIL_SUBJECT", subjectParams);
104    }
105    
106    @Override
107    protected String _getMailUri (Map<String, String> parameters)
108    {
109        return "cocoon://_plugins/web/consistency/" + parameters.get("siteName") + "/inconsistent-contents-mail.html";
110    }
111    
112    @Override
113    protected Map<String, String> _getEmailParams()
114    {
115        Map<String, String> params = new HashMap<>();
116        
117        String siteName = _getCurrentSiteName();
118        
119        StringBuilder url = new StringBuilder(_baseUrl);
120        if (!_baseUrl.endsWith("/"))
121        {
122            url.append('/');
123        }
124        url.append(siteName).append("/index.html?uitool=uitool-global-consistency");
125        
126        params.put("url", url.toString());
127        
128        Site site = _siteManager.getSite(siteName);
129        String siteTitle = site.getTitle();
130        
131        params.put("siteTitle", siteTitle);
132        params.put("siteName", siteName);
133        
134        return params;
135    }
136    
137    /**
138     * Retrieves the names of the sites from given parameters
139     * @param siteAsMap JSON {@link Map} containing the sites names
140     * @return the names of the sites
141     */
142    @SuppressWarnings("unchecked")
143    protected List<String> _getSiteName(String siteAsMap)
144    {
145        Map<String, Object> mapSite = _jsonUtils.convertJsonToMap(siteAsMap);
146        return mapSite.containsKey("sites") ? (List<String>) mapSite.get("sites") : List.of();
147    }
148    
149    /**
150     * Retrieves the current site name
151     * @return the current site name
152     */
153    protected String _getCurrentSiteName()
154    {
155        Request request = ContextHelper.getRequest(_context);
156        return (String) request.getAttribute("siteName");
157    }
158}