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 @@ -772,12 +772,12 @@ private void loadWork() {
filename = fileChooser.getSelectedFile().getCanonicalPath();
final SedaVersion sedaVersionFromWorkspace = Work.getSeda2VersionFromFile(filename);

if (sedaVersionFromWorkspace != sedaVersion) {
if ((sedaVersionFromWorkspace != null) && (sedaVersionFromWorkspace != sedaVersion)) {
if (
UserInteractionDialog.getUserAnswer(
mainWindow,
"Pour charger ce fichier il faut faire passer l'interface en " +
sedaVersion +
sedaVersionFromWorkspace +
"\n" +
"Voulez-vous continuer?",
"Confirmation",
Expand Down
33 changes: 32 additions & 1 deletion resip/src/main/java/fr/gouv/vitam/tools/resip/data/Work.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/
package fr.gouv.vitam.tools.resip.data;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
Expand All @@ -49,6 +50,7 @@
import fr.gouv.vitam.tools.sedalib.core.*;
import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageDeserializer;
import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageSerializer;
import fr.gouv.vitam.tools.sedalib.core.seda.SedaContext;
import fr.gouv.vitam.tools.sedalib.core.seda.SedaVersion;
import fr.gouv.vitam.tools.sedalib.utils.SEDALibException;
import fr.gouv.vitam.tools.sedalib.utils.SEDALibProgressLogger;
Expand Down Expand Up @@ -82,6 +84,11 @@ public class Work {
*/
static final String JSON_FILENAME = "work.json";

/**
* The SEDA version assumed for save files created before the version was serialized.
*/
static final SedaVersion DEFAULT_SEDA_VERSION = SedaVersion.V2_1;

/**
* The version of this object used for to distinct serialization in prefs or on
* disk.
Expand Down Expand Up @@ -180,6 +187,7 @@ public static SedaVersion getSeda2VersionFromFile(String file) throws ResipExcep
module.addDeserializer(ExportContext.class, new NullDeserializer<ExportContext>());
module.addDeserializer(CreationContext.class, new NullDeserializer<CreationContext>());
mapper.registerModule(module);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
ZipEntry ze = zis.getNextEntry();
if (!ze.getName().equals(JSON_FILENAME)) throw new ResipException(
"Resip: Le fichier [" + file + "] n'est pas une sauvegarde de session Resip"
Expand All @@ -191,7 +199,7 @@ public static SedaVersion getSeda2VersionFromFile(String file) throws ResipExcep
e
);
}
return ow.version;
return ow.version == null ? DEFAULT_SEDA_VERSION : ow.version;
}

/**
Expand All @@ -209,13 +217,17 @@ public static Work createFromFile(String file) throws ResipException {
module.addSerializer(DataObjectPackage.class, new DataObjectPackageSerializer());
module.addDeserializer(DataObjectPackage.class, new DataObjectPackageDeserializer());
mapper.registerModule(module);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
ZipEntry ze = zis.getNextEntry();
if (!ze.getName().equals(JSON_FILENAME)) throw new ResipException(
"Resip: Le fichier [" + file + "] n'est pas une sauvegarde de session Resip"
);
ow = mapper.readValue(zis, Work.class);

// some fields need to be computed or defined after the load phase from Json
if (ow.version == null) ow.version = SedaContext.getVersion() == null
? DEFAULT_SEDA_VERSION
: SedaContext.getVersion();
ow.getDataObjectPackage().getGhostRootAu().setDataObjectPackage(ow.getDataObjectPackage());
for (Map.Entry<String, ArchiveUnit> pair : ow
.getDataObjectPackage()
Expand Down Expand Up @@ -254,6 +266,7 @@ public static Work createFromFile(String file) throws ResipException {
*/
public void save(String file) {
try {
if (version == null) version = SedaContext.getVersion();
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(DataObjectPackage.class, new DataObjectPackageSerializer());
Expand Down Expand Up @@ -307,6 +320,24 @@ public void setCreationContext(CreationContext creationContext) {
this.creationContext = creationContext;
}

/**
* Gets the SEDA version of this work.
*
* @return the SEDA version
*/
public SedaVersion getVersion() {
return version;
}

/**
* Sets the SEDA version of this work.
*
* @param version the new SEDA version
*/
public void setVersion(SedaVersion version) {
this.version = version;
}

/**
* Gets the global metadata context.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class TreatmentParameters {
*/
public TreatmentParameters() {
EventBus.subscribe(SedaVersionChangedEvent.class, event -> {
this.sedaVersion = event.getNewVersion();
if (event.getNewVersion() != null) this.sedaVersion = event.getNewVersion();
});
}

Expand All @@ -88,7 +88,7 @@ private String canonizeCategoryName(String category) {
*/
public TreatmentParameters(Preferences preferences) {
EventBus.subscribe(SedaVersionChangedEvent.class, event -> {
this.sedaVersion = event.getNewVersion();
if (event.getNewVersion() != null) this.sedaVersion = event.getNewVersion();
});

final String categoriesString = preferences
Expand Down Expand Up @@ -143,7 +143,12 @@ public void toPrefs(Preferences preferences) {
);
}
preferences.getPrefProperties().setProperty("treatmentParameters.dupMax", Integer.toString(dupMax));
preferences.getPrefProperties().setProperty("treatmentParameters.seda2Version", sedaVersion.toString());
preferences
.getPrefProperties()
.setProperty(
"treatmentParameters.seda2Version",
(sedaVersion == null ? SedaVersion.V2_1 : sedaVersion).toString()
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright French Prime minister Office/SGMAP/DINSIC/Vitam Program (2019-2022)
* and the signatories of the "VITAM - Accord du Contributeur" agreement.
*
* contact@programmevitam.fr
*
* This software is a computer program whose purpose is to provide
* tools for construction and manipulation of SIP (Submission
* Information Package) conform to the SEDA (Standard d’Échange
* de données pour l’Archivage) standard.
*
* This software is governed by the CeCILL-C license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL-C
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package fr.gouv.vitam.tools.resip.parameters;

import fr.gouv.vitam.tools.resip.UseTestFiles;
import fr.gouv.vitam.tools.resip.event.EventBus;
import fr.gouv.vitam.tools.resip.event.SedaVersionChangedEvent;
import org.junit.jupiter.api.Test;

import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* The type Treatment parameters test.
*/
class TreatmentParametersTest implements UseTestFiles {

/**
* Test that preferences can be stored even if no SEDA version has been defined,
* which was breaking the load of a save file with a "->null" error message.
*/
@Test
void TestToPrefsWithoutDefinedSedaVersion() {
TreatmentParameters treatmentParameters = new TreatmentParameters();
treatmentParameters.setDefaultPrefs();
treatmentParameters.sedaVersion = null;

Properties properties = Preferences.getInstance().getPrefProperties();
properties.remove("treatmentParameters.seda2Version");

assertDoesNotThrow(() -> treatmentParameters.toPrefs(Preferences.getInstance()));
assertEquals("2.1", properties.getProperty("treatmentParameters.seda2Version"));
}

/**
* Test that a null SEDA version event, published for example when loading a save
* file with an unknown version, doesn't erase the current SEDA version.
*/
@Test
void TestNullSedaVersionEventDoesntEraseCurrentVersion() {
TreatmentParameters treatmentParameters = new TreatmentParameters();
treatmentParameters.setDefaultPrefs();
treatmentParameters.sedaVersion = null;

EventBus.publish(new SedaVersionChangedEvent(null));

assertDoesNotThrow(() -> treatmentParameters.toPrefs(Preferences.getInstance()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
*/
package fr.gouv.vitam.tools.resip.parameters;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import fr.gouv.vitam.tools.resip.UseTestFiles;
import fr.gouv.vitam.tools.resip.data.Work;
import fr.gouv.vitam.tools.resip.utils.ResipException;
Expand All @@ -48,17 +50,23 @@
import fr.gouv.vitam.tools.sedalib.core.DataObjectPackage;
import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageDeserializer;
import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageSerializer;
import fr.gouv.vitam.tools.sedalib.core.seda.SedaContext;
import fr.gouv.vitam.tools.sedalib.core.seda.SedaVersion;
import fr.gouv.vitam.tools.sedalib.inout.importer.DiskToArchiveTransferImporter;
import fr.gouv.vitam.tools.sedalib.utils.SEDALibException;
import fr.gouv.vitam.tools.sedalib.utils.SEDALibProgressLogger;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -141,4 +149,69 @@ void TestResipWorkSerializationDeserialization()
mapper.writeValue(new FileOutputStream("./target/tmpJunit/junit_resiptWork_after.json"), dssip);
assertEquals(ssip, sdssip);
}

/**
* Test that the SEDA version of a work is saved and read back, so that a save file
* created by Resip can be loaded again without any interface version switch.
*
* @throws ResipException the resip exception
* @throws IOException the io exception
*/
@Test
void TestResipWorkSedaVersionSerializationDeserialization() throws ResipException, IOException {
SedaVersion previousVersion = SedaContext.getVersion();
try {
SedaContext.setVersion(SedaVersion.V2_2);
String file = "./target/tmpJunit/junit_resipWork_seda22.rsp";
new File("./target/tmpJunit").mkdirs();

Work ow = new Work(new DataObjectPackage(), null, new ExportContext());
ow.save(file);

assertEquals(SedaVersion.V2_2, Work.getSeda2VersionFromFile(file));
assertEquals(SedaVersion.V2_2, Work.createFromFile(file).getVersion());
} finally {
SedaContext.setVersion(previousVersion);
}
}

/**
* Test that a save file created before the SEDA version was serialized is still
* readable, and is considered as a SEDA 2.1 one.
*
* @throws ResipException the resip exception
* @throws IOException the io exception
*/
@Test
void TestResipWorkWithoutSedaVersionIsReadAsSeda21() throws ResipException, IOException {
SedaVersion previousVersion = SedaContext.getVersion();
try {
SedaContext.setVersion(SedaVersion.V2_2);
String file = "./target/tmpJunit/junit_resipWork_seda22.rsp";
String legacyFile = "./target/tmpJunit/junit_resipWork_legacy.rsp";
new File("./target/tmpJunit").mkdirs();

new Work(new DataObjectPackage(), null, new ExportContext()).save(file);
removeVersionFromSaveFile(file, legacyFile);

assertEquals(SedaVersion.V2_1, Work.getSeda2VersionFromFile(legacyFile));
} finally {
SedaContext.setVersion(previousVersion);
}
}

private void removeVersionFromSaveFile(String source, String destination) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode workNode;
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(source))) {
zis.getNextEntry();
workNode = mapper.readTree(zis);
}
((ObjectNode) workNode).remove("version");
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destination))) {
zos.putNextEntry(new ZipEntry("work.json"));
zos.write(mapper.writeValueAsBytes(workNode));
zos.closeEntry();
}
}
}
Loading