EMC Developer Network

Determine the Repository Format (dm_format) from a MIME Type

November 2005

Software Environment

FeatureTested on
Operating SystemWindows 2000 Server SP4
CompilerSun JDK 1.4.2_08
RuntimeSun JRE 1.4.2_08
DFC5.3 SP1
Content Server5.3

Abstract

This snippet attempts to determine the repository format (dm_format) from an Internet MIME type

Snippet


/*
 * Created on Nov 7, 2005
 *
 * EMC Documentum Developer Program 2005
 */
package com.documentum.devprog.snippets;

import com.documentum.fc.common.DfException;
import com.documentum.fc.common.IDfLoginInfo;

import com.documentum.fc.client.DfQuery;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfCollection;
import com.documentum.fc.client.IDfQuery;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;

public class MimeTypeToFormat
{
    public static void main(String[] args)
    {
        String username = "dmadmin";
        String password = "dmadmin";
        String repoName = "devprog";
        String mimeType = "vnd.ms-project";
        IDfSessionManager sessMgr = null;
        IDfSession sess = null;
        try
        {
            sessMgr = createSessionManager();
            addIdentity(sessMgr, username, password, repoName);
            sess = sessMgr.getSession(repoName);
            String format = getRepsitoryFormatFromMimeType(sess, mimeType);
            System.out.println("MIME Type: " + mimeType);
            System.out.println("Repository Format: " + format);

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if ((sessMgr != null) && (sess != null))
            {
                sessMgr.release(sess);
            }
        }

    }

    /**
     * Reverse maps a mime-type to a repository format.
     * 
     * @param session
     *            The repository session
     * @param mimeType
     *            The mime-type
     * @return The corresponding repository format. If multiple formats are
     *         found <br>
     *         the first one is returned by default. A <code>null</code> is
     *         <br>
     *         returned if no format can be found.
     */
    private static String getRepsitoryFormatFromMimeType(IDfSession session,
            String mimeType) throws DfException
    {

        mimeType = mimeType.trim();
        int indexOfSemiColon = mimeType.indexOf(";");
        if (indexOfSemiColon != -1)
        {
            mimeType = mimeType.substring(0, indexOfSemiColon);
        }
        else
        {
            int indexOfSpace = mimeType.indexOf(" ");
            if (indexOfSpace != -1)
            {
                mimeType = mimeType.substring(0, indexOfSpace);
            }
        }

        StringBuffer bufFormatQuery = new StringBuffer(32);
        bufFormatQuery
                .append("select name from dm_format where mime_type like '%");
        bufFormatQuery.append(mimeType).append("%'");
        IDfQuery formatQuery = new DfQuery();
        formatQuery.setDQL(bufFormatQuery.toString());
        IDfCollection formats = formatQuery.execute(session,
                IDfQuery.READ_QUERY);
        try
        {
            String contentType = null;
            if (formats.next())
            {
                contentType = formats.getString("name");
                return contentType;
            }
            else
            {
                return null;
            }
        }
        finally
        {
            if (formats != null)
            {
                formats.close();
            }
        }

    }

    /**
     * Creates a new session manager instance. The session manager does not have
     * any identities associated with it.
     * 
     * @return a new session manager object.
     * @throws DfException
     */
    private static IDfSessionManager createSessionManager() throws DfException
    {
        IDfClientX clientX = new DfClientX();
        IDfClient localClient = clientX.getLocalClient();
        IDfSessionManager sessMgr = localClient.newSessionManager();
        return sessMgr;
    }

    /**
     * Adds a new identity to the session manager.
     * 
     */
    private static void addIdentity(IDfSessionManager sm, String username,
            String password, String repoName) throws DfException
    {
        IDfClientX clientX = new DfClientX();

        IDfLoginInfo li = clientX.getLoginInfo();
        li.setUser(username);
        li.setPassword(password);

        // check if session manager already has an identity.
        // if yes, remove it.
        if (sm.hasIdentity(repoName))
        {
            sm.clearIdentity(repoName);
        }

        sm.setIdentity(repoName, li);
    }

}

	

Using the Code

The above code snippet comes with a main method and is thus a standalone program. To use it, copy-paste the entire code into a new Java class created in your IDE. Adjust the package and class name of the pasted code according to your preference. Change the username,password, repository name and any other necessary variables. Compile and run the code. For details on setting up your IDE for DFC development, refer to the article Setting up a DFC Development Environment