public int lock(String userName) throws ClientException {

    LockInfo lockInfo = getLockInfo();
    boolean canLock = false;
    int returnCode = LOCKED_OK;

    if (lockInfo != null) {
      // document is already locked
      String lockingUser = lockInfo.getUserName();

      if (lockingUser == null) {
        canLock = true;
      } else {
        if (lockingUser.equals(userName)) {
          canLock = true;
          return ALREADY_LOCKED_BY_YOU;
        } else {
          if (isLockExpired()) {
            canLock = true;
            returnCode = LOCK_BORROWED;
          } else {
            canLock = false;
            return CAN_NOT_BORROW_LOCK;
          }
        }
      }
    }

    CoreSession session = getSessionFromDoc(targetDoc);
    session.setLock(targetDoc.getRef(), getLockToken(userName));
    session.save();

    return returnCode;
  }
  public int unlock(String userName) throws ClientException {

    LockInfo lockInfo = getLockInfo();
    boolean canUnLock = false;
    int returnCode = NOT_LOCKED;

    if (lockInfo != null) {
      // document is already locked
      String lockingUser = lockInfo.getUserName();

      if (lockingUser == null) {
        canUnLock = true;
        returnCode = NOT_LOCKED;
      } else {
        if (lockingUser.equals(userName)) {
          canUnLock = true;
          returnCode = ALREADY_LOCKED_BY_YOU;
        } else {
          if (isLockExpired()) {
            canUnLock = true;
            returnCode = LOCK_EXPIRED;
          } else {
            canUnLock = false;
            return CAN_NOT_UNLOCK;
          }
        }
      }
    }

    if (canUnLock) {
      getSessionFromDoc(targetDoc).unlock(targetDoc.getRef());
    }

    return returnCode;
  }
  public boolean isLocked() throws ClientException {
    LockInfo info = getLockInfo();
    if (info == null || info.getUserName() == null) {
      return false;
    }

    return !isLockExpired();
  }