// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ChangeCADName.java

package wt.epm.upgrade;

import java.io.*;
import java.sql.*;
import wt.method.MethodContext;
import wt.method.RemoteAccess;
import wt.pom.*;
import wt.services.LightweightServiceHelper;
import wt.util.WTException;
import wt.util.WTProperties;

public class ChangeCADName
{
    public static class Server
        implements RemoteAccess
    {

        public static Boolean ChangeCADName(String s, String s1, String s2, String s3)
        {
            FileWriter filewriter;
            Connection connection;
            String s4;
            String s5;
            long l;
            File file = new File(WTProperties.getLocalProperties().getProperty("wt.logs.dir"));
            File file1 = new File(file, "ChangeCADName.log");
            filewriter = new FileWriter(file1);
            s4 = null;
            s5 = null;
            l = Long.valueOf(s2).longValue();
            try
            {
                WTConnection wtconnection = (WTConnection)MethodContext.getContext().getConnection();
                connection = wtconnection.getConnection();
                break MISSING_BLOCK_LABEL_90;
            }
            catch(Exception exception)
            {
                filewriter.write("Exception in getting connection");
            }
            return Boolean.FALSE;
            Transaction transaction = null;
            Boolean boolean1;
            transaction = new Transaction();
            transaction.start();
            if(ChangeCADName.columnExists(connection, "EPMDOCUMENTMASTER", "CADNAME"))
                break MISSING_BLOCK_LABEL_137;
            filewriter.write("CADNames do not exist in this database!");
            boolean1 = Boolean.TRUE;
            return boolean1;
            boolean flag;
            Object obj1;
            flag = false;
            if(ChangeCADName.tableExists(connection, "EPMSEPFAMILYTABLE"))
                flag = true;
            filewriter.write("Upgrading from release ");
            boolean flag1 = false;
            PreparedStatement preparedstatement = connection.prepareStatement("SELECT cadname, ownerApplication FROM EPMDocumentMaster WHERE idA2A2 = ? AND documentNumber = ? AND authoringApplication = ? ");
            preparedstatement.setLong(1, l);
            preparedstatement.setString(2, s);
            preparedstatement.setString(3, s1);
            for(ResultSet resultset = preparedstatement.executeQuery(); resultset.next(); filewriter.write("origName = " + s4 + " ownerApp = " + s5 + "\n"))
            {
                s4 = resultset.getString(1);
                s5 = resultset.getString(2);
                flag1 = true;
            }

            if(flag1)
                break MISSING_BLOCK_LABEL_310;
            filewriter.write("No matching Document Name/Authoring Application/oid found.  Please check your input values");
            obj1 = Boolean.FALSE;
            return ((Boolean) (obj1));
            if(!s5.equals("PROINTRALINKGATEWAY"))
                break MISSING_BLOCK_LABEL_353;
            if(s4 != null)
                filewriter.write("This tool does not support rename for CAD Documents published by Pro/IntraLink Gateway.  Please change it in Pro/IntraLink and republish.");
            else
                filewriter.write("This CAD Document does not have a CADName already set, and this tool does not support setting CADNames \nfor CAD Documents published by Pro/Intralink Gateway.  During upgrade, the CADName will be set \nto be the same as the CAD Document Number.  Please change the number in Pro/IntraLink and republish.\n");
            obj1 = Boolean.FALSE;
            return ((Boolean) (obj1));
            if(s5.equals("OPTEGRAGATEWAY"))
                break MISSING_BLOCK_LABEL_811;
            if(!s1.equals("CADDS5") && !s1.equals("CATIA"))
                break MISSING_BLOCK_LABEL_414;
            if(s4 != null)
                filewriter.write("This tool does not support rename for this authoring application.  Please use the rename command in the application.\n");
            else
                filewriter.write("This CAD Document does not have a CADName already set, and this tool does not support setting CADNames \nfor CAD Documents for this authoring application.  During upgrade, the CADName will be set \nto be the same as the CAD Document Number.  Please change the number in the application.\n");
            obj1 = Boolean.FALSE;
            return ((Boolean) (obj1));
            Object obj2;
            if(!s1.equals("PROE"))
                break MISSING_BLOCK_LABEL_532;
            obj1 = connection.prepareStatement("SELECT COUNT(*) FROM StringDefinition a0, EPMDocument a1, StringValue a2 WHERE a1.ida3MasterReference = ?  AND a0.name = 'Legend_Primary_Files'  AND a2.ida3a6 = a0.ida2a2 AND a2.ida3a4 = a1.ida2a2");
            ((PreparedStatement) (obj1)).setLong(1, l);
            ResultSet resultset1 = ((PreparedStatement) (obj1)).executeQuery();
            resultset1.next();
            int i1 = resultset1.getInt(1);
            if(i1 != 0)
                break MISSING_BLOCK_LABEL_509;
            if(s4 != null)
                filewriter.write("This tool does not support rename for this authoring application.  Please use the rename command in the application.\n");
            else
                filewriter.write("This CAD Document does not have a CADName already set, and this tool does not support setting CADNames \nfor CAD Documents for this authoring application.  During upgrade, the CADName will be set \nto be the same as the CAD Document Number.  Please change the number in the application.\n");
            obj2 = Boolean.FALSE;
            return ((Boolean) (obj2));
            if(s4 != null)
                break MISSING_BLOCK_LABEL_811;
            filewriter.write("This CAD Document does not have a CADName already set, and this tool does not support setting CADNames \nfor CAD Documents for this authoring application.  In order to change the filename in the application \nuse File->SaveAs functionality and then checkin the CAD Document.\n");
            obj2 = Boolean.FALSE;
            return ((Boolean) (obj2));
            if(!s1.equals("IDEAS") || flag)
                break MISSING_BLOCK_LABEL_564;
            filewriter.write("This tool does not support rename for CAD Documents authored by IDEAS.  \nThe CADName will automatically be changed during upgrade to a value stored internally \nto the document.");
            obj1 = Boolean.FALSE;
            return ((Boolean) (obj1));
            if(!s1.equals("ACAD") && !s1.equals("CATIAV5") && !s1.equals("SOLIDWORKS") && !s1.equals("UG") && !s1.equals("INVENTOR") && !s1.equals("IDEAS"))
                break MISSING_BLOCK_LABEL_811;
            if(s4 != null)
                break MISSING_BLOCK_LABEL_641;
            filewriter.write("This CAD Document does not have a CADName already set, and this tool does not support setting CADNames \nfor CAD Documents for this authoring application.  In order to change the filename in the application \nuse File->SaveAs functionality and then checkin the CAD Document.");
            obj1 = Boolean.FALSE;
            return ((Boolean) (obj1));
            Boolean boolean3;
            boolean flag2 = false;
            if(!flag)
            {
                PreparedStatement preparedstatement2 = connection.prepareStatement("SELECT COUNT(*) FROM EPMContainedIn a0, EPMDocument a1 WHERE a1.ida3MasterReference = ? AND a1.ida2a2 = a0.ida3b5");
                preparedstatement2.setLong(1, l);
                ResultSet resultset2 = preparedstatement2.executeQuery();
                resultset2.next();
                int j1 = resultset2.getInt(1);
                if(j1 > 0)
                    flag2 = true;
            } else
            {
                PreparedStatement preparedstatement3 = connection.prepareStatement("SELECT DISTINCT familyTableStatus FROM EPMDocument WHERE ida3MasterReference = ? and latestIterationInfo = 1");
                preparedstatement3.setLong(1, l);
                ResultSet resultset3 = preparedstatement3.executeQuery();
                do
                {
                    if(!resultset3.next())
                        break;
                    if(resultset3.getInt(1) == 2)
                        flag2 = true;
                } while(true);
            }
            if(!flag2)
                break MISSING_BLOCK_LABEL_811;
            filewriter.write("This tool does not support rename for family table generics with authoring application " + s1 + ".  You are allowed to are allowed to rename instances.\n");
            boolean3 = Boolean.FALSE;
            return boolean3;
            if(s4 != null)
                break MISSING_BLOCK_LABEL_834;
            filewriter.write("This CAD Document does not have a CADName already set, and this tool does not support setting CADNames \nfor CAD Documents for this authoring application.  During upgrade, the CADName will be set \nto be the same as the CAD Document Number.  Please change the number in the application.\n");
            flag2 = Boolean.FALSE;
            return flag2;
            PreparedStatement preparedstatement1 = connection.prepareStatement("UPDATE EPMDocumentMaster SET cadname = ? WHERE ida2a2 = ?");
            preparedstatement1.setString(1, s3);
            preparedstatement1.setLong(2, l);
            int i = preparedstatement1.executeUpdate();
            filewriter.write(i + " rows were updated on EPMDocumentMaster\n");
            preparedstatement1.close();
            PreparedStatement preparedstatement4 = connection.prepareStatement("UPDATE EPMCADNameSpace SET CADName = LOWER(?) WHERE ida3a3 = ? AND CADName = ?");
            preparedstatement4.setString(1, s3);
            preparedstatement4.setLong(2, l);
            preparedstatement4.setString(3, s4);
            i = preparedstatement4.executeUpdate();
            filewriter.write(i + " rows were updated in EPMCADNameSpace\n");
            preparedstatement4.close();
            if(!s5.equals("OPTEGRAGATEWAY") && !flag)
            {
                j1 = connection.prepareStatement("UPDATE ApplicationData SET role = 'PRIMARY' WHERE ida2a2 IN (SELECT A0.ida2a2  FROM ApplicationData A0, HolderToContent A1, EPMDocument A2, EPMDocumentMaster A3  WHERE ((a0.filename = ?) OR (a0.filename = ?))  AND a1.ida3a5 = a2.ida2a2  AND a2.ida3MasterReference = a3.ida2a2  AND a1.ida3b5 = a0.ida2a2 AND a3.ida2a2 = ?)");
                j1.setString(1, s4);
                j1.setString(2, s4 + ".jar");
                j1.setLong(3, l);
                int j = j1.executeUpdate();
                filewriter.write(j + " contents set to PRIMARY for original cadname\n");
                j1.close();
            }
            if(s1.equals("ACAD") || s1.equals("CATIAV5") || s1.equals("SOLIDWORKS") || s1.equals("UG") || s1.equals("INVENTOR") || s1.equals("IDEAS"))
            {
                j1 = connection.prepareStatement("SELECT count(*) FROM ApplicationData A0, HolderToContent A1, EPMDocument A2, EPMDocumentMaster A3 WHERE a0.role = 'PRIMARY' AND a1.ida3a5 = a2.ida2a2 AND a2.ida3MasterReference = a3.ida2a2 AND a1.ida3b5 = a0.ida2a2 AND a3.ida2a2 = ? AND a2.latestIterationInfo = 1");
                j1.setLong(1, l);
                ResultSet resultset4 = j1.executeQuery();
                int k1 = 0;
                if(resultset4.next())
                    k1 = resultset4.getInt(1);
                if(k1 == 0)
                {
                    String s6 = "UPDATE EPMMemberLink SET asStoredChildName = ? WHERE idA3B5 = ? and asStoredChildName IS NULL";
                    PreparedStatement preparedstatement5 = connection.prepareStatement(s6);
                    preparedstatement5.setString(1, s4);
                    preparedstatement5.setLong(2, l);
                    int k = preparedstatement5.executeUpdate();
                    filewriter.write(k + " member links updated with asStoredChildName\n");
                    String s7 = "UPDATE EPMReferenceLink SET asStoredChildName = ? WHERE idA3B5 = ? and asStoredChildName IS NULL";
                    PreparedStatement preparedstatement6 = connection.prepareStatement(s7);
                    preparedstatement6.setString(1, s4);
                    preparedstatement6.setLong(2, l);
                    k = preparedstatement5.executeUpdate();
                    filewriter.write(k + " reference links updated with asStoredChildName\n");
                }
            }
            transaction.commit();
            transaction = null;
            j1 = Boolean.TRUE;
            return j1;
            Object obj;
            obj;
            Boolean boolean2;
            ((SQLException) (obj)).printStackTrace();
            for(; obj != null; obj = ((SQLException) (obj)).getNextException())
            {
                filewriter.write("SQLState: " + ((SQLException) (obj)).getSQLState() + "\n");
                filewriter.write("Code:     " + ((SQLException) (obj)).getErrorCode() + "\n");
                filewriter.write("Message:  " + ((SQLException) (obj)).getMessage() + "\n");
            }

            boolean2 = Boolean.FALSE;
            return boolean2;
            obj;
            ((PersistenceException) (obj)).printStackTrace();
            boolean2 = Boolean.FALSE;
            return boolean2;
            local;
            if(transaction != null)
                transaction.rollback();
            filewriter.close();
            JVM INSTR ret 30;
            IOException ioexception;
            ioexception;
            ioexception.printStackTrace();
            return Boolean.FALSE;
        }

        public Server()
        {
        }
    }


