001/* 002 * Copyright 2019 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.lheo; 017 018import java.io.IOException; 019import java.nio.file.AccessDeniedException; 020import java.time.LocalDateTime; 021import java.time.format.DateTimeFormatter; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.ProcessingException; 028import org.apache.cocoon.environment.ObjectModelHelper; 029import org.apache.cocoon.environment.Request; 030import org.apache.cocoon.environment.Response; 031import org.apache.cocoon.generation.ServiceableGenerator; 032import org.xml.sax.SAXException; 033 034import org.ametys.core.right.RightManager; 035import org.ametys.core.right.RightManager.RightResult; 036import org.ametys.odf.program.AbstractProgram; 037 038/** 039 * Generates catalog to LHEO 040 */ 041public class GlobalExportToLHEOGenerator extends ServiceableGenerator 042{ 043 /** The export LHEO manager */ 044 protected ExportToLHEOManager _exportLHEOManager; 045 046 /** The LHEO query manager */ 047 protected LHEOQueryManager _lheoQueryManager; 048 049 /** The right manager */ 050 protected RightManager _rightManager; 051 052 @Override 053 public void service(ServiceManager smanager) throws ServiceException 054 { 055 super.service(smanager); 056 _exportLHEOManager = (ExportToLHEOManager) smanager.lookup(ExportToLHEOManager.ROLE); 057 _lheoQueryManager = (LHEOQueryManager) smanager.lookup(LHEOQueryManager.ROLE); 058 _rightManager = (RightManager) smanager.lookup(RightManager.ROLE); 059 } 060 061 @Override 062 public void generate() throws IOException, SAXException, ProcessingException 063 { 064 if (_rightManager.currentUserHasRight(ExportToLHEOManager.RIGHT_EXPORT_LHEO, "/${WorkspaceName}") != RightResult.RIGHT_ALLOW) 065 { 066 throw new AccessDeniedException("User tried to export a catalog to LHEO without sufficient rights"); 067 } 068 069 Request request = ObjectModelHelper.getRequest(objectModel); 070 071 String catalog = request.getParameter("catalog"); 072 String lang = request.getParameter("lang"); 073 074 List<AbstractProgram> programList = _lheoQueryManager.getAbstractProgramsToExport(catalog, lang, Map.of()); 075 076 // Configure the response header 077 Response response = ObjectModelHelper.getResponse(objectModel); 078 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddhhmmss"); 079 response.setHeader("Content-Disposition", "attachment; filename=\"LHEO_" + catalog + "_" + LocalDateTime.now().format(formatter) + ".xml" + "\""); 080 081 _exportLHEOManager.saxLHEO(contentHandler, programList); 082 } 083}