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

package com.ptc.windchill.enterprise.templates.doc.server;

import com.ptc.core.foundation.type.server.impl.TypeHelper;
import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.windchill.enterprise.doc.server.DocumentUtility;
import com.ptc.windchill.enterprise.templates.TemplatesObjectDelegate;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import wt.doc.WTDocument;
import wt.enterprise.*;
import wt.fc.*;
import wt.inf.container.*;
import wt.pds.PDSObjectInput;
import wt.util.*;

public class TemplatesWTDocumentDelegate
    implements TemplatesObjectDelegate, Externalizable
{

    public TemplatesWTDocumentDelegate()
    {
    }

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

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

    protected boolean readVersion(TemplatesWTDocumentDelegate thisObject, ObjectInput input, long readSerialVersionUID, boolean passThrough, boolean superDone)
        throws IOException, ClassNotFoundException
    {
        boolean success = true;
        if(readSerialVersionUID != 0xd4b6b5eeea339daL)
        {
            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 != 0xe9fcf92fe6703be6L)
            throw new InvalidClassException(CLASSNAME, "Local class not compatible: stream classdesc externalizationVersionUID=" + readSerialVersionUID + " local class externalizationVersionUID=" + 0xd4b6b5eeea339daL);
        else
            return success;
    }

    protected Templateable initNewTemplate(Templateable template, String objType, String context)
        throws WTException
    {
        try
        {
            TypeIdentifier type_id = TypeHelper.getTypeIdentifier(objType);
            template = setType((WTDocument)template, type_id);
            template = DocumentUtility.setOldAttributes((WTDocument)template);
            template = DocumentUtility.setTemplate((WTDocument)template, true);
        }
        catch(WTException wte)
        {
            throw new WTException(wte, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "1", null);
        }
        return template;
    }

    protected Templateable initNewTemplate(Templateable template, TypeIdentifier objType, WTContainer context)
        throws WTException
    {
        template = DocumentUtility.setOldAttributes((WTDocument)template);
        template = DocumentUtility.setTemplate((WTDocument)template, true);
        try
        {
            ((WTContained)template).setContainer(context);
        }
        catch(WTPropertyVetoException wtpve)
        {
            throw new WTException(wtpve, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "1", null);
        }
        return template;
    }

    public Templateable newTemplate(String objType, String context)
        throws WTException
    {
        ReferenceFactory referenceFactory = new ReferenceFactory();
        WTContainer container = null;
        if(context != null)
            if(context.equalsIgnoreCase("Classic"))
                container = WTContainerHelper.service.getClassicContainer();
            else
                container = (WTContainer)referenceFactory.getReference(context).getObject();
        TypeIdentifier ti = TypeHelper.getTypeIdentifier(objType);
        return newTemplate(ti, container);
    }

    public Templateable newTemplate(TypeIdentifier objType, WTContainer context)
        throws WTException
    {
        WTDocument template = null;
        template = getNewWTDocument(objType);
        initNewTemplate(template, objType, context);
        return template;
    }

    public RevisionControlled newObjFromTemplate(Templateable objTemplate, String context)
        throws WTException
    {
        WTDocument document = null;
        try
        {
            document = (WTDocument)EnterpriseHelper.service.newCopy((RevisionControlled)objTemplate);
            document.setContainer(((WTDocument)objTemplate).getContainer());
        }
        catch(WTPropertyVetoException wte)
        {
            throw new WTException(wte, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "2", null);
        }
        catch(WTException wte)
        {
            throw new WTException(wte, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "2", null);
        }
        return document;
    }

    protected WTDocument getNewWTDocument(TypeIdentifier objType)
        throws WTException
    {
        WTDocument document = WTDocument.newWTDocument();
        document = setType(document, objType);
        return document;
    }

    protected WTDocument setType(WTDocument document, TypeIdentifier objType)
        throws WTException
    {
        Class doc_class = document.getClass();
        Class obj_class = TypeHelper.getTypeClass(objType);
        if(!doc_class.equals(obj_class))
        {
            String classname = obj_class.getName();
            int i = classname.lastIndexOf('.');
            String just_classname = classname.substring(i + 1);
            String just_method = "new" + just_classname;
            try
            {
                Method method_to_run = obj_class.getMethod(just_method, null);
                document = (WTDocument)method_to_run.invoke(null, null);
            }
            catch(InvocationTargetException ite)
            {
                throw new WTException(ite);
            }
            catch(IllegalAccessException iae)
            {
                throw new WTException(iae);
            }
            catch(NoSuchMethodException nsme)
            {
                throw new WTException(nsme);
            }
        }
        document = (WTDocument)TypeHelper.setType(document, objType);
        return document;
    }

    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.enterprise.templates.doc.server.serverResource";
    private static final String CLASSNAME;
    static final long serialVersionUID = 1L;
    public static final long EXTERNALIZATION_VERSION_UID = 0xd4b6b5eeea339daL;
    protected static final long OLD_FORMAT_VERSION_UID = 0xe9fcf92fe6703be6L;
    private static boolean VERBOSE = false;

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