001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others. 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 * Contributors: 016 * Vladimir Pasquier <[email protected]> 017 */ 018 019package org.nuxeo.ecm.platform.groups.audit.seam; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.Serializable; 026import java.util.ArrayList; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030 031import javax.activation.MimetypesFileTypeMap; 032import javax.faces.context.ExternalContext; 033import javax.faces.context.FacesContext; 034import javax.servlet.ServletOutputStream; 035import javax.servlet.http.HttpServletResponse; 036 037import org.apache.commons.io.IOUtils; 038import org.apache.commons.logging.Log; 039import org.apache.commons.logging.LogFactory; 040import org.jboss.seam.ScopeType; 041import org.jboss.seam.annotations.In; 042import org.jboss.seam.annotations.Install; 043import org.jboss.seam.annotations.Name; 044import org.jboss.seam.annotations.Scope; 045import org.nuxeo.ecm.core.api.CoreSession; 046import org.nuxeo.ecm.core.api.DocumentModel; 047import org.nuxeo.ecm.core.api.NuxeoGroup; 048import org.nuxeo.ecm.platform.contentview.seam.ContentViewActions; 049import org.nuxeo.ecm.platform.groups.audit.service.ExcelExportService; 050import org.nuxeo.ecm.platform.query.api.PageProvider; 051import org.nuxeo.ecm.platform.usermanager.UserManager; 052import org.nuxeo.runtime.api.Framework; 053 054/** 055 * Export group manager seam bean to export groups definition in excel file 056 */ 057@Name("exportGroupManagementActions") 058@Scope(ScopeType.CONVERSATION) 059@Install(precedence = Install.APPLICATION) 060public class ExportGroupManagementActions implements Serializable { 061 062 private static final long serialVersionUID = 1L; 063 064 public static final Log log = LogFactory.getLog(ExportGroupManagementActions.class); 065 066 @In(create = true, required = false) 067 protected transient CoreSession documentManager; 068 069 @In(create = true) 070 protected transient ContentViewActions contentViewActions; 071 072 public String downloadExcelAllGroupsExport() { 073 FacesContext context = FacesContext.getCurrentInstance(); 074 ExternalContext econtext = context.getExternalContext(); 075 HttpServletResponse response = (HttpServletResponse) econtext.getResponse(); 076 File excelReport = excelExportAllGroupsDefinition(); 077 078 response.setContentType(new MimetypesFileTypeMap().getContentType(excelReport)); 079 response.setHeader("Content-disposition", "attachment; filename=\"" + excelReport.getName() + "\""); 080 response.setHeader("Content-Length", String.valueOf(excelReport.length())); 081 try { 082 ServletOutputStream os = response.getOutputStream(); 083 InputStream in = new FileInputStream(excelReport); 084 IOUtils.copy(in, os); 085 os.flush(); 086 in.close(); 087 os.close(); 088 context.responseComplete(); 089 } catch (IOException e) { 090 log.error("Failure : " + e.getMessage()); 091 } 092 return null; 093 } 094 095 public String downloadExcelListedGroupsExport() { 096 FacesContext context = FacesContext.getCurrentInstance(); 097 ExternalContext econtext = context.getExternalContext(); 098 HttpServletResponse response = (HttpServletResponse) econtext.getResponse(); 099 File excelReport = excelExportListedGroupsDefinition(); 100 101 response.setContentType(new MimetypesFileTypeMap().getContentType(excelReport)); 102 response.setHeader("Content-disposition", "attachment; filename=\"" + excelReport.getName() + "\""); 103 response.setHeader("Content-Length", String.valueOf(excelReport.length())); 104 try { 105 ServletOutputStream os = response.getOutputStream(); 106 InputStream in = new FileInputStream(excelReport); 107 IOUtils.copy(in, os); 108 os.flush(); 109 in.close(); 110 os.close(); 111 context.responseComplete(); 112 } catch (IOException e) { 113 log.error("Failure : " + e.getMessage()); 114 } 115 return null; 116 } 117 118 protected File excelExportAllGroupsDefinition() { 119 ExcelExportService exportService = Framework.getService(ExcelExportService.class); 120 return exportService.getExcelReport("exportAllGroupsAudit"); 121 } 122 123 protected File excelExportListedGroupsDefinition() { 124 ExcelExportService exportService = Framework.getService(ExcelExportService.class); 125 return exportService.getExcelReport("exportListedGroupsAudit", getDataInject()); 126 } 127 128 private Map<String, Object> getDataInject() { 129 UserManager userManager = Framework.getService(UserManager.class); 130 List<NuxeoGroup> groups = new ArrayList<>(); 131 PageProvider currentPP = contentViewActions.getCurrentContentView().getCurrentPageProvider(); 132 List<DocumentModel> groupModels = (ArrayList<DocumentModel>) currentPP.getCurrentPage(); 133 for (DocumentModel groupModel : groupModels) { 134 NuxeoGroup group = userManager.getGroup(groupModel.getId()); 135 groups.add(group); 136 } 137 Map<String, Object> beans = new HashMap<>(); 138 beans.put("groups", groups); 139 beans.put("userManager", userManager); 140 return beans; 141 } 142}