Search in sources :

Example 1 with MCREvent

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

the class MCRMetadataManager method fireEvent.

private static void fireEvent(MCRBase base, MCRBase oldBase, String eventType) {
    boolean objectEvent = base instanceof MCRObject;
    String type = objectEvent ? MCREvent.OBJECT_TYPE : MCREvent.DERIVATE_TYPE;
    final MCREvent evt = new MCREvent(type, eventType);
    if (objectEvent) {
        evt.put(MCREvent.OBJECT_KEY, base);
    } else {
        evt.put(MCREvent.DERIVATE_KEY, base);
    }
    Optional.ofNullable(oldBase).ifPresent(b -> evt.put(objectEvent ? MCREvent.OBJECT_OLD_KEY : MCREvent.DERIVATE_OLD_KEY, b));
    if (MCREvent.DELETE_EVENT.equals(eventType)) {
        MCREventManager.instance().handleEvent(evt, MCREventManager.BACKWARD);
    } else {
        MCREventManager.instance().handleEvent(evt);
    }
}
Also used : MCREvent(org.mycore.common.events.MCREvent)

Example 2 with MCREvent

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

the class MCRFileMetaEventHandler method handleDerivateUpdated.

@Override
protected void handleDerivateUpdated(MCREvent evt, MCRDerivate der) {
    HashSet<MCRCategLinkReference> before = new HashSet<>();
    before.addAll(CATEGLINK_SERVICE.getReferences(der.getId().toString()));
    handleDerivateDeleted(evt, der);
    handleDerivateCreated(evt, der);
    HashSet<MCRCategLinkReference> after = new HashSet<>();
    after.addAll(CATEGLINK_SERVICE.getReferences(der.getId().toString()));
    HashSet<MCRCategLinkReference> combined = new HashSet<>(before);
    combined.addAll(after);
    for (MCRCategLinkReference ref : combined) {
        MCRObjectID derId = der.getId();
        String path = ref.getObjectID();
        MCRPath file = MCRPath.getPath(derId.toString(), path);
        BasicFileAttributes attrs;
        try {
            attrs = Files.readAttributes(file, BasicFileAttributes.class);
        } catch (IOException e) {
            LOGGER.warn("File is linked to category but cannot be read:{}{}", der.getId(), ref.getObjectID(), e);
            continue;
        }
        MCREvent fileEvent = new MCREvent(MCREvent.PATH_TYPE, MCREvent.INDEX_EVENT);
        fileEvent.put(MCREvent.PATH_KEY, file);
        fileEvent.put(MCREvent.FILEATTR_KEY, attrs);
        MCREventManager.instance().handleEvent(fileEvent);
    }
}
Also used : MCREvent(org.mycore.common.events.MCREvent) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet)

Example 3 with MCREvent

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

the class MCRDirectory method delete.

/**
 * Deletes this directory and its content stored in the system
 */
@Override
public void delete() throws MCRPersistenceException {
    ensureNotDeleted();
    Stream.of(getChildren()).forEach(MCRFilesystemNode::delete);
    BasicFileAttributes attrs = getBasicFileAttributes();
    MCRPath path = toPath();
    super.delete();
    MCREvent evt = new MCREvent(MCREvent.PATH_TYPE, MCREvent.DELETE_EVENT);
    evt.put(MCREvent.PATH_KEY, path);
    evt.put(MCREvent.FILEATTR_KEY, attrs);
    MCREventManager.instance().handleEvent(evt);
    children = null;
    numChildDirsHere = 0;
    numChildDirsTotal = 0;
    numChildFilesHere = 0;
    numChildFilesTotal = 0;
}
Also used : MCREvent(org.mycore.common.events.MCREvent) MCRPath(org.mycore.datamodel.niofs.MCRPath) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 4 with MCREvent

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

the class MCRFile method adjustMetadata.

public synchronized void adjustMetadata(FileTime lastModified, String md5, long newSize) throws IOException {
    long sizeDiff = newSize - getSize();
    if (sizeDiff == 0 && this.md5.equals(md5) && lastModified == null) {
        return;
    }
    if (!MD5_HEX_PATTERN.matcher(md5).matches()) {
        throw new IllegalArgumentException("MD5 sum is not valid: " + md5);
    }
    this.md5 = md5;
    this.size = newSize;
    touch(lastModified, false);
    if (hasParent()) {
        getParent().sizeOfChildChanged(sizeDiff);
    }
    // If file content has changed, call event handlers to index content
    String type = isNew() ? MCREvent.CREATE_EVENT : MCREvent.UPDATE_EVENT;
    MCREvent event = new MCREvent(MCREvent.PATH_TYPE, type);
    // to support old events
    event.put(MCRFileEventHandlerBase.FILE_TYPE, this);
    event.put(MCREvent.PATH_KEY, toPath());
    event.put(MCREvent.FILEATTR_KEY, getBasicFileAttributes());
    MCREventManager.instance().handleEvent(event);
    setNew(false);
}
Also used : MCREvent(org.mycore.common.events.MCREvent)

Example 5 with MCREvent

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

the class MCRFileEventHandlerBase method toMCRFileEvent.

private MCREvent toMCRFileEvent(MCREvent source, Path path, BasicFileAttributes attrs) {
    if (!(path.getFileSystem() instanceof MCRIFSFileSystem)) {
        LOGGER.error("Cannot transform path from {} to MCRFile.", path.getFileSystem());
        return null;
    }
    if (attrs != null && !attrs.isDirectory()) {
        MCREvent target = new MCREvent(FILE_TYPE, source.getEventType());
        // includes probably "file";
        target.putAll(source);
        if (!target.contains(FILE_TYPE)) {
            if (target.getEventType().equals(MCREvent.DELETE_EVENT)) {
                LOGGER.warn("Could not restore MCRFile for Path event: {}", path);
                return null;
            } else {
                // fileKey is always internal id;
                MCRFile file = MCRFile.getFile(attrs.fileKey().toString());
                if (file == null) {
                    LOGGER.warn("Could not restore MCRFile with id {} for Path event: {}", attrs.fileKey(), path);
                    return null;
                }
                target.put(MCRFILE_EVENT_KEY, file);
            }
        }
        LOGGER.info("Transformed {} -> {}", source, target);
        return target;
    }
    return null;
}
Also used : MCREvent(org.mycore.common.events.MCREvent) MCRIFSFileSystem(org.mycore.datamodel.niofs.ifs1.MCRIFSFileSystem)

Aggregations

MCREvent (org.mycore.common.events.MCREvent)9 IOException (java.io.IOException)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3 UncheckedIOException (java.io.UncheckedIOException)2 MCRPath (org.mycore.datamodel.niofs.MCRPath)2 Gson (com.google.gson.Gson)1 HashSet (java.util.HashSet)1 List (java.util.List)1 BiConsumer (java.util.function.BiConsumer)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 MCRHIBConnection (org.mycore.backend.hibernate.MCRHIBConnection)1 MCRCoreVersion (org.mycore.common.MCRCoreVersion)1 MCRException (org.mycore.common.MCRException)1 MCREventHandlerBase (org.mycore.common.events.MCREventHandlerBase)1 MCRCategLinkReference (org.mycore.datamodel.classifications2.MCRCategLinkReference)1 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)1 MCRObject (org.mycore.datamodel.metadata.MCRObject)1 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)1 MCRIFSFileSystem (org.mycore.datamodel.niofs.ifs1.MCRIFSFileSystem)1