Skip to content

Commit f075cb1

Browse files
authored
Merge pull request #106 from ProgrammeVitam/bugs_14125
bugs #14125 fix: missing ArchiveUnitProfile at CSV export/import
2 parents 953e4ef + c30fd24 commit f075cb1

6 files changed

Lines changed: 130 additions & 6 deletions

File tree

sedalib/src/main/java/fr/gouv/vitam/tools/sedalib/inout/exporter/DataObjectPackageToCSVMetadataExporter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
package fr.gouv.vitam.tools.sedalib.inout.exporter;
3939

4040
import fr.gouv.vitam.tools.sedalib.core.*;
41+
import fr.gouv.vitam.tools.sedalib.metadata.ArchiveUnitProfile;
4142
import fr.gouv.vitam.tools.sedalib.metadata.content.Content;
4243
import fr.gouv.vitam.tools.sedalib.metadata.data.FileInfo;
4344
import fr.gouv.vitam.tools.sedalib.metadata.management.Management;
@@ -350,11 +351,20 @@ private void computeCsvHeader() throws SEDALibException {
350351
Set<String> curHeaderNames = new HashSet<>();
351352
List<String> sortedHeaderNames;
352353
for (ArchiveUnit au : dataObjectPackage.getAuInDataObjectPackageIdMap().values()) {
354+
ArchiveUnitProfile archiveUnitProfile = au.getArchiveUnitProfile();
355+
if (archiveUnitProfile != null) curHeaderNames.addAll(archiveUnitProfile.externToCsvList().keySet());
353356
Management management = au.getManagement();
354357
if (management != null) curHeaderNames.addAll(management.externToCsvList().keySet());
355358
curHeaderNames.addAll(au.getContent().externToCsvList(dataObjectPackage.getExportMetadataList()).keySet());
356359
}
357-
sortedHeaderNames = getSortedHeaderNames(new ArrayList<>(), curHeaderNames, "", "Content", Content.class);
360+
sortedHeaderNames = getSortedHeaderNames(
361+
new ArrayList<>(),
362+
curHeaderNames,
363+
"",
364+
"ArchiveUnitProfile",
365+
ArchiveUnitProfile.class
366+
);
367+
sortedHeaderNames.addAll(getSortedHeaderNames(new ArrayList<>(), curHeaderNames, "", "Content", Content.class));
358368
sortedHeaderNames.addAll(
359369
getSortedHeaderNames(new ArrayList<>(), curHeaderNames, "", "Management", Management.class)
360370
);
@@ -461,9 +471,15 @@ private void generateCsvLine(ArchiveUnit au, ArchiveUnit parentAu, Path auRelati
461471
contentMetadataHashMap = au.getContent().externToCsvList(dataObjectPackage.getExportMetadataList());
462472
Management management = au.getManagement();
463473
if (management != null) managementMetadataHashMap = management.externToCsvList();
474+
ArchiveUnitProfile archiveUnitProfile = au.getArchiveUnitProfile();
475+
LinkedHashMap<String, String> archiveUnitProfileMetadataHashMap = null;
476+
if (archiveUnitProfile != null) archiveUnitProfileMetadataHashMap = archiveUnitProfile.externToCsvList();
477+
464478
for (String header : headerNames) {
465479
value = contentMetadataHashMap.get(header);
466480
if ((value == null) && (managementMetadataHashMap != null)) value = managementMetadataHashMap.get(header);
481+
if ((value == null) && (archiveUnitProfileMetadataHashMap != null)) value =
482+
archiveUnitProfileMetadataHashMap.get(header);
467483
if (value == null) value = "";
468484
else value = "\"" + value.replace("\"", "\"\"") + "\"";
469485
csvPrintStream.print(separator + value);

sedalib/src/main/java/fr/gouv/vitam/tools/sedalib/inout/importer/CSVMetadataFormatter.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public ValueAttrMetadataTag(boolean isValue, MetadataTag tag) {
124124
PARENTFILE,
125125
OBJECTFILES
126126
);
127-
private MetadataTag rootTag, contentTag, managementTag;
127+
private MetadataTag rootTag, contentTag, managementTag, archiveUnitProfileTag;
128128
private LinkedHashMap<Integer, ValueAttrMetadataTag> tagHeaderColumnMapping;
129129
private int numberOfMandatoryHeaderFound;
130130
private int columnCount;
@@ -224,6 +224,8 @@ private MetadataTag getInSubTagsMap(MetadataTag tag, String name, int rank) thro
224224
contentTag = subTag;
225225
} else if (name.equals("Management")) {
226226
managementTag = subTag;
227+
} else if (name.equals("ArchiveUnitProfile")) {
228+
archiveUnitProfileTag = subTag;
227229
} else {
228230
throw new SEDALibException("Métadonnées [" + name + "] non conforme SEDA.");
229231
}
@@ -272,7 +274,8 @@ private void analyseTags(String[] headerRow) throws SEDALibException {
272274
}
273275
if (
274276
headerRow[numberOfMandatoryHeaderFound].startsWith("Content.") ||
275-
headerRow[numberOfMandatoryHeaderFound].startsWith("Management.")
277+
headerRow[numberOfMandatoryHeaderFound].startsWith("Management.") ||
278+
headerRow[numberOfMandatoryHeaderFound].startsWith("ArchiveUnitProfile")
276279
) {
277280
rootTag = new MetadataTag(null, null);
278281
contentTag = null;
@@ -578,6 +581,19 @@ public String extractManagementXML() throws SEDALibException {
578581
return generateTagXML(managementTag);
579582
}
580583

584+
/**
585+
* Extract the XML ArchiveUnitProfile metadata
586+
*
587+
* @return the XML ArchiveUnitProfile metadata or null
588+
* @throws SEDALibException the seda lib exception
589+
*/
590+
public String extractArchiveUnitProfileXML() throws SEDALibException {
591+
if (archiveUnitProfileTag == null) {
592+
return "";
593+
}
594+
return generateTagXML(archiveUnitProfileTag);
595+
}
596+
581597
/**
582598
* Gets guid.
583599
*

sedalib/src/main/java/fr/gouv/vitam/tools/sedalib/inout/importer/CSVMetadataToDataObjectPackageImporter.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ private class Line {
133133
* The Management xml metadata.
134134
*/
135135
String managementXMLMetadata;
136+
/**
137+
* The ArchiveUnitProfile xml metadata.
138+
*/
139+
String archiveUnitProfileXMLMetadata;
136140
/**
137141
* The Au.
138142
*/
@@ -152,7 +156,8 @@ public Line(
152156
String file,
153157
String objectFiles,
154158
String contentXMLMetadata,
155-
String managementXMLMetadata
159+
String managementXMLMetadata,
160+
String archiveUnitProfileXMLMetadata
156161
) {
157162
this.guid = guid;
158163
this.parentGUID = parentGUID;
@@ -161,6 +166,7 @@ public Line(
161166
else this.objectFiles = Arrays.asList(objectFiles.split("\\|"));
162167
this.contentXMLMetadata = contentXMLMetadata;
163168
this.managementXMLMetadata = managementXMLMetadata;
169+
this.archiveUnitProfileXMLMetadata = archiveUnitProfileXMLMetadata;
164170
this.au = null;
165171
}
166172
}
@@ -273,11 +279,12 @@ private boolean readCSVFile() throws SEDALibException, InterruptedException {
273279
try {
274280
currentLine = new Line(
275281
metadataFormatter.getGUID(row),
276-
metadataFormatter.getParentGUID(row), //NOSONAR
282+
metadataFormatter.getParentGUID(row), // NOSONAR
277283
metadataFormatter.getFile(row),
278284
metadataFormatter.getObjectFiles(row),
279285
metadataFormatter.doFormatAndExtractContentXML(row),
280-
metadataFormatter.extractManagementXML()
286+
metadataFormatter.extractManagementXML(),
287+
metadataFormatter.extractArchiveUnitProfileXML()
281288
);
282289
} catch (SEDALibException e) {
283290
throw new SEDALibException("Erreur sur la ligne " + lineCount, e);
@@ -317,6 +324,10 @@ private ArchiveUnit createLineAU(Line line) throws SEDALibException, Interrupted
317324
au.setManagementXmlData(line.managementXMLMetadata);
318325
au.getManagement();
319326
}
327+
if (!line.archiveUnitProfileXMLMetadata.isEmpty()) {
328+
au.setArchiveUnitProfileXmlData(line.archiveUnitProfileXMLMetadata);
329+
au.getArchiveUnitProfile();
330+
}
320331
Path path = getAbsolutePath(line.file);
321332
DataObjectGroup implicitDog = null;
322333
if (isExtendedFormat) {

sedalib/src/main/java/fr/gouv/vitam/tools/sedalib/metadata/ArchiveUnitProfile.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
import fr.gouv.vitam.tools.sedalib.metadata.namedtype.StringType;
4141

42+
import java.util.LinkedHashMap;
43+
4244
/**
4345
* The Class ArchiveUnitProfile.
4446
* <p>
@@ -67,4 +69,19 @@ public ArchiveUnitProfile() {
6769
public ArchiveUnitProfile(String value) {
6870
super("ArchiveUnitProfile", value);
6971
}
72+
73+
/**
74+
* Export the ArchiveUnitProfile metadata to csv List for the csv metadata file.
75+
* <p>
76+
* In the HashMap result, the key is a metadata path of a leaf and the value is
77+
* the leaf of the metadata value.
78+
*
79+
* @return the linked hash map with header title as key and metadata value as
80+
* value
81+
*/
82+
public LinkedHashMap<String, String> externToCsvList() {
83+
LinkedHashMap<String, String> result = new LinkedHashMap<>();
84+
result.put("ArchiveUnitProfile", getValue());
85+
return result;
86+
}
7087
}

sedalib/src/test/java/fr/gouv/vitam/tools/sedalib/inout/CSVMetadataExporterTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
3939

4040
import fr.gouv.vitam.tools.sedalib.SedaContextExtension;
4141
import fr.gouv.vitam.tools.sedalib.TestUtilities;
42+
import fr.gouv.vitam.tools.sedalib.core.ArchiveUnit;
43+
import fr.gouv.vitam.tools.sedalib.core.DataObjectPackage;
4244
import fr.gouv.vitam.tools.sedalib.inout.exporter.DataObjectPackageToCSVMetadataExporter;
4345
import fr.gouv.vitam.tools.sedalib.inout.importer.DiskToArchiveTransferImporter;
4446
import fr.gouv.vitam.tools.sedalib.inout.importer.WindowsShortcut;
47+
import fr.gouv.vitam.tools.sedalib.metadata.ArchiveUnitProfile;
4548
import fr.gouv.vitam.tools.sedalib.utils.ResourceUtils;
4649
import fr.gouv.vitam.tools.sedalib.utils.SEDALibException;
4750
import org.apache.commons.io.FileUtils;
@@ -354,4 +357,33 @@ void exportZipOK() throws SEDALibException, InterruptedException, IOException {
354357
)
355358
).isTrue();
356359
}
360+
361+
@Test
362+
void exportCSVWithArchiveUnitProfile() throws SEDALibException, InterruptedException, IOException {
363+
DataObjectPackage dop = new DataObjectPackage();
364+
ArchiveUnit au = new ArchiveUnit();
365+
au.setInDataObjectPackageId("AU1");
366+
au.setDefaultContent("Title AU1", "Item");
367+
au.setArchiveUnitProfile(new ArchiveUnitProfile("MyProfile"));
368+
dop.addArchiveUnit(au);
369+
dop.addRootAu(au);
370+
371+
DataObjectPackageToCSVMetadataExporter csvMetadataExporter;
372+
String temporaryFile = "target/tmpJunit/CSVMetadataExporterProfile/ExportedMetadata.csv";
373+
eraseAll("target/tmpJunit/CSVMetadataExporterProfile");
374+
csvMetadataExporter = new DataObjectPackageToCSVMetadataExporter(
375+
dop,
376+
"UTF8",
377+
';',
378+
ALL_DATAOBJECTS,
379+
false,
380+
0,
381+
null
382+
);
383+
csvMetadataExporter.doExportToCSVMetadataFile(temporaryFile);
384+
385+
String generatedFileContent = FileUtils.readFileToString(new File(temporaryFile), "UTF8");
386+
assertThat(generatedFileContent).contains("ArchiveUnitProfile");
387+
assertThat(generatedFileContent).contains("MyProfile");
388+
}
357389
}

sedalib/src/test/java/fr/gouv/vitam/tools/sedalib/inout/CSVMetadataToDataObjectPackageImporterTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,17 @@
4747
import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageDeserializer;
4848
import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageSerializer;
4949
import fr.gouv.vitam.tools.sedalib.inout.importer.CSVMetadataToDataObjectPackageImporter;
50+
import fr.gouv.vitam.tools.sedalib.metadata.namedtype.NamedTypeMetadata;
5051
import fr.gouv.vitam.tools.sedalib.utils.ResourceUtils;
5152
import fr.gouv.vitam.tools.sedalib.utils.SEDALibException;
5253
import org.junit.jupiter.api.Test;
5354
import org.junit.jupiter.api.extension.ExtendWith;
5455

5556
import java.io.FileNotFoundException;
57+
import java.io.IOException;
58+
import java.nio.charset.StandardCharsets;
59+
import java.nio.file.Files;
60+
import java.nio.file.Path;
5661

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

222227
assertThatThrownBy(cmi::doImport).hasMessageContaining("ligne 4"); // for StringType;
223228
}
229+
230+
@Test
231+
void importCSVWithArchiveUnitProfile() throws IOException, SEDALibException, InterruptedException {
232+
Path tempDir = Files.createTempDirectory("csvImportTest");
233+
Files.createFile(tempDir.resolve("AU1"));
234+
Path csvFile = tempDir.resolve("metadata.csv");
235+
String csvContent =
236+
"ID;File;ParentID;ArchiveUnitProfile;Content.Title;Content.DescriptionLevel\n" +
237+
"AU1;AU1;;MyProfile;Title AU1;Item\n";
238+
Files.write(csvFile, csvContent.getBytes(StandardCharsets.UTF_8));
239+
240+
CSVMetadataToDataObjectPackageImporter cmi = new CSVMetadataToDataObjectPackageImporter(
241+
csvFile.toString(),
242+
"UTF-8",
243+
';',
244+
null
245+
);
246+
cmi.doImport();
247+
248+
DataObjectPackage dop = cmi.getDataObjectPackage();
249+
ArchiveUnit au = dop.getArchiveUnitById("Import-AU1");
250+
assertThat(au).isNotNull();
251+
assertThat(au.getArchiveUnitProfile().getValue()).isEqualTo("MyProfile");
252+
assertThat(((NamedTypeMetadata) au.getContent().getFirstNamedMetadata("Title")).getValue()).isEqualTo(
253+
"Title AU1"
254+
);
255+
}
224256
}

0 commit comments

Comments
 (0)