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(rights = "CMS_Rights_SearchAsyncExport", context = "/cms") 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 String id = _generateUniqueId(); 075 076 String cron = ""; 077 String schedulableId = "org.ametys.cms.search.schedule.Export"; 078 079 UserIdentity user = _currentUserProvider.getUser(); 080 081 Map<String, Object> typedParams = new HashMap<>(); 082 typedParams.put("type", type); 083 typedParams.put("recipient", recipient); 084 typedParams.put("searchParams", searchParams); 085 typedParams.put("lang", lang); 086 typedParams.put("exportUrl", exportUrl); 087 088 Runnable runnable = new DefaultRunnable(id, label, description, FireProcess.NOW, cron, schedulableId, false, false, false, null, true, user, typedParams); 089 090 Map<String, Object> result = new HashMap<>(); 091 try 092 { 093 _scheduler.scheduleJob(runnable); 094 } 095 catch (SchedulerException e) 096 { 097 getLogger().error("An error occured when trying to create the task " + id, e); 098 result.put("error", "scheduler-error"); 099 return result; 100 } 101 102 result.put("id", id); 103 return result; 104 } 105 106 /** 107 * Generate an unique ID for a scheduler label 108 * @return a unique id for this sceduler 109 * @throws SchedulerException Something went wrong 110 */ 111 protected String _generateUniqueId() throws SchedulerException 112 { 113 String value = "async-export-solr-search"; 114 int i = 2; 115 String suffixedValue = value; 116 while (_scheduler.getScheduler().checkExists(new JobKey(suffixedValue, Scheduler.JOB_GROUP))) 117 { 118 suffixedValue = value + i; 119 i++; 120 } 121 122 return suffixedValue; 123 } 124 125}