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.live;
017
018import java.io.IOException;
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.commons.lang3.exception.ExceptionUtils;
023import org.quartz.JobExecutionContext;
024
025import org.ametys.core.observation.Observer;
026import org.ametys.core.schedule.Schedulable;
027import org.ametys.core.schedule.progression.ContainerProgressionTracker;
028import org.ametys.core.ui.mail.StandardMailBodyHelper;
029import org.ametys.runtime.config.Config;
030import org.ametys.runtime.i18n.I18nizableText;
031
032/**
033 * A {@link Schedulable} job which rebuild and populate the live workspace.
034 * This ensures data integrity inside the live workspace if by mistake
035 * synchronization {@link Observer}s fails to update the live workspace.
036 */
037public class RebuildLiveAllWorkspaceSchedulable extends AbstractRebuildLiveWorkspaceSchedulable
038{
039    @Override
040    protected void _rebuildLiveWorkspace(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
041    {
042        _rebuildLiveComponent.rebuildLiveWorkspace(progressionTracker);
043    }
044    
045    @Override
046    protected boolean _isMailBodyInHTML(JobExecutionContext context) throws Exception
047    {
048        return true;
049    }
050    
051    @Override
052    protected I18nizableText _getSuccessMailSubject(JobExecutionContext context)
053    {
054        return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDALL_SUCCESS_MAIL_SUBJECT");
055    }
056    
057    @Override
058    protected String _getSuccessMailBody(JobExecutionContext context)
059    {
060        try
061        {
062            return StandardMailBodyHelper.newHTMLBody()
063                    .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDALL_SUCCESS_MAIL_BODY_TITLE"))
064                    .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDALL_SUCCESS_MAIL_BODY"))
065                    .build();
066        }
067        catch (IOException e)
068        {
069            getLogger().warn("Failed to build HTML email body for rebuild live result. Fallback to no wrapped email", e);
070            return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDALL_SUCCESS_MAIL_BODY"));
071        }
072    }
073    
074    @Override
075    protected I18nizableText _getErrorMailSubject(JobExecutionContext context)
076    {
077        return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDALL_ERROR_MAIL_SUBJECT");
078    }
079    
080    @Override
081    protected String _getErrorMailBody(JobExecutionContext context, Throwable throwable)
082    {
083        String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/");
084        String url = cmsUrl + "/_admin/";
085        String error = ExceptionUtils.getStackTrace(throwable);
086        
087        try
088        {
089            return StandardMailBodyHelper.newHTMLBody()
090                    .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDALL_ERROR_MAIL_BODY_TITLE"))
091                    .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDALL_ERROR_MAIL_BODY", List.of(url)))
092                    .withDetails(null, error, true)
093                    .build();
094        }
095        catch (IOException e)
096        {
097            getLogger().warn("Failed to build HTML email body for rebuild live result. Fallback to no wrapped email", e);
098            return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDALL_ERROR_MAIL_BODY", List.of(url, error)));
099        }
100    }
101}