@Override public void receive(LockInfo message) { LockKey key = new LockKey(message.getEntityName(), message.getEntityId()); if (message.getUser() != null) { LockInfo lockInfo = locks.get(key); if (lockInfo == null || lockInfo.getSince().before(message.getSince())) { locks.put(key, message); } } else { locks.remove(key); } }
@Override public void unlock(String name, String id) { LockInfo lockInfo = locks.remove(new LockKey(name, id)); if (lockInfo != null) { log.debug("Unlocked " + name + "/" + id); clusterManager.send(new LockInfo(null, name, id)); } }
public void unlock() { LockInfo lockInfo = table.getSingleSelected(); if (lockInfo != null) { service.unlock(lockInfo.getEntityName(), lockInfo.getEntityId()); refresh(); if (lockInfo.getEntityId() != null) { showNotification(formatMessage("hasBeenUnlockedWithId", lockInfo.getEntityName(), lockInfo.getEntityId()), NotificationType.HUMANIZED); } else { showNotification(formatMessage("hasBeenUnlockedWithoutId", lockInfo.getEntityName()), NotificationType.HUMANIZED); } } }
/** * Pessimistic lock before start of editing, if it is configured for the entity. */ protected boolean lockIfNeeded(Entity entity) { MetaClass metaClass = getMetaClassForLocking(entity); LockInfo lockInfo = AppBeans.get(LockService.class).lock(metaClass.getName(), entity.getId().toString()); if (lockInfo == null) { justLocked = true; } else if (!(lockInfo instanceof LockNotSupported)) { showNotification( messages.getMainMessage("entityLocked.msg"), String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUser().getLogin(), AppBeans.get(DatatypeFormatter.class).formatDateTime(lockInfo.getSince()) ), Frame.NotificationType.HUMANIZED ); return false; } return true; }
@Override public void expireLocks() { log.debug("Expiring locks"); ArrayList<LockKey> list = new ArrayList<>(locks.keySet()); for (LockKey key : list) { LockInfo lockInfo = locks.get(key); if (lockInfo != null) { LockDescriptor ld = getConfig().get(key.name); if (ld == null) { log.debug("Lock " + key.name + "/" + key.id + " configuration not found, remove it"); locks.remove(key); } else { Integer timeoutSec = ld.getTimeoutSec(); if (timeoutSec != null && timeoutSec > 0) { Date since = lockInfo.getSince(); if (since.getTime() + timeoutSec * 1000 < AppBeans.get(TimeSource.class).currentTimestamp().getTime()) { log.debug("Lock " + key.name + "/" + key.id + " expired"); locks.remove(key); } } } } } }
/** * Pessimistic lock before start of editing, if it is configured for the entity. */ protected boolean lockIfNeeded(Entity entity) { LockService lockService = getBeanLocator().get(LockService.class); LockInfo lockInfo = lockService.lock(getLockName(), entity.getId().toString()); if (lockInfo == null) { justLocked = true; } else if (!(lockInfo instanceof LockNotSupported)) { Messages messages = getBeanLocator().get(Messages.class); DatatypeFormatter datatypeFormatter = getBeanLocator().get(DatatypeFormatter.class); Notifications notifications = getScreenContext().getNotifications(); notifications.create(NotificationType.HUMANIZED) .withCaption(messages.getMainMessage("entityLocked.msg")) .withDescription(String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUser().getLogin(), datatypeFormatter.formatDateTime(lockInfo.getSince()))) .show(); return false; } return true; }
@Override public LockInfo lock(String name, String id) { LockKey key = new LockKey(name, id); LockInfo lockInfo = locks.get(key); if (lockInfo != null) { log.debug("Already locked: " + lockInfo); return lockInfo; } LockDescriptor ld = getConfig().get(name); if (ld == null) { return new LockNotSupported(); } lockInfo = new LockInfo(userSessionSource.getUserSession().getCurrentOrSubstitutedUser(), name, id); locks.put(key, lockInfo); log.debug("Locked " + name + "/" + id); clusterManager.send(lockInfo); return null; }