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

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

import com.ptc.windchill.enterprise.templates.TemplatesContextDelegate;
import com.ptc.windchill.enterprise.templates.TemplatesUtility;
import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import wt.doc.WTDocument;
import wt.enterprise.RevisionControlled;
import wt.enterprise.Templateable;
import wt.fc.*;
import wt.folder.*;
import wt.inf.container.*;
import wt.lifecycle.*;
import wt.org.*;
import wt.pds.PDSObjectInput;
import wt.project.Role;
import wt.util.*;
import wt.vc.wip.*;
import wt.workflow.work.WorkflowHelper;
import wt.workflow.work.WorkflowService;

public class TemplatesEnterpriseWTDocumentDelegate
    implements TemplatesContextDelegate, Externalizable
{

    public TemplatesEnterpriseWTDocumentDelegate()
    {
    }

    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(TemplatesEnterpriseWTDocumentDelegate 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 != 0x36556656af2b3adL)
            throw new InvalidClassException(CLASSNAME, "Local class not compatible: stream classdesc externalizationVersionUID=" + readSerialVersionUID + " local class externalizationVersionUID=" + 0xd4b6b5eeea339daL);
        else
            return success;
    }

    public Templateable doCreateTemplateContextTasks(Templateable object, HashMap inputHash)
        throws WTException
    {
        String context = null;
        try
        {
            WTContainer container = null;
            context = (String)inputHash.get("context");
            if(context.equalsIgnoreCase("Classic"))
                ((WTDocument)object).setContainer(WTContainerHelper.service.getClassicContainer());
            else
            if(context != null)
            {
                container = (WTContainer)referenceFactory.getReference(context).getObject();
                ((WTDocument)object).setContainer(container);
            }
            String orgDN = (String)inputHash.get("OrganizationDN");
            WTOrganization wtorg = null;
            if(orgDN != null)
            {
                orgDN = orgDN.trim();
                wt.org.WTPrincipal p = OrganizationServicesHelper.manager.getPrincipalByDN(orgDN);
                wtorg = (WTOrganization)p;
                ((WTDocument)object).setOrganization(wtorg);
            }
            String orgRef = (String)inputHash.get("orgRef");
            if(orgRef != null && orgRef.length() > 0)
                ((WTDocument)object).setOrganizationReference(WTPrincipalReference.newWTPrincipalReference(new ObjectIdentifier(orgRef)));
            String location = (String)inputHash.get("folderRef");
            String locationRef = (String)inputHash.get("location");
            if(location == null && locationRef == null)
                location = "/Default";
            if(location != null)
            {
                Folder folder = FolderHelper.service.getFolder(location);
                FolderHelper.assignLocation((WTDocument)object, folder);
            }
            Folder folderN = null;
            if(locationRef != null)
            {
                folderN = (Folder)referenceFactory.getReference(locationRef.trim()).getObject();
                FolderHelper.assignLocation((WTDocument)object, folderN);
            }
        }
        catch(WTPropertyVetoException wte)
        {
            throw new WTException(wte, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "1", null);
        }
        catch(WTException wte)
        {
            throw new WTException(wte, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "1", null);
        }
        return object;
    }

    public RevisionControlled doCreateFromTemplateContextTasks(RevisionControlled object, Templateable template, HashMap inputHash)
        throws WTException
    {
        String context = null;
        try
        {
            WTContainer container = null;
            context = (String)inputHash.get("context");
            if(context.equalsIgnoreCase("Classic"))
                ((WTDocument)object).setContainer(WTContainerHelper.service.getClassicContainer());
            else
            if(context != null)
            {
                container = (WTContainer)referenceFactory.getReference(context).getObject();
                if(WTContainerHelper.isClassicContainer(container) && VERBOSE)
                    System.out.println("Setting context to the Classic Container");
                ((WTDocument)object).setContainer(container);
            }
            String orgDN = (String)inputHash.get("OrganizationDN");
            WTOrganization wtorg = null;
            if(orgDN != null)
            {
                orgDN = orgDN.trim();
                wt.org.WTPrincipal p = OrganizationServicesHelper.manager.getPrincipalByDN(orgDN);
                wtorg = (WTOrganization)p;
                ((WTDocument)object).setOrganization(wtorg);
            }
            String org_Ref = (String)inputHash.get("orgRef");
            if(org_Ref != null && org_Ref.length() > 0)
                ((WTDocument)object).setOrganizationReference(WTPrincipalReference.newWTPrincipalReference(new ObjectIdentifier(org_Ref)));
            String location = (String)inputHash.get("folderRef");
            String locationRef = (String)inputHash.get("location");
            if(location == null && locationRef == null)
                location = "/Default";
            if(location != null)
            {
                Folder folder = FolderHelper.service.getFolder(location);
                FolderHelper.assignLocation((WTDocument)object, folder);
            }
            Folder folderN = null;
            if(locationRef != null)
            {
                folderN = (Folder)referenceFactory.getReference(locationRef.trim()).getObject();
                FolderHelper.assignLocation((WTDocument)object, folderN);
            }
        }
        catch(WTException wte)
        {
            throw new WTException(wte, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "4", null);
        }
        catch(WTPropertyVetoException wtpve)
        {
            throw new WTException(wtpve, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "3", null);
        }
        return object;
    }

    public Templateable doPostCreateTemplateContextTasks(Templateable object, HashMap inputHash)
        throws WTException
    {
        String submit = null;
        WTDocument doc = null;
        doc = (WTDocument)object;
        submit = (String)inputHash.get("submit");
        if(submit != null && submit.equalsIgnoreCase("true"))
        {
            submit = submit.trim();
            Enumeration e = null;
            for(int i = 0; i < 10; i++)
            {
                e = WorkflowHelper.service.getWorkItems(doc, Role.SUBMITTER);
                if(e.hasMoreElements())
                    break;
                try
                {
                    Thread.sleep(1000L);
                }
                catch(InterruptedException ie) { }
            }

            try
            {
                doc = (WTDocument)LifeCycleHelper.service.submitForApproval(doc);
            }
            catch(LifeCycleException lce)
            {
                Object param[] = {
                    doc.getName()
                };
                throw new WTException(lce, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "6", param);
            }
            catch(WTException wte)
            {
                Object param[] = {
                    doc.getName()
                };
                throw new WTException(wte, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "6", param);
            }
        }
        return doc;
    }

    public RevisionControlled doPostCreateFromTemplateContextTasks(RevisionControlled object, Templateable template, HashMap inputHash)
        throws WTException
    {
        String submit = null;
        WTDocument doc = null;
        doc = (WTDocument)object;
        submit = (String)inputHash.get("submit");
        if(submit != null && submit.equalsIgnoreCase("true"))
        {
            submit = submit.trim();
            Enumeration e = null;
            for(int i = 0; i < 10; i++)
            {
                e = WorkflowHelper.service.getWorkItems(doc, Role.SUBMITTER);
                if(e.hasMoreElements())
                    break;
                try
                {
                    Thread.sleep(1000L);
                }
                catch(InterruptedException ie) { }
            }

            try
            {
                doc = (WTDocument)LifeCycleHelper.service.submitForApproval(doc);
            }
            catch(LifeCycleException lce)
            {
                Object param[] = {
                    doc.getName()
                };
                throw new WTException(lce, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "6", param);
            }
            catch(WTException wte)
            {
                Object param[] = {
                    doc.getName()
                };
                throw new WTException(wte, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "6", param);
            }
        }
        object = doc;
        if(TemplatesUtility.getFilenameFormat() != null && (object instanceof WTDocument))
            object = (WTDocument)TemplatesUtility.setFilename((WTDocument)object);
        String keepCheckedOut = null;
        keepCheckedOut = (String)inputHash.get("keepCheckedOut");
        if(keepCheckedOut != null)
        {
            keepCheckedOut = keepCheckedOut.trim();
            if(keepCheckedOut.equalsIgnoreCase("true"))
            {
                CheckoutLink checkedout = null;
                Folder folder = WorkInProgressHelper.service.getCheckoutFolder();
                if(folder == null)
                    throw new WTException("com.ptc.windchill.enterprise.templates.doc.server.serverResource", "5", null);
                try
                {
                    checkedout = WorkInProgressHelper.service.checkout(object, folder, "");
                }
                catch(WTPropertyVetoException wtpve)
                {
                    throw new WTException(wtpve, "com.ptc.windchill.enterprise.templates.doc.server.serverResource", "4", null);
                }
                wt.vc.wip.Workable workingObj = checkedout.getWorkingCopy();
                return (RevisionControlled)workingObj;
            }
        }
        return object;
    }

    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 = 0x36556656af2b3adL;
    private static final ReferenceFactory referenceFactory = new ReferenceFactory();
    private static boolean VERBOSE = false;

    static 
    {
        CLASSNAME = (com.ptc.windchill.enterprise.templates.doc.server.TemplatesEnterpriseWTDocumentDelegate.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.TemplatesEnterpriseWTDocumentDelegate : Error reading com.ptc.windchill.enterprise.* properties");
            t.printStackTrace(System.err);
            throw new ExceptionInInitializerError(t);
        }
    }
}
