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.exception.ExceptionUtils; 024import org.quartz.JobExecutionContext; 025 026import org.ametys.cms.schedule.AbstractSendingMailSchedulable; 027import org.ametys.core.schedule.Schedulable; 028import org.ametys.core.schedule.progression.ContainerProgressionTracker; 029import org.ametys.core.ui.mail.StandardMailBodyHelper; 030import org.ametys.core.util.HttpUtils; 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, String language) 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 .withLanguage(language) 076 .build(); 077 } 078 catch (IOException e) 079 { 080 getLogger().warn("Failed to build HTML email body for indexation result. Fallback to no wrapped email", e); 081 return _i18nUtils.translate(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_SUCCESS_MAIL_BODY"), language); 082 } 083 084 } 085 086 @Override 087 protected I18nizableText _getErrorMailSubject(JobExecutionContext context) 088 { 089 return new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_SUBJECT"); 090 } 091 092 @Override 093 protected String _getErrorMailBody(JobExecutionContext context, String language, Throwable throwable) 094 { 095 String cmsUrl = HttpUtils.sanitize(Config.getInstance().getValue("cms.url")); 096 String url = cmsUrl + "/_admin/"; 097 String error = ExceptionUtils.getStackTrace(throwable); 098 099 try 100 { 101 return StandardMailBodyHelper.newHTMLBody() 102 .withTitle(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY_TITLE")) 103 .withMessage(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY", List.of(url))) 104 .withDetails(null, error, true) 105 .withLanguage(language) 106 .build(); 107 } 108 catch (IOException e) 109 { 110 getLogger().warn("Failed to build HTML email body for indexation result. Fallback to no wrapped email", e); 111 return _i18nUtils.translate(new I18nizableText("plugin.cms", "PLUGINS_CMS_SOLR_GLOBAL_INDEXATION_ERROR_MAIL_BODY"), language); 112 } 113 } 114}