001/*
002 *  Copyright 2016 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.live;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.lang.StringUtils;
024import org.apache.commons.lang3.exception.ExceptionUtils;
025import org.quartz.JobDataMap;
026import org.quartz.JobExecutionContext;
027
028import org.ametys.core.schedule.Schedulable;
029import org.ametys.core.util.JSONUtils;
030import org.ametys.plugins.core.schedule.Scheduler;
031import org.ametys.runtime.config.Config;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.repository.site.SiteManager;
035
036/**
037 * A {@link Schedulable} job which rebuild and populate the live workspace of a site.
038 */
039public class RebuildLiveSiteWorkspaceSchedulable extends AbstractRebuildLiveWorkspaceSchedulable
040{
041    /** The key for the site to rebuild the live workspace */
042    protected static final String JOBDATAMAP_SITE_KEY = "siteName";
043    
044    private static final String __JOBDATAMAP_SITE_KEY = Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_SITE_KEY;
045    
046    /** The site amanger */
047    protected SiteManager _siteManager;
048
049    /** The utils for JSON */
050    protected JSONUtils _jsonUtils;
051
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
057        _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE);
058    }
059    
060    @Override
061    protected void _rebuildLiveWorkspace(JobExecutionContext context) throws Exception
062    {
063        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
064        String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY);
065        Site site = _getSite(siteAsMap);
066        
067        _rebuildLiveComponent.rebuildLive(site);
068    }
069    
070    @Override
071    protected I18nizableText _getSuccessMailSubject(JobExecutionContext context)
072    {
073        String siteTitle = _getSiteTitle(context);
074        return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_SUBJECT", List.of(siteTitle));
075    }
076    
077    @Override
078    protected I18nizableText _getSuccessMailBody(JobExecutionContext context)
079    {
080        String siteTitle = _getSiteTitle(context);
081        return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_BODY", List.of(siteTitle));
082    }
083    
084    @Override
085    protected I18nizableText _getErrorMailSubject(JobExecutionContext context)
086    {
087        String siteTitle = _getSiteTitle(context);
088        return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_SUBJECT", List.of(siteTitle));
089    }
090    
091    @Override
092    protected I18nizableText _getErrorMailBody(JobExecutionContext context, Throwable throwable)
093    {
094        String siteTitle = _getSiteTitle(context);
095        
096        String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/");
097        String url = cmsUrl + "/_admin/";
098        
099        String error = ExceptionUtils.getStackTrace(throwable);
100        
101        return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_BODY", List.of(siteTitle, url, error));
102    }
103    
104    private String _getSiteTitle(JobExecutionContext context)
105    {
106        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
107        String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY);
108        
109        Site site = _getSite(siteAsMap);
110        return site.getTitle() != null ? site.getTitle() : site.getName();
111    }
112    
113    private Site _getSite(String siteAsMap)
114    {
115        Map<String, Object> mapSite = _jsonUtils.convertJsonToMap(siteAsMap);
116        @SuppressWarnings("unchecked")
117        List<String> sites = (List<String>) mapSite.get("sites");
118        String siteName = sites.get(0);
119        
120        return _siteManager.getSite(siteName);
121    }
122}