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

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

import com.ptc.core.HTMLtemplateutil.server.processors.WizardProcessor;
import com.ptc.windchill.enterprise.nativeapp.server.ApplicationIntegrationFactory;
import java.io.*;
import java.util.*;
import wt.doc.WTDocument;
import wt.fc.*;
import wt.pds.PDSObjectInput;
import wt.templateutil.processor.RedirectObject;
import wt.util.*;

public class DocumentTemplateProcessor extends WizardProcessor
    implements Externalizable
{

    public DocumentTemplateProcessor()
    {
    }

    public void writeExternal(ObjectOutput output)
        throws IOException
    {
        output.writeLong(0xa6700984c6548630L);
        super.writeExternal(output);
    }

    public void readExternal(ObjectInput input)
        throws IOException, ClassNotFoundException
    {
        long readSerialVersionUID = input.readLong();
        readVersion(this, input, readSerialVersionUID, false, false);
    }

    protected boolean readVersion(DocumentTemplateProcessor thisObject, ObjectInput input, long readSerialVersionUID, boolean passThrough, boolean superDone)
        throws IOException, ClassNotFoundException
    {
        boolean success = true;
        if(readSerialVersionUID == 0xa6700984c6548630L)
        {
            if(!superDone)
                super.readExternal(input);
        } else
        {
            success = readOldVersion(input, readSerialVersionUID, passThrough, superDone);
            if(input instanceof PDSObjectInput)
                EvolvableHelper.requestRewriteOfEvolvedBlobbedObject();
        }
        return success;
    }

    private boolean readOldVersion(ObjectInput input, long readSerialVersionUID, boolean passThrough, boolean superDone)
        throws IOException, ClassNotFoundException
    {
        boolean success = true;
        if(readSerialVersionUID != 0x971c6711f3638a32L)
            if(!superDone)
            {
                success = super.readVersion(this, input, readSerialVersionUID, false, false);
                if(success && !passThrough && readSerialVersionUID != 0x5c9ab67f442bc0d1L)
                    readVersion(this, input, input.readLong(), false, true);
            } else
            {
                throw new InvalidClassException(CLASSNAME, "Local class not compatible: stream classdesc externalizationVersionUID=" + readSerialVersionUID + " local class externalizationVersionUID=" + 0xa6700984c6548630L);
            }
        return success;
    }

    public void createDocumentFromTemplate(Properties parameters, Locale locale, OutputStream os)
        throws WTException
    {
        Properties formData = getFormData();
        if(formData != null)
        {
            String form_name = parameters.getProperty("formName");
            if(form_name == null || form_name.equals(""))
                throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "2", null);
            if(VERBOSE)
                System.out.println("Form Name = " + form_name);
            HashMap input_data = new HashMap(5);
            if(VERBOSE)
                System.out.println("Create new document from template ...");
            String number = formData.getProperty("Number");
            if(number != null)
            {
                number = number.trim();
                if(VERBOSE)
                    System.out.println("number = " + number);
                input_data.put("number", number);
            }
            String name = formData.getProperty("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 = title.trim();
                if(VERBOSE)
                    System.out.println("title = " + title);
                input_data.put("title", title);
            }
            String description = formData.getProperty("Description");
            if(description != null)
            {
                description = description.trim();
                if(VERBOSE)
                    System.out.println("description = " + description);
                input_data.put("description", description);
            }
            String repository = getRepository(form_name);
            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 obj_id = getFormValueFromSession(form_name, "Template", "");
            if(obj_id == null || obj_id.equals(""))
                throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "7", null);
            WTDocument template = null;
            try
            {
                template = (WTDocument)(new ReferenceFactory()).getReference(obj_id).getObject();
            }
            catch(Exception e)
            {
                throw new WTException(e, "com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "8", null);
            }
            WTDocument wtdoc = ApplicationIntegrationFactory.createDocumentFromTemplate(template, input_data, locale, "html");
            if(VERBOSE)
                System.out.println("Document Template created successfully.  " + wtdoc.getDisplayIdentifier().getLocalizedMessage(locale));
            setContextObj(wtdoc);
            setContextAction("ObjProps");
            RedirectObject redirect_object = new RedirectObject();
            redirect_object.setRedirection(getState());
        }
    }

    public void createDocumentTemplate(Properties parameters, Locale locale, OutputStream os)
        throws WTException
    {
        Properties formData = getFormData();
        if(formData != null)
        {
            String form_name = formData.getProperty("formName");
            if(form_name == null || form_name.equals(""))
                throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "2", null);
            if(VERBOSE)
                System.out.println("Form Name = " + form_name);
            HashMap input_data = new HashMap(5);
            if(VERBOSE)
                System.out.println("Create new template document ...");
            String number = formData.getProperty("Number");
            if(number != null)
            {
                number = number.trim();
                if(VERBOSE)
                    System.out.println("number = " + number);
                input_data.put("number", number);
            }
            String name = formData.getProperty("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 = title.trim();
                if(VERBOSE)
                    System.out.println("title = " + title);
                input_data.put("title", title);
            }
            String description = formData.getProperty("Description");
            if(description != null)
            {
                description = description.trim();
                if(VERBOSE)
                    System.out.println("description = " + description);
                input_data.put("description", description);
            }
            String repository = getRepository(form_name);
            if(repository == null || repository.equals(""))
                throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "9", null);
            repository = repository.trim();
            if(VERBOSE)
                System.out.println("context = " + repository);
            input_data.put("context", repository);
            String obj_type = getObjectType(form_name);
            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);
            WTDocument wtdoc = ApplicationIntegrationFactory.createDocumentTemplate(obj_type, input_data, locale, "html");
            if(VERBOSE)
                System.out.println("Document Template created successfully.  " + wtdoc.getDisplayIdentifier().getLocalizedMessage(locale));
            setContextObj(wtdoc);
            setContextAction("ObjProps");
            RedirectObject redirect_object = new RedirectObject();
            redirect_object.setRedirection(getState());
        }
    }

    public void getCreateDocumentFromTemplateIdentity(Properties parameters, Locale locale, OutputStream os)
        throws WTException
    {
        String docIdentity = "";
        String name = getFormData().getProperty("Name");
        if(VERBOSE)
            System.out.println("name=" + name);
        Object param[] = {
            name
        };
        docIdentity = WTMessage.getLocalizedMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "0", param, locale);
        PrintWriter out = getPrintWriter(os, locale);
        out.print(docIdentity);
        out.flush();
    }

    public void getCreateTemplateDocumentIdentity(Properties parameters, Locale locale, OutputStream os)
        throws WTException
    {
        String docIdentity = "";
        String name = getFormData().getProperty("Name");
        if(VERBOSE)
            System.out.println("name=" + name);
        Object param[] = {
            name
        };
        docIdentity = WTMessage.getLocalizedMessage("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "1", param, locale);
        PrintWriter out = getPrintWriter(os, locale);
        out.print(docIdentity);
        out.flush();
    }

    private String getRepository(String form_name)
        throws WTException
    {
        return getFormValueFromSession(form_name, "Repository", "");
    }

    private String getObjectType(String formName)
        throws WTException
    {
        return getFormValueFromSession(formName, "ObjectType", "");
    }

    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;
    static final long serialVersionUID = 1L;
    public static final long EXTERNALIZATION_VERSION_UID = 0xa6700984c6548630L;
    protected static final long OLD_FORMAT_VERSION_UID = 0x971c6711f3638a32L;
    protected static boolean VERBOSE = false;

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