001/* 002 * Copyright 2017 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.plugins.odfweb.catalog; 017 018import java.util.Collections; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024 025import org.ametys.core.ui.Callable; 026import org.ametys.core.user.User; 027import org.ametys.core.user.UserIdentity; 028import org.ametys.odf.catalog.Catalog; 029import org.ametys.plugins.odfweb.repository.OdfPageHandler; 030import org.ametys.runtime.i18n.I18nizableText; 031import org.ametys.web.repository.page.Page; 032 033/** 034 * DAO for ODF catalog. 035 * Override {@link Catalog} to handle web specificities. 036 */ 037public class CatalogDAO extends org.ametys.odf.catalog.CatalogDAO 038{ 039 private OdfPageHandler _odfPageHandler; 040 041 @Override 042 public void service(ServiceManager smanager) throws ServiceException 043 { 044 super.service(smanager); 045 _odfPageHandler = (OdfPageHandler) smanager.lookup(OdfPageHandler.ROLE); 046 } 047 048 @Callable 049 @Override 050 public Map<String, Object> removeCatalog(String id) 051 { 052 Catalog catalog = _resolver.resolveById(id); 053 Page page = _odfPageHandler.getOdfRootPage(null, null, catalog.getName()); 054 055 if (page != null) 056 { 057 Map<String, Object> result = new HashMap<>(); 058 result.put("error", new I18nizableText("plugin.odf-web", "PLUGINS_ODFWEB_CATALOG_DELETE_ERROR_ODFROOT", Collections.singletonList(page.getTitle()))); 059 return result; 060 } 061 062 return super.removeCatalog(id); 063 064 } 065 066 /** 067 * Generates the PDF of this catalog. 068 * @param name The name (code) of the catalog to export 069 * @param contextualParams The contextual parameters 070 * @return An empty map, or an error 071 */ 072 @Override 073 @Callable 074 public Map<String, Object> generatePDF(String name, Map<String, Object> contextualParams) 075 { 076 Map<String, Object> result = new HashMap<>(); 077 078 if (org.ametys.odf.export.pdf.GeneratePDFEngine.isRunning()) 079 { 080 result.put("message", "already-running"); 081 } 082 else 083 { 084 GeneratePDFEngine generatePDFEngine = new GeneratePDFEngine(); 085 086 UserIdentity identity = _currentUserProvider.getUser(); 087 User currentUser = _userManager.getUser(identity.getPopulationId(), identity.getLogin()); 088 089 generatePDFEngine.setContextualParameters(contextualParams); 090 generatePDFEngine.setCatalog(name); 091 generatePDFEngine.setIssuer(currentUser); 092 093 try 094 { 095 // Initialize and configure the engine. 096 generatePDFEngine.initialize(_manager, _context); 097 098 // Create the thread, mark it as daemon and start it. 099 Thread engineThread = new Thread(generatePDFEngine, "GeneratePDFEngine"); 100 engineThread.setDaemon(true); 101 engineThread.start(); 102 } 103 catch (Exception e) 104 { 105 throw new RuntimeException("Unable to initialize the generate pdf engine", e); 106 } 107 } 108 109 return result; 110 } 111}