Search in sources :

Example 21 with MCRDirectory

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);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SAXBuilder(org.jdom2.input.SAXBuilder) MCRException(org.mycore.common.MCRException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) InputStream(java.io.InputStream) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException)

Aggregations

MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)21 MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)13 MCRPath (org.mycore.datamodel.niofs.MCRPath)13 MCRFile (org.mycore.datamodel.ifs.MCRFile)10 NoSuchFileException (java.nio.file.NoSuchFileException)9 IOException (java.io.IOException)7 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 NotDirectoryException (java.nio.file.NotDirectoryException)4 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)3 Path (java.nio.file.Path)3 CopyOption (java.nio.file.CopyOption)2 FileSystemException (java.nio.file.FileSystemException)2 InvalidPathException (java.nio.file.InvalidPathException)2 ProviderMismatchException (java.nio.file.ProviderMismatchException)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 GregorianCalendar (java.util.GregorianCalendar)2 MCRException (org.mycore.common.MCRException)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)2