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.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.lang3.StringUtils;
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
082        List<String> subjecti18nParams = new ArrayList<>();
083        subjecti18nParams.add(siteTitle);
084        return new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_SUCCESS_MAIL_SUBJECT", subjecti18nParams);
085    }
086    
087    @Override
088    protected I18nizableText _getSuccessMailBody(JobExecutionContext context)
089    {
090        String siteTitle = _getSiteTitle(context);
091
092        List<String> subjecti18nParams = new ArrayList<>();
093        subjecti18nParams.add(siteTitle);
094        return new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_SUCCESS_MAIL_BODY", subjecti18nParams);
095    }
096    
097    @Override
098    protected I18nizableText _getErrorMailSubject(JobExecutionContext context)
099    {
100        String siteTitle = _getSiteTitle(context);
101
102        List<String> subjecti18nParams = new ArrayList<>();
103        subjecti18nParams.add(siteTitle);
104        return new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_ERROR_MAIL_SUBJECT", subjecti18nParams);
105    }
106    
107    @Override
108    protected I18nizableText _getErrorMailBody(JobExecutionContext context)
109    {
110        String siteTitle = _getSiteTitle(context);
111        
112        String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/");
113        String url = cmsUrl + "/_admin/";
114        
115        List<String> bodyi18nParams = new ArrayList<>();
116        bodyi18nParams.add(siteTitle);
117        bodyi18nParams.add(url);
118        return new I18nizableText("plugin.web", "PLUGINS_WEB_SOLR_SITE_INDEXATION_ERROR_MAIL_BODY", bodyi18nParams);
119    }
120    
121    private String _getSiteTitle(JobExecutionContext context)
122    {
123        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
124        String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY);
125        
126        Site site = _getSite(siteAsMap);
127        return site.getTitle() != null ? site.getTitle() : site.getName();
128    }
129    
130    private Site _getSite(String siteAsMap)
131    {
132        Map<String, Object> mapSite = _jsonUtils.convertJsonToMap(siteAsMap);
133        @SuppressWarnings("unchecked")
134        List<String> sites = (List<String>) mapSite.get("sites");
135        String siteName = sites.get(0);
136        
137        return _siteManager.getSite(siteName);
138    }
139}