Checksum Aspect – Part 3

As I mentioned in my last post, testing the checksum Aspect didn’t work out exactly as I had expected.  I had naively hoped to just install the Aspect and Webtop would know what to do with it — like TBOs.  Well, I was somewhat disappointed to learn that Aspects and Webtop don’t work that way.   As it turned out, Aspects cannot be applied in Webtop, nor do Aspect attributes automatically display in Webtop.  So, to test the checksum Aspect, I had to create a test harness.  The test harness wasn’t anything special, but I do want to review the portion of the code that conducts the tests, because it demonstrates several nuances with using Aspects in DFC code.

Here are the pertinent parts of the test code.

// #1 - get object to test
IDfSysObject sObj = (IDfSysObject) session.getObjectByQualification("dm_document where object_name = 'test_obj'");
if (sObj == null) {
	System.out.println("Could not find object with name 'test_obj'");
	return;
}
System.out.println("Got object " + sObj.getObjectName() + " (" + sObj.getObjectId().toString() + ")");

// #2 - check if aspect already attached
IDfList aspectList = ((IDfAspects) sObj).getAspects();
for (int i=0; i < aspectList.getCount(); i++) {
	System.out.println("found attached aspect: " + ((String) aspectList.get(i)));

	if (((String) aspectList.get(i)).equalsIgnoreCase("checksum_aspect")) {
		((IDfAspects) sObj).detachAspect("checksum_aspect", null);
		sObj.save();
		sObj = (IDfSysObject) session.getObject(sObj.getObjectId());
		System.out.println("Detached aspect " + aspect );
	}
}

// #3 - attach aspect
((IDfAspects) sObj).attachAspect("checksum_aspect", null);
sObj.save();
sObj = (IDfSysObject) session.getObject(sObj.getObjectId());
System.out.println("\nAttached aspect checksum_aspect");

// #4 - get initial values
System.out.println("\nInitial Values");
System.out.println(checksum_value + " = " + sObj.getString("checksum_aspect.checksum"));
System.out.println(checksum_date + " = " + sObj.getString("checksum_aspect.checksum_date"));
System.out.println(checksum_algorithm + " = " + sObj.getString("checksum_aspect.checksum_algorithm"));

// #5 - set checksum
System.out.println("\nCalculate checksum with default algorithm");
((IChecksumAspect) sObj).updateChecksum(null);
sObj.save();
System.out.println(checksum_value + " = " + sObj.getString("checksum_aspect.checksum"));
System.out.println(checksum_date + " = " + sObj.getString("checksum_aspect.checksum_date"));
System.out.println(checksum_algorithm + " = " + sObj.getString("checksum_aspect.checksum_algorithm"));

// #6 - various calls to the Aspect methods
System.out.println("\nStraight calls to aspect methods");
System.out.println("checksum is: " + ((IChecksumAspect) sObj).calculateChecksum(null));

// set checksum again with different algorithm
System.out.println("\nCalculate checksum with MD5 algorithm");
((IChecksumAspect) sObj).updateChecksum("MD5");
sObj.save();
System.out.println(checksum_value + " = " + ((IChecksumAspect) sObj).getChecksum());
System.out.println(checksum_date + " = " + ((IChecksumAspect) sObj).getChecksumDate().asString(DfTime.DF_TIME_PATTERN_DEFAULT));
System.out.println(checksum_algorithm + " = " + ((IChecksumAspect) sObj).getChecksumAlgorithm());

// validate checksum
System.out.println("\nValidate checksum");
System.out.println(checksum_value + " = " + sObj.getString("checksum_aspect.checksum"));
System.out.println("checksum = " + ((IChecksumAspect) sObj).calculateChecksum(sObj.getString("checksum_aspect.checksum_algorithm")));
if (((IChecksumAspect) sObj).validateChecksum()) {
	System.out.println("Checksum is valid");
} else {
	System.out.println("Checksum is NOT valid");
}

// #7 - change content and validate checksum
String filename = sObj.getFile("c:/temp/" + sObj.getObjectName() + "." + sObj.getFormat().getDOSExtension());
FileWriter fw = new FileWriter(filename, true);
fw.append("0123456789abcdef");
fw.flush();
fw.close();
sObj.setContentType(sObj.getFormat().getName());
sObj.setFile(filename);
sObj.save();
System.out.println("\nModified content...");

// re-validate checksum
System.out.println("\nRe-validate checksum");
System.out.println(checksum_value + " = " + sObj.getString("checksum_aspect.checksum"));
System.out.println("checksum = " + ((IChecksumAspect) sObj).calculateChecksum(sObj.getString("checksum_aspect.checksum_algorithm")));
if (((IChecksumAspect) sObj).validateChecksum()) {
	System.out.println("Checksum is valid");
} else {
	System.out.println("Checksum is NOT valid");
}

// #8 update checksum and revalidate
System.out.println("\nRe-calculate checksum with default algorithm");
((IChecksumAspect) sObj).updateChecksum(null);
sObj.save();
System.out.println(checksum_value + " = " + sObj.getString("checksum_aspect.checksum"));
System.out.println(checksum_date + " = " + sObj.getString("checksum_aspect.checksum_date"));
System.out.println(checksum_algorithm + " = " + sObj.getString("checksum_aspect.checksum_algorithm"));

// re-validate
System.out.println("\nRe-validate checksum");
System.out.println(checksum_value + " = " + sObj.getString("checksum_aspect.checksum"));
System.out.println("checksum = " + ((IChecksumAspect) sObj).calculateChecksum(sObj.getString("checksum_aspect.checksum_algorithm")));
if (((IChecksumAspect) sObj).validateChecksum()) {
	System.out.println("Checksum is valid");
} else {
	System.out.println("Checksum is NOT valid");
}

Notes:

  1. I created a test object called ‘test_obj’ and hard coded the name here. Change this as necessary.
  2. Because I ran this test multiple times on the same object, the Aspect could already be attached.  So, here I am checking if the Aspect is already attached and detaching it if it is.  Trying to attach an Aspect that is already attached will cause an Exception.
  3. Now that I know the Aspect is not attached, I attach it.
  4. Get the initial values from the Aspect attributes.  These will be empty since the Aspect was just attached.  Note how the attributes are addressed.
  5. Update the checksum and print the results.
  6. Make some calls directly to the Aspect methods.  Note how the IDfSysObject must be cast to the IChecksumAspect interface to access the Aspect methods.
  7. Append some gibberish to the end of the content file and validate the checksum.  This should fail because the content has changed sense the checksum was calculated.
  8. Update the checksum and re-validate it.  This time the validation should pass.

Here is some output from my run of the test harness.

Got object test_obj (0900000180001d05)
found attached aspect: checksum_aspect
Detached aspect checksum_aspect
Attached aspect checksum_aspect
Initial Values
checksum_aspect.checksum =
checksum_aspect.checksum_date = nulldate
checksum_aspect.checksum_algorithm =
Calculate checksum with default algorithm
checksum_aspect.checksum = 43e43e62594fd3eab975eeb01fb9fc264a012bb690c06913b0c43b52ce404c32
checksum_aspect.checksum_date = 1/27/2011 2:28:02 PM
checksum_aspect.checksum_algorithm = SHA-256
Straight calls to aspect methods
checksum is: 43e43e62594fd3eab975eeb01fb9fc264a012bb690c06913b0c43b52ce404c32
Calculate checksum with MD5 algorithm
checksum_aspect.checksum = 5e9b8d83aa6171151d1cfd640d1cbb05
checksum_aspect.checksum_date = 1/27/2011 2:28:02 PM
checksum_aspect.checksum_algorithm = MD5
Validate checksum
checksum_aspect.checksum = 5e9b8d83aa6171151d1cfd640d1cbb05
checksum = 5e9b8d83aa6171151d1cfd640d1cbb05
Checksum is valid
Modified content...
Re-validate checksum
checksum_aspect.checksum = 5e9b8d83aa6171151d1cfd640d1cbb05
checksum = 269e905edad7e496f93db4752822543c
Checksum is NOT valid
Re-calculate checksum with default algorithm
checksum_aspect.checksum = 860ec8c8d3f211abde1992d14341224a4c422d030902c6196077b47da7020861
checksum_aspect.checksum_date = 1/27/2011 2:28:03 PM
checksum_aspect.checksum_algorithm = SHA-256
Re-validate checksum
checksum_aspect.checksum = 860ec8c8d3f211abde1992d14341224a4c422d030902c6196077b47da7020861
checksum = 860ec8c8d3f211abde1992d14341224a4c422d030902c6196077b47da7020861
Checksum is valid

The test harness code is included in the Composer project here.

In my next post I will explain how I finally exposed the checksum capability in Webtop.

Advertisements

About Scott
I have been implementing Documentum solutions since 1997. In 2005, I published a book about developing Documentum solutions for the Documentum Desktop Client (ISBN 0595339689). In 2010, I began this blog as a record of interesting and (hopefully) helpful bits of information related to Documentum, and as a creative outlet.

2 Responses to Checksum Aspect – Part 3

  1. Pingback: Checksum Aspect – Part 0 « dm_misc: Miscellaneous Documentum Tidbits and Information

  2. Pingback: Checksum Aspect – Part 2 « dm_misc: Miscellaneous Documentum Tidbits and Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: