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.cms.search.solr;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.quartz.JobKey;
026import org.quartz.SchedulerException;
027
028import org.ametys.core.schedule.Runnable;
029import org.ametys.core.schedule.Runnable.FireProcess;
030import org.ametys.core.ui.Callable;
031import org.ametys.core.user.CurrentUserProvider;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.plugins.core.impl.schedule.DefaultRunnable;
034import org.ametys.plugins.core.schedule.Scheduler;
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.runtime.plugin.component.AbstractLogEnabled;
037
038/**
039 * Export a solr search
040 */
041public class AsyncExportLauncher extends AbstractLogEnabled implements Component, Serviceable
042{
043    /** The Avalon Role */
044    public static final String ROLE = AsyncExportLauncher.class.getName();
045    
046    /** The provider of current user */
047    protected CurrentUserProvider _currentUserProvider;
048    
049    /** Scheduler */
050    protected Scheduler _scheduler;
051
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
055        _scheduler = (Scheduler) manager.lookup(Scheduler.ROLE);
056    }
057    
058    /**
059     * Launch a search export
060     * @param type csv, xml or word
061     * @param recipient email of the recipient of the export
062     * @param searchParams search parameters
063     * @param lang language for the mail
064     * @param exportUrl the internal url for export
065     * @return A result map
066     * @throws SchedulerException if an error occured
067     */
068    @Callable
069    public Map<String, Object> add(String type, String recipient, String searchParams, String lang, String exportUrl)  throws SchedulerException
070    {
071        I18nizableText label = new I18nizableText("plugin.cms", "UITOOL_SEARCH_ASYNC_EXPORT_TASK_TITLE");
072        I18nizableText description = new I18nizableText("plugin.cms", "UITOOL_SEARCH_ASYNC_EXPORT_TASK_DESCRIPTION");
073        
074        
075        String id = _generateUniqueId();
076
077        String cron = "";
078        String schedulableId = "org.ametys.cms.search.schedule.Export";
079        
080        UserIdentity user = _currentUserProvider.getUser();
081        
082        Map<String, Object> typedParams = new HashMap<>();
083        typedParams.put("type", type);
084        typedParams.put("recipient", recipient);
085        typedParams.put("searchParams", searchParams);
086        typedParams.put("lang", lang);
087        typedParams.put("exportUrl", exportUrl);
088        
089        Runnable runnable = new DefaultRunnable(id, label, description, FireProcess.NOW, cron, schedulableId, false, false, false, null, true, user, typedParams);
090        
091        Map<String, Object> result = new HashMap<>();
092        try
093        {
094            _scheduler.scheduleJob(runnable);
095        }
096        catch (SchedulerException e)
097        {
098            getLogger().error("An error occured when trying to create the task " + id, e);
099            result.put("error", "scheduler-error");
100            return result;
101        }
102        
103        result.put("id", id);
104        return result;
105    }
106    
107    /**
108     * Generate an unique ID for a scheduler label
109     * @return a unique id for this sceduler
110     * @throws SchedulerException Something went wrong
111     */
112    protected String _generateUniqueId() throws SchedulerException
113    {
114        String value = "async-export-solr-search";
115        int i = 2;
116        String suffixedValue = value;
117        while (_scheduler.getScheduler().checkExists(new JobKey(suffixedValue, Scheduler.JOB_GROUP)))
118        {
119            suffixedValue = value + i;
120            i++;
121        }
122        
123        return suffixedValue;
124    }
125
126}