Search in sources :

Example 31 with MCRPersistenceException

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

the class MCRMODSMetadataShareAgent method distributeMetadata.

/* (non-Javadoc)
     * @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritMetadata(org.mycore.datamodel.metadata.MCRObject)
     */
@Override
public void distributeMetadata(MCRObject holder) throws MCRPersistenceException, MCRAccessException {
    MCRMODSWrapper holderWrapper = new MCRMODSWrapper(holder);
    List<MCRMetaLinkID> children = holder.getStructure().getChildren();
    if (!children.isEmpty()) {
        LOGGER.info("Update inherited metadata");
        for (MCRMetaLinkID childIdRef : children) {
            MCRObjectID childId = childIdRef.getXLinkHrefID();
            if (MCRMODSWrapper.isSupported(childId)) {
                LOGGER.info("Update: {}", childIdRef);
                MCRObject child = MCRMetadataManager.retrieveMCRObject(childId);
                MCRMODSWrapper childWrapper = new MCRMODSWrapper(child);
                inheritToChild(holderWrapper, childWrapper);
                LOGGER.info("Saving: {}", childIdRef);
                try {
                    MCRMetadataManager.update(child);
                } catch (MCRPersistenceException | MCRAccessException e) {
                    throw new MCRPersistenceException("Error while updating inherited metadata", e);
                }
            }
        }
    }
    Collection<String> recipientIds = MCRLinkTableManager.instance().getSourceOf(holder.getId(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE);
    for (String rId : recipientIds) {
        MCRObjectID recipientId = MCRObjectID.getInstance(rId);
        if (MCRMODSWrapper.isSupported(recipientId)) {
            LOGGER.info("distribute metadata to {}", rId);
            MCRObject recipient = MCRMetadataManager.retrieveMCRObject(recipientId);
            MCRMODSWrapper recipientWrapper = new MCRMODSWrapper(recipient);
            for (Element relatedItem : recipientWrapper.getLinkedRelatedItems()) {
                String holderId = relatedItem.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
                if (holder.getId().toString().equals(holderId)) {
                    @SuppressWarnings("unchecked") Filter<Content> sharedMetadata = (Filter<Content>) Filters.element("part", MCRConstants.MODS_NAMESPACE).negate();
                    relatedItem.removeContent(sharedMetadata);
                    relatedItem.addContent(holderWrapper.getMODS().cloneContent());
                    LOGGER.info("Saving: {}", recipientId);
                    try {
                        MCRMetadataManager.update(recipient);
                    } catch (MCRPersistenceException | MCRAccessException e) {
                        throw new MCRPersistenceException("Error while updating shared metadata", e);
                    }
                }
            }
        }
    }
}
Also used : Element(org.jdom2.Element) MCRAccessException(org.mycore.access.MCRAccessException) MCRObject(org.mycore.datamodel.metadata.MCRObject) Filter(org.jdom2.filter.Filter) Content(org.jdom2.Content) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 32 with MCRPersistenceException

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

the class MCRCategLinkServiceImpl method setLinks.

@Override
public void setLinks(MCRCategLinkReference objectReference, Collection<MCRCategoryID> categories) {
    EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
    categories.stream().distinct().forEach(categID -> {
        final MCRCategory category = getMCRCategory(entityManager, categID);
        if (category == null) {
            throw new MCRPersistenceException("Could not link to unknown category " + categID);
        }
        MCRCategoryLinkImpl link = new MCRCategoryLinkImpl(category, objectReference);
        if (LOGGER.isDebugEnabled()) {
            MCRCategory linkedCategory = link.getCategory();
            StringBuilder debugMessage = new StringBuilder("Adding Link from ").append(linkedCategory.getId());
            if (linkedCategory instanceof MCRCategoryImpl) {
                debugMessage.append("(").append(((MCRCategoryImpl) linkedCategory).getInternalID()).append(") ");
            }
            debugMessage.append("to ").append(objectReference);
            LOGGER.debug(debugMessage.toString());
        }
        entityManager.persist(link);
        LOGGER.debug("===DONE: {}", link.id);
    });
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 33 with MCRPersistenceException

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

the class MCRCategoryDAOImpl method deleteCategory.

@Override
public void deleteCategory(MCRCategoryID id) {
    EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
    LOGGER.debug("Will get: {}", id);
    MCRCategoryImpl category = getByNaturalID(entityManager, id);
    if (category == null) {
        throw new MCRPersistenceException("Category " + id + " was not found. Delete aborted.");
    }
    LOGGER.debug("Will delete: {}", category.getId());
    MCRCategory parent = category.parent;
    category.detachFromParent();
    entityManager.remove(category);
    if (parent != null) {
        entityManager.flush();
        LOGGER.debug("Left: {} Right: {}", category.getLeft(), category.getRight());
        // always add +1 for the currentNode
        int nodes = 1 + (category.getRight() - category.getLeft()) / 2;
        final int increment = nodes * -2;
        // decrement left and right values by nodes
        updateLeftRightValue(entityManager, category.getRootID(), category.getLeft(), increment);
    }
    updateTimeStamp();
    updateLastModified(category.getRootID());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 34 with MCRPersistenceException

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

the class MCRXMLMetadataEventHandler method handleStoreEvent.

private void handleStoreEvent(MCREvent evt, MCRBase obj) {
    String eventType = evt.getEventType();
    MCRObjectID id = obj.getId();
    try {
        switch(eventType) {
            case MCREvent.REPAIR_EVENT:
            case MCREvent.UPDATE_EVENT:
            case MCREvent.CREATE_EVENT:
                MCRBaseContent content = new MCRBaseContent(obj);
                Date modified = obj.getService().getDate(MCRObjectService.DATE_TYPE_MODIFYDATE);
                switch(eventType) {
                    case MCREvent.REPAIR_EVENT:
                        MCRContent retrieveContent = metaDataManager.retrieveContent(id);
                        if (isUptodate(retrieveContent, content)) {
                            return;
                        }
                    case MCREvent.UPDATE_EVENT:
                        metaDataManager.update(id, content, modified);
                        break;
                    case MCREvent.CREATE_EVENT:
                        metaDataManager.create(id, content, modified);
                        break;
                }
                evt.put("content", content);
                break;
            case MCREvent.DELETE_EVENT:
                metaDataManager.delete(id);
                break;
            default:
                throw new IllegalArgumentException("Invalid event type " + eventType + " for object " + id);
        }
    } catch (IOException e) {
        throw new MCRPersistenceException("Error while handling '" + eventType + "' event of '" + id + "'", e);
    }
}
Also used : MCRBaseContent(org.mycore.common.content.MCRBaseContent) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRContent(org.mycore.common.content.MCRContent) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Date(java.util.Date)

Example 35 with MCRPersistenceException

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

the class MCRXMLMetadataManager method create.

/**
 * Stores metadata of a new MCRObject in the persistent store.
 *
 * @param mcrid the MCRObjectID
 * @param xml the xml metadata of the MCRObject
 * @param lastModified the date of last modification to set
 * @return the stored metadata as IFS2 object
 */
public MCRStoredMetadata create(MCRObjectID mcrid, MCRContent xml, Date lastModified) throws IOException {
    MCRStoredMetadata sm;
    try {
        sm = getStore(mcrid).create(xml, mcrid.getNumberAsInteger());
    } catch (JDOMException e) {
        throw new MCRPersistenceException("Error while storing object: " + mcrid, e);
    }
    sm.setLastModified(lastModified);
    MCRConfiguration.instance().systemModified();
    return sm;
}
Also used : MCRStoredMetadata(org.mycore.datamodel.ifs2.MCRStoredMetadata) JDOMException(org.jdom2.JDOMException) 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