Agent to create a backup of you names.nsf weekly

As an administrator, I often need to put me back in time to check something on the domino infrastructure. My Domino Domain is very small so I can afford to make a backup of my names.nsf every week.

This class, creat a copy of each documents of the names.nsf, zip it and attach it to a document… enjoy !


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import lotus.domino.*;
import java.io.*;
import java.util.zip.*;

/* -----------------------------------------------------------------------------------------------------------------------
* Class : JavaAgent
* Date : 12.01.2011
* Author : Pierre Koerber
* Desc. : Permet de faire un backup du PAB et de le zipper.
*/

public class JavaAgent extends AgentBase {

public void NotesMain() {

try {

Session session = getSession();
AgentContext agentContext = session.getAgentContext();

// get the curr db
Database curDb ;
curDb = agentContext.getCurrentDatabase() ;

// (Your code goes here)
Database db ;
db = session.getDatabase(curDb.getServer(), "names.nsf") ;

DateTime timenow = session.createDateTime("Today");
timenow.setNow();

String sName = new String("") ;
String sZipName = new String("") ;
sName = db.getFilePath() ;

System.out.println("OS current temporary directory is " + System.getProperty("java.io.tmpdir"));

sName = System.getProperty("java.io.tmpdir") ;
sName = sName + "names_backup_" + timenow.getDateOnly().replace('.', '_') + ".nsf";

sZipName = System.getProperty("java.io.tmpdir") ;
sZipName = sZipName + "names_backup_" + timenow.getDateOnly().replace('.', '_') + ".zip";

// copy the database
System.out.println("Create a copy of the names.nsf =" + db.getServer() + "-" + db.getFilePath()) ;

this.deleteIfExist(sName) ;
Database newDb ;
newDb = db.createCopy("", sName) ;

// copy all the documents
DocumentCollection dc ;
Document d ;

dc = db.getAllDocuments() ;
d = dc.getFirstDocument() ;

while(d != null){

// No deletion stubs...
if (d.isDeleted() == false){
d.copyToDatabase(newDb) ;
}
d = dc.getNextDocument() ;
}

// Compress the stuff
System.out.println("Compressing file from " + sName + " to " + sZipName) ;

// delete the files if exist
this.deleteIfExist(sZipName) ;

// compress the database
this.doZipExt(sName,sZipName) ;

// create a document and attach the backup
System.out.println("Attach everything to the archive document") ;
Document doc ;
doc = curDb.createDocument() ;

doc.replaceItemValue("backupName", "Sauvegarde names.nsf of " + db.getServer() + " - " + timenow.getDateOnly()) ;
doc.replaceItemValue("Subject", doc.getItemValueString("backupName")) ;
doc.replaceItemValue("backupDate", timenow) ;
doc.replaceItemValue("agentName", agentContext.getCurrentAgent().getName()) ;
doc.replaceItemValue("Form", "backup") ;

lotus.domino.RichTextItem rt = doc.createRichTextItem("Body") ;
rt.embedObject(EmbeddedObject.EMBED_ATTACHMENT,null, sZipName, "archive") ;

// Save the document
doc.save(true, true);

// delete the database
this.deleteIfExist(sName) ;
this.deleteIfExist(sZipName) ;
System.out.println("Done");

} catch(Exception e) {
e.printStackTrace();
}
}

public void deleteIfExist(String sName){
File f = new File(sName) ;
if (f.exists()) {
System.out.println("File : " + sName + " deleted");
f.delete() ;
}

}

public void doZipExt(String sFileName, String sZipFileName) throws IOException{
System.out.println("Creating zip file :" + sZipFileName) ;

File sourceFile = new File(sFileName) ;

// Create our zip file
ZipOutputStream zipfile = new ZipOutputStream(new FileOutputStream(sZipFileName));
FileInputStream filetozip = new FileInputStream(sFileName);
zipfile.putNextEntry(new ZipEntry(sourceFile.getName()));

// Loop through the contents writing it to the zip file output stream.
int len;
byte[] buffer = new byte[1024];

while ((len = filetozip.read(buffer)) > 0) {
zipfile.write(buffer,0,len);
}

// Close the entry and the current file.
zipfile.closeEntry();
filetozip.close();
// When done, close the zip file.
zipfile.close();
}

}

