Skip to content

Commit

Permalink
NXP-28548: Add csvImport source to document imported through csv
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinleturc committed Mar 16, 2021
1 parent ff89a99 commit 8ce0405
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -113,6 +117,7 @@ protected Map<String, Serializable> prepareValues(Map<String, Serializable> valu
@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
Expand Up @@ -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 @@ -681,4 +686,32 @@ protected void testImportTotals(CSVImporterOptions options) throws IOException,
assertEquals(336, importStatus.getTotalNumberOfDocument());
assertEquals(336, importStatus.getNumberOfProcessedDocument());
}

// @since 11.5
@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());
}
}

0 comments on commit 8ce0405

Please sign in to comment.