Search in sources :

Example 21 with MCRPersistenceException

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

the class MCRFile method setContentFrom.

public long setContentFrom(InputStream source, boolean storeContentChange) throws MCRPersistenceException {
    ensureNotDeleted();
    String old_md5 = md5;
    long old_size = size;
    String old_storageID = storageID;
    String old_storeID = storeID;
    MCRContentStore old_store = getContentStore();
    initContentFields();
    MCRContentInputStream cis = new MCRContentInputStream(source);
    byte[] header = cis.getHeader();
    contentTypeID = MCRFileContentTypeFactory.detectType(getName(), header).getID();
    MCRContentStore store = MCRContentStoreFactory.selectStore(this);
    storageID = store.storeContent(this, cis);
    storeID = store.getID();
    long new_size = cis.getLength();
    String new_md5 = cis.getMD5String();
    if ((old_storageID.length() != 0) && (!(old_storageID.equals(storageID) && (old_storeID.equals(storeID))))) {
        old_store.deleteContent(old_storageID);
    }
    boolean changed = new_size != old_size || new_md5.equals(old_md5);
    if (changed) {
        if (storeContentChange) {
            try {
                adjustMetadata(FileTime.fromMillis(System.currentTimeMillis()), new_md5, new_size);
            } catch (IOException e) {
                throw new MCRPersistenceException("Error while updating file metadata", e);
            }
        } else {
            this.md5 = new_md5;
        }
    }
    return new_size - old_size;
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 22 with MCRPersistenceException

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

the class MCRVersioningMetadataStoreTest method verifyRevPropsFail.

@Test
public void verifyRevPropsFail() throws Exception {
    getVersStore().verify();
    deletedVersions();
    getVersStore().verify();
    File baseDIR = new File(URI.create(getVersStore().repURL.toString()));
    File revProp = new File(baseDIR.toURI().resolve("db/revprops/0/2"));
    assertTrue("is not a file " + revProp, revProp.isFile());
    revProp.setWritable(true);
    new PrintWriter(revProp).close();
    revProp.setWritable(false);
    try {
        getVersStore().verify();
    } catch (MCRPersistenceException e) {
        return;
    }
    fail("Verify finished without error");
}
Also used : File(java.io.File) MCRPersistenceException(org.mycore.common.MCRPersistenceException) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 23 with MCRPersistenceException

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

the class MCRVersioningMetadataStoreTest method verifyRevFail.

@Test
public void verifyRevFail() throws Exception {
    getVersStore().verify();
    deletedVersions();
    getVersStore().verify();
    File baseDIR = new File(URI.create(getVersStore().repURL.toString()));
    File revProp = new File(baseDIR.toURI().resolve("db/revs/0/2"));
    assertTrue("is not a file " + revProp, revProp.isFile());
    new PrintWriter(revProp).close();
    try {
        getVersStore().verify();
    } catch (MCRPersistenceException e) {
        return;
    }
    fail("Verify finished without error");
}
Also used : File(java.io.File) MCRPersistenceException(org.mycore.common.MCRPersistenceException) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 24 with MCRPersistenceException

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

the class MCRMetadataManager method update.

/**
 * Updates the derivate or creates it if it does not exist yet.
 *
 * @throws MCRPersistenceException
 *                if a persistence problem is occurred
 * @throws MCRAccessException
 *                if write permission to object or derivate is missing
 */
public static void update(final MCRDerivate mcrDerivate) throws MCRPersistenceException, MCRAccessException {
    MCRObjectID id = mcrDerivate.getId();
    // check deletion mark
    if (MCRMarkManager.instance().isMarkedForDeletion(id)) {
        return;
    }
    if (!MCRMetadataManager.exists(id)) {
        MCRMetadataManager.create(mcrDerivate);
        return;
    }
    if (!MCRAccessManager.checkPermission(id, PERMISSION_WRITE)) {
        throw MCRAccessException.missingPermission("Update derivate", id.toString(), PERMISSION_WRITE);
    }
    File fileSourceDirectory = null;
    if (mcrDerivate.getDerivate().getInternals() != null && mcrDerivate.getDerivate().getInternals().getSourcePath() != null) {
        fileSourceDirectory = new File(mcrDerivate.getDerivate().getInternals().getSourcePath());
        if (!fileSourceDirectory.exists()) {
            LOGGER.warn("{}: the directory {} was not found.", id, fileSourceDirectory);
            fileSourceDirectory = null;
        }
    }
    // get the old Item
    MCRDerivate old = MCRMetadataManager.retrieveMCRDerivate(id);
    // remove the old link to metadata
    MCRMetaLinkID oldLink = old.getDerivate().getMetaLink();
    MCRMetaLinkID newLink = mcrDerivate.getDerivate().getMetaLink();
    if (!oldLink.equals(newLink)) {
        MCRObjectID oldMetadataObjectID = oldLink.getXLinkHrefID();
        MCRObjectID newMetadataObjectID = newLink.getXLinkHrefID();
        if (!oldMetadataObjectID.equals(newLink.getXLinkHrefID())) {
            try {
                MCRMetadataManager.removeDerivateFromObject(oldMetadataObjectID, id);
            } catch (final MCRException e) {
                LOGGER.warn(e.getMessage(), e);
            }
        }
        // add the link to metadata
        final MCRMetaLinkID der = new MCRMetaLinkID("derobject", id, null, mcrDerivate.getLabel(), newLink.getXLinkRole());
        addOrUpdateDerivateToObject(newMetadataObjectID, der);
    }
    // update the derivate
    mcrDerivate.getService().setDate("createdate", old.getService().getDate("createdate"));
    if (!mcrDerivate.getService().isFlagTypeSet(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
        for (String flagCreatedBy : old.getService().getFlags(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
            mcrDerivate.getService().addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, flagCreatedBy);
        }
    }
    MCRMetadataManager.updateMCRDerivateXML(mcrDerivate);
    // update to IFS
    if (fileSourceDirectory != null) {
        Path sourcePath = fileSourceDirectory.toPath();
        MCRPath targetPath = MCRPath.getPath(id.toString(), "/");
        try {
            Files.walkFileTree(sourcePath, new MCRTreeCopier(sourcePath, targetPath));
        } catch (Exception exc) {
            throw new MCRPersistenceException("Unable to update IFS. Copy failed from " + sourcePath.toAbsolutePath() + " to target " + targetPath.toAbsolutePath(), exc);
        }
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) MCRPath(org.mycore.datamodel.niofs.MCRPath) File(java.io.File) 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 25 with MCRPersistenceException

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

the class MCRMetadataManager method deleteDerivate.

private static void deleteDerivate(String derivateID) throws MCRPersistenceException {
    try {
        MCRPath rootPath = MCRPath.getPath(derivateID, "/");
        if (!Files.exists(rootPath)) {
            LOGGER.info("Derivate does not exist: {}", derivateID);
            return;
        }
        Files.walkFileTree(rootPath, MCRRecursiveDeleter.instance());
        rootPath.getFileSystem().removeRoot(derivateID);
    } catch (Exception exc) {
        throw new MCRPersistenceException("Unable to delete derivate " + derivateID, exc);
    }
}
Also used : 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)

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