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.util.List;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.commons.lang3.StringUtils;
023import org.apache.commons.lang3.exception.ExceptionUtils;
024import org.quartz.JobExecutionContext;
025
026import org.ametys.cms.schedule.AbstractSendingMailSchedulable;
027import org.ametys.core.schedule.Schedulable;
028import org.ametys.runtime.config.Config;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * A {@link Schedulable} job for indexing all the workspaces.
033 */
034public class GlobalWorkspaceIndexerSchedulable extends AbstractSendingMailSchedulable
035{
036    /** The workspace indexer */
037    protected WorkspaceIndexer _workspaceIndexer;
038
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        super.service(manager);
043        _workspaceIndexer = (WorkspaceIndexer) manager.lookup(WorkspaceIndexer.ROLE);
044    }
045    
046    @Override
047    protected void _doExecute(JobExecutionContext context) throws IndexingException
048    {
049        _workspaceIndexer.indexAllWorkspaces();
050    }
051    
052    @Override
053    protected I18nizableText _getSuccessMailSubject(JobExecutionContext context)
054    {
055        return new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_SUBJECT");
056    }
057    
058    @Override
059    protected I18nizableText _getSuccessMailBody(JobExecutionContext context)
060    {
061        return new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_BODY");
062    }
063    
064    @Override
065    protected I18nizableText _getErrorMailSubject(JobExecutionContext context)
066    {
067        return new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_SUBJECT");
068    }
069    
070    @Override
071    protected I18nizableText _getErrorMailBody(JobExecutionContext context, Throwable throwable)
072    {
073        String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/");
074        String url = cmsUrl + "/_admin/";
075        String error = ExceptionUtils.getStackTrace(throwable);
076        return new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY", List.of(url, error));
077    }
078}