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