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