001/* 002 * Copyright 2022 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.odf.schedulable; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.commons.lang3.StringUtils; 024import org.apache.commons.lang3.exception.ExceptionUtils; 025import org.quartz.JobDataMap; 026import org.quartz.JobExecutionContext; 027import org.quartz.JobExecutionException; 028 029import org.ametys.cms.schedule.AbstractSendingMailSchedulable; 030import org.ametys.odf.catalog.Catalog; 031import org.ametys.odf.catalog.CatalogsManager; 032import org.ametys.plugins.core.schedule.Scheduler; 033import org.ametys.runtime.i18n.I18nizableText; 034import org.ametys.runtime.i18n.I18nizableTextParameter; 035 036/** 037 * Schedulable to delete a catalog 038 */ 039public class DeleteCatalogSchedulable extends AbstractSendingMailSchedulable 040{ 041 /** Schedulable ID */ 042 public static final String SCHEDULABLE_ID = DeleteCatalogSchedulable.class.getName(); 043 044 /** The job data map key for the catalog Id to delete*/ 045 public static final String JOBDATAMAP_CATALOG_NAME_KEY = "catalog"; 046 047 private CatalogsManager _catalogsManager; 048 049 @Override 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 _catalogsManager = (CatalogsManager) manager.lookup(CatalogsManager.ROLE); 053 super.service(manager); 054 } 055 056 @Override 057 protected void _doExecute(JobExecutionContext context) throws Exception 058 { 059 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 060 061 String catalogName = jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_CATALOG_NAME_KEY); 062 if (StringUtils.isEmpty(catalogName)) 063 { 064 throw new JobExecutionException("The catalog name cannot be empty."); 065 } 066 Catalog catalog = _catalogsManager.getCatalog(catalogName); 067 if (catalog == null) 068 { 069 throw new JobExecutionException("The catalog " + catalogName + " can't be retrieved."); 070 } 071 072 Map<String, Object> deleteCatalogResult = _catalogsManager.deleteCatalog(catalog); 073 if (deleteCatalogResult.containsKey("error")) 074 { 075 switch ((String) deleteCatalogResult.get("error")) 076 { 077 case "unknown-catalog": 078 throw new JobExecutionException("The catalog with id " + catalogName + " can not be resolved."); 079 case "referencing-contents": 080 throw new JobExecutionException("Impossible to delete catalog with id " + catalogName + ". There are contents outside of the catalog referencing the contents in the catalog to delete. See previous log for more info."); 081 default: 082 throw new JobExecutionException("Something bad happened while trying to delete the catalog " + catalogName); 083 } 084 } 085 } 086 087 @Override 088 protected I18nizableText _getSuccessMailSubject(JobExecutionContext context) throws Exception 089 { 090 return new I18nizableText("plugin.odf", "PLUGINS_ODF_SCHEDULABLE_DELETE_CATALOG_SUCCESS_MAIL_SUBJECT", _getI18nParams(context)); 091 } 092 093 @Override 094 protected I18nizableText _getSuccessMailBody(JobExecutionContext context) throws Exception 095 { 096 return new I18nizableText("plugin.odf", "PLUGINS_ODF_SCHEDULABLE_DELETE_CATALOG_SUCCESS_MAIL_BODY", _getI18nParams(context)); 097 } 098 099 @Override 100 protected I18nizableText _getErrorMailSubject(JobExecutionContext context) throws Exception 101 { 102 return new I18nizableText("plugin.odf", "PLUGINS_ODF_SCHEDULABLE_DELETE_CATALOG_ERROR_MAIL_SUBJECT", _getI18nParams(context)); 103 } 104 105 @Override 106 protected I18nizableText _getErrorMailBody(JobExecutionContext context, Throwable throwable) throws Exception 107 { 108 Map<String, I18nizableTextParameter> i18nParams = _getI18nParams(context); 109 String error = ExceptionUtils.getStackTrace(throwable); 110 i18nParams.put("error", new I18nizableText(error)); 111 return new I18nizableText("plugin.odf", "PLUGINS_ODF_SCHEDULABLE_DELETE_CATALOG_ERROR_MAIL_BODY", i18nParams); 112 } 113 114 private Map<String, I18nizableTextParameter> _getI18nParams(JobExecutionContext context) 115 { 116 Map<String, I18nizableTextParameter> i18nParams = new HashMap<>(); 117 118 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 119 String catalogName = jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_CATALOG_NAME_KEY); 120 i18nParams.put("catalogName", new I18nizableText(catalogName)); 121 return i18nParams; 122 } 123} 124