Personal tools

May 13, 2010

managing replicas on database using Lotusscript

How to manage database replication options using Lotusscript also if user doesn't have manager access

Customer request was: 

Disable replication on the database if user never replicate it in the last week

User only has author rights in ACL database (which is a replica of a management deployed on n users laptops) 
How to get it programmatically even if the user has no rights manager? 

If you try to access the object NotesReplication and try to do something like that:

Dim session As New NotesSession
Dim db As NotesDatabase
Dim rep As NotesReplication
Set db = session.CurrentDatabase
Set rep = db.ReplicationInfo
If rep.Disabled Then
  rep.Disabled = False
End If
Call rep.Save()

You get the following error: 

"Error supplied to access product object method" 

The key is to use the Notes API, in particular NSFDbReplicaInfo 

API in this case exploit user credentials of id who wrote the Lotusscript code, so you should sign the agent with the ID of the administrator or with a user who has manager rights to the database 

We must define the following class in the "Declaration" agent 

Dim rc As Integer 

Type DBREPLICAINFO 
ID AS TIMED 
flags As Integer 
CutoffInterval As Integer 
As cutoff TimeDate 
End Type 

Declare Function Lib W32_NSFDbOpen "nnotes.dll" Alias "NSFDbOpen" (ByVal dbname As String, hdb As Long) As Integer 
Declare Function Lib W32_NSFDbClose "nnotes.dll" Alias "NSFDbClose" (ByVal hdb As Long) As Integer 
Declare Function Lib W32_NSFDbReplicaInfoGet "nnotes.dll" Alias "NSFDbReplicaInfoGet" (ByVal HDB As Long As retReplicationInfo DBREPLICAINFO) As Integer 
Declare Function Lib W32_NSFDbReplicaInfoSet "nnotes.dll" Alias "NSFDbReplicaInfoSet" (ByVal HDB As Long As retReplicationInfo DBREPLICAINFO) As Integer 
Declare Function Lib W32_OSLockObject "nnotes.dll" Alias "OSLockObject" (ByVal handle) As Long 
Declare Sub OSUnlockObject Lib "NNOTES.DLL" Alias "OSUnlockObject" (ByVal handle) 
Declare Sub W32_OSMemFree Lib "NNOTES.DLL" Alias "OSMemFree" (ByVal handle) 


Class NotesReplicationSettings 

Private hdb As Long 
As private retReplicationInfo DBREPLICAINFO 
Private prvdb As NotesDatabase 
Private flgDBExist As Integer 

Sub Delete 
If hdb <> 0 Then Call W32_NSFDbClose (hdb) 
End Sub 

Sub New (inpNotesDatabase As NotesDatabase) 

As String Dim sDatabase 
As New NotesSession Dim uaesession 
Dim rc As Integer 

Me.flgDBExist = False 

'Get a valid NotesDatabase to the specified database 
If inpNotesDatabase Is Nothing Then 
Error 14104, "NotesUserActivity: Object Database is invalid" 
Exit Sub 
End If 

September prvdb = New NotesDatabase (inpNotesDatabase.Server, inpNotesDatabase.FilePath) 

If (prvdb.Server = "") Or (uaesession.IsOnServer) Then 
sdatabase = prvdb.filepath 
Else 
sdatabase prvdb.server = + "!" + Prvdb.filepath 
End If 

'Open the target database 
rc = W32_NSFDbOpen (sDatabase, Me.hDb) 
If rc <> 0 Then 
Me.flgDBExist = False 
End If 

'Set the Replication Information 
rc = W32_NSFDbReplicaInfoGet (Me.hDb, Me.retReplicationInfo) 
If rc <> 0 Then 
Me.flgDBExist = False 
End If 
Me.flgDBExist = True 
End Sub 

DBExist As Integer Public Function 
DBExist = Me.flgDBExist 
End Function 

Public Function Parent As NotesDatabase 
Set Parent = prvdb 
End Function 

Public Function SetDisableReplica (sFlag As Integer) As Integer 

As Long Dim puActivity 

If Not Then Me.flgDBExist 
Error 14104, "Notes DB not opened" 
SetDisableReplica = False 
Exit Function 
End If 

Me.retReplicationInfo.flags = (Me.retReplicationInfo.flags Or & H4) 

rc = W32_NSFDbReplicaInfoSet (Me.hDb, Me.retReplicationInfo) 
If rc <> 0 Then 
Me.flgDBExist = False 
End If 

End Function 
End Class

get an instance in initialize agent method as follows (db is target database to manage):

Dim nrs As New NotesReplicationSettings (db)  

and disable (or enable depending on the parameter that is passed, 1 or 0) replication: 

nrs.SetDisableReplica (1)  

or  

nrs.SetDisableReplica (0)  

And that's all

Filed under: ,
comments powered by Disqus