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

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

import com.ptc.core.HTMLtemplateutil.server.processors.EntityTaskDelegate;
import com.ptc.core.foundation.type.common.impl.AttributeValueException;
import com.ptc.core.foundation.type.server.impl.TypeHelper;
import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.core.meta.common.impl.WCTypeIdentifier;
import com.ptc.windchill.enterprise.doc.server.DocumentUtility;
import com.ptc.windchill.enterprise.nativeapp.server.ApplicationIntegrationFactory;
import com.ptc.windchill.enterprise.nativeapp.server.WindchillAttributeUtility;
import java.beans.PropertyVetoException;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.*;
import wt.content.*;
import wt.doc.WTDocument;
import wt.facade.scm.ScmFacade;
import wt.fc.*;
import wt.httpgw.HTTPRequest;
import wt.httpgw.LanguagePreference;
import wt.inf.container.*;
import wt.pom.Transaction;
import wt.pom.UniquenessException;
import wt.rule.util.InitRuleHelper;
import wt.templateutil.processor.*;
import wt.util.*;

public class CreateDocumentFormTaskDelegate extends FormTaskDelegate
    implements Serializable
{

    public CreateDocumentFormTaskDelegate()
    {
        locale = null;
    }

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

    public void processAction(ContentHTTPStream contentStream)
        throws Exception
    {
        if(VERBOSE)
            System.out.println("^^^^^^^^^CreateDocumentFormTaskDelegate.processAction()");
        locale = LanguagePreference.getLocale(getState().getRequestObj().getProperty("cgi.http_accept_language", ""));
        Properties fData = getFormData();
        Properties qData = getQueryData();
        String formName = (String)fData.get("formName");
        String pageName = (String)fData.get("pageName");
        if(formName == null || formName.equals(""))
            formName = (String)qData.get("formName");
        if(formName == null || formName.equals(""))
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "2", null);
        clearValues(pageName, fData);
        EntityTaskDelegate.saveCustomFormDataInSession(getState().getCookieManager(), formName, fData);
        boolean validForm = validateForm(pageName, fData);
        setContextClassName((wt.doc.WTDocument.class).getName());
        if(!hasActiveRepository(formName))
            setContextAction("ErrorRelaunchWizard");
        else
        if(validForm)
        {
            if(getContextAction().equals("DoCreateDocument"))
            {
                String cachedContentInfo = fData.getProperty("cachedContentInfo");
                if(cachedContentInfo == null)
                    cachedContentInfo = getFormValueFromSession(formName, "cachedContentInfo", null);
                if(VERBOSE)
                    System.out.println("\n\nCreateDocumentFTD.processAction -- cachedContentInfo = \"" + cachedContentInfo + "\"");
                if(cachedContentInfo != null && hasContentToUpload(fData))
                {
                    if(VERBOSE)
                        System.out.println("CreateDocumentFTD.processAction -- cachedContentInfo exists and hasContentToUpload so skipping to upload first");
                    switchPages("CreateDocumentProcessing", null, (wt.doc.WTDocument.class).getName());
                    return;
                }
                if(VERBOSE)
                    System.out.println("\n\nCreateDocumentFTD.processAction -- create document first");
                doCreation(formName);
            } else
            if(getContextAction().equals("CommitDocumentCreation"))
            {
                if(VERBOSE)
                    System.out.println("\n\nCreateDocumentFTD.processAction -- CommitDocumentCreation");
                doCreation(formName);
            } else
            {
                switchPages((String)qData.get("nextAction"), null, (wt.doc.WTDocument.class).getName());
            }
        } else
        {
            switchPages(getContextAction(), null, (wt.doc.WTDocument.class).getName());
        }
    }

    private void doCreation(String formName)
        throws WTException
    {
        Properties formData;
        String contextObj;
        HashMap input_data;
        String number;
        String name;
        boolean keep;
        String obj_type;
        Transaction trx;
        if(VERBOSE)
            System.out.println("^^^^^^^^^CreateDocumentFormTaskDelegate.doCreation()");
        formData = getFormData();
        contextObj = null;
        if(formData == null)
            break MISSING_BLOCK_LABEL_1703;
        if(VERBOSE)
            System.out.println("Form Name = " + formName);
        input_data = new HashMap(5);
        if(VERBOSE)
            System.out.println("Create new document ...");
        number = formData.getProperty("Number");
        if(number == null)
            number = getFormValueFromSession((String)formData.get("formName"), "Number", "");
        if(number != null)
        {
            number = number.trim();
            if(VERBOSE)
                System.out.println("number = " + number);
            input_data.put("number", number);
        }
        name = formData.getProperty("Name");
        if(name == null)
            name = getFormValueFromSession((String)formData.get("formName"), "Name", "");
        if(name != null)
        {
            name = name.trim();
            if(VERBOSE)
                System.out.println("name = " + name);
            input_data.put("name", name);
        }
        String title = formData.getProperty("Title");
        if(title == null)
            title = getFormValueFromSession((String)formData.get("formName"), "Title", "");
        if(title != null)
        {
            title = title.trim();
            if(VERBOSE)
                System.out.println("title = " + title);
            input_data.put("title", title);
        }
        String description = formData.getProperty("Description");
        if(description == null)
            description = getFormValueFromSession((String)formData.get("formName"), "Description", "");
        if(description != null)
        {
            description = description.trim();
            if(VERBOSE)
                System.out.println("description = " + description);
            input_data.put("description", description);
        }
        String repository = getRepository(formName);
        if(repository == null || repository.equals(""))
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "9", null);
        if(VERBOSE)
            System.out.println("Repository from the session = " + repository);
        input_data.put("context", repository);
        String keep_checked_out = formData.getProperty("KeepCheckedOut");
        if(keep_checked_out == null)
            keep_checked_out = getFormValueFromSession((String)formData.get("formName"), "KeepCheckedOut", "");
        if(VERBOSE)
            System.out.println("formData KeepCheckedOut = " + keep_checked_out);
        keep = false;
        if(keep_checked_out != null && keep_checked_out.equalsIgnoreCase("true"))
            keep = true;
        if(VERBOSE)
            System.out.println("keep_checked_out = " + keep);
        obj_type = getObjectType(formName);
        if(obj_type == null || obj_type.equals(""))
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "13", null);
        if(VERBOSE)
            System.out.println("Type = " + obj_type);
        String org = formData.getProperty("OrganizationDN");
        if(org == null)
            org = getFormValueFromSession((String)formData.get("formName"), "OrganizationDN", "");
        if(org == null || org.equals(""))
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "109", null);
        org = org.trim();
        if(VERBOSE)
            System.out.println("OrganizationDN = " + org);
        input_data.put("OrganizationDN", org);
        String folderRef = formData.getProperty("FolderLocation");
        if(folderRef == null)
            folderRef = getFormValueFromSession((String)formData.get("formName"), "FolderLocation", "");
        if(folderRef.equals("SelectFolder"))
        {
            folderRef = formData.getProperty("FolderPicker");
            if(folderRef == null)
                folderRef = getFormValueFromSession((String)formData.get("formName"), "FolderPicker", "");
        } else
        {
            folderRef = null;
        }
        if(folderRef != null && !folderRef.trim().equals(""))
        {
            folderRef = folderRef.trim();
            input_data.put("folderRef", folderRef);
        }
        trx = null;
        trx = new Transaction();
        trx.start();
        ServletSessionCookie cookie = getState().getCookieManager().getCookie(formName);
        Properties soft_attribs = cookie.toProperties();
        input_data = WindchillAttributeUtility.propertiesToHashMap(soft_attribs, input_data, WTContext.getContext().getTimeZone(), locale);
        WTDocument wtdoc = ApplicationIntegrationFactory.createDocument(obj_type, input_data, keep, locale, "html");
        EntityTaskDelegate.setEntityAttributes(wtdoc, false, formData, locale);
        if(VERBOSE)
            System.out.println("Document created successfully.  " + wtdoc.getDisplayIdentifier().getLocalizedMessage(locale));
        setContextObj(wtdoc);
        String cachedContentInfo = formData.getProperty("cachedContentInfo");
        if(cachedContentInfo == null)
            cachedContentInfo = getFormValueFromSession(formName, "cachedContentInfo", null);
        if(VERBOSE)
            System.out.println("cachedContentInfo = " + cachedContentInfo);
        if(cachedContentInfo != null && cachedContentInfo.length() > 0)
        {
            persistCachedContentChanges(cachedContentInfo);
            if(VERBOSE)
                System.out.println("Document content persisted successfully.");
        } else
        if(hasContentToUpload(formData))
        {
            if(VERBOSE)
                System.out.println("Document content hasn't been uploaded and needs to be");
            setContextAction("CreateDocumentProcessing");
        } else
        if(VERBOSE)
            System.out.println("Document has no content, so we're done!");
        String targetType = (String)formData.get("targetType");
        if(targetType == null || targetType.equals(""))
            targetType = getFormValueFromSession((String)formData.get("formName"), "targetType", "");
        String filename = (String)formData.get("File");
        if(filename == null || filename.equals(""))
            filename = getFormValueFromSession((String)formData.get("formName"), "File", "");
        String scmname = (String)formData.get("SCMHidden");
        if(scmname == null || scmname.equals(""))
            scmname = getFormValueFromSession((String)formData.get("formName"), "SCMHidden", "");
        if(targetType != null && targetType.equalsIgnoreCase("URL"))
        {
            persistPrimaryURL(wtdoc, filename);
            formData.put("File", "");
            formData.put("targetType", "NONE");
        } else
        if(targetType != null && targetType.equalsIgnoreCase("SCM"))
        {
            persistPrimarySCM(wtdoc, scmname);
            formData.put("File", "");
            formData.put("targetType", "NONE");
        }
        processSCMAttachments(wtdoc, formData);
        processURLAttachments(wtdoc, formData);
        performCleanup();
        setContextObj(wtdoc);
        trx.commit();
        trx = null;
        if(VERBOSE)
            System.out.println("Document creation transaction committed.  " + wtdoc.getDisplayIdentifier().getLocalizedMessage(locale));
        if(trx != null)
            trx.rollback();
        break MISSING_BLOCK_LABEL_1703;
        WTException e;
        e;
        boolean caughtException = false;
        Throwable nested = e.getNestedThrowable();
        do
        {
            if(nested == null || !(nested instanceof WTException))
                break;
            if(nested instanceof UniquenessException)
            {
                Object param[] = {
                    number, name
                };
                addToResponseHeaders(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "117", param));
                caughtException = true;
                contextObj = "CreateDocument2";
                break;
            }
            nested = ((WTException)nested).getNestedThrowable();
        } while(true);
        if(e instanceof AttributeValueException)
        {
            caughtException = true;
            contextObj = "CreateDocument3";
            addToResponseHeaders(e);
        }
        if(!caughtException)
        {
            contextObj = "CreateDocument2";
            addToResponseHeaders(e);
            e.printStackTrace();
        }
        setContextObj(null);
        setContextAction(contextObj);
        if(trx != null)
            trx.rollback();
        break MISSING_BLOCK_LABEL_1703;
        e;
        e.printStackTrace();
        addToResponseHeaders(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "126", null));
        setContextObj(null);
        contextObj = "CreateDocument2";
        setContextAction(contextObj);
        if(trx != null)
            trx.rollback();
        break MISSING_BLOCK_LABEL_1703;
        Exception exception;
        exception;
        if(trx != null)
            trx.rollback();
        throw exception;
    }

    private boolean validateForm(String pageName, Properties fData)
        throws WTException
    {
        String formName = (String)fData.get("formName");
        if(VERBOSE)
        {
            System.out.println("^^^^^^^^^CreateDocumentFormTaskDelegate.validateForm()");
            ServletSessionCookie cookie = getState().getCookieManager().getCookie(formName);
            Properties sessionContents = cookie.toProperties();
            System.out.println("****\ncurrent session contents:\n" + sessionContents + "\n****\n");
        }
        if(getContextAction().equals("DoCreateDocument"))
        {
            String targetType = (String)fData.get("targetType");
            if(targetType == null || targetType.equals(""))
                targetType = getFormValueFromSession((String)fData.get("formName"), "targetType", "");
            WTContainer container = (WTContainer)rf.getReference(getRepository(formName)).getObject();
            String obj_type = getObjectType(formName);
            TypeIdentifier typeIdentifier = TypeHelper.getTypeIdentifier(obj_type);
            boolean user_enter_number = !InitRuleHelper.isAutoNumber(typeIdentifier.getTypename(), WTContainerRef.newWTContainerRef(container));
            boolean number_null = false;
            String number = null;
            String name = (String)fData.get("Name");
            if(name == null || name.equals(""))
                name = getFormValueFromSession((String)fData.get("formName"), "Name", "");
            if(user_enter_number)
            {
                number = (String)fData.get("Number");
                if(number == null || number.equals(""))
                {
                    number = getFormValueFromSession(formName, "Number", "");
                    if(number == null || number.equals(""))
                        number_null = true;
                }
            }
            String filename = (String)fData.get("File");
            if(filename == null || filename.equals(""))
                filename = getFormValueFromSession(formName, "File", "");
            if(PRIMARY_REQUIRED && (filename == null || filename.trim().length() == 0) || name == null || name.trim().length() == 0 || number_null)
            {
                setContextAction("CreateDocument2");
                addToResponseHeaders(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "12", null));
                return false;
            }
            if(!DocumentUtility.checkNameLength(name))
            {
                setContextAction("CreateDocument2");
                Object args[] = {
                    new Integer(DocumentUtility.getNameUpperLimit())
                };
                addToResponseHeaders(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "115", args));
                return false;
            }
            if(!DocumentUtility.checkNumberLength(number))
            {
                setContextAction("CreateDocument2");
                Object args[] = {
                    new Integer(DocumentUtility.getNumberUpperLimit())
                };
                addToResponseHeaders(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "116", args));
                return false;
            }
            String title = (String)fData.get("Title");
            if(title == null || title.equals(""))
                title = getFormValueFromSession((String)fData.get("formName"), "Title", "");
            if(!DocumentUtility.checkTitleLength(title))
            {
                setContextAction("CreateDocument2");
                Object args[] = {
                    new Integer(DocumentUtility.getTitleUpperLimit())
                };
                addToResponseHeaders(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "73", args));
                return false;
            }
            String orgID = (String)fData.get("OrganizationDN");
            boolean orgID_null = false;
            if(orgID == null || orgID.equals(""))
            {
                orgID = getFormValueFromSession(formName, "OrganizationDN", "");
                if(orgID == null || orgID.equals(""))
                    orgID_null = true;
            }
            if(orgID_null)
            {
                setContextAction("CreateDocument2");
                addToResponseHeaders(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "12", null));
                return false;
            } else
            {
                return true;
            }
        }
        if(getContextAction().equals("CreateDocument1"))
        {
            Class objClass = null;
            String object_type = getFormValueFromSession(formName, "ObjectType", "");
            if(VERBOSE)
                System.out.println("object type" + object_type);
            if(object_type.equals(""))
            {
                addToResponseHeaders(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "129", null));
                return false;
            }
            if(object_type != null)
                try
                {
                    WCTypeIdentifier wcti = (WCTypeIdentifier)TypeHelper.getTypeIdentifier(object_type);
                    objClass = Class.forName(wcti.getClassnameFromTypename());
                }
                catch(ClassNotFoundException cnfe)
                {
                    setContextAction("CreateDocument1");
                    addToResponseHeaders(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "129", null));
                    return false;
                }
            return true;
        } else
        {
            return true;
        }
    }

    private String getRepository(String formName)
        throws WTException
    {
        if(VERBOSE)
            System.out.println("^^^^^^^^^CreateDocumentFormTaskDelegate.getRepository()");
        return getFormValueFromSession(formName, "Repository", "");
    }

    private String getObjectType(String formName)
        throws WTException
    {
        if(VERBOSE)
            System.out.println("^^^^^^^^^CreateDocumentFormTaskDelegate.getObjectType()");
        return getFormValueFromSession(formName, "ObjectType", "");
    }

    public String getFormValueFromSession(String formName, String inputFieldName, String defaultValue)
        throws WTException
    {
        if(VERBOSE)
            System.out.println("^^^^^^^^^CreateDocumentFormTaskDelegate.getFormValueFromSession()");
        if(formName == null || formName.equals(""))
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "2", null);
        if(inputFieldName == null || inputFieldName.equals(""))
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "14", null);
        ServletSessionCookie cookie = getState().getCookieManager().getCookie(formName);
        String value = (String)cookie.get(inputFieldName);
        if(value == null && defaultValue != null)
            value = defaultValue;
        return value;
    }

    protected boolean hasActiveRepository(String form_name)
    {
        try
        {
            Object context_obj = getContextObj();
            WTContainer container = null;
            if(VERBOSE)
                System.out.println("^^^^^^^^^CreateDocumentFormTaskDelegate.hasActiveRepository(form_name = \"" + form_name + "\"");
            if(form_name.equals("CreateTemplateDocumentWizard"))
            {
                String container_strg = getRepositorySelection(form_name);
                if(container_strg == null || container_strg.equals(""))
                    throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "9", null);
                container = (WTContainer)rf.getReference(container_strg).getObject();
            } else
            if(context_obj instanceof WTContainer)
            {
                container = (WTContainer)getContextObj();
                ServletSessionCookie cookie = getState().getCookieManager().getCookie(form_name);
                cookie.put("Repository", rf.getReferenceString(container));
            } else
            if(context_obj instanceof WTContained)
                container = ((WTContained)context_obj).getContainer();
            if(container == null)
            {
                String container_strg = getFormValueFromSession(form_name, "Repository", "");
                if(container_strg == null || container_strg.equals(""))
                    throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "9", null);
            }
        }
        catch(WTException e)
        {
            if(VERBOSE)
                System.out.println("Active repository:  NO");
            e.printStackTrace();
            addToResponseHeaders(e);
            return false;
        }
        if(VERBOSE)
            System.out.println("Active repository:  YES");
        return true;
    }

    private String getRepositorySelection(String form_name)
        throws WTException
    {
        if(form_name.equals("CreateTemplateDocumentWizard"))
        {
            if(VERBOSE)
                System.out.println("Creating a template in the administrator page, get the value from the form.");
            String radio_selection = getFormValueFromSession(form_name, "repos", "");
            String repos_select = "";
            if(radio_selection.equals("docrepos"))
            {
                repos_select = getFormValueFromSession(form_name, "repos_drop", "");
                if(VERBOSE)
                    System.out.println("Radio repository selected = " + repos_select);
            } else
            if(radio_selection.equals("prodrepos"))
            {
                repos_select = getFormValueFromSession(form_name, "product_drop", "");
                if(VERBOSE)
                    System.out.println("Radio product repository selected = " + repos_select);
            } else
            {
                throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "9", null);
            }
            if(repos_select == null || repos_select.equals(""))
            {
                throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "9", null);
            } else
            {
                ServletSessionCookie cookie = getState().getCookieManager().getCookie(form_name);
                cookie.put("Repository", repos_select);
                return repos_select;
            }
        } else
        {
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "9", null);
        }
    }

    public void clearValues(String pageName, Properties fData)
        throws WTException
    {
        String formName = (String)fData.get("formName");
        if(pageName != null && pageName.equals("CreateDocumentForm1"))
        {
            String sessionType = getObjectType(formName);
            if(sessionType.length() > 0 && !sessionType.equals((String)fData.get("ObjectType")))
            {
                String description = getFormValueFromSession(formName, "Description", "");
                String fileName = getFormValueFromSession(formName, "File", "");
                String title = getFormValueFromSession(formName, "Title", "");
                String docName = getFormValueFromSession(formName, "Name", "");
                String docNumber = getFormValueFromSession(formName, "Number", "");
                String repository = getFormValueFromSession(formName, "Repository", "");
                String folderlocation = getFormValueFromSession(formName, "FolderLocation", "");
                String folderpicker = getFormValueFromSession(formName, "FolderPicker", "");
                String orgDN = getFormValueFromSession(formName, "OrganizationDN", "");
                if(description.length() > 0)
                    fData.put("Description", description);
                if(fileName.length() > 0)
                    fData.put("File", fileName);
                if(title.length() > 0)
                    fData.put("Title", title);
                if(docName.length() > 0)
                    fData.put("Name", docName);
                if(docNumber.length() > 0)
                    fData.put("Number", docNumber);
                if(repository.length() > 0)
                    fData.put("Repository", repository);
                if(folderlocation.length() > 0)
                    fData.put("FolderLocation", folderlocation);
                if(folderpicker.length() > 0)
                    fData.put("FolderPicker", folderpicker);
                if(orgDN.length() > 0)
                    fData.put("OrganizationDN", orgDN);
                deleteFormDataFromSession(formName);
            }
        }
    }

    protected void persistPrimarySCM(FormatContentHolder document, String scm)
        throws WTException, WTPropertyVetoException, PropertyVetoException
    {
        if(VERBOSE)
            System.out.println("\npersistPrimarySCM:  " + scm);
        if(scm == null || scm.length() == 0)
            return;
        boolean success;
        if(InstalledProperties.isInstalled("Windchill.SoftwareConfigMgmtIntegration"))
            success = ScmFacade.getInstance().createScmData(document, scm);
        else
            return;
    }

    protected void persistPrimaryURL(FormatContentHolder document, String url)
        throws WTException, WTPropertyVetoException, PropertyVetoException
    {
        ContentHolder holder;
        wt.content.ContentItem item;
        URLData urlData;
        Transaction trx;
        if(VERBOSE)
            System.out.println("\npersistPrimaryURL:  " + url);
        if(url == null || url.length() == 0)
            return;
        holder = document;
        holder = (FormatContentHolder)ContentHelper.service.getContents(holder);
        item = ContentHelper.getPrimary((FormatContentHolder)holder);
        urlData = null;
        trx = new Transaction();
        String urlLocation;
        trx.start();
        if(item == null)
        {
            urlData = URLData.newURLData(holder);
            break MISSING_BLOCK_LABEL_168;
        }
        if(!(item instanceof URLData))
            break MISSING_BLOCK_LABEL_143;
        urlData = (URLData)item;
        urlLocation = urlData.getUrlLocation();
        if(url.equals(urlLocation))
        {
            if(trx != null)
                trx.rollback();
            return;
        }
        break MISSING_BLOCK_LABEL_168;
        if(item instanceof ApplicationData)
        {
            ContentServerHelper.service.deleteContent(holder, item);
            urlData = URLData.newURLData(holder);
        }
        urlData.setUrlLocation(url);
        urlData.setRole(ContentRoleType.toContentRoleType("PRIMARY"));
        urlData = ContentServerHelper.service.updateContent(holder, urlData);
        holder = ContentServerHelper.service.updateHolderFormat((FormatContentHolder)holder);
        trx.commit();
        trx = null;
        if(trx != null)
            trx.rollback();
        return;
        Exception exception;
        exception;
        if(trx != null)
            trx.rollback();
        throw exception;
    }

    private boolean hasContentToUpload(Properties fData)
        throws WTException
    {
        if(VERBOSE)
            System.out.println("^^^^^^^^^CreateDocumentFormTaskDelegate.hasContentToUpload( fData )");
        String formName = (String)fData.get("formName");
        String filename = (String)fData.get("File");
        if(filename == null || filename.equals(""))
            filename = getFormValueFromSession(formName, "File", "");
        String addAttachments = (String)fData.get("addAttachments");
        if(addAttachments == null || addAttachments.equals(""))
            addAttachments = getFormValueFromSession(formName, "addAttachments", "");
        return filename != null && filename.trim().length() != 0 || addAttachments != null && addAttachments.trim().length() != 0;
    }

    public void persistCachedContentChanges(String cachedContentInfo)
        throws Exception
    {
        Vector postEntryV = parseIntoPostEntries(cachedContentInfo);
        ContentHolder holder = (ContentHolder)getContextObj();
        ObjectIdentifier myHolder = PersistenceHelper.getObjectIdentifier(holder);
        String qStr = myHolder.getStringValue();
        ContentHttp.saveContent(qStr, postEntryV);
    }

    private Vector parseIntoPostEntries(String cachedContentInfo)
    {
        if(VERBOSE)
            System.out.println("parseIntoPostEntries()" + cachedContentInfo);
        if(cachedContentInfo == null || cachedContentInfo.length() == 0)
            return null;
        String RECORD_DELIM = ";;;zzz";
        String entryArray[] = cachedContentInfo.split(RECORD_DELIM);
        Vector postEntryV = new Vector();
        for(int i = 0; i < entryArray.length; i++)
        {
            PostEntry postEntry = new PostEntry(entryArray[i]);
            postEntryV.addElement(postEntry);
        }

        if(VERBOSE)
            System.out.println("\n\nAfter re-creating PostEntry vector from concatenated strings");
        if(VERBOSE)
            dumpPostEntries(postEntryV);
        return postEntryV;
    }

    private void dumpPostEntries(Vector postEntryV)
    {
        for(int i = 0; i < postEntryV.size(); i++)
        {
            PostEntry entry = (PostEntry)postEntryV.elementAt(i);
            System.out.println(entry.toStringForm());
        }

    }

    private void performCleanup()
    {
        if(VERBOSE)
            System.out.println("\n\nCreateDocFTD.performCleanup()");
        Properties props = getQueryData();
        if(VERBOSE)
            System.out.println("query data = " + props);
        String formName = (String)props.get("formName");
        String uploaded = (String)props.get("uploaded");
        try
        {
            if(uploaded != null && uploaded.equalsIgnoreCase("true"))
            {
                long checksum = 0L;
                try
                {
                    checksum = Long.parseLong((String)props.get("checksum"));
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                FormatContentHolder holder = (FormatContentHolder)getContextObj();
                holder = (FormatContentHolder)ContentHelper.service.getContents(holder);
                wt.content.ContentItem item = ContentHelper.getPrimary(holder);
                if(item instanceof ApplicationData)
                {
                    ApplicationData app = (ApplicationData)item;
                    app.setChecksum(checksum);
                    try
                    {
                        ContentHelper.service.updateAppData(holder, app);
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        DefaultTemplateProcessor.addToFeedbackCookie(new WTMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "23", null), getState());
        if(formName != null && formName.length() > 0)
            deleteFormDataFromSession(formName);
        setContextAction("ComplexCloseWizardWindow");
        getContextProperties().put("actionPerformed", "CREATE");
        getContextProperties().put("refreshOnly", "true");
    }

    protected void processSCMAttachments(FormatContentHolder document, Properties form_data)
        throws WTException, WTPropertyVetoException, PropertyVetoException
    {
        if(VERBOSE)
            System.out.println("processSCMAttachments  form_data = " + form_data + "\n\n");
        String secondaryContent = (String)form_data.get("displayAttachments");
        if(secondaryContent == null || secondaryContent.equals(""))
            secondaryContent = getFormValueFromSession((String)form_data.get("formName"), "displayAttachments", "");
        if(secondaryContent != null && secondaryContent.length() > 0)
        {
            String RECORD_DELIM = ";;;zzz";
            String secondaries[] = secondaryContent.split(RECORD_DELIM);
            for(int i = 0; i < secondaries.length; i++)
                if(secondaries[i].indexOf("SCM") == 0)
                    persistSCMAttachment(document, secondaries[i]);

        }
    }

    protected void persistSCMAttachment(FormatContentHolder holder, String scmAttachment)
        throws WTException, WTPropertyVetoException, PropertyVetoException
    {
        if(VERBOSE)
            System.out.println("\npersistSCMAttachment:  " + scmAttachment);
        String DELIM = ";;;qqq";
        String attachData[] = scmAttachment.split(DELIM);
        String action = null;
        if(attachData.length >= 10)
            action = attachData[9];
        if(VERBOSE)
        {
            System.out.println("action = " + action);
            System.out.println("scmLocation = " + attachData[7]);
            System.out.println("scmId = " + attachData[1]);
        }
        if(action != null);
        boolean success;
        if(action.equals("ADD") && InstalledProperties.isInstalled("Windchill.SoftwareConfigMgmtIntegration"))
            success = ScmFacade.getInstance().createScmData(holder, attachData[7]);
    }

    protected void processURLAttachments(FormatContentHolder document, Properties form_data)
        throws WTException
    {
        if(VERBOSE)
            System.out.println("processURLAttachments  form_data = " + form_data + "\n\n");
        String secondaryContent = (String)form_data.get("displayAttachments");
        if(secondaryContent == null || secondaryContent.equals(""))
            secondaryContent = getFormValueFromSession((String)form_data.get("formName"), "displayAttachments", "");
        if(secondaryContent != null && secondaryContent.length() > 0)
        {
            String RECORD_DELIM = ";;;zzz";
            String secondaries[] = secondaryContent.split(RECORD_DELIM);
            for(int i = 0; i < secondaries.length; i++)
            {
                if(secondaries[i].indexOf("URL") != 0)
                    continue;
                try
                {
                    persistURLAttachment(document, secondaries[i]);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }

        }
    }

    protected void persistURLAttachment(FormatContentHolder holder, String urlAttachment)
        throws WTException, WTPropertyVetoException, PropertyVetoException
    {
        if(VERBOSE)
            System.out.println("\npersistURLAttachment:  " + urlAttachment);
        String DELIM = ";;;qqq";
        String attachData[] = urlAttachment.split(DELIM);
        String action = null;
        if(attachData.length >= 10)
            action = attachData[9];
        if(VERBOSE)
        {
            System.out.println("action = " + action);
            System.out.println("urlLocation = " + attachData[7]);
            System.out.println("description = " + attachData[3]);
        }
        if(action == null)
            return;
        if(action.equals("ADD"))
            persistURLAttachment(holder, attachData[7], attachData[3]);
    }

    protected void persistURLAttachment(FormatContentHolder document, String url, String description)
        throws WTException, WTPropertyVetoException, PropertyVetoException
    {
        ContentHolder holder = document;
        URLData urlData = null;
        urlData = URLData.newURLData(holder);
        urlData.setUrlLocation(url);
        if(description != null && description.length() > 0)
            urlData.setDescription(description);
        urlData.setRole(ContentRoleType.toContentRoleType("SECONDARY"));
        urlData = ContentServerHelper.service.updateContent(holder, urlData);
    }

    private static final String RESOURCE = "com.ptc.windchill.pdmlink.doc.server.processors.processorsResource";
    private static final String CLASSNAME;
    private Locale locale;
    protected static boolean VERBOSE = false;
    private static final String REPOSITORY_FIELDNAME = "Repository";
    private static final String REPOS_RADIO_ELEMENT = "repos";
    private static final String REPOS_RADIO = "docrepos";
    private static final String REPOS_PROD_RADIO = "prodrepos";
    private static final String REPOS_DROP = "repos_drop";
    private static final String PROD_DROP = "product_drop";
    private static final String OBJECT_TYPE_FIELDNAME = "ObjectType";
    protected static boolean PRIMARY_REQUIRED = false;
    protected static final ReferenceFactory rf = new ReferenceFactory();

    static 
    {
        CLASSNAME = (com.ptc.windchill.pdmlink.doc.server.processors.CreateDocumentFormTaskDelegate.class).getName();
        try
        {
            WTProperties properties = WTProperties.getLocalProperties();
            VERBOSE = properties.getProperty("wt.doc.verbose", false);
            PRIMARY_REQUIRED = properties.getProperty("wt.doc.primaryContentRequired", true);
        }
        catch(Throwable t)
        {
            System.err.println("com.ptc.windchill.pdmlink.doc.server.processors.CreateDocumentFormTaskDelegate : Error reading wt.doc.* properties");
        }
    }
}
