001/* 002 * (C) Copyright 2013 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * Contributors: 014 * Vladimir Pasquier <vpasquier@nuxeo.com> 015 */ 016 017package org.nuxeo.ecm.platform.groups.audit.seam; 018 019import java.io.File; 020import java.io.FileInputStream; 021import java.io.IOException; 022import java.io.InputStream; 023import java.io.Serializable; 024import java.util.ArrayList; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029import javax.activation.MimetypesFileTypeMap; 030import javax.faces.context.ExternalContext; 031import javax.faces.context.FacesContext; 032import javax.servlet.ServletOutputStream; 033import javax.servlet.http.HttpServletResponse; 034 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037import org.jboss.seam.ScopeType; 038import org.jboss.seam.annotations.In; 039import org.jboss.seam.annotations.Install; 040import org.jboss.seam.annotations.Name; 041import org.jboss.seam.annotations.Scope; 042import org.nuxeo.common.utils.FileUtils; 043import org.nuxeo.ecm.core.api.CoreSession; 044import org.nuxeo.ecm.core.api.DocumentModel; 045import org.nuxeo.ecm.core.api.NuxeoGroup; 046import org.nuxeo.ecm.platform.contentview.seam.ContentViewActions; 047import org.nuxeo.ecm.platform.groups.audit.service.ExcelExportService; 048import org.nuxeo.ecm.platform.query.api.PageProvider; 049import org.nuxeo.ecm.platform.usermanager.UserManager; 050import org.nuxeo.runtime.api.Framework; 051 052/** 053 * Export group manager seam bean to export groups definition in excel file 054 */ 055@Name("exportGroupManagementActions") 056@Scope(ScopeType.CONVERSATION) 057@Install(precedence = Install.APPLICATION) 058public class ExportGroupManagementActions implements Serializable { 059 060 private static final long serialVersionUID = 1L; 061 062 public static final Log log = LogFactory.getLog(ExportGroupManagementActions.class); 063 064 @In(create = true, required = false) 065 protected transient CoreSession documentManager; 066 067 @In(create = true) 068 protected transient ContentViewActions contentViewActions; 069 070 public String downloadExcelAllGroupsExport() { 071 FacesContext context = FacesContext.getCurrentInstance(); 072 ExternalContext econtext = context.getExternalContext(); 073 HttpServletResponse response = (HttpServletResponse) econtext.getResponse(); 074 File excelReport = excelExportAllGroupsDefinition(); 075 076 response.setContentType(new MimetypesFileTypeMap().getContentType(excelReport)); 077 response.setHeader("Content-disposition", "attachment; filename=\"" + excelReport.getName() + "\""); 078 response.setHeader("Content-Length", String.valueOf(excelReport.length())); 079 try { 080 ServletOutputStream os = response.getOutputStream(); 081 InputStream in = new FileInputStream(excelReport); 082 FileUtils.copy(in, os); 083 os.flush(); 084 in.close(); 085 os.close(); 086 context.responseComplete(); 087 } catch (IOException e) { 088 log.error("Failure : " + e.getMessage()); 089 } 090 return null; 091 } 092 093 public String downloadExcelListedGroupsExport() { 094 FacesContext context = FacesContext.getCurrentInstance(); 095 ExternalContext econtext = context.getExternalContext(); 096 HttpServletResponse response = (HttpServletResponse) econtext.getResponse(); 097 File excelReport = excelExportListedGroupsDefinition(); 098 099 response.setContentType(new MimetypesFileTypeMap().getContentType(excelReport)); 100 response.setHeader("Content-disposition", "attachment; filename=\"" + excelReport.getName() + "\""); 101 response.setHeader("Content-Length", String.valueOf(excelReport.length())); 102 try { 103 ServletOutputStream os = response.getOutputStream(); 104 InputStream in = new FileInputStream(excelReport); 105 FileUtils.copy(in, os); 106 os.flush(); 107 in.close(); 108 os.close(); 109 context.responseComplete(); 110 } catch (IOException e) { 111 log.error("Failure : " + e.getMessage()); 112 } 113 return null; 114 } 115 116 protected File excelExportAllGroupsDefinition() { 117 ExcelExportService exportService = Framework.getLocalService(ExcelExportService.class); 118 return exportService.getExcelReport("exportAllGroupsAudit"); 119 } 120 121 protected File excelExportListedGroupsDefinition() { 122 ExcelExportService exportService = Framework.getLocalService(ExcelExportService.class); 123 return exportService.getExcelReport("exportListedGroupsAudit", getDataInject()); 124 } 125 126 private Map<String, Object> getDataInject() { 127 UserManager userManager = Framework.getLocalService(UserManager.class); 128 List<NuxeoGroup> groups = new ArrayList<NuxeoGroup>(); 129 PageProvider currentPP = contentViewActions.getCurrentContentView().getCurrentPageProvider(); 130 List<DocumentModel> groupModels = (ArrayList<DocumentModel>) currentPP.getCurrentPage(); 131 for (DocumentModel groupModel : groupModels) { 132 NuxeoGroup group = userManager.getGroup(groupModel.getId()); 133 groups.add(group); 134 } 135 Map<String, Object> beans = new HashMap<String, Object>(); 136 beans.put("groups", groups); 137 beans.put("userManager", userManager); 138 return beans; 139 } 140}