Search in sources :

Example 26 with MCRPersistenceException

use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.

the class MCRMetadataManager method importDerivate.

private static void importDerivate(String derivateID, Path sourceDir) throws MCRPersistenceException {
    try {
        MCRPath rootPath = MCRPath.getPath(derivateID, "/");
        if (Files.exists(rootPath)) {
            LOGGER.info("Derivate does already exist: {}", derivateID);
        }
        rootPath.getFileSystem().createRoot(derivateID);
        Files.walkFileTree(sourceDir, new MCRTreeCopier(sourceDir, rootPath));
    } catch (Exception exc) {
        throw new MCRPersistenceException("Unable to import derivate " + derivateID + " from source " + sourceDir.toAbsolutePath(), exc);
    }
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 27 with MCRPersistenceException

use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.

the class MCRObjectCommands method replaceParent.

/**
 * Moves object to new parent.
 *
 * @param sourceId
 *            object that should be attached to new parent
 * @param newParentId
 *            the ID of the new parent
 * @throws MCRAccessException see {@link MCRMetadataManager#update(MCRObject)}
 */
@MCRCommand(syntax = "set parent of {0} to {1}", help = "replaces a parent of an object (first parameter) to the given new one (second parameter)", order = 300)
public static void replaceParent(String sourceId, String newParentId) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    // child
    MCRObject sourceMCRObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(sourceId));
    // old parent
    MCRObjectID oldParentId = sourceMCRObject.getStructure().getParentID();
    MCRObjectID newParentObjectID = MCRObjectID.getInstance(newParentId);
    if (newParentObjectID.equals(oldParentId)) {
        LOGGER.info("Object {} is already child of {}", sourceId, newParentId);
        return;
    }
    MCRObject oldParentMCRObject = null;
    if (oldParentId != null) {
        try {
            oldParentMCRObject = MCRMetadataManager.retrieveMCRObject(oldParentId);
        } catch (Exception exc) {
            LOGGER.error("Unable to get old parent object {}, its probably deleted.", oldParentId, exc);
        }
    }
    // change href to new parent
    LOGGER.info("Setting link in \"{}\" to parent \"{}\"", sourceId, newParentObjectID);
    MCRMetaLinkID parentLinkId = new MCRMetaLinkID("parent", 0);
    parentLinkId.setReference(newParentObjectID, null, null);
    sourceMCRObject.getStructure().setParent(parentLinkId);
    if (oldParentMCRObject != null) {
        // remove Child in old parent
        LOGGER.info("Remove child \"{}\" in old parent \"{}\"", sourceId, oldParentId);
        oldParentMCRObject.getStructure().removeChild(sourceMCRObject.getId());
        LOGGER.info("Update old parent \"{}\n", oldParentId);
        MCRMetadataManager.update(oldParentMCRObject);
    }
    LOGGER.info("Update \"{}\" in datastore (saving new link)", sourceId);
    MCRMetadataManager.update(sourceMCRObject);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Structure: {}", sourceMCRObject.getStructure().isValid());
        LOGGER.debug("Object: {}", sourceMCRObject.isValid());
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URISyntaxException(java.net.URISyntaxException) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) TransformerException(javax.xml.transform.TransformerException) MCRException(org.mycore.common.MCRException) MCRAccessException(org.mycore.access.MCRAccessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 28 with MCRPersistenceException

use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.

the class MCRUploadHandlerIFS method receiveFile.

@Override
public synchronized long receiveFile(String path, InputStream in, long length, String checksum) throws IOException, MCRPersistenceException, MCRAccessException {
    LOGGER.debug("incoming receiveFile request: {} {} {} bytes", path, checksum, length);
    this.setProgressText(path);
    List<Path> tempFiles = new LinkedList<>();
    Supplier<Path> tempFileSupplier = () -> {
        try {
            Path tempFile = Files.createTempFile(derivateID + "-" + path.hashCode(), ".upload");
            tempFiles.add(tempFile);
            return tempFile;
        } catch (IOException e) {
            throw new UncheckedIOException("Error while creating temp File!", e);
        }
    };
    try (InputStream fIn = preprocessInputStream(path, in, length, tempFileSupplier)) {
        if (rootDir == null) {
            // MCR-1376: Create derivate only if at least one file was successfully uploaded
            prepareUpload();
        }
        MCRPath file = getFile(path);
        LOGGER.info("Creating file {}.", file);
        Files.copy(fIn, file, StandardCopyOption.REPLACE_EXISTING);
        return tempFiles.isEmpty() ? length : Files.size(tempFiles.stream().reduce((a, b) -> b).get());
    } finally {
        tempFiles.stream().filter(Files::exists).forEach((tempFilePath) -> {
            try {
                Files.delete(tempFilePath);
            } catch (IOException e) {
                LOGGER.error("Could not delete temp file {}", tempFilePath);
            }
        });
        this.filesUploaded++;
        int progress = (int) (((float) this.filesUploaded / (float) getNumFiles()) * 100f);
        this.setProgress(progress);
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MCRFileAttributes(org.mycore.datamodel.niofs.MCRFileAttributes) Constructor(java.lang.reflect.Constructor) Supplier(java.util.function.Supplier) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) StandardCopyOption(java.nio.file.StandardCopyOption) MCRAccessManager(org.mycore.access.MCRAccessManager) DirectoryStream(java.nio.file.DirectoryStream) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS) LinkedList(java.util.LinkedList) MCRAccessException(org.mycore.access.MCRAccessException) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) Files(java.nio.file.Files) MCRPath(org.mycore.datamodel.niofs.MCRPath) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) MCRAccessInterface(org.mycore.access.MCRAccessInterface) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRProcessableStatus(org.mycore.common.processing.MCRProcessableStatus) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) InputStream(java.io.InputStream) InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) LinkedList(java.util.LinkedList)

