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);
}
}
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);
}
}
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;
}
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);
}
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;
}
Aggregations