// 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:   AddDocumentsDelegate.java

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

import com.ptc.core.HTMLtemplateutil.server.processors.EntityTaskDelegate;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.*;
import wt.content.ContentHTTPStream;
import wt.doc.*;
import wt.fc.*;
import wt.introspection.ClassInfo;
import wt.templateutil.processor.*;
import wt.util.WTException;
import wt.util.WTProperties;

public class AddDocumentsDelegate extends FormTaskDelegate
    implements Serializable
{

    public AddDocumentsDelegate()
    {
    }

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

    public void processAction(ContentHTTPStream contentStream)
        throws Exception
    {
        String formName = null;
        if(VERBOSE)
        {
            System.out.println("DEBUG:AddDocumentsDelegate.processAction object ci:" + getContextObj());
            System.out.println("DEBUG:AddDocumentsDelegate.processAction context action:" + getContextAction());
        }
        Properties formData = getFormData();
        Properties queryData = getQueryData();
        formName = (String)formData.get("formName");
        String pageName = (String)formData.get("pageName");
        if(formName == null)
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "2", null);
        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);
        }
        if(getContextAction().equals("AddDocumentObjects"))
        {
            Vector selected_documents = getSelectedDocuments(formData);
            int size = selected_documents.size();
            if(size == 0)
                throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "121", null);
            Enumeration result = WTDocumentHelper.service.getOrderedUsesWTDocumentMasters(doc);
            Vector documents = new Vector();
            ReferenceFactory refFactory = new ReferenceFactory();
            WTDocumentMaster doc_master;
            for(; result.hasMoreElements(); documents.add(refFactory.getReferenceString(doc_master)))
                doc_master = (WTDocumentMaster)result.nextElement();

            for(int i = 0; i < size; i++)
                documents.add(selected_documents.get(i));

            orderDocuments(doc, documents);
            deleteFormDataFromSession(formName);
        } else
        if(getContextAction().equals("AddAndReorderDocumentObjects"))
        {
            Vector documents = getReorderedDocuments(formData);
            orderDocuments(doc, documents);
            deleteFormDataFromSession(formName);
        } else
        {
            Vector selected_documents = getSelectedDocuments(formData);
            if(selected_documents.size() == 0)
                throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "120", null);
            EntityTaskDelegate.saveEntityFormDataInSession(getState().getCookieManager(), formName, formData);
            switchPages((String)queryData.get("nextAction"), null, (wt.doc.WTDocument.class).getName());
        }
        RedirectObject redirectObj = new RedirectObject();
        redirectObj.setRedirection(getState());
        break MISSING_BLOCK_LABEL_534;
        WTException wte;
        wte;
        wte.printStackTrace();
        addToResponseHeaders(wte);
        setContextAction("CreateDocAssociationFailed");
        deleteFormDataFromSession(formName);
        RedirectObject redirectObj = new RedirectObject();
        redirectObj.setRedirection(getState());
        break MISSING_BLOCK_LABEL_534;
        Exception exception;
        exception;
        RedirectObject redirectObj = new RedirectObject();
        redirectObj.setRedirection(getState());
        throw exception;
    }

    private Vector getSelectedDocuments(Properties formData)
    {
        String selectedDocs = (String)formData.get("selectedDocs");
        Vector documents = parseStringToVector(selectedDocs, DELIM);
        return documents;
    }

    private Vector getReorderedDocuments(Properties formData)
    {
        String list = (String)formData.get("newOrder");
        if(VERBOSE)
            System.out.println("DEBUG:AddDocumentsDelegate.processAction newOrder = " + list);
        if(list == null || list.length() <= 0)
        {
            if(VERBOSE)
                System.out.println("DEBUG:AddDocumentsDelegate.processAction list is empty.");
            return new Vector();
        } else
        {
            Vector documents = parseStringToVector(list, DELIM);
            return documents;
        }
    }

    private void orderDocuments(WTDocument doc, Vector documents)
        throws WTException
    {
        WTDocument new_doc = WTDocumentHelper.service.createOrderedUsageLinks(doc, documents, "", "");
        Properties context_properties = getContextProperties();
        if(context_properties == null)
            context_properties = new Properties();
        ReferenceFactory factory = new ReferenceFactory();
        context_properties.put("oldOid", factory.getReferenceString(ObjectReference.newObjectReference(doc.getPersistInfo().getObjectIdentifier())));
        if(new_doc != null)
            context_properties.put("newOid", factory.getReferenceString(ObjectReference.newObjectReference(new_doc.getPersistInfo().getObjectIdentifier())));
        else
            context_properties.put("newOid", factory.getReferenceString(ObjectReference.newObjectReference(doc.getPersistInfo().getObjectIdentifier())));
        setContextProperties(context_properties);
        setContextAction("CreateDocAssocationsSuccess");
        setContextObj(doc);
        if(VERBOSE)
        {
            System.out.println("DEBUG:AddDocumentsDelegate set\n\toldOid=" + context_properties.getProperty("oldOid") + "\n\tnewOid=" + context_properties.getProperty("newOid"));
            System.out.println("DEBUG:AddDocumentsDelegate set\n\tcontext_properties=" + context_properties);
            System.out.println("DEBUG:AddDocumentsDelegate next context action = " + getContextAction());
        }
    }

    private Vector parseStringToVector(String parseString, String delimiter)
    {
        int cur_pos = 0;
        int start_pos = 0;
        Vector strings = new Vector();
        boolean done = false;
        int delim_length = delimiter.length();
        for(; !done; start_pos = cur_pos + delim_length)
        {
            cur_pos = parseString.indexOf(delimiter, start_pos);
            String item;
            if(cur_pos == -1)
            {
                done = true;
                if(start_pos >= parseString.length())
                    continue;
                item = parseString.substring(start_pos);
                if(item != null && item.length() > 0)
                    strings.addElement(item);
                continue;
            }
            item = parseString.substring(start_pos, cur_pos);
            if(item != null && item.length() > 0)
                strings.addElement(item);
        }

        return strings;
    }

    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.AddDocumentsDelegate.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);
        }
    }
}
