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.indexing;
017
018import java.io.IOException;
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.lang3.StringUtils;
025import org.apache.commons.lang3.exception.ExceptionUtils;
026import org.quartz.JobDataMap;
027import org.quartz.JobExecutionContext;
028
029import org.ametys.cms.indexing.IndexingException;
030import org.ametys.cms.schedule.AbstractSendingMailSchedulable;
031import org.ametys.core.schedule.Schedulable;
032import org.ametys.core.schedule.progression.ContainerProgressionTracker;
033import org.ametys.core.ui.mail.StandardMailBodyHelper;
034import org.ametys.core.util.JSONUtils;
035import org.ametys.plugins.core.schedule.Scheduler;
036import org.ametys.runtime.config.Config;
037import org.ametys.runtime.i18n.I18nizableText;
038import org.ametys.web.repository.site.Site;
039import org.ametys.web.repository.site.SiteManager;
040
041/**
042 * A {@link Schedulable} job for indexing a site.
043 */
044public class SiteIndexerSchedulable extends AbstractSendingMailSchedulable
045{
046    /** The key for the site to rebuild the live workspace */
047    protected static final String JOBDATAMAP_SITE_KEY = "siteName";
048    
049    private static final String __JOBDATAMAP_SITE_KEY = Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_SITE_KEY;
050    
051    /** The site amanger */
052    protected SiteManager _siteManager;
053
054    /** The utils for JSON */
055    protected JSONUtils _jsonUtils;
056    
057    /** The site indexer */
058    protected SiteIndexer _siteIndexer;
059    
060    @Override
061    public void service(ServiceManager manager) throws ServiceException
062    {
063        super.service(manager);
064        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
065        _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE);
066        _siteIndexer = (SiteIndexer) manager.lookup(SiteIndexer.ROLE);
067    }
068    
069    @Override
070    protected void _doExecute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws IndexingException
071    {
072        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
073        
074        String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY);
075        Site site = _getSite(siteAsMap);
076        
077        _siteIndexer.indexSite(site, progressionTracker);
078    }
079    
080    @Override
081    protected boolean _isMailBodyInHTML(JobExecutionContext context) throws Exception
082    {
083        return true;
084    }
085    
086    @Override
087    protected I18nizableText _getSuccessMailSubject(JobExecutionContext context)
088    {
089        String siteTitle = _getSiteTitle(context);
090        return new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_SUCCESS_MAIL_SUBJECT", List.of(siteTitle));
091    }
092    
093    @Override
094    protected String _getSuccessMailBody(JobExecutionContext context)
095    {
096        String siteTitle = _getSiteTitle(context);
097        
098        try
099        {
100            return StandardMailBodyHelper.newHTMLBody()
101                    .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_SUCCESS_MAIL_BODY_TITLE", List.of(siteTitle)))
102                    .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_SUCCESS_MAIL_BODY", List.of(siteTitle)))
103                    .build();
104        }
105        catch (IOException e)
106        {
107            getLogger().warn("Failed to build HTML email body for site indexation result. Fallback to no wrapped email", e);
108            return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_SUCCESS_MAIL_BODY", List.of(siteTitle)));
109        }
110    }
111    
112    @Override
113    protected I18nizableText _getErrorMailSubject(JobExecutionContext context)
114    {
115        String siteTitle = _getSiteTitle(context);
116        return new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_ERROR_MAIL_SUBJECT", List.of(siteTitle));
117    }
118    
119    @Override
120    protected String _getErrorMailBody(JobExecutionContext context, Throwable throwable)
121    {
122        String siteTitle = _getSiteTitle(context);
123        
124        String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/");
125        String url = cmsUrl + "/_admin/";
126        
127        String error = ExceptionUtils.getStackTrace(throwable);
128        
129        try
130        {
131            return StandardMailBodyHelper.newHTMLBody()
132                    .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_ERROR_MAIL_BODY_TITLE", List.of(siteTitle)))
133                    .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_ERROR_MAIL_BODY", List.of(siteTitle, url)))
134                    .withDetails(null, error, true)
135                    .build();
136        }
137        catch (IOException e)
138        {
139            getLogger().warn("Failed to build HTML email body for site indexation result. Fallback to no wrapped email", e);
140            return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_ERROR_MAIL_BODY", List.of(siteTitle, url, error)));
141        }
142    }
143    
144    private String _getSiteTitle(JobExecutionContext context)
145    {
146        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
147        String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY);
148        
149        Site site = _getSite(siteAsMap);
150        return site.getTitle() != null ? site.getTitle() : site.getName();
151    }
152    
153    private Site _getSite(String siteAsMap)
154    {
155        Map<String, Object> mapSite = _jsonUtils.convertJsonToMap(siteAsMap);
156        @SuppressWarnings("unchecked")
157        List<String> sites = (List<String>) mapSite.get("sites");
158        String siteName = sites.get(0);
159        
160        return _siteManager.getSite(siteName);
161    }
162}