Commit e34b80a5 authored by granier's avatar granier
Browse files

Ajout du plugin FilenameGuidMappingPlugin qui permet la création d'un

mapping entre les noms des fichiers dans les bordereaux et les nom des
fichier sur le système.
Appel TEI.
parent daad6a29
/*
* Copyright French Prime minister Office/SGMAP/DINSIC/Vitam Program (2015-2020)
*
* contact.vitam@culture.gouv.fr
*
* This software is a computer program whose purpose is to implement a digital archiving back-office system managing
* high volumetry securely and efficiently.
*
* This software is governed by the CeCILL 2.1 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 2.1 license as
* circulated by CEA, CNRS and INRIA at the following URL "https://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 2.1 license and that you
* accept its terms.
*/
package fr.gouv.vitam.worker.core.plugin;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.gouv.vitam.common.SedaConstants;
import fr.gouv.vitam.common.logging.VitamLogger;
import fr.gouv.vitam.common.logging.VitamLoggerFactory;
import fr.gouv.vitam.common.model.IngestWorkflowConstants;
import fr.gouv.vitam.common.model.ItemStatus;
import fr.gouv.vitam.common.model.StatusCode;
import fr.gouv.vitam.common.model.VitamAutoCloseable;
import fr.gouv.vitam.common.model.processing.ProcessingUri;
import fr.gouv.vitam.processing.common.exception.ProcessingException;
import fr.gouv.vitam.processing.common.parameter.WorkerParameters;
import fr.gouv.vitam.worker.common.HandlerIO;
import fr.gouv.vitam.worker.core.handler.ActionHandler;
import fr.gouv.vitam.worker.core.plugin.pojo.ValidatorResult;
/**
* FilenameGuidMappingPlugin Plugin.<br>
*/
public class FilenameGuidMappingPlugin extends ActionHandler implements VitamAutoCloseable {
private static final VitamLogger LOGGER = VitamLoggerFactory.getInstance(FilenameGuidMappingPlugin.class);
/**
* File format treatment
*/
public static final String FILENAME_GUID_MAPPING = "FILENAME_GUID_MAPPING";
/**
* FileFormat PUID field key
*/
public static final String PUID = "PUID";
/**
* Error list for file format treatment
*/
private static final int OG_INPUT_RANK = 0;
private static final int FILENAME_GUID_MAPPING_OUT_RANK = 0;
private static final int FILENAME_GUID_MAPPING_IN_RANK = 1;
private static final int OG_OUT_RANK = 1;
private final ObjectMapper mapper = new ObjectMapper();
/**
* Empty constructor
*/
public FilenameGuidMappingPlugin() {}
@Override
public ItemStatus execute(WorkerParameters params, HandlerIO handlerIO) {
checkMandatoryParameters(params);
LOGGER.debug("FilenameGuidMappingPlugin running ...");
final ItemStatus itemStatus = new ItemStatus(FILENAME_GUID_MAPPING);
try {
// Get objectGroup metadatas
final JsonNode jsonOG = (JsonNode) handlerIO.getInput(OG_INPUT_RANK);
Map<String, String> filenameGuidMap = new HashMap<String, String>();
ProcessingUri uri = handlerIO.getOutput(0);
String filename = uri.getPath();
File file = handlerIO.getNewLocalFile(filename);
if (file.exists() && file.length()!=0 ) {
try {
filenameGuidMap = mapper.readValue(file, new TypeReference <Map <String, String>>() {});
} catch (JsonParseException e) {
LOGGER.error("impossible d'analyser le fichier json {}.", file.getAbsolutePath());
} catch (JsonMappingException e) {
LOGGER.error("impossible de mapper le fichier json {} sur la classe ValidatorResult.", file.getAbsolutePath());
} catch (IOException e) {
LOGGER.error("impossible d'accéder au fichier {}.", file.getAbsolutePath());
}
}
final Map<String, String> objectIdToUri = getMapOfObjectsIdsAndUris(jsonOG);
final JsonNode qualifiers = jsonOG.get(SedaConstants.PREFIX_QUALIFIERS);
if (qualifiers != null) {
final List<JsonNode> versions = qualifiers.findValues(SedaConstants.TAG_VERSIONS);
if (versions != null && !versions.isEmpty()) {
for (final JsonNode versionsArray : versions) {
for (final JsonNode version : versionsArray) {
if (version.get(SedaConstants.TAG_PHYSICAL_ID) == null) {
final String objectId = version.get(SedaConstants.PREFIX_ID).asText();
// Retrieve the file
String fileName = version.get(SedaConstants.TAG_FILE_INFO).get("Filename").asText();
filenameGuidMap.put(fileName, objectIdToUri.get(objectId));
if (StatusCode.FATAL.equals(itemStatus.getGlobalStatus())) {
return new ItemStatus(FILENAME_GUID_MAPPING).setItemsStatus(FILENAME_GUID_MAPPING, itemStatus);
}
}
}
}
}
}
mapper.writeValue(file, filenameGuidMap);
// add it to the handler IO
handlerIO.addOutputResult(0, file);
} catch (final ProcessingException e) {
LOGGER.error(e);
itemStatus.increment(StatusCode.FATAL);
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (itemStatus.getGlobalStatus().getStatusLevel() == StatusCode.UNKNOWN.getStatusLevel()) {
itemStatus.increment(StatusCode.OK);
}
LOGGER.debug("FormatIdentificationActionHandler response: " + itemStatus.getGlobalStatus());
return new ItemStatus(FILENAME_GUID_MAPPING).setItemsStatus(itemStatus.getItemId(), itemStatus);
}
private Map<String, String> getMapOfObjectsIdsAndUris(JsonNode jsonOG) {
final Map<String, String> binaryObjectsToStore = new HashMap<>();
// Filter on objectGroup objects ids to retrieve only binary objects
// informations linked to the ObjectGroup
final JsonNode work = jsonOG.get(SedaConstants.PREFIX_WORK);
final JsonNode qualifiers = work.get(SedaConstants.PREFIX_QUALIFIERS);
if (qualifiers == null) {
return binaryObjectsToStore;
}
final List<JsonNode> versions = qualifiers.findValues(SedaConstants.TAG_VERSIONS);
if (versions == null || versions.isEmpty()) {
return binaryObjectsToStore;
}
for (final JsonNode version : versions) {
for (final JsonNode binaryObject : version) {
if (binaryObject.get(SedaConstants.TAG_PHYSICAL_ID) == null) {
binaryObjectsToStore.put(binaryObject.get(SedaConstants.PREFIX_ID).asText(),
binaryObject.get(SedaConstants.TAG_URI).asText());
}
}
}
return binaryObjectsToStore;
}
}
......@@ -79,7 +79,11 @@ public class FormatValidatorPlugin extends ActionHandler {
private static final String FORMAT_VALIDATOR_PLUGIN = "FORMAT_VALIDATOR_PLUGIN";
private static final int OG_IN_RANK = 0;
private static final int REFERENTIAL_INGEST_CONTRACT_PARAMETERS_RANK = 1;
private static final int FILENAME_GUID_MAPPING_IN_RANK = 2;
private static final String FILE_FORMAT_PUID_NOT_FOUND = "PUID_NOT_FOUND";
private static final String FILE_FORMAT_NOT_FOUND_REFERENTIAL_ERROR = "NOT_FOUND_REFERENTIAL";
......@@ -99,6 +103,8 @@ public class FormatValidatorPlugin extends ActionHandler {
private static final List<String> FORMAT_VIDEO_PUID_LIST = Arrays.asList("fmt/6", "fmt/141", "fmt/199", "fmt/203", "fmt/569", "fmt/945");
private static final List<String> VIDEO_CODEC_LIST = Arrays.asList("AVC", "Theora");
private static final List<String> AUDIO_CODEC_LIST = Arrays.asList("AAC", "AAC LC", "Vorbis", "PCM");
ResourceBundle bundle = ResourceBundle.getBundle("pac-messages");;
......@@ -141,7 +147,7 @@ public class FormatValidatorPlugin extends ActionHandler {
boolean metadataUpdated = false;
try {
final JsonNode jsonOG = (JsonNode) handlerIO.getInput(0);
final JsonNode jsonOG = (JsonNode) handlerIO.getInput(OG_IN_RANK);
final Map<String, String> objectIdToUri = getMapOfObjectsIdsAndUris(jsonOG);
final JsonNode qualifiers = jsonOG.get(SedaConstants.PREFIX_QUALIFIERS);
......@@ -197,7 +203,6 @@ public class FormatValidatorPlugin extends ActionHandler {
LOGGER.warn("erreur lors de la validation du format" + e.getMessage());
}
}
}
}
}
......@@ -467,10 +472,15 @@ public class FormatValidatorPlugin extends ActionHandler {
// Cas particulier TEI
if (IdentifiedFormat.getName().equals("TEI")) {
File fileMap = (File)handlerIO.getInput(FILENAME_GUID_MAPPING_IN_RANK);
final Map<String, String> filenameGuidMap = mapper.readValue(fileMap, new TypeReference <Map <String, String>>() {});
JsonNode schemaNode = version.get(TAG_METADATA).get(TAG_TEXT).get(TAG_SCHEMA);
if (schemaNode.size() > 0) {
String oddFileName = schemaNode.get(0).asText();
File oddFile = loadFileFromWorkspace(handlerIO, "content/" + oddFileName);
String oddPath = filenameGuidMap.get(oddFileName);
File oddFile = loadFileFromWorkspace(handlerIO, oddPath);
validator.setFile(file, oddFile);
}
// Cas particulier XML
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment