Saturday, August 29, 2009

Retrieving document using DKDDO, DKLobICM and PID string

Uggh this might be cryptic and useless for some out there. But if you use IBM CM 8.4 with Type 4 JDBC driver, you may find this useful if you know what to look for. Certainly one of the more confusing IBM products. Even the name and variation of the Content Manager products make your head spin. If you have to work on this I pity you... nuff said...

Here's a snippet of codes using DKDDO and DKLobICM to retrieve a document using PID string from content manager:

public void retrieveDoc(String pidString) throws Exception {
  if(pidString == null || pidString.trim().length() == 0) {
    throw new Exception("PID string is null - nothing to retrieve.");
  }

  DKDatastoreICM dsICM = getDataStore();

  DKDDO ddo = dsICM.createDDOFromPID(pidString);
        
  DKRetrieveOptionsICM retrOpts = DKRetrieveOptionsICM.createInstance(dsICM);
  retrOpts.baseAttributes(true);  // Get Base Attributes
  retrOpts.partsList(true);       // Get the parts
  retrOpts.resourceContent(true); // Get the document content

  ddo.retrieve(retrOpts.dkNVPair());    // Retrieve from content manager

  // Get the Dynamic Data Objects (DDO) parts 
  DKParts parts = null;
  short dataID = ddo.dataId(DKConstant.DK_CM_NAMESPACE_ATTR, DKConstant.DK_CM_DKPARTS);
  if (dataID != 0) {    // 
    parts = (DKParts)ddo.getData(dataID);
  } else { // The namespace or data-item name (parts) is not found
    throw new Exception("The document parts not found for pid: " + pidString);
  }

  // Get the base ICM content from the parts.
  DKLobICM icmBase = null;
  if(parts != null) {
    // Iterate through the parts.
    // Whoever develops the IBM java libraries isn't a Java guy, probably a C++ guy
    // All Java developers know Class names started with a capital letter.
    dkIterator iter = (dkIterator) parts.createIterator();
    while (iter.more()) {
      DKDDO d = (DKDDO)iter.next();
      if(d instanceof DKLobICM && ICMBASE_OBJECT_TYPE_STR.equalsIgnoreCase(d.getObjectType())) {
        System.out.println("Found ICMBASE object with mimeType: " + ((DKLobICM)d).getMimeType());
        icmBase = (DKLobICM) d;
      }
    }
  }

  // Did we find the ICMBASE object? If yes, and it has content, write it to an external file.
  if(icmBase != null) {
    byte content[] = icmBase.getContent();
    if (content != null) {
      OutputStream os = new FileOutputStream(new File("testdoc2.tiff"));
      os.write(content);
    }
  } else {
    System.out.println("Oops... can't find the document with pid: " + pidString);
  }
} 
Off course the entire code set is more complicated. This is just snippet after all.

1 comment:

  1. Thanks for the snippet!

    You were far more generous with your assumption that the Java libraries weren't developed by a "Java guy" because I have been ranting about out this is the most terrible API I have ever seen.. regardless of language.

    ReplyDelete