Monday, June 23, 2008

How to set a document to its next major version using DFC

The code below overrides DfDocument.doCheckin in my TBO (Type Based Object). The method sets the document to its next major version when it is promoted to lifecycle state Final.

protected void doPromote(String state, boolean override, boolean fTestOnly, Object[] extArgs ) throws DfException {

super.doPromote(state, override, fTestOnly, extArgs);


if (this.getCurrentStateName().equals("Final")){
if(this.isCheckedOut() == true){
throw new DfException("Cannot version if object is checked out.");
}

String newMajorVersion = this.getVersionPolicy().getNextMajorLabel();
this.mark(newMajorVersion);
this.save();
}
}
If the code above is not flexible enough... Below is the example from Documentum that uses operations to set the document to the next major version by overriding DfDocument.doCheckin.

public static void doCheckin( String strPath, IDfSession session ) throws DfException {
IDfClientX clientx = new DfClientX();
IDfCheckinOperation operation = clientx.getCheckinOperation();

IDfSysObject sysObj = (IDfSysObject) session.getObjectByPath( strPath );
if( sysObj == null ) {
System.out.println("Object " + strPath + " can not be found.");
return;
}

if (sysObj.isCheckedOut() == false) {
System.out.println("Object " + strPath + " is not checked out.");
return;
}

IDfCheckinNode node;
if( sysObj.isVirtualDocument() ) {
IDfVirtualDocument vDoc = sysObj.asVirtualDocument( "CURRENT", false );
node = (IDfCheckinNode)operation.add(vDoc);
} else {
node = (IDfCheckinNode)operation.add(sysObj);
}

//Other options for setCheckinVersion: VERSION_NOT_SET, NEXT_MAJOR,
// NEXT_MINOR, BRANCH_VERSION
operation.setCheckinVersion(IDfCheckinOperation.NEXT_MAJOR);
operation.execute();

}

Tuesday, June 17, 2008

Java 1.4 get node text

The Java JRE 1.4 is missing the method org.w3c.dom.Node.getTextContent().

java.lang.NoSuchMethodError: org.w3c.dom.Node.getTextContent()

This is unfortunate, but you DO NOT have to upgrade to Java JRE 1.5! Use the function below as a replacement for getTextContent().


    /**
* Recursive implementation of the method
* org.w3c.dom.Node.getTextContent which is present in JDK 1.5 but not 1.4
* @author Tobias Hinnerup
* @param node Node that you need to get the text content of
* @return
*/

private static String getTextContent(Node node) {
Node child;
String sContent = node.getNodeValue() != null ? node.getNodeValue() : "";

NodeList nodes = node.getChildNodes();
for(int i = 0; i < nodes.getLength(); i++) {
child = nodes.item(i);
sContent += child.getNodeValue() != null ? child.getNodeValue() : "";
if(nodes.item(i).getChildNodes().getLength() > 0) {
sContent += getTextContent(nodes.item(i));
}
}

return sContent;
}

Tuesday, June 3, 2008

How to store an array in a Java properties file

Below describes how to store a two-dimensional array in a properties file.

Delineate the items in the array by commas and semicolons.

The properties file would be like:


myProperty=name1,age1,job1,location1;name2,age2,job2,location2;

So each person is divided by a semicolin, but the attribute of that person by a comma. The same code works with any amount of attributes per person.

Split up the property into a String[][] variable type:
    //get array split up by the semicolin
String[] a = propFile.getProperty(propertyName).split(";");

//create the two dimensional array with correct size
String[][] array = new String[a.length][a.length];

//combine the arrays split by semicolin and comma
for(int i = 0;i < a.length;i++) {
array[i] = a[i].split(",");
}

Now you have a two dimensional array from a property file!

Below is a complete example:


Properties File
#delineated by Name,Capital,Nickname;Name,Capital,Nickname; etc....
stateInfo=California,Sacramento,The Golden State;Pennsylvania,Philadelphia,Keystone State;Texas,Austin,Lone Star State

Java Code
package myProject;

import java.io.FileInputStream;
import java.util.Properties;

public class Main {

//Key for 2nd part of array
final static int NAME = 0;
final static int CAPITAL = 1;
final static int NICKNAME = 2;

public static void main(String[] args) throws Exception {

//get properties file
Properties prop = new Properties();
prop.load(new FileInputStream("C:\\myStatesPropertiesFile.txt"));

//get two dimensional array from the properties file that has been delineated
String[][] stateInfos = fetchArrayFromPropFile("stateInfo",prop);


//below code will print out all the states, their capitals, and nicknames
for (int i = 0; i < stateInfos.length; i++) {
System.out.print("State "+ i + ":");
System.out.print("\n");
System.out.print("Name: " + stateInfos[i][NAME]);
System.out.print("\n");
System.out.print("Capital: " + stateInfos[i][CAPITAL]);
System.out.print("\n");
System.out.print("NickName: " + stateInfos[i][NICKNAME]);
System.out.print("\n");

}
}

/**
* Creates two dimensional array from delineated string in properties file
* @param propertyName name of the property as in the file
* @param propFile the instance of the Properties file that has the property
* @return two dimensional array
*/

private static String[][] fetchArrayFromPropFile(String propertyName, Properties propFile) {

//get array split up by the semicolin
String[] a = propFile.getProperty(propertyName).split(";");

//create the two dimensional array with correct size
String[][] array = new String[a.length][a.length];

//combine the arrays split by semicolin and comma
for(int i = 0;i < a.length;i++) {
array[i] = a[i].split(",");
}
return array;
}
}