Example 29 with MCRPersistenceException

use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.

the class MCRCreateDateDOIGenerator method generate.

@Override
public MCRDigitalObjectIdentifier generate(MCRObjectID mcrID, String additional) throws MCRPersistentIdentifierException {
    Date createdate = MCRMetadataManager.retrieveMCRObject(mcrID).getService().getDate("createdate");
    if (createdate != null) {
        MCRISO8601Date mcrdate = new MCRISO8601Date();
        mcrdate.setDate(createdate);
        String format = mcrdate.format(DATE_PATTERN, Locale.ENGLISH);
        Optional<MCRDigitalObjectIdentifier> parse = mcrdoiParser.parse(prefix + "/" + format);
        MCRPersistentIdentifier doi = parse.get();
        return (MCRDigitalObjectIdentifier) doi;
    } else {
        throw new MCRPersistenceException("The object " + mcrID + " doesn't have a createdate!");
    }
}
Also used : MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date) Date(java.util.Date)

Example 30 with MCRPersistenceException

use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.

the class MCRMetsSave method getFileGroup.

private static Element getFileGroup(Document mets, String fileGrpUSE) {
    // alter the mets document
    XPathExpression<Element> xpath;
    String fileGroupXPathString = String.format(Locale.ROOT, "mets:mets/mets:fileSec/mets:fileGrp[@USE='%s']", fileGrpUSE);
    xpath = XPathFactory.instance().compile(fileGroupXPathString, Filters.element(), null, MCRConstants.METS_NAMESPACE);
    Element element = xpath.evaluateFirst(mets);
    if (element == null) {
        // section does not exist
        Element fileGroupElement = new FileGrp(fileGrpUSE).asElement();
        String fileSectionPath = "mets:mets/mets:fileSec";
        xpath = XPathFactory.instance().compile(fileSectionPath, Filters.element(), null, MCRConstants.METS_NAMESPACE);
        Element fileSectionElement = xpath.evaluateFirst(mets);
        if (fileSectionElement == null) {
            throw new MCRPersistenceException("There is no fileSection in mets.xml!");
        }
        fileSectionElement.addContent(fileGroupElement);
        element = fileGroupElement;
    }
    return element;
}
Also used : Element(org.jdom2.Element) FileGrp(org.mycore.mets.model.files.FileGrp) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Aggregations

MCRPersistenceException (org.mycore.common.MCRPersistenceException)36 IOException (java.io.IOException)18 MCRAccessException (org.mycore.access.MCRAccessException)13 JDOMException (org.jdom2.JDOMException)9 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)8 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)8 MCRPath (org.mycore.datamodel.niofs.MCRPath)8 MCRException (org.mycore.common.MCRException)7 SAXException (org.xml.sax.SAXException)6 File (java.io.File)5 PersistenceException (javax.persistence.PersistenceException)5 Path (java.nio.file.Path)4 UncheckedIOException (java.io.UncheckedIOException)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3 Date (java.util.Date)3 EntityManager (javax.persistence.EntityManager)3 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)3 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)3 SignedJWT (com.nimbusds.jwt.SignedJWT)2 PrintWriter (java.io.PrintWriter)2