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