/* bcwti
 *
 * Copyright (c) 2010 Parametric Technology Corporation (PTC). All Rights Reserved.
 *
 * This software is the confidential and proprietary information of PTC
 * and is subject to the terms of a software license agreement. You shall
 * not disclose such confidential information and shall use it only in accordance
 * with the terms of the license agreement.
 *
 * ecwti
 */

package com.ptc.windchill.esi.examples;

import com.ptc.windchill.esi.esidoc.PartDocInfo;

import java.util.HashMap;

import wt.doc.Document;

import wt.fc.ObjectIdentifier;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;

import wt.httpgw.GatewayServletHelper;
import wt.httpgw.URLFactory;

import wt.util.WTException;

public class Example5MyPartDocInfo
   extends PartDocInfo
{
   public String getDocumentURL()
   {
      String url = null;

      try
      {
         // Generate a URL to download the document content.

         URLFactory urlFactory = new URLFactory();
         HashMap map = new HashMap();

         map.put("action", "DownloadContent");
         map.put("oid", getDocumentOID());

         url = GatewayServletHelper.buildAuthenticatedHREF(
            urlFactory, "wt.enterprise.URLProcessor", "URLTemplateAction", map);
      }
      catch (WTException e)
      {
         // Use the default logic which generates a URL to display
         //    the document properties.

         url = super.getDocumentURL();
      }

      return url;
   }

   private String getDocumentOID()
      throws WTException
   {
      Persistable p = (Persistable)getDocument();
      ObjectIdentifier oid = PersistenceHelper.getObjectIdentifier(p);

      return oid.getStringValue();
   }
}

