001/* 002 * (C) Copyright 2012 Nuxeo SA (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 * 014 * Contributors: 015 * Thomas Roger 016 */ 017 018package org.nuxeo.ecm.csv; 019 020import java.io.File; 021import java.io.InputStream; 022import java.io.Serializable; 023import java.util.Collections; 024import java.util.List; 025 026import org.apache.commons.io.FilenameUtils; 027import org.jboss.seam.ScopeType; 028import org.jboss.seam.annotations.In; 029import org.jboss.seam.annotations.Install; 030import org.jboss.seam.annotations.Name; 031import org.jboss.seam.annotations.Observer; 032import org.jboss.seam.annotations.Scope; 033import org.nuxeo.ecm.core.api.CoreSession; 034import org.nuxeo.ecm.csv.CSVImportLog.Status; 035import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 036import org.nuxeo.ecm.webapp.helpers.EventNames; 037import org.nuxeo.runtime.api.Framework; 038import org.richfaces.event.FileUploadEvent; 039import org.richfaces.model.UploadedFile; 040 041/** 042 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 043 * @since 5.7 044 */ 045@Scope(ScopeType.CONVERSATION) 046@Name("csvImportActions") 047@Install(precedence = Install.FRAMEWORK) 048public class CSVImportActions implements Serializable { 049 050 private static final long serialVersionUID = 1L; 051 052 @In(create = true, required = false) 053 protected transient CoreSession documentManager; 054 055 @In(create = true, required = false) 056 protected transient NavigationContext navigationContext; 057 058 protected File csvFile; 059 060 protected String csvFileName; 061 062 protected boolean notifyUserByEmail = false; 063 064 protected String csvImportId; 065 066 public boolean getNotifyUserByEmail() { 067 return notifyUserByEmail; 068 } 069 070 public void setNotifyUserByEmail(boolean notifyUserByEmail) { 071 this.notifyUserByEmail = notifyUserByEmail; 072 } 073 074 public void uploadListener(FileUploadEvent event) throws Exception { 075 UploadedFile item = event.getUploadedFile(); 076 // FIXME: check if this needs to be tracked for deletion 077 csvFile = File.createTempFile("FileManageActionsFile", null); 078 InputStream in = event.getUploadedFile().getInputStream(); 079 org.nuxeo.common.utils.FileUtils.copyToFile(in, csvFile); 080 csvFileName = FilenameUtils.getName(item.getName()); 081 } 082 083 public void importCSVFile() { 084 if (csvFile != null) { 085 CSVImporterOptions options = new CSVImporterOptions.Builder().sendEmail(notifyUserByEmail).build(); 086 CSVImporter csvImporter = Framework.getLocalService(CSVImporter.class); 087 csvImportId = csvImporter.launchImport(documentManager, 088 navigationContext.getCurrentDocument().getPathAsString(), csvFile, csvFileName, options); 089 } 090 } 091 092 public String getImportingCSVFilename() { 093 return csvFileName; 094 } 095 096 public CSVImportStatus getImportStatus() { 097 if (csvImportId == null) { 098 return null; 099 } 100 CSVImporter csvImporter = Framework.getLocalService(CSVImporter.class); 101 return csvImporter.getImportStatus(csvImportId); 102 } 103 104 public List<CSVImportLog> getLastLogs(int maxLogs) { 105 if (csvImportId == null) { 106 return Collections.emptyList(); 107 } 108 CSVImporter csvImporter = Framework.getLocalService(CSVImporter.class); 109 return csvImporter.getLastImportLogs(csvImportId, maxLogs); 110 } 111 112 public List<CSVImportLog> getSkippedAndErrorLogs() { 113 if (csvImportId == null) { 114 return Collections.emptyList(); 115 } 116 CSVImporter csvImporter = Framework.getLocalService(CSVImporter.class); 117 return csvImporter.getImportLogs(csvImportId, Status.SKIPPED, Status.ERROR); 118 } 119 120 public CSVImportResult getImportResult() { 121 if (csvImportId == null) { 122 return null; 123 } 124 CSVImporter csvImporter = Framework.getLocalService(CSVImporter.class); 125 return csvImporter.getImportResult(csvImportId); 126 } 127 128 @Observer(EventNames.NAVIGATE_TO_DOCUMENT) 129 public void resetState() { 130 csvFile = null; 131 csvFileName = null; 132 csvImportId = null; 133 notifyUserByEmail = false; 134 } 135}