在 xPages 中处理文档锁定的最佳方法? [英] Best way to deal with document locking in xPages?

查看:23
本文介绍了在 xPages 中处理文档锁定的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 xPages 中处理文档锁定的最佳方法是什么?目前我们使用标准的软锁定,它似乎在 Notes 客户端中运行良好.

What is the best way to deal with document locking in xPages? Currently we use the standard soft locking and it seems to work fairly well in the Notes client.

在 xPages 中,我考虑使用允许文档锁定"功能,但我担心人们会在不使用关闭或保存按钮的情况下关闭浏览器,然后锁定将永远不会被清除.

In xPages I considered using the "Allow Document Locking" feature but I am worried that people would close the browser without using a close or save button then the lock would never be cleared.

有没有办法在用户关闭会话时清除锁定?我没有看到这样的事件.

Is there a way to clear the locks when the user has closed his session? I am seeing no such event.

或者有没有更简单的方法来锁定文档?

Or is there an easier way to have document locking?

我意识到我可以使用代理清除锁,但是什么时候运行它?我会想某个晚上然后我相当肯定锁应该不再真正处于活动状态.

I realize I can clear the locks using an agent but when to run it? I would think sometime a night then I am fairly certain the lock should no longer really be active.

推荐答案

这是我正在使用的代码:

Here is code I'm using:

/* DOCUMENT LOCKING */
/*

    use the global object "documentLocking" with:
    .lock(doc) -> locks a document
    .unlock(doc) -> unlocks a document
    .isLocked(doc) -> returns true/false
    .lockedBy(doc) -> returns name of lock holder
    .lockedDT(doc) -> returns datetime stamp of lock

*/ 
function ynDocumentLocking() {

    /*

        a lock is an entry in the application scope

        with key = "$ynlock_"+UNID

        containing an array with
        (0) = username of lock holder
        (1) = timestamp of lock


    */

    var lockMaxAge = 60 * 120; // in seconds, default 120 min

    this.getUNID = function(v) {
        if (!v) return null;
        if (typeof v == "NotesXspDocument") return v.getDocument().getUniversalID();
        if (typeof v == "string") return v;
        return v.getUniversalID();
    }

    /* puts a lock into application scope */
    this.lock = function(doc:NotesDocument) {
        var a = new Array(1);
        a[0] = @UserName();
        a[1] = @Now();
        applicationScope.put("$ynlock_"+this.getUNID(doc), a);  
        // print("SET LOCK "+"$ynlock_"+doc.getUniversalID()+" / "+a[0]+" / "+a[1]);
    }   

    /* removes a lock from the application scope */
    this.unlock = function(doc:NotesDocument) {
        applicationScope.put("$ynlock_"+this.getUNID(doc), null);
        //print("REMOVED LOCK for "+"$ynlock_"+doc.getUniversalID());
    }

    this.isLocked = function(doc:NotesDocument) {
        try {
            //print("ISLOCKED for "+"$ynlock_"+doc.getUniversalID());       

            // check how old the lock is
            var v = applicationScope.get("$ynlock_"+this.getUNID(doc));
            if (!v) {
                //print("no lock found -> return false");
                return false;   
            }

            // if lock holder is the current user, treat as not locked
            if (v[0] == @UserName()) {
                //print("lock holder = user -> not locked");
                return false;
            }


            var dLock:NotesDateTime = session.createDateTime(v[1]);
            var dNow:NotesDateTime = session.createDateTime(@Now());
            // diff is in seconds
            //print("time diff="+dNow.timeDifference(dLock)+" dLock="+v[1]+" now="+@Now());
            // if diff > x seconds then remove lock, it not locked
            if (dNow.timeDifference(dLock) > lockMaxAge) {
                // print("LOCK is older than maxAge "+lockMaxAge+" -> returning false");
                return false;
            }
            //print("return true");
            return true;
        // TODO: check how old the lock is
        } catch (e) {
            print("ynDocumentLocking.isLocked: "+e);
        }

    }

    this.lockedBy = function(doc:NotesDocument) {
        try {
        var v = applicationScope.get("$ynlock_"+this.getUNID(doc));
        if (!v) return "";
        //print("ISLOCKEDBY "+"$ynlock_"+doc.getUniversalID()+" = "+v[0]);
        return v[0];

        } catch (e) {
            print("ynDocumentLocking.isLockedBy: "+e);
        }
    }

    this.lockedDT = function(doc:NotesDocument) {
        try {
        var v = applicationScope.get("$ynlock_"+this.getUNID(doc));
        if (!v) return "";
        return v[1];

        } catch (e) {
            print("ynDocumentLocking.isLockedBy: "+e);
        }
    }

}

var documentLocking = new ynDocumentLocking();

这篇关于在 xPages 中处理文档锁定的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