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.cms.indexing;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Optional;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang3.exception.ExceptionUtils;
025import org.quartz.JobDataMap;
026import org.quartz.JobExecutionContext;
027
028import org.ametys.cms.content.indexing.solr.SolrIndexer;
029import org.ametys.cms.schedule.AbstractSendingMailSchedulable;
030import org.ametys.core.schedule.Schedulable;
031import org.ametys.core.schedule.progression.ContainerProgressionTracker;
032import org.ametys.core.ui.mail.StandardMailBodyHelper;
033import org.ametys.core.util.HttpUtils;
034import org.ametys.plugins.core.schedule.Scheduler;
035import org.ametys.runtime.config.Config;
036import org.ametys.runtime.i18n.I18nizableText;
037
038/**
039 * A {@link Schedulable} job for indexing all the workspaces.
040 */
041public class GlobalWorkspaceIndexerSchedulable extends AbstractSendingMailSchedulable
042{
043    private static final String __JOBDATAMAP_RELOAD_ACL_KEY = Scheduler.PARAM_VALUES_PREFIX + "reloadAcl";
044    
045    /** The workspace indexer */
046    protected WorkspaceIndexer _workspaceIndexer;
047    
048    /** The Solr indexer */
049    protected SolrIndexer _solrIndexer;
050
051    @Override
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        super.service(manager);
055        _workspaceIndexer = (WorkspaceIndexer) manager.lookup(WorkspaceIndexer.ROLE);
056        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
057    }
058    
059    @Override
060    protected void _doExecute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
061    {
062        // Index all workspaces
063        _workspaceIndexer.indexAllWorkspaces(progressionTracker);
064        
065        // Reload ACL cache
066        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
067        Boolean reloadAcl = Optional.ofNullable(jobDataMap.getBooleanValue(__JOBDATAMAP_RELOAD_ACL_KEY)).orElse(Boolean.TRUE);
068        if (reloadAcl)
069        {
070            _solrIndexer.reloadAclCache();
071        }
072    }
073
074    @Override
075    protected boolean _isMailBodyInHTML(JobExecutionContext context) throws Exception
076    {
077        return true;
078    }
079    
080    @Override
081    protected I18nizableText _getSuccessMailSubject(JobExecutionContext context)
082    {
083        return new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_SUBJECT");
084    }
085    
086    @Override
087    protected String _getSuccessMailBody(JobExecutionContext context, String language)
088    {
089        try
090        {
091            return StandardMailBodyHelper.newHTMLBody()
092                    .withTitle(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_BODY_TITLE"))
093                    .withMessage(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_BODY"))
094                    .withLanguage(language)
095                    .build();
096        }
097        catch (IOException e)
098        {
099            getLogger().warn("Failed to build HTML email body for indexation result. Fallback to no wrapped email", e);
100            return _i18nUtils.translate(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_BODY"), language);
101        }
102        
103    }
104    
105    @Override
106    protected I18nizableText _getErrorMailSubject(JobExecutionContext context)
107    {
108        return new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_SUBJECT");
109    }
110    
111    @Override
112    protected String _getErrorMailBody(JobExecutionContext context, String language, Throwable throwable)
113    {
114        String cmsUrl = HttpUtils.sanitize(Config.getInstance().getValue("cms.url"));
115        String url = cmsUrl + "/_admin/";
116        String error = ExceptionUtils.getStackTrace(throwable);
117        
118        try
119        {
120            return StandardMailBodyHelper.newHTMLBody()
121                    .withTitle(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY_TITLE"))
122                    .withMessage(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY", List.of(url)))
123                    .withDetails(null, error, true)
124                    .withLanguage(language)
125                    .build();
126        }
127        catch (IOException e)
128        {
129            getLogger().warn("Failed to build HTML email body for indexation result. Fallback to no wrapped email", e);
130            return _i18nUtils.translate(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY"), language);
131        }
132    }
133}