Generating an excelsheet with POI…

It’s very easy for an lotusscript developer to generate an excel sheet with some OLE… but when the code should be run on the server it’s getting bad.

My last entry was about to pass a vector to the java API from lotusscript. Here some background to create the excel sheet with POI.

Of course you should customize this code to fit your solution. If it run on the server with a lot of instances, remember to do some stress tests to avoid some servers issues.

Greetings…

First get – POI Export with poi 3.2

http://www.criverapr.com/2008/10/using-poi-to-export-lotus-notes-data-to.html

Second – ls2j and dynamic array
http://www-10.lotus.com/ldd/nd6forum.nsf/c21908baf7e06eb085256a39006eae9f/45bc5292d67167508525706500525a52?OpenDocument

Dynamic arrays in lotusscript with ls2j

Last week, I was facing a tricky issue. I wanted to use some java to create an excel sheet. The POI java API requires a java vector. I created a dynamic array in lotusscript (to adjust the numbers of rows of my excel sheet) but when I called the POI API I’ve got an ugly error. The trick is to declare a variant, then affect the dynamic array to the variant and pass it to the JAVA API. So easy !

http://www-10.lotus.com/ldd/nd6forum.nsf/c21908baf7e06eb085256a39006eae9f/45bc5292d67167508525706500525a52?OpenDocument

Lotus notes FT-Search, looking for non present field…

I wanted to do a lotusscript FT-search of the non existence of a field in documents… let’s begin some head aches… hopefully with google I found quickly a solution.

NOT [fieldname] is present

Thanks to Alan… here the full post :

http://www.alanlepofsky.net/alepofsky/alanblog.nsf/dx/searching-for-blank-fields?opendocument&comments

Lotusscript geo-loc part II

* Remember my last post… opening the brower for the user with the map of the place diplayed in the notes document… I’m doing it better.
* You need to know that the in Switzerland there is a special way to record localisation. So you need some code to convert it in GPS to use them in Google.
* Here some links for you background :

‘ Some backgroup doc                 :        http://www.swisstopo.admin.ch/internet/swisstopo/fr/home/topics/survey.html
‘ Online source                        :         http://www.swisstopo.admin.ch/internet/swisstopo/fr/home/products/software/products/skripts.html
‘ Java project to convert         :         http://www.i3s.unice.fr/~johan/gps/
‘ Online convertor                         :         http://topo.epfl.ch/misc/transcoco/transcoco_compute.php
* Here the GoogleMapStreetUrl version 2.0

Class GoogleMapStreetUrl

 Private sUrl            As String
 Private sStreet         As String
 Private sCity                 As String

 Private sZoom                As String

 Private apiUrl                List As String

 Private x                        As Double
 Private y                        As Double

 Private lat                As Double
 Private lng                As Double

 Private sGeoCode        As String

 Sub new()
 sZoom = 13

 apiUrl("STREET") = "http://maps.google.com/maps?f=q&source=s_q&hl=fr&geocode=&q=%LOC%,%STREET%"
 apiUrl("GEOCODE") = "http://maps.google.com/maps?f=q&source=s_q&hl=fr&geocode=&q=&sll=%1&z=%2&saddr=%3"

 'sUrl = "http://maps.google.com/maps?f=q&source=s_q&hl=fr&geocode=&q=%LOC%,%STREET%"
 End Sub

 Public Function setZoom(sZ As Integer) As Integer
 sZoom = Cstr(sZ)
 End Function

 Public Function setStreet(psStreet As String) As Integer
 sUrl = apiUrl("STREET")
 sStreet = Replace(psStreet, " ", "+")
 End Function

 Public Function setCity(psCity As String) As Integer
 sUrl = apiUrl("STREET")
 sCity = Replace(psCity, " ", "+")
 End Function

 Public Function getUrl() As String
 Dim sNewUrl As String
 sNewUrl = Replace(sUrl, "%LOC%",sCity)
 sNewUrl = Replace(sNewUrl, "%STREET%",sStreet)
 sNewUrl = Replace(sNewUrl, "%1", sGeoCode)
 sNewUrl = Replace(sNewUrl, "%3", sGeoCode)
 sNewUrl = Replace(sNewUrl, "%2", sZoom)
 getUrl = sNewUrl
 End Function

 Public Function setLatLong(lng As Double, lat As Double) As Integer
 sUrl =apiUrl("GEOCODE")
 sGeoCode = Cstr(lat) + "," + Cstr(lng)
 End Function

 Public Function setCH1903_YX(y As Double, x As Double) As Integer
 sUrl =apiUrl("GEOCODE")
 Dim g As New CH1903toWGS84
 sGeoCode = g.getLatLong(y, x)
 End Function

 Public Function setLat(lat As Double) As Integer
 sUrl =apiUrl("GEOCODE")

 End Function

 Public Function open() As String
 Dim url As String
 url = Me.getUrl()
 Call ShellExecute ( GetActiveWindow(), "open", url, "", "", WIN_NORMAL)
 End Function

