Sunday, April 6, 2008

Intralink Scripting: To Be or Not To Be, How to Best Answer the Question

Checking for Object Existence with CSPIObjectInfo and CSPIVObjectInfo

When processing Intralink objects in Java from a non-Intralink source, such as a text file or database, the typical process is to use Locate to search for the item to ensure that it exists. While this is usually sufficient, there are times when the overheard of using Locate is undesirable. The good news is that there is an alternative using the ObjectInfo derived classes.

Continuing on my discussion of ObjectInfo classes, I'll discuss how to apply these classes to determine whether a PI (i.e. abc.prt) or a PIV (i.e. abc.prt/main/A/0) exists.

If we're checking for "my_assembly.asm", we'd follow these steps. First declare an object of type CSPIObjectInfo. Then attempt to create the object using the createByKey() method of the ObjectInfo class, providing the type as ObjectInfo.tPI and name in the variable piName.

Although I have it captured in a try{} block, it won't throw an exception if the PI doesn't exist, instead as you may have guessed from the code, in merely returns null. If the object is null, it doesn't exist.

String piName = "my_assembly.asm";
CSObjectInfo pi_oi = null;

try {
pi_oi = (CSObjectInfo)ObjectInfo.createByKey( ObjectInfo.tPI, piName );
if (pi_oi == null) {
System.out.println( "pi_oi is null" );
}
else {
System.out.println( "pi_oi getName(): " + pi_oi.getName() );
}
}
catch (Exception e) {
System.out.println( "CSObjectInfo createByKey exception: " + e.getMessage() );
}

To determine is a PIV exists (i.e. "my_assembly.asm", main branch, revision 1, version 0), ObjectInfo.tPIV is the corresponding type to use also with the createByKey() method of the ObjectInfo class. It also has the same behavior, returning null is the PIV does not exist.
String pivName = "my_assembly.asm/main/1/0";
CSPIVObjectInfo piv_oi = null;

try {
piv_oi = (CSPIVObjectInfo)ObjectInfo.createByKey( ObjectInfo.tPIV, pivName );
if (piv_oi == null) {
System.out.println( "piv_oi is null" );
}
else {
System.out.println( "piv_oi getName(): " + piv_oi.getName() );
}
}
catch (Exception e) {
System.out.println( "CSPIVObjectInfo createByKey exception: " + e.getMessage() );
}

The major weakness with this approach for PIV's is that you have to know of it's name, revision, and version ahead of time. If you want to know what the latest revision/version is, the Locate approach will give you both confirmation of existence and knowledge of the revision/version at the same time.

You'll have to choose when it makes sense to use it, but it remains a very powerful technique nonetheless.

No comments: