DFC Code to Automatically Build Folder Paths

If there is one piece of code I have rewritten more times than the login routine, it is that which will create nested folders in the repository given a fully qualified path.  I have implemented this code in WDK, TBOs, SBOs, Captiva, jobs, and migration code.  For example, suppose you have a process that ingests news feeds and stores them according to wire service, year, month, and day.  Your storage structure in the Docbase might look like this:

  • /News/wire service/AP/2012/Jan/01
  • /News/wire service/AP/2012/Jan/02 …
  • /News/wire service/Reuters/2012/Feb/14 …

Get the idea?  Each service’s stories are stored in a unique folder according to the day they were recieved.

If an automated process is recieving, classifying, and storing these stories it would need to be able to create new folders based upon the date or wire service.  Usually this information is readily available from the process ingesting the content.  So, it would be nice to simply construct the desired storage path as a String, create the necessary elements of the path, and link the newly ingested content to the proper folder.  The code for the method dmCreateStoragePath(IDfSession session, String path) below does just that.

    public IDfFolder dmCreateStoragePath(IDfSession session, String path) throws Exception {
        IDfFolder folder = null;

        // first see if the folder already exists
        folder = (IDfFolder) session.getObjectByQualification("dm_folder where any r_folder_path='" + path + "'");

        // if not build it
        if (null == folder) {
            // split path into separate folders
            String[] dirs = path.split("/");

            // loop through path folders and build
            String dm_path = "";
            for (int i=0; i<dirs.length; i++) {

                if (dirs[i].length() > 0) {

                    // build up path
                    dm_path = dm_path + "/" + dirs[i];

                    // see if this path exists
                    IDfFolder testFolder = (IDfFolder) session.getObjectByQualification("dm_folder where any r_folder_path='" + dm_path + "'");
                    if (null == testFolder) {

                        // check if a cabinet need to be made
                        if (dm_path.equalsIgnoreCase("/" + dirs[i])) {
                            IDfFolder cab = (IDfFolder) session.newObject("dm_cabinet");
                            cab.setObjectName(dirs[i]);
                            cab.save();
                         // else make a folder
                         } else {
                             folder = (IDfFolder) session.newObject("dm_folder");
                             folder.setObjectName(dirs[i]);

                             // link it to parent
                             String parent_path = "";
                             for (int j=0; j < i; j++) {
                                if (dirs[j].length() > 0) {
                                    parent_path = parent_path + "/" + dirs[j];
                                }
                             }
                         folder.link(parent_path);
                         folder.save();
                        }
                    }
                 }
            }
        }
        return folder;
    }

To use this code in your ingestion program, you can simply do this:

  IDfSysObject newStory = (IDfSysObject) session.newObject("dm_document");
  // do some stuff with newStory
  IDfFolder newFolder = dmCreateStoragePath(session, "/News/wire service/Fox News/2012/May/07");
  newStory.link(newFolder.getObjectId());
  newStory.save();

Where the path, “/News/wire service/Fox News/2012/May/07” is build dynamically from metadata. Any or none of the components of this path may exist. The dmCreateStoragePath() method builds what is necessary. Note that this code does not make any accomodation for ACLs placed on the created cabinet or folders, but could easily be modified to do so.

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.

6 Responses to DFC Code to Automatically Build Folder Paths

  1. Nice, but I thought you could do this using only xcp activity templates now.? Though, I still believe it is useful to have code like this too. Also, the CS student in me wants very badly to make this recursive.

    Like

    • Scott says:

      Yep, if you are using xCP you can create nested folders using the folder activity template. D2 will also do it in the creation matrix configuration. I did this once as a recursive method too, but I can’t find that code (I think it might have been in VB). If someone wants to submit a recursive version I will certainly post it!

      Like

  2. kris-emc says:

    this code will not compile, can’t believe nobody tried it.

    Like

    • Scott says:

      Huh. You are right; sorry about that. I seem to have been a little over zealous with the “}”s. I think I have it fixed. Weird, I use this code all the time.

      Like

  3. kris-emc says:

    Yup too much “}”, the code works fine. This is very useful and easier to read compare to the “recursive folder” sample from EMC network. I’ve created a C# version and will be posting it when I get the chance. Of course I will reference your blog.

    Like

  4. Pingback: “The Basics” Series | dm_misc: Miscellaneous Documentum 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: