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.core;
021
022import org.nuxeo.ecm.csv.core.CSVImportLog.Status;
023import org.nuxeo.ecm.core.api.CoreSession;
024
025import java.io.File;
026import java.util.Arrays;
027import java.util.List;
028import java.util.stream.Collectors;
029
030/**
031 * @since 5.7
032 */
033public class CSVImporterImpl implements CSVImporter {
034
035    @Override
036    public String launchImport(CoreSession session, String parentPath, File csvFile, String csvFileName,
037            CSVImporterOptions options) {
038        return new CSVImporterWork(session.getRepositoryName(), parentPath, session.getPrincipal().getName(), csvFile,
039                csvFileName, options).launch();
040    }
041
042    @Override
043    public CSVImportStatus getImportStatus(String id) {
044        return CSVImporterWork.getStatus(id);
045    }
046
047    @Override
048    public List<CSVImportLog> getImportLogs(String id) {
049        return getLastImportLogs(id, -1);
050    }
051
052    @Override
053    public List<CSVImportLog> getImportLogs(String id, Status... status) {
054        return getLastImportLogs(id, -1, status);
055    }
056
057    // @SuppressWarnings("unchecked")
058    @Override
059    public List<CSVImportLog> getLastImportLogs(String id, int max) {
060        List<CSVImportLog> importLogs = CSVImporterWork.getLastImportLogs(id);
061        max = (max == -1 || max > importLogs.size()) ? importLogs.size() : max;
062        return importLogs.subList(importLogs.size() - max, importLogs.size());
063    }
064
065    @Override
066    public List<CSVImportLog> getLastImportLogs(String id, int max, CSVImportLog.Status... status) {
067        List<CSVImportLog> importLogs = getLastImportLogs(id, max);
068        return status.length == 0 ? importLogs : filterImportLogs(importLogs, status);
069    }
070
071    protected List<CSVImportLog> filterImportLogs(List<CSVImportLog> importLogs, CSVImportLog.Status... status) {
072        List<CSVImportLog.Status> statusList = Arrays.asList(status);
073        return importLogs.stream().filter(log -> statusList.contains(log.getStatus())).collect(Collectors.toList());
074    }
075
076    @Override
077    public CSVImportResult getImportResult(String id) {
078        return CSVImportResult.fromImportLogs(getLastImportLogs(id, -1));
079    }
080
081}