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.exception.ExceptionUtils;
022import org.quartz.JobExecutionContext;
023
024import org.ametys.core.schedule.progression.ContainerProgressionTracker;
025import org.ametys.core.ui.mail.StandardMailBodyHelper;
026import org.ametys.core.util.HttpUtils;
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, String language)
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                    .withLanguage(language)
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_BUILD_CONTENTS_WITHOUT_SITE_SUCCESS_MAIL_BODY"), language);
071        }
072    }
073    
074    @Override
075    protected I18nizableText _getErrorMailSubject(JobExecutionContext context)
076    {
077        return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_ERROR_MAIL_SUBJECT");
078    }
079    
080    @Override
081    protected String _getErrorMailBody(JobExecutionContext context, String language, Throwable throwable)
082    {
083        String cmsUrl = HttpUtils.sanitize(Config.getInstance().getValue("cms.url"));
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_BUILD_CONTENTS_WITHOUT_SITE_ERROR_MAIL_BODY_TITLE"))
091                    .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_ERROR_MAIL_BODY", List.of(url)))
092                    .withDetails(null, error, true)
093                    .withLanguage(language)
094                    .build();
095        }
096        catch (IOException e)
097        {
098            getLogger().warn("Failed to build HTML email body for rebuild live result. Fallback to no wrapped email", e);
099            return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILD_CONTENTS_WITHOUT_SITE_ERROR_MAIL_BODY", List.of(url, error)), language);
100        }
101    }
102}