End Class

Public Class CH1903toWGS84

 '
 ' Class to convert CH1903 to WGS84
 ' Some backgroup doc                 :        http://www.swisstopo.admin.ch/internet/swisstopo/fr/home/topics/survey.html
 ' Online source                        :         http://www.swisstopo.admin.ch/internet/swisstopo/fr/home/products/software/products/skripts.html
 ' Java project to convert         :         http://www.i3s.unice.fr/~johan/gps/
 ' Online convertor                         :         http://topo.epfl.ch/misc/transcoco/transcoco_compute.php

 ' one sample
 '        CH1903        Y        537546
 '        CH1903        X        151681
 '
 ' --> gives
 '        WGS84        longitude        6.625644
 '         WGS84        latitude        46.514783

 Public Function getLatLong(y As Double, x As Double) As String
 Dim sTxt As String
 sTxt = Cstr(Me.CHtoWGSlat(y,x))
 sTxt = sTxt + "," + Cstr(Me.CHtoWGSlng(y,x))
 getLatLong = sTxt
 End Function

 Public Function CHtoWGSlat(y As Double, x As Double) As Double

 Dim  y_aux         As Double
 Dim  x_aux         As Double
 Dim         lat                 As Double

 y_aux = (y - 600000)/1000000
 x_aux = (x - 200000)/1000000

 lat = 16.9023892 +  3.238272 * x_aux -  0.270978 * (y_aux ^ 2) -  0.002528 * (x_aux ^ 2)        -  0.0447   * (y_aux ^ 2) * x_aux        -  0.0140   * (x_aux ^ 3)

 lat = lat * 100/36

 CHtoWGSlat = lat
 End Function

 Public Function CHtoWGSlng(y As Double, x As Double)  As Double
 Dim  y_aux         As Double
 Dim  x_aux         As Double
 Dim         lng                 As Double

 y_aux = (y - 600000)/1000000
 x_aux = (x - 200000)/1000000

 lng = 2.6779094 + 4.728982 * y_aux         + 0.791484 * y_aux * x_aux        + 0.1306   * y_aux * (x_aux ^ 2)
 lng = lng * 100/36
 CHtoWGSlng = lng
 End Function

End Class

My iPad and me !

Already one month that I’ve got my iPad… and I don’t know how I have lived before it. I’m a

computer professional and I’m not afraid of a computer. So when Steve announced the iPad I felt a
little bit frustrated because I was wondering what will be the use of this big iPhone.

This tool is very cool and I want it to surf the web.

The cons

1. Don’t use it for writing

I’m a french speaker. So when you want to do special french characters it will takes you hour to
write something. It ‘s adapted to write short text and commands. It’s a little bit hard to
navigate in a big text in an edition mode.

2. You need a computer

