// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) deadcode 
// Source File Name:   DownloadDocumentDelegate.java

package com.ptc.windchill.pdmlink.doc.server.processors;

import java.beans.PropertyVetoException;
import java.io.*;
import java.net.URL;
import java.util.*;
import wt.content.*;
import wt.doc.WTDocument;
import wt.doc.WTDocumentMaster;
import wt.fc.*;
import wt.httpgw.*;
import wt.identity.IdentityFactory;
import wt.introspection.ClassInfo;
import wt.templateutil.processor.*;
import wt.util.*;

public class DownloadDocumentDelegate extends FormTaskDelegate
    implements Serializable
{

    public DownloadDocumentDelegate()
    {
    }

    /**
     * @deprecated Method processAction is deprecated
     */

    public void processAction(ContentHTTPStream contentStream)
        throws Exception
    {
        if(VERBOSE)
            System.out.println("DEBUG:DownloadDocumentDelegate.processAction.");
        Properties formData = getFormData();
        WTDocument doc = null;
        Persistable ci = (Persistable)getContextObj();
        if(ci instanceof WTDocument)
        {
            doc = (WTDocument)getContextObj();
        } else
        {
            Object params[] = {
                ci.getClassInfo().getClassname()
            };
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "67", params);
        }
        setContextObj(doc);
        String applet_tag = getAppletTag(formData);
        formData.put("appletTag", applet_tag);
        setContextProperties(formData);
        RedirectObject redirectObj = new RedirectObject();
        redirectObj.setRedirection(getState());
        return;
        WTException wte;
        wte;
        wte.printStackTrace();
        addToResponseHeaders(wte);
        RedirectObject redirectObj = new RedirectObject();
        redirectObj.setRedirection(getState());
        break MISSING_BLOCK_LABEL_182;
        Exception exception;
        exception;
        RedirectObject redirectObj = new RedirectObject();
        redirectObj.setRedirection(getState());
        throw exception;
    }

    private String getAppletTag(Properties formData)
        throws WTException
    {
        java.util.Locale locale = LanguagePreference.getLocale(getState().getRequestObj().getProperty("cgi.http_accept_language", ""));
        String selectedDocs = formData.getProperty("selectedDocs");
        String treeDocs = formData.getProperty("treeDocs");
        if(VERBOSE)
            System.out.println("getAppletTag selectedDocs = " + selectedDocs + " treeDocs = " + treeDocs);
        int cur_pos = 0;
        int start_pos = 0;
        boolean done = false;
        int delim_length = DELIM.length();
        boolean id = true;
        Vector treeIds = new Vector();
        Vector treeOids = new Vector();
        for(; !done; start_pos = cur_pos + delim_length)
        {
            String item = null;
            cur_pos = treeDocs.indexOf(DELIM, start_pos);
            if(cur_pos == -1)
            {
                done = true;
                if(start_pos < treeDocs.length())
                    item = treeDocs.substring(start_pos);
                if(VERBOSE)
                    System.out.println("getAppletTag item = " + item);
            } else
            {
                item = treeDocs.substring(start_pos, cur_pos);
                if(VERBOSE)
                    System.out.println("getAppletTag item = " + item);
            }
            if(item == null || item.length() <= 0)
                continue;
            if(id)
                treeIds.addElement(item);
            else
                treeOids.addElement(item);
            id = !id;
        }

        Vector doc_refs = new Vector();
        Vector primaries = new Vector();
        cur_pos = 0;
        start_pos = 0;
        done = false;
        String treeNodeString = "TreeTableNode_";
        ReferenceFactory factory = new ReferenceFactory();
        do
        {
            if(done)
                break;
            String ref = null;
            cur_pos = selectedDocs.indexOf(DELIM, start_pos);
            if(cur_pos == -1)
            {
                done = true;
                if(start_pos < selectedDocs.length())
                    ref = selectedDocs.substring(start_pos);
            } else
            {
                ref = selectedDocs.substring(start_pos, cur_pos);
            }
            if(ref != null && ref.length() > 0)
            {
                String oid = null;
                if(ref.startsWith(treeNodeString))
                {
                    String tree_index = ref.substring(treeNodeString.length());
                    int vector_index = treeIds.indexOf(tree_index);
                    oid = (String)treeOids.elementAt(vector_index);
                } else
                if(ref.startsWith("VR") || ref.startsWith("OR"))
                    oid = ref;
                Persistable object = factory.getReference(oid).getObject();
                FormatContentHolder holder = null;
                if(object instanceof WTDocumentMaster)
                {
                    Object params[] = {
                        IdentityFactory.getDisplayIdentifier(object).getLocalizedMessage(locale)
                    };
                    throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "119", params);
                }
                if(object instanceof FormatContentHolder)
                {
                    holder = (FormatContentHolder)object;
                    try
                    {
                        holder = (FormatContentHolder)ContentHelper.service.getContents(holder);
                    }
                    catch(PropertyVetoException pve)
                    {
                        Object params[] = {
                            IdentityFactory.getDisplayIdentifier(holder).getLocalizedMessage(locale)
                        };
                        throw new WTException(pve, "com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "106", params);
                    }
                } else
                {
                    Object params[] = {
                        IdentityFactory.getDisplayIdentifier(object).getLocalizedMessage(locale)
                    };
                    throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "106", params);
                }
                ContentItem ci = ContentHelper.getPrimary(holder);
                if(ci != null)
                {
                    doc_refs.addElement(holder);
                    primaries.addElement(ci);
                }
                start_pos = cur_pos + delim_length;
            }
        } while(true);
        int size = doc_refs.size();
        if(size == 0)
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "112", null);
        if(VERBOSE)
            System.out.println("Documents that will be downloaded are:");
        ContentHolder content_holders[] = new ContentHolder[size];
        ContentItem content_items[] = new ContentItem[size];
        for(int i = 0; i < size; i++)
        {
            FormatContentHolder holder = (FormatContentHolder)doc_refs.elementAt(i);
            content_holders[i] = holder;
            ContentItem ci = (ContentItem)primaries.elementAt(i);
            content_items[i] = ci;
            if(!VERBOSE)
                continue;
            if(holder instanceof WTDocument)
                System.out.println(((WTDocument)holder).getName());
            else
                System.out.println("FormatContentHolder other than a document " + holder);
        }

        String closeUrl = null;
        ReferenceFactory rf = new ReferenceFactory();
        wt.httpgw.URLFactory urlFactory = getState().getURLFactory();
        HashMap map = new HashMap();
        map.put("action", "CloseWindow");
        map.put("formName", "ViewDocumentWizard");
        Persistable context_obj = (Persistable)getContextObj();
        String contextOid = null;
        if(context_obj != null)
            contextOid = (new ReferenceFactory()).getReferenceString(context_obj);
        if(contextOid != null)
            map.put("OID", contextOid);
        else
            map.put("class", "wt.doc.WTDocument");
        closeUrl = GatewayServletHelper.buildAuthenticatedURL(urlFactory, "wt.enterprise.URLProcessor", "generateForm", map).toExternalForm();
        String applet_tag = null;
        try
        {
            applet_tag = ContentHtmlUtils.initiateContentDownloadHtml(content_holders, content_items, closeUrl, locale, VERBOSE);
        }
        catch(IOException ioe)
        {
            throw new WTException(ioe, "com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "107", null);
        }
        catch(PropertyVetoException pve)
        {
            throw new WTException(pve, "com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "107", null);
        }
        catch(Exception e)
        {
            throw new WTException(e, "com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "107", null);
        }
        if(applet_tag == null || applet_tag.length() == 0)
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "107", null);
        else
            return applet_tag;
    }

    static Class _mthclass$(String x0)
    {
        return Class.forName(x0);
        ClassNotFoundException x1;
        x1;
        throw (new NoClassDefFoundError()).initCause(x1);
    }

    private static final String RESOURCE = "com.ptc.windchill.pdmlink.doc.server.processors.processorsResource";
    private static final String CLASSNAME;
    protected static boolean VERBOSE = false;
    protected static String DELIM;

    static 
    {
        CLASSNAME = (com.ptc.windchill.pdmlink.doc.server.processors.DownloadDocumentDelegate.class).getName();
        try
        {
            WTProperties properties = WTProperties.getLocalProperties();
            VERBOSE = properties.getProperty("com.ptc.windchill.pdmlink.doc.client.verbose", false);
            DELIM = properties.getProperty("wt.doc.hiddenFieldDelimiter", ";;;qqq");
        }
        catch(Throwable t)
        {
            t.printStackTrace(System.err);
            throw new ExceptionInInitializerError(t);
        }
    }
}
