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

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

import java.io.*;
import java.util.Properties;
import wt.content.ContentHTTPStream;
import wt.doc.WTDocumentConfigSpec;
import wt.doc.WTDocumentStandardConfigSpec;
import wt.fc.*;
import wt.lifecycle.State;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.templateutil.processor.*;
import wt.util.*;
import wt.vc.baseline.ManagedBaseline;
import wt.vc.config.BaselineConfigSpec;
import wt.vc.config.LifeCycleConfigSpec;

public class ChooseConfigSpecFormTaskDelegate extends FormTaskDelegate
    implements Serializable
{

    public ChooseConfigSpecFormTaskDelegate()
    {
    }

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

    public void processAction(ContentHTTPStream contentStream)
        throws Exception
    {
        Properties fData = getFormData();
        Properties qData = getQueryData();
        String formName = (String)fData.get("formName");
        String pageName = (String)fData.get("pageName");
        String redirectURL = (String)fData.get("RETURN_URL");
        if(formName == null)
            throw new WTException("com.ptc.windchill.pdmlink.doc.server.processors.processorsResource", "55", null);
        boolean formOK = validateForm(pageName, fData);
        saveFormDataInSession(formName, fData);
        if(formOK)
            if(getContextAction().equals("DoChooseDocConfigSpec"))
            {
                doCreation(formName);
                RedirectObject redirectObj = new RedirectObject();
                fData.put("TARGET", "_parent");
                redirectObj.setRedirection(redirectURL, fData, getState());
            } else
            {
                doSwitch(formName);
            }
    }

    private boolean validateForm(String pageName, Properties data)
        throws Exception
    {
        if(pageName.equals("ChooseLatestDocConfigSpec"))
        {
            boolean dataOK = true;
            return dataOK;
        }
        if(pageName.equals("ChooseBaselineDocConfigSpec"))
        {
            boolean dataOK = true;
            return dataOK;
        } else
        {
            return true;
        }
    }

    private void doCreation(String formName)
        throws Exception
    {
        updateConfigSpec(formName);
        deleteFormDataFromSession(formName);
    }

    private void doSwitch(String formName)
        throws Exception
    {
        updateConfigSpec(formName);
        switchPages((String)getQueryData().get("nextAction"), null, null);
        deleteFormDataFromSession(formName);
    }

    private String encode(Serializable obj)
        throws WTException
    {
        return Encoder.encode(obj);
        IOException ioe;
        ioe;
        throw new WTException(ioe);
    }

    private Object decode(String encodedConfigSpec)
        throws WTException
    {
        return Encoder.decode(encodedConfigSpec);
        ClassNotFoundException cnfe;
        cnfe;
        throw new WTException(cnfe);
        IOException ioe;
        ioe;
        throw new WTException(ioe);
    }

    private void updateConfigSpec(String formName)
        throws WTException, WTPropertyVetoException
    {
        ServletSessionCookie cookie = getState().getCookieManager().getCookie(formName);
        String pageName = (String)cookie.get("pageName");
        String configSpecString = (String)cookie.get("DOC_CONFIG_SPEC");
        if(VERBOSE)
            System.out.println("Config Spec String from cookie = " + configSpecString);
        ReferenceFactory factory = new ReferenceFactory();
        WTDocumentConfigSpec spec = (WTDocumentConfigSpec)decode(configSpecString);
        if(pageName.equals("ChooseLatestDocConfigSpec"))
        {
            WTDocumentStandardConfigSpec standard = spec.getStandard();
            String stateFieldValue = (String)cookie.get("std_lcstate");
            String workingIncludedValue = (String)cookie.get("workingIncluded");
            State newState = null;
            if(stateFieldValue != null && !stateFieldValue.equals(""))
                standard.setLifeCycleState(State.toState(stateFieldValue));
            else
                standard.setLifeCycle(new LifeCycleConfigSpec());
            standard.setWorkingIncluded("true".equals(workingIncludedValue));
            spec.setStandardActive(true);
        } else
        if(pageName.equals("ChooseBaselineDocConfigSpec"))
        {
            String baselineFieldValue = (String)cookie.get("baseline");
            String baselineReferenceFieldValue = (String)cookie.get("bl_baselineReference");
            if(!baselineReferenceFieldValue.equals("none"))
            {
                ManagedBaseline managedBaseline = (ManagedBaseline)factory.getReference(baselineReferenceFieldValue).getObject();
                spec.setBaseline(BaselineConfigSpec.newBaselineConfigSpec(managedBaseline));
                spec.setBaselineActive(true);
            } else
            if(baselineFieldValue != null && !baselineFieldValue.equals(""))
            {
                QuerySpec querySpec = new QuerySpec(wt.vc.baseline.ManagedBaseline.class);
                querySpec.appendSearchCondition(new SearchCondition(wt.vc.baseline.ManagedBaseline.class, "name", "=", baselineFieldValue));
                QueryResult result = PersistenceHelper.manager.find(querySpec);
                if(result.size() == 1)
                {
                    ManagedBaseline managedBaseline = (ManagedBaseline)result.nextElement();
                    spec.setBaseline(BaselineConfigSpec.newBaselineConfigSpec(managedBaseline));
                    spec.setBaselineActive(true);
                }
            }
        }
        getFormData().remove("DOC_CONFIG_SPEC");
        getFormData().put("DOC_CONFIG_SPEC", encode(spec));
        Properties queryData = getQueryData();
        String cookieName = queryData.getProperty("cookieName");
        if(cookieName == null || cookieName.length() == 0)
            cookieName = getFormData().getProperty("cookieName");
        ServletSessionCookie servSessCookie = getState().getCookieManager().getCookie(cookieName);
        servSessCookie.put("configSpec", spec);
    }

    private static final String RESOURCE = "com.ptc.windchill.pdmlink.doc.server.processors.processorsResource";
    private static final String CLASSNAME;
    public static final String NAME_FIELD = "NameField";
    public static final String NUMBER_FIELD = "NumberField";
    public static final String IS_RELEASED_FIELD = "IsReleasedField";
    public static final String DESCRIPTION_FIELD = "DescriptionField";
    private static boolean VERBOSE;

    static 
    {
        CLASSNAME = (com.ptc.windchill.pdmlink.doc.server.processors.ChooseConfigSpecFormTaskDelegate.class).getName();
        try
        {
            WTProperties properties = WTProperties.getLocalProperties();
            VERBOSE = properties.getProperty("wt.templateutil.processor.verbose", false);
            VERBOSE = properties.getProperty("com.ptc.windchill.pdmlink.doc.client.verbose", VERBOSE);
        }
        catch(Throwable t)
        {
            t.printStackTrace(System.err);
            throw new ExceptionInInitializerError(t);
        }
    }
}
