Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
package fr.gouv.vitam.tools.sedalib.inout.exporter;

import fr.gouv.vitam.tools.sedalib.core.*;
import fr.gouv.vitam.tools.sedalib.metadata.ArchiveUnitProfile;
import fr.gouv.vitam.tools.sedalib.metadata.content.Content;
import fr.gouv.vitam.tools.sedalib.metadata.data.FileInfo;
import fr.gouv.vitam.tools.sedalib.metadata.management.Management;
Expand Down Expand Up @@ -350,11 +351,20 @@ private void computeCsvHeader() throws SEDALibException {
Set<String> curHeaderNames = new HashSet<>();
List<String> sortedHeaderNames;
for (ArchiveUnit au : dataObjectPackage.getAuInDataObjectPackageIdMap().values()) {
ArchiveUnitProfile archiveUnitProfile = au.getArchiveUnitProfile();
if (archiveUnitProfile != null) curHeaderNames.addAll(archiveUnitProfile.externToCsvList().keySet());
Management management = au.getManagement();
if (management != null) curHeaderNames.addAll(management.externToCsvList().keySet());
curHeaderNames.addAll(au.getContent().externToCsvList(dataObjectPackage.getExportMetadataList()).keySet());
}
sortedHeaderNames = getSortedHeaderNames(new ArrayList<>(), curHeaderNames, "", "Content", Content.class);
sortedHeaderNames = getSortedHeaderNames(
new ArrayList<>(),
curHeaderNames,
"",
"ArchiveUnitProfile",
ArchiveUnitProfile.class
);
sortedHeaderNames.addAll(getSortedHeaderNames(new ArrayList<>(), curHeaderNames, "", "Content", Content.class));
sortedHeaderNames.addAll(
getSortedHeaderNames(new ArrayList<>(), curHeaderNames, "", "Management", Management.class)
);
Expand Down Expand Up @@ -461,9 +471,15 @@ private void generateCsvLine(ArchiveUnit au, ArchiveUnit parentAu, Path auRelati
contentMetadataHashMap = au.getContent().externToCsvList(dataObjectPackage.getExportMetadataList());
Management management = au.getManagement();
if (management != null) managementMetadataHashMap = management.externToCsvList();
ArchiveUnitProfile archiveUnitProfile = au.getArchiveUnitProfile();
LinkedHashMap<String, String> archiveUnitProfileMetadataHashMap = null;
if (archiveUnitProfile != null) archiveUnitProfileMetadataHashMap = archiveUnitProfile.externToCsvList();

for (String header : headerNames) {
value = contentMetadataHashMap.get(header);
if ((value == null) && (managementMetadataHashMap != null)) value = managementMetadataHashMap.get(header);
if ((value == null) && (archiveUnitProfileMetadataHashMap != null)) value =
archiveUnitProfileMetadataHashMap.get(header);
if (value == null) value = "";
else value = "\"" + value.replace("\"", "\"\"") + "\"";
csvPrintStream.print(separator + value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public ValueAttrMetadataTag(boolean isValue, MetadataTag tag) {
PARENTFILE,
OBJECTFILES
);
private MetadataTag rootTag, contentTag, managementTag;
private MetadataTag rootTag, contentTag, managementTag, archiveUnitProfileTag;
private LinkedHashMap<Integer, ValueAttrMetadataTag> tagHeaderColumnMapping;
private int numberOfMandatoryHeaderFound;
private int columnCount;
Expand Down Expand Up @@ -224,6 +224,8 @@ private MetadataTag getInSubTagsMap(MetadataTag tag, String name, int rank) thro
contentTag = subTag;
} else if (name.equals("Management")) {
managementTag = subTag;
} else if (name.equals("ArchiveUnitProfile")) {
archiveUnitProfileTag = subTag;
} else {
throw new SEDALibException("Métadonnées [" + name + "] non conforme SEDA.");
}
Expand Down Expand Up @@ -272,7 +274,8 @@ private void analyseTags(String[] headerRow) throws SEDALibException {
}
if (
headerRow[numberOfMandatoryHeaderFound].startsWith("Content.") ||
headerRow[numberOfMandatoryHeaderFound].startsWith("Management.")
headerRow[numberOfMandatoryHeaderFound].startsWith("Management.") ||
headerRow[numberOfMandatoryHeaderFound].startsWith("ArchiveUnitProfile")
) {
rootTag = new MetadataTag(null, null);
contentTag = null;
Expand Down Expand Up @@ -578,6 +581,19 @@ public String extractManagementXML() throws SEDALibException {
return generateTagXML(managementTag);
}

/**
* Extract the XML ArchiveUnitProfile metadata
*
* @return the XML ArchiveUnitProfile metadata or null
* @throws SEDALibException the seda lib exception
*/
public String extractArchiveUnitProfileXML() throws SEDALibException {
if (archiveUnitProfileTag == null) {
return "";
}
return generateTagXML(archiveUnitProfileTag);
}

/**
* Gets guid.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ private class Line {
* The Management xml metadata.
*/
String managementXMLMetadata;
/**
* The ArchiveUnitProfile xml metadata.
*/
String archiveUnitProfileXMLMetadata;
/**
* The Au.
*/
Expand All @@ -152,7 +156,8 @@ public Line(
String file,
String objectFiles,
String contentXMLMetadata,
String managementXMLMetadata
String managementXMLMetadata,
String archiveUnitProfileXMLMetadata
) {
this.guid = guid;
this.parentGUID = parentGUID;
Expand All @@ -161,6 +166,7 @@ public Line(
else this.objectFiles = Arrays.asList(objectFiles.split("\\|"));
this.contentXMLMetadata = contentXMLMetadata;
this.managementXMLMetadata = managementXMLMetadata;
this.archiveUnitProfileXMLMetadata = archiveUnitProfileXMLMetadata;
this.au = null;
}
}
Expand Down Expand Up @@ -273,11 +279,12 @@ private boolean readCSVFile() throws SEDALibException, InterruptedException {
try {
currentLine = new Line(
metadataFormatter.getGUID(row),
metadataFormatter.getParentGUID(row), //NOSONAR
metadataFormatter.getParentGUID(row), // NOSONAR
metadataFormatter.getFile(row),
metadataFormatter.getObjectFiles(row),
metadataFormatter.doFormatAndExtractContentXML(row),
metadataFormatter.extractManagementXML()
metadataFormatter.extractManagementXML(),
metadataFormatter.extractArchiveUnitProfileXML()
);
} catch (SEDALibException e) {
throw new SEDALibException("Erreur sur la ligne " + lineCount, e);
Expand Down Expand Up @@ -317,6 +324,10 @@ private ArchiveUnit createLineAU(Line line) throws SEDALibException, Interrupted
au.setManagementXmlData(line.managementXMLMetadata);
au.getManagement();
}
if (!line.archiveUnitProfileXMLMetadata.isEmpty()) {
au.setArchiveUnitProfileXmlData(line.archiveUnitProfileXMLMetadata);
au.getArchiveUnitProfile();
}
Path path = getAbsolutePath(line.file);
DataObjectGroup implicitDog = null;
if (isExtendedFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,22 @@ public ArchiveUnitProfile() {
public ArchiveUnitProfile(String value) {
super("ArchiveUnitProfile", value);
}

/**
* Export the ArchiveUnitProfile metadata to csv List for the csv metadata file.
* <p>
* In the HashMap result, the key is a metadata path of a leaf and the value is
* the leaf of the metadata value.
*
* @return the linked hash map with header title as key and metadata value as
* value
* @throws fr.gouv.vitam.tools.sedalib.utils.SEDALibException the seda lib
* exception
*/
public java.util.LinkedHashMap<String, String> externToCsvList()
throws fr.gouv.vitam.tools.sedalib.utils.SEDALibException {
java.util.LinkedHashMap<String, String> result = new java.util.LinkedHashMap<>();
result.put("ArchiveUnitProfile", getValue());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@

import fr.gouv.vitam.tools.sedalib.SedaContextExtension;
import fr.gouv.vitam.tools.sedalib.TestUtilities;
import fr.gouv.vitam.tools.sedalib.core.ArchiveUnit;
import fr.gouv.vitam.tools.sedalib.core.DataObjectPackage;
import fr.gouv.vitam.tools.sedalib.inout.exporter.DataObjectPackageToCSVMetadataExporter;
import fr.gouv.vitam.tools.sedalib.inout.importer.DiskToArchiveTransferImporter;
import fr.gouv.vitam.tools.sedalib.inout.importer.WindowsShortcut;
import fr.gouv.vitam.tools.sedalib.metadata.ArchiveUnitProfile;
import fr.gouv.vitam.tools.sedalib.utils.ResourceUtils;
import fr.gouv.vitam.tools.sedalib.utils.SEDALibException;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -354,4 +357,33 @@ void exportZipOK() throws SEDALibException, InterruptedException, IOException {
)
).isTrue();
}

@Test
void exportCSVWithArchiveUnitProfile() throws SEDALibException, InterruptedException, IOException {
DataObjectPackage dop = new DataObjectPackage();
ArchiveUnit au = new ArchiveUnit();
au.setInDataObjectPackageId("AU1");
au.setDefaultContent("Title AU1", "Item");
au.setArchiveUnitProfile(new ArchiveUnitProfile("MyProfile"));
dop.addArchiveUnit(au);
dop.addRootAu(au);

DataObjectPackageToCSVMetadataExporter csvMetadataExporter;
String temporaryFile = "target/tmpJunit/CSVMetadataExporterProfile/ExportedMetadata.csv";
eraseAll("target/tmpJunit/CSVMetadataExporterProfile");
csvMetadataExporter = new DataObjectPackageToCSVMetadataExporter(
dop,
"UTF8",
';',
ALL_DATAOBJECTS,
false,
0,
null
);
csvMetadataExporter.doExportToCSVMetadataFile(temporaryFile);

String generatedFileContent = FileUtils.readFileToString(new File(temporaryFile), "UTF8");
assertThat(generatedFileContent).contains("ArchiveUnitProfile");
assertThat(generatedFileContent).contains("MyProfile");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@
import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageDeserializer;
import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageSerializer;
import fr.gouv.vitam.tools.sedalib.inout.importer.CSVMetadataToDataObjectPackageImporter;
import fr.gouv.vitam.tools.sedalib.metadata.namedtype.NamedTypeMetadata;
import fr.gouv.vitam.tools.sedalib.utils.ResourceUtils;
import fr.gouv.vitam.tools.sedalib.utils.SEDALibException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
Expand Down Expand Up @@ -221,4 +226,31 @@ void importLineKOCSV() throws SEDALibException {

assertThatThrownBy(cmi::doImport).hasMessageContaining("ligne 4"); // for StringType;
}

@Test
void importCSVWithArchiveUnitProfile() throws IOException, SEDALibException, InterruptedException {
Path tempDir = Files.createTempDirectory("csvImportTest");
Files.createFile(tempDir.resolve("AU1"));
Path csvFile = tempDir.resolve("metadata.csv");
String csvContent =
"ID;File;ParentID;ArchiveUnitProfile;Content.Title;Content.DescriptionLevel\n" +
"AU1;AU1;;MyProfile;Title AU1;Item\n";
Files.write(csvFile, csvContent.getBytes(StandardCharsets.UTF_8));

CSVMetadataToDataObjectPackageImporter cmi = new CSVMetadataToDataObjectPackageImporter(
csvFile.toString(),
"UTF-8",
';',
null
);
cmi.doImport();

DataObjectPackage dop = cmi.getDataObjectPackage();
ArchiveUnit au = dop.getArchiveUnitById("Import-AU1");
assertThat(au).isNotNull();
assertThat(au.getArchiveUnitProfile().getValue()).isEqualTo("MyProfile");
assertThat(((NamedTypeMetadata) au.getContent().getFirstNamedMetadata("Title")).getValue()).isEqualTo(
"Title AU1"
);
}
}