Without a computer, you could do fast nothing. At the first start up iPad claims to be connected. One
big part of the iPad miracle is all the pre-processing done on your computer.
I tried to do some RDP… it not very conftable. You need a computer to upgrade the system

3. Wellcome in the Apple world

You are in the club… but hard to go out the fence. Everything is locked down by default. You
can unlock it but it’s your responsability.

4. Apple store… give me you money.

The apple store is a cash machine… for apple. You’re sollicited to pay for everything on the
iPad and sometimes it’s not for the goods. Apple has succedded in the micro-payment strategy.
Nothing is very

5. Keep it synch

The synch process it a little bit slow (I’m working under windows)

The pros…

1. It’s amazing to surf with your finger

It’s a pretty cool experience to go and conquer the web with your finger. The zoom is so easy and
it’s too cool to click anywhere.

2. Just open it and your surfing

Before that when you need to go on the we if you’re not on your computer it takes you time to do
it. Now just click, the iPad is ready and go… no wait !

3. Application on one click !

The same for the application. No wait… everything is fast… it

4. And it goes on, goes on…

The battery is very cool and it is serious… you can “work” for 10 hours. I understand why there
is no USB and Webcam and other battery consumer… the iPad is optimized and you can feel it !

5. The perfect tool for non-geek…

My little boy and my wife are what we can call technically, non-geek person… and you know what,
they feel confortable with the iPad. This is the perfect demonstration.

6. And too cool the appStore…

Here it’s like when going to the … you can find everything but nothing. A lot of stuff but

Conclusion… I think that Apple has done a kind of miracle. When I’m facing my computer screen
which is very big… I often wonder to do it with my fingers… The iPad has open a new area in
the computer world. Now the computer won’t be personnal again.

Me and my iPad

Already one month that I’ve got my iPad… and I don’t know how I have lived before it. I’m a
computer professional and I’m not afraid of a computer. So when Steve announced the iPad I felt a
little bit frustrated because I was wondering what will be the use of this big iPhone.

This tool is very cool and I want it to surf the web.

The cons

1. Don’t use it for writing

I’m a french speaker. So when you want to do special french characters it will takes you hour to
write something. It ‘s adapted to write short text and commands. It’s a little bit hard to
navigate in a big text in an edition mode.

2. You need a computer

Without a computer, you could do fast nothing. At the first start up iPad claims to be connected. One
big part of the iPad miracle is all the pre-processing done on your computer.
I tried to do some RDP… it not very conftable. You need a computer to upgrade the system

3. Wellcome in the Apple world

You are in the club… but hard to go out the fence. Everything is locked down by default. You
can unlock it but it’s your responsability.

4. Apple store… give me you money.

The apple store is a cash machine… for apple. You’re sollicited to pay for everything on the
iPad and sometimes it’s not for the goods. Apple has succedded in the micro-payment strategy.
Nothing is very

5. Keep it synch

The synch process it a little bit slow (I’m working under windows)

The pros…

1. It’s amazing to surf with your finger

It’s a pretty cool experience to go and conquer the web with your finger. The zoom is so easy and
it’s too cool to click anywhere.

2. Just open it and your surfing

Before that when you need to go on the we if you’re not on your computer it takes you time to do
it. Now just click, the iPad is ready and go… no wait !

3. Application on one click !

The same for the application. No wait… everything is fast… it

4. And it goes on, goes on…

The battery is very cool and it is serious… you can “work” for 10 hours. I understand why there
is no USB and Webcam and other battery consumer… the iPad is optimized and you can feel it !

5. The perfect tool for non-geek…

My little boy and my wife are what we can call technically, non-geek person… and you know what,
they feel confortable with the iPad. This is the perfect demonstration.

6. And too cool the appStore…

Here it’s like when going to the … you can find everything but nothing. A lot of stuff but

Conclusion… I think that Apple has done a kind of miracle. When I’m facing my computer screen
which is very big… I often wonder to do it with my fingers… The iPad has open a new area in
the computer world. Now the computer won’t be personnal again