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