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;
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.JobExecutionContext;
026
027import org.ametys.cms.schedule.AbstractSendingMailSchedulable;
028import org.ametys.core.schedule.Schedulable;
029import org.ametys.core.schedule.progression.ContainerProgressionTracker;
030import org.ametys.core.ui.mail.StandardMailBodyHelper;
031import org.ametys.runtime.config.Config;
032import org.ametys.runtime.i18n.I18nizableText;
033
034/**
035 * A {@link Schedulable} job for indexing all the workspaces.
036 */
037public class GlobalWorkspaceIndexerSchedulable extends AbstractSendingMailSchedulable
038{
039    /** The workspace indexer */
040    protected WorkspaceIndexer _workspaceIndexer;
041
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _workspaceIndexer = (WorkspaceIndexer) manager.lookup(WorkspaceIndexer.ROLE);
047    }
048    
049    @Override
050    protected void _doExecute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws IndexingException
051    {
052        _workspaceIndexer.indexAllWorkspaces(progressionTracker);
053    }
054
055    @Override
056    protected boolean _isMailBodyInHTML(JobExecutionContext context) throws Exception
057    {
058        return true;
059    }
060    
061    @Override
062    protected I18nizableText _getSuccessMailSubject(JobExecutionContext context)
063    {
064        return new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_SUBJECT");
065    }
066    
067    @Override
068    protected String _getSuccessMailBody(JobExecutionContext context)
069    {
070        try
071        {
072            return StandardMailBodyHelper.newHTMLBody()
073                    .withTitle(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_BODY_TITLE"))
074                    .withMessage(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_BODY"))
075                    .build();
076        }
077        catch (IOException e)
078        {
079            getLogger().warn("Failed to build HTML email body for indexation result. Fallback to no wrapped email", e);
080            return _i18nUtils.translate(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_BODY"));
081        }
082        
083    }
084    
085    @Override
086    protected I18nizableText _getErrorMailSubject(JobExecutionContext context)
087    {
088        return new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_SUBJECT");
089    }
090    
091    @Override
092    protected String _getErrorMailBody(JobExecutionContext context, Throwable throwable)
093    {
094        String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/");
095        String url = cmsUrl + "/_admin/";
096        String error = ExceptionUtils.getStackTrace(throwable);
097        
098        try
099        {
100            return StandardMailBodyHelper.newHTMLBody()
101                    .withTitle(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY_TITLE"))
102                    .withMessage(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY", List.of(url)))
103                    .withDetails(null, error, true)
104                    .build();
105        }
106        catch (IOException e)
107        {
108            getLogger().warn("Failed to build HTML email body for indexation result. Fallback to no wrapped email", e);
109            return _i18nUtils.translate(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY"));
110        }
111    }
112}