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.util.ArrayList; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.commons.lang.StringUtils; 025import org.quartz.JobDataMap; 026import org.quartz.JobExecutionContext; 027 028import org.ametys.core.schedule.Schedulable; 029import org.ametys.core.util.JSONUtils; 030import org.ametys.plugins.core.schedule.Scheduler; 031import org.ametys.runtime.config.Config; 032import org.ametys.runtime.i18n.I18nizableText; 033import org.ametys.web.repository.site.Site; 034import org.ametys.web.repository.site.SiteManager; 035 036/** 037 * A {@link Schedulable} job which rebuild and populate the live workspace of a site. 038 */ 039public class RebuildLiveSiteWorkspaceSchedulable extends AbstractRebuildLiveWorkspaceSchedulable 040{ 041 /** The key for the site to rebuild the live workspace */ 042 protected static final String JOBDATAMAP_SITE_KEY = "siteName"; 043 044 private static final String __JOBDATAMAP_SITE_KEY = Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_SITE_KEY; 045 046 /** The site amanger */ 047 protected SiteManager _siteManager; 048 049 /** The utils for JSON */ 050 protected JSONUtils _jsonUtils; 051 052 @Override 053 public void service(ServiceManager manager) throws ServiceException 054 { 055 super.service(manager); 056 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 057 _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE); 058 } 059 060 @Override 061 protected void _rebuildLiveWorkspace(JobExecutionContext context) throws Exception 062 { 063 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 064 String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY); 065 Site site = _getSite(siteAsMap); 066 067 _rebuildLiveComponent.rebuildLive(site); 068 } 069 070 @Override 071 protected I18nizableText _getSuccessMailSubject(JobExecutionContext context) 072 { 073 String siteTitle = _getSiteTitle(context); 074 075 List<String> subjecti18nParams = new ArrayList<>(); 076 subjecti18nParams.add(siteTitle); 077 return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_SUBJECT", subjecti18nParams); 078 } 079 080 @Override 081 protected I18nizableText _getSuccessMailBody(JobExecutionContext context) 082 { 083 String siteTitle = _getSiteTitle(context); 084 085 List<String> subjecti18nParams = new ArrayList<>(); 086 subjecti18nParams.add(siteTitle); 087 return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_BODY", subjecti18nParams); 088 } 089 090 @Override 091 protected I18nizableText _getErrorMailSubject(JobExecutionContext context) 092 { 093 String siteTitle = _getSiteTitle(context); 094 095 List<String> subjecti18nParams = new ArrayList<>(); 096 subjecti18nParams.add(siteTitle); 097 return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_SUBJECT", subjecti18nParams); 098 } 099 100 @Override 101 protected I18nizableText _getErrorMailBody(JobExecutionContext context) 102 { 103 String siteTitle = _getSiteTitle(context); 104 105 String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/"); 106 String url = cmsUrl + "/_admin/"; 107 108 List<String> bodyi18nParams = new ArrayList<>(); 109 bodyi18nParams.add(siteTitle); 110 bodyi18nParams.add(url); 111 return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_BODY", bodyi18nParams); 112 } 113 114 private String _getSiteTitle(JobExecutionContext context) 115 { 116 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 117 String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY); 118 119 Site site = _getSite(siteAsMap); 120 return site.getTitle() != null ? site.getTitle() : site.getName(); 121 } 122 123 private Site _getSite(String siteAsMap) 124 { 125 Map<String, Object> mapSite = _jsonUtils.convertJsonToMap(siteAsMap); 126 @SuppressWarnings("unchecked") 127 List<String> sites = (List<String>) mapSite.get("sites"); 128 String siteName = sites.get(0); 129 130 return _siteManager.getSite(siteName); 131 } 132}