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.FilenameUtils;
030import org.jboss.seam.ScopeType;
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Install;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Observer;
035import org.jboss.seam.annotations.Scope;
036import org.nuxeo.ecm.core.api.Blobs;
037import org.nuxeo.ecm.core.api.CoreSession;
038import org.nuxeo.ecm.csv.core.CSVImportLog;
039import org.nuxeo.ecm.csv.core.CSVImportResult;
040import org.nuxeo.ecm.csv.core.CSVImportStatus;
041import org.nuxeo.ecm.csv.core.CSVImporter;
042import org.nuxeo.ecm.csv.core.CSVImporterOptions;
043import org.nuxeo.ecm.csv.core.CSVImporterOptions.ImportMode;
044import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
045import org.nuxeo.ecm.webapp.helpers.EventNames;
046import org.nuxeo.runtime.api.Framework;
047import org.richfaces.event.FileUploadEvent;
048import org.richfaces.model.UploadedFile;
049
050;
051
052/**
053 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
054 * @since 5.7
055 */
056@Scope(ScopeType.CONVERSATION)
057@Name("csvImportActions")
058@Install(precedence = Install.FRAMEWORK)
059public class CSVImportActions implements Serializable {
060
061    private static final long serialVersionUID = 1L;
062
063    @In(create = true, required = false)
064    protected transient CoreSession documentManager;
065
066    @In(create = true, required = false)
067    protected transient NavigationContext navigationContext;
068
069    protected File csvFile;
070
071    protected String csvFileName;
072
073    protected boolean notifyUserByEmail = false;
074
075    protected String csvImportId;
076
077    /**
078     * @since 8.4
079     */
080    protected Boolean useImportMode = false;
081
082    public boolean getNotifyUserByEmail() {
083        return notifyUserByEmail;
084    }
085
086    public void setNotifyUserByEmail(boolean notifyUserByEmail) {
087        this.notifyUserByEmail = notifyUserByEmail;
088    }
089
090    public Boolean getUseImportMode() {
091        return useImportMode;
092    }
093
094    public void setUseImportMode(Boolean importMode) {
095        this.useImportMode = importMode;
096    }
097
098    protected ImportMode getImportMode() {
099        return useImportMode ? ImportMode.IMPORT : ImportMode.CREATE;
100    }
101
102    public void uploadListener(FileUploadEvent event) throws Exception {
103        UploadedFile item = event.getUploadedFile();
104        // FIXME: check if this needs to be tracked for deletion
105        csvFile = Framework.createTempFile("FileManageActionsFile", null);
106        InputStream in = event.getUploadedFile().getInputStream();
107        org.nuxeo.common.utils.FileUtils.copyToFile(in, csvFile);
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}