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.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang.StringUtils;
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 _initializeErrorMail(JobExecutionContext context)
062    {
063        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
064        String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY);
065        
066        Site site = _getSite(siteAsMap);
067        String siteTitle = site.getTitle() != null ? site.getTitle() : site.getName();
068        
069        List<String> subjecti18nParams = new ArrayList<>();
070        subjecti18nParams.add(siteTitle);
071        __ERROR_MAIL_SUBJECT = new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_SUBJECT", subjecti18nParams);
072        
073        String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/");
074        String url = cmsUrl + "/_admin/";
075        
076        List<String> bodyi18nParams = new ArrayList<>();
077        bodyi18nParams.add(siteTitle);
078        bodyi18nParams.add(url);
079        __ERROR_MAIL_BODY = new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_BODY", bodyi18nParams);
080    }
081    
082    @Override
083    protected void _doExecute(JobExecutionContext context) throws Exception
084    {
085        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
086        String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY);
087        Site site = _getSite(siteAsMap);
088        
089        _rebuildLiveComponent.rebuildLive(site);
090    }
091    
092    private Site _getSite(String siteAsMap)
093    {
094        Map<String, Object> mapSite = _jsonUtils.convertJsonToMap(siteAsMap);
095        @SuppressWarnings("unchecked")
096        List<String> sites = (List<String>) mapSite.get("sites");
097        String siteName = sites.get(0);
098        
099        return _siteManager.getSite(siteName);
100    }
101}