use of org.mycore.datamodel.ifs.MCRDirectory in project mycore by MyCoRe-Org.
the class MCRTransferPackageUtil method importDerivate.
/**
* Imports a derivate from the given target directory path.
*
* @param targetDirectory path to the extracted *.tar archive
* @param derivateId the derivate to import
* @throws JDOMException derivate xml couldn't be read
* @throws IOException some file system stuff went wrong
* @throws MCRAccessException you do not have the permissions to do the import
*/
public static void importDerivate(Path targetDirectory, String derivateId) throws JDOMException, IOException, MCRAccessException {
SAXBuilder sax = new SAXBuilder();
Path derivateDirectory = targetDirectory.resolve(CONTENT_DIRECTORY).resolve(derivateId);
Path derivatePath = derivateDirectory.resolve(derivateId + ".xml");
LOGGER.info(MessageFormat.format("Importing {0}", derivatePath.toAbsolutePath().toString()));
MCRDerivate der = new MCRDerivate(sax.build(derivatePath.toFile()));
MCRMetadataManager.update(der);
MCRDirectory dir = MCRDirectory.getRootDirectory(der.getId().toString());
if (dir == null) {
LOGGER.info("Creating missing {} {}", MCRFilesystemNode.class.getSimpleName(), der.getId());
final MCRDirectory difs = new MCRDirectory(der.getId().toString());
der.getDerivate().getInternals().setIFSID(difs.getID());
MCRMetadataManager.update(der);
}
try (Stream<Path> stream = Files.find(derivateDirectory, 5, filterDerivateDirectory(derivateId, derivateDirectory))) {
stream.forEach(path -> {
String targetPath = derivateDirectory.relativize(path).toString();
try (InputStream in = Files.newInputStream(path)) {
Files.copy(in, MCRPath.getPath(derivateId, targetPath), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ioExc) {
throw new MCRException("Unable to add file " + path.toAbsolutePath() + " to derivate " + derivateId, ioExc);
}
});
}
}
Aggregations