001/*
002 *  Copyright 2023 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.lang3.StringUtils;
022import org.apache.commons.lang3.exception.ExceptionUtils;
023import org.quartz.JobExecutionContext;
024
025import org.ametys.core.schedule.progression.ContainerProgressionTracker;
026import org.ametys.core.ui.mail.StandardMailBodyHelper;
027import org.ametys.runtime.config.Config;
028import org.ametys.runtime.i18n.I18nizableText;
029
030
031/**
032 * Rebuild live workspace for all contents without site.
033 * 
034 * This schedulable will send a report mail at the end end of execution
035 */
036public class RebuildContentsWithoutSiteLiveWorkspaceSchedulable extends AbstractRebuildLiveWorkspaceSchedulable
037{
038    @Override
039    protected void _rebuildLiveWorkspace(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
040    {
041        _rebuildLiveComponent.rebuildContentsWithoutSiteWorkspace(progressionTracker);
042    }
043    
044    @Override
045    protected boolean _isMailBodyInHTML(JobExecutionContext context) throws Exception
046    {
047        return true;
048    }
049    
050    @Override
051    protected I18nizableText _getSuccessMailSubject(JobExecutionContext context)
052    {
053        return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_SUCCESS_MAIL_SUBJECT");
054    }
055    
056    @Override
057    protected String _getSuccessMailBody(JobExecutionContext context)
058    {
059        try
060        {
061            return StandardMailBodyHelper.newHTMLBody()
062                    .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_SUCCESS_MAIL_BODY_TITLE"))
063                    .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_SUCCESS_MAIL_BODY"))
064                    .build();
065        }
066        catch (IOException e)
067        {
068            getLogger().warn("Failed to build HTML email body for rebuild live result. Fallback to no wrapped email", e);
069            return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_SUCCESS_MAIL_BODY"));
070        }
071    }
072    
073    @Override
074    protected I18nizableText _getErrorMailSubject(JobExecutionContext context)
075    {
076        return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_ERROR_MAIL_SUBJECT");
077    }
078    
079    @Override
080    protected String _getErrorMailBody(JobExecutionContext context, Throwable throwable)
081    {
082        String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/");
083        String url = cmsUrl + "/_admin/";
084        String error = ExceptionUtils.getStackTrace(throwable);
085        
086        try
087        {
088            return StandardMailBodyHelper.newHTMLBody()
089                    .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_ERROR_MAIL_BODY_TITLE"))
090                    .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_ERROR_MAIL_BODY", List.of(url)))
091                    .withDetails(null, error, true)
092                    .build();
093        }
094        catch (IOException e)
095        {
096            getLogger().warn("Failed to build HTML email body for rebuild live result. Fallback to no wrapped email", e);
097            return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_ERROR_MAIL_BODY", List.of(url, error)));
098        }
099    }
100}