Link to home
Start Free TrialLog in
Avatar of BobMc
BobMc

asked on

How to delete documents from view

I'm not a Lotus Notes expert, so forgive any misuse of terminology
I'm hoping this isnt too complicated, but I've oodles of question points available to spend on it if it does...

I have a domino database that receives messages daily from lots of different sources, and an agent that identified the ones Im looking for, and puts them into a view.
I want to scan the view, keep the latest document from each source (its a unique document subject), and delete all older copies.

I've written some code (Perl) that uses the Notes.NotesSession object to:
Open the view
Create a viewnavigator
and loop through each document via ViewEntry ->GetNextDocument, so I can extract all the details I need.

I intend to compare (for each document subject) the posted date in order to determine the one I need to keep, and mark the 'other' one for deletion.
This could be either from either the current viewentry I have, or the one I've stored previously.

I've got it happily printing out what its doing, what records its looking at, and identifying the correct ones to delete, but haven't got it yet actually doing it

I dont have any problems with the coding, but I dont understand the Domino COM implementation well enough to know if:

1. is this an acceptable way to do it?
2. Is there a way to order the documents in the viewnavigator so I get the newly posted ones first?
3. If I need to delete a previously 'stored' document reference - what would I need to store in order to retrieve it, and do the deletions affect the viewnavigator.

any advice or code snippets appreciated - I dont mind what language they come in!

hope this makes sense....
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Re your questions:
1) Id' say no, it is not; why don't you use Domino the way it is supposed to be used? You can program that function in a database agent, and it'll be activated automatically by the server. No need for external applications whatsoever.
2) Yes, you have to reorder the view itself: for example, the first column could contain the function @Created; then, set it to decreasing order
3) I think I don't understand... what if your first column were the subject, and the second column the creation date?

> any advice or code snippets appreciated - I dont mind what language they come in!
I might have some advice for you, shall I continue in Dutch or French...?    ;-))
It's acceptable, why not (unless you can access Domino database design, then I agree with Sjef).

If you don't have access to domino db design, then you'll have to find max date/time the doc is modified, that's no problem...

Why don't you post the code you have so far?
Avatar of BobMc
BobMc

ASKER

Thanks for the responses. Yes, I understand this would be better / easier done within Domino, but I don't have that luxury in this case.

I've posted the interesting bit of the code I have so far.
I'm keeping track of the newest document by subject in a hash, so it doesnt matter what order I search the documents. I either need to delete the current document, or the one I already referenced in the hash.
I was sort of hoping that I could save a unique ID for each document, and delete it with that, but Im unsure how to proceed

I'll all ears if theres a better / different approach to doing this via COM that I should consider.

$NotesSession = Win32::OLE->new('Notes.NotesSession')
                or die "Cannot start Lotus Notes Session object.\n";
 
$NotesDB = $NotesSession->GetDatabase($servername, $databasename)
               or die ("Can't open database $databasename\n");
 
$NotesView=$NotesDB->GetView($viewname)
               or die ("Can't open view $viewname\n");
 
$NotesViewNav=$NotesView->CreateViewNav;
$NotesViewEntry=$NotesViewNav->GetFirstDocument;
 
my $doccount=$NotesViewNav->{Count};
print "There are $doccount documents in view $viewname to process\n";
 
my $docindex=0;     ## Current doc position in ViewNav - dont know if this is useful
my %LatestDocs=();  ## Capture the details of the latest posted
                    ## document for each subject
 
while (defined ($NotesViewEntry)) {
  ++$docindex;
  $NotesDocument = $NotesViewEntry->Document;
 
  ## extract all the fields at once
  my $doccollection = $NotesDocument->{Items};
 
  ## convert to perl hash ref
  my $docitems=&CollectionToHash($doccollection);
 
  ##
  ## We assume that the subject will never change, and is unique per server
  ## So we need to check that within the view, this is the latest (or only)
  ## document (subject) for this server.
  ## If the current subject is not in the hash, so its the first we've found for
  ## this server, and we store it
  ## If the current subject is already in the hash, and the current is older,
  ## we delete it
  ## If the current subject is already in the hash, and the current is newer,
  ## we delete the stored one, and store the current one
  ##
  ## Need to ensure we extract the next document before we delete anything.
  ##
  ## Subject is "title string : server name". Dont need title bit
  my ($subject) = ($docitems-> {Subject} =~ /:(.*)/);
  my $posteddate= $docitems->{PostedDate};
 
  print "Doc[ $docindex / $doccount ]:  Subject $subject\t\tPosted at $posteddate\n";
 
  ## Convert date to numeric for comparison (tz excepting)
  my $eposteddate=DateStringToEpoch($posteddate);
  ## Do we already have one of these?
  if (exists($LatestDocs{$subject})) {
    ## Already got one - is it newer that the current one?
    if ($LatestDocs{$subject} >= $eposteddate) {
      ## Stored doc is not older -> keep stored one and delete current
      print "Delete current doc for $subject, posted at $posteddate\n";
    } else {
      ## current doc is newer
      ## so delete stored doc, and save current details
      print "Replace stored doc for $subject posted at "
          .EpochToDateString($LatestDocs{$subject})."\n";
      print "With current doc for $subject posted at $posteddate\n";
    }
  } else {
    ## new subject => this one is newest for now, so store it
    print "New - store current doc for $subject \n";
    $LatestDocs{$subject}=$eposteddate;
  }
  last unless $docindex < $debugdoccount;
  $NotesViewEntry=$NotesViewNav->GetNextDocument($NotesViewEntry);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And to move back to a document, use view.GetDocumentByUnid()
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BobMc

ASKER

thanks for the help guys, I've managed to trash several replicas now, so I know I'm on the right track :)
LOL

Very good!!