001/*
002 *  Copyright 2021 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;
020import java.util.Objects;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang3.StringUtils;
025import org.apache.commons.lang3.exception.ExceptionUtils;
026import org.quartz.JobDataMap;
027import org.quartz.JobExecutionContext;
028import org.quartz.JobExecutionException;
029
030import org.ametys.cms.schedule.AbstractSendingMailSchedulable;
031import org.ametys.odf.catalog.Catalog;
032import org.ametys.odf.catalog.CatalogsManager;
033import org.ametys.plugins.core.schedule.Scheduler;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.runtime.i18n.I18nizableTextParameter;
036
037/**
038 * Schedulable to copy a catalog.
039 */
040public class CopyCatalogSchedulable extends AbstractSendingMailSchedulable
041{
042    /** Schedulable ID */
043    public static final String SCHEDULABLE_ID = CopyCatalogSchedulable.class.getName();
044    
045    /** The key for the source catalog */
046    public static final String JOBDATAMAP_SRC_CATALOG_KEY = "srcCatalog";
047    
048    /** The key for the destinationcatalog */
049    public static final String JOBDATAMAP_DEST_CATALOG_KEY = "destCatalog";
050    
051    /** The catalogs manager */
052    protected CatalogsManager _catalogsManager;
053    
054    @Override
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        super.service(manager);
058        _catalogsManager = (CatalogsManager) manager.lookup(CatalogsManager.ROLE);
059    }
060    
061    @Override
062    protected void _doExecute(JobExecutionContext context) throws Exception
063    {
064        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
065        
066        // Check source catalog
067        String srcCatalogName = jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_SRC_CATALOG_KEY);
068        if (StringUtils.isEmpty(srcCatalogName))
069        {
070            throw new JobExecutionException("The source catalog name cannot be empty.");
071        }
072        
073        // Check destination catalog
074        String destCatalogName = jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_DEST_CATALOG_KEY);
075        if (StringUtils.isEmpty(destCatalogName))
076        {
077            throw new JobExecutionException("The destination catalog name cannot be empty.");
078        }
079        
080        // Check that source and destination catalog are different
081        if (Objects.equals(srcCatalogName, destCatalogName))
082        {
083            throw new JobExecutionException("The source and destination catalogs cannot be the same.");
084        }
085        
086        // Check the existence of the source catalog
087        Catalog srcCatalog = _catalogsManager.getCatalog(srcCatalogName);
088        if (srcCatalog == null)
089        {
090            throw new JobExecutionException("The source catalog should exist.");
091        }
092
093        // Check the existence of the destination catalog
094        Catalog destCatalog = _catalogsManager.getCatalog(destCatalogName);
095        if (destCatalog == null)
096        {
097            destCatalog = _catalogsManager.createCatalog(destCatalogName, destCatalogName);
098        }
099        
100        _catalogsManager.copyCatalog(destCatalog, srcCatalog);
101    }
102
103    @Override
104    protected I18nizableText _getSuccessMailSubject(JobExecutionContext context) throws Exception
105    {
106        return new I18nizableText("plugin.odf", "PLUGINS_ODF_SCHEDULABLE_COPY_CATALOG_SUCCESS_MAIL_SUBJECT");
107    }
108
109    @Override
110    protected I18nizableText _getSuccessMailBody(JobExecutionContext context) throws Exception
111    {
112        return new I18nizableText("plugin.odf", "PLUGINS_ODF_SCHEDULABLE_COPY_CATALOG_SUCCESS_MAIL_BODY", _getCommonI18nParams(context));
113    }
114
115    @Override
116    protected I18nizableText _getErrorMailSubject(JobExecutionContext context) throws Exception
117    {
118        return new I18nizableText("plugin.odf", "PLUGINS_ODF_SCHEDULABLE_COPY_CATALOG_ERROR_MAIL_SUBJECT");
119    }
120
121    @Override
122    protected I18nizableText _getErrorMailBody(JobExecutionContext context, Throwable throwable) throws Exception
123    {
124        Map<String, I18nizableTextParameter> i18nParams = _getCommonI18nParams(context);
125        String error = ExceptionUtils.getStackTrace(throwable);
126        i18nParams.put("error", new I18nizableText(error));
127        return new I18nizableText("plugin.odf", "PLUGINS_ODF_SCHEDULABLE_COPY_CATALOG_ERROR_MAIL_BODY", i18nParams);
128    }
129    
130    private Map<String, I18nizableTextParameter> _getCommonI18nParams(JobExecutionContext context)
131    {
132        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
133        
134        Map<String, I18nizableTextParameter> i18nParams = new HashMap<>();
135        i18nParams.put("srcCatalog", new I18nizableText(jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_SRC_CATALOG_KEY)));
136        i18nParams.put("destCatalog", new I18nizableText(jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_DEST_CATALOG_KEY)));
137        return i18nParams;
138    }
139}