Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement NXP-28548 add csv import source #4712

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright 2012-2018 Nuxeo (http://nuxeo.com/) and others.
* (C) Copyright 2012-2021 Nuxeo (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,13 +55,17 @@ public class DefaultCSVImporterDocumentFactory implements CSVImporterDocumentFac

public static final List<String> IGNORE_FIELDS_ON_UPDATE = Collections.singletonList(NXQL.ECM_LIFECYCLESTATE);

/** @since 11.5 */
public static final String CSV_IMPORT_SOURCE = "csvImport";

protected CSVImporterOptions importerOptions = CSVImporterOptions.DEFAULT_OPTIONS;

@Override
public void createDocument(CoreSession session, String parentPath, String name, String type,
Map<String, Serializable> values) {
values = prepareValues(values);
DocumentModel doc = session.createDocumentModel(parentPath, name, type);
doc.putContextData(CoreSession.SOURCE, CSV_IMPORT_SOURCE);

if (importerOptions.importMode.equals(ImportMode.IMPORT)) {
setLifeCycleState(values, doc, IMPORT_LIFECYCLE_STATE);
Expand All @@ -87,7 +91,8 @@ public void createDocument(CoreSession session, String parentPath, String name,
}
}

protected void setLifeCycleState(Map<String, Serializable> values, DocumentModel doc, String lifeCyclePropertyName) {
protected void setLifeCycleState(Map<String, Serializable> values, DocumentModel doc,
String lifeCyclePropertyName) {
if (values.containsKey(NXQL.ECM_LIFECYCLESTATE)) {
doc.putContextData(lifeCyclePropertyName, values.get(NXQL.ECM_LIFECYCLESTATE));
values.remove(NXQL.ECM_LIFECYCLESTATE);
Expand All @@ -104,14 +109,15 @@ protected Map<String, Serializable> prepareValues(Map<String, Serializable> valu
if (StringUtils.isNotBlank(creator) && !contributors.contains(creator)) {
contributors.add(creator);
}
values.put(DC_CONTRIBUTORS, contributors.toArray(new String[contributors.size()]));
values.put(DC_CONTRIBUTORS, contributors.toArray(new String[0]));
}
return values;
}

@Override
public void updateDocument(CoreSession session, DocumentRef docRef, Map<String, Serializable> values) {
DocumentModel doc = session.getDocument(docRef);
doc.putContextData(CoreSession.SOURCE, CSV_IMPORT_SOURCE);
for (Map.Entry<String, Serializable> entry : values.entrySet()) {
if (!IGNORE_FIELDS_ON_UPDATE.contains(entry.getKey())) {
doc.setPropertyValue(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
* (C) Copyright 2012-2021 Nuxeo (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,14 @@

package org.nuxeo.ecm.csv.core;

import static java.util.function.Predicate.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.nuxeo.ecm.core.api.CoreSession.SOURCE;
import static org.nuxeo.ecm.csv.core.DefaultCSVImporterDocumentFactory.CSV_IMPORT_SOURCE;

import java.io.File;
import java.io.IOException;
Expand All @@ -50,6 +53,8 @@
import org.nuxeo.ecm.core.api.security.ACE;
import org.nuxeo.ecm.core.api.security.ACL;
import org.nuxeo.ecm.core.api.security.ACP;
import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
import org.nuxeo.ecm.core.event.test.CapturingEventListener;
import org.nuxeo.ecm.core.schema.utils.DateParser;
import org.nuxeo.ecm.core.test.CoreFeature;
import org.nuxeo.ecm.csv.core.CSVImporterOptions.ImportMode;
Expand Down Expand Up @@ -474,7 +479,7 @@ public void shouldSetCreatorToTheUserImporting() throws InterruptedException {
TransactionHelper.startTransaction();

try {
CoreSession leelaSession = openSessionAs("leela");
CoreSession leelaSession = coreFeature.getCoreSession("leela");
String importId = csvImporter.launchImport(leelaSession, "/", getCSVBlob(DOCS_WITHOUT_CONTRIBUTORS_CSV),
options);

Expand Down Expand Up @@ -672,8 +677,7 @@ public void shouldComputeTotalAtEndIfSmallerThanThreshold() throws IOException,

protected void testImportTotals(CSVImporterOptions options) throws IOException, InterruptedException {
TransactionHelper.commitOrRollbackTransaction();
String importId = csvImporter.launchImport(session, "/", getCSVBlob(DOCS_OK_BIG_CSV),
options);
String importId = csvImporter.launchImport(session, "/", getCSVBlob(DOCS_OK_BIG_CSV), options);
workManager.awaitCompletion(10000, TimeUnit.SECONDS);
TransactionHelper.startTransaction();

Expand All @@ -683,8 +687,33 @@ protected void testImportTotals(CSVImporterOptions options) throws IOException,
assertEquals(336, importStatus.getNumberOfProcessedDocument());
}

public CoreSession openSessionAs(String username) {
return coreFeature.getCoreSession(username);
}
/**
* NXP-28548
*/
@Test
public void shouldSetCsvImportSource() throws InterruptedException, IOException {
TransactionHelper.commitOrRollbackTransaction();
String importId;
try (var listener = new CapturingEventListener("documentCreated")) {
CSVImporterOptions options = new CSVImporterOptions.Builder().importMode(ImportMode.CREATE).build();
importId = csvImporter.launchImport(session, "/", getCSVBlob(DOCS_OK_CSV), options);
workManager.awaitCompletion(10000, TimeUnit.SECONDS);

boolean allHasCsvImportSource = listener.streamCapturedEventContexts(DocumentEventContext.class)
.map(DocumentEventContext::getSourceDocument)
// exclude auto versioned note
.filter(not(DocumentModel::isVersion))
.map(doc -> doc.getContextData(SOURCE))
.allMatch(CSV_IMPORT_SOURCE::equals);
assertTrue("Some imported documents doesn't have the csvImport source", allHasCsvImportSource);
}
TransactionHelper.startTransaction();

CSVImportStatus importStatus = csvImporter.getImportStatus(importId);
assertNotNull(importStatus);
assertTrue(importStatus.isComplete());

List<CSVImportLog> importLogs = csvImporter.getImportLogs(importId);
assertEquals(3, importLogs.size());
}
}