    public ChangeCADName()
    {
    }

    public static void main(String args[])
        throws WTException
    {
        if(args.length == 4)
        {
            System.out.println("Attempting to change the CADName of Document Number " + args[0] + " with authoring application " + args[1] + " and database id " + args[2] + " to " + args[3]);
            Boolean boolean1 = (Boolean)LightweightServiceHelper.invoke("ChangeCADName", "wt.epm.upgrade.ChangeCADName$Server", new Class[] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            }, args);
            if(!boolean1.booleanValue())
                System.out.println("There are errors!  Please check the ChangeCADName log in the logs directory. ");
            else
                System.out.println("Update successfully made.  Please check details in the ChangeCADName log in the logs directory");
        } else
        {
            printUsage();
        }
        System.exit(0);
    }

    private static boolean columnExists(Connection connection, String s, String s1)
        throws SQLException
    {
        DatabaseMetaData databasemetadata = connection.getMetaData();
        ResultSet resultset = databasemetadata.getColumns(null, databasemetadata.getUserName(), s, s1);
        boolean flag = false;
        if(resultset.next())
            flag = true;
        resultset.close();
        return flag;
    }

    private static boolean tableExists(Connection connection, String s)
        throws SQLException
    {
        DatabaseMetaData databasemetadata = connection.getMetaData();
        ResultSet resultset = databasemetadata.getTables(null, databasemetadata.getUserName(), s, new String[] {
            "TABLE"
        });
        boolean flag = false;
        if(resultset.next())
            flag = true;
        resultset.close();
        return flag;
    }

    private static void printUsage()
    {
        System.out.println("\n   USAGE: ChangeCADName [documentNumber authoringApplication oid newCADName]");
        System.out.println("\n    The values returned from the EPMDocCADNameConflictCheck utility can be used in this utility");
    }


}
