// 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:   EPMDocCADNameConflictCheck.java

package wt.epm.upgrade;

import java.io.*;
import java.sql.*;
import java.util.HashMap;
import wt.method.MethodContext;
import wt.method.RemoteAccess;
import wt.pom.WTConnection;
import wt.services.LightweightServiceHelper;
import wt.util.WTException;
import wt.util.WTProperties;

public class EPMDocCADNameConflictCheck
{
    public static class Server
        implements RemoteAccess
    {
        protected static class CadNameValue
        {

            String docNumber;
            String authApp;
            long masterId;

            protected CadNameValue(String s, String s1, long l)
            {
                docNumber = s;
                authApp = s1;
                masterId = l;
            }
        }

        protected static class CadNameProjKey
        {

            public boolean equals(Object obj)
            {
                if(obj instanceof CadNameProjKey)
                    return cadName.equals(((CadNameProjKey)obj).cadName) && projId == ((CadNameProjKey)obj).projId;
                else
                    return false;
            }

            public int hashCode()
            {
                String s = cadName + ":" + projId;
                return s.hashCode();
            }

            String cadName;
            long projId;

            CadNameProjKey(String s, long l)
            {
                cadName = s;
                projId = l;
            }
        }


        public static Boolean EPMDocCADNameConflictCheck()
            throws SQLException
        {
            PreparedStatement preparedstatement;
            PreparedStatement preparedstatement1;
            PreparedStatement preparedstatement2;
            PreparedStatement preparedstatement3;
            PreparedStatement preparedstatement4;
            PreparedStatement preparedstatement5;
            PreparedStatement preparedstatement6;
            FileWriter filewriter;
            Connection connection;
            boolean flag;
            preparedstatement = null;
            preparedstatement1 = null;
            preparedstatement2 = null;
            preparedstatement3 = null;
            preparedstatement4 = null;
            preparedstatement5 = null;
            preparedstatement6 = null;
            Object obj = null;
            File file = new File(WTProperties.getLocalProperties().getProperty("wt.logs.dir"));
            File file1 = new File(file, "EPMDocCADNameConflict.log");
            filewriter = new FileWriter(file1);
            flag = false;
            Object obj1 = null;
            Object obj2 = null;
            try
            {
                WTConnection wtconnection = (WTConnection)MethodContext.getContext().getConnection();
                connection = wtconnection.getConnection();
                break MISSING_BLOCK_LABEL_108;
            }
            catch(Exception exception)
            {
                filewriter.write("Exception in getting connection");
            }
            return Boolean.FALSE;
            Boolean boolean1;
            System.out.println("Checking CADName conflicts");
            if(EPMDocCADNameConflictCheck.columnExists(connection, "EPMDOCUMENTMASTER", "CADNAME"))
                break MISSING_BLOCK_LABEL_146;
            filewriter.write("CADNames do not exist in this database.  Conflict checking not needed.\n");
            boolean1 = Boolean.TRUE;
            return boolean1;
            Boolean boolean3;
            boolean flag1 = false;
            boolean flag2 = EPMDocCADNameConflictCheck.columnExists(connection, "EPMDOCUMENTMASTER", "IDA3CONTAINERREFERENCE");
            if(!flag2 && EPMDocCADNameConflictCheck.tableExists(connection, "PROJECTDOMAINLINK"))
                flag1 = true;
            boolean flag3 = false;
            boolean flag4 = false;
            if(EPMDocCADNameConflictCheck.tableExists(connection, "EPMSEPFAMILYTABLE"))
            {
                flag3 = true;
                WTProperties wtproperties = WTProperties.getLocalProperties();
                String s1 = wtproperties.getProperty("wt.epm.CADNameUniquenessOption");
                if(s1 != null && s1.equals("org"))
                    flag4 = true;
            }
            String s = "SELECT a2.value2, a2.ida2a2 FROM StringDefinition a0, EPMDocument a1, StringValue a2 WHERE (a0.name = 'Legend_Primary_Files' OR a0.name = 'Legend:Primary Files') AND a2.ida3a6 = a0.ida2a2 AND a2.ida3a4 = a1.ida2a2 AND a1.ida3MasterReference = ? AND a1.latestIterationInfo = 1 ORDER BY a1.versionida2versioninfo DESC ";
            preparedstatement = connection.prepareStatement(s);
            String s2 = "SELECT a2.value2, a2.ida2a2 FROM StringDefinition a0, EPMDocument a1, StringValue a2 WHERE (a0.name = 'tdm_identifier') AND a2.ida3a6 = a0.ida2a2 AND a2.ida3a4 = a1.ida2a2 AND a1.ida3MasterReference = ? AND a1.latestIterationInfo = 1 ORDER BY a1.versionida2versioninfo DESC ";
            preparedstatement1 = connection.prepareStatement(s2);
            String s3 = "SELECT pdl.classnameKeyRoleAObjectRef, pdl.ida3a5 FROM ProjectDomainLink pdl, EPMDocument d WHERE d.ida3MasterReference = ? AND d.latestIterationInfo = 1 AND d.ida3DomainRef = pdl.ida3b5 ORDER BY d.versionida2versioninfo DESC ";
            preparedstatement2 = connection.prepareStatement(s3);
            String s4 = "SELECT gm.CADName FROM EPMDocument i, EPMDocument g, EPMDocumentMaster gm, EPMContainedIn cii, EPMContainedIn cig, EPMSepFamilyTable ft WHERE (i.familyTableStatus = 1 OR i.familyTableStatus = 3) AND i.idA3MasterReference = ? AND i.idA2A2 = cii.idA3A5 AND gm.idA2A2 = g.idA3MasterReference AND g.idA2A2 = cig.idA3A5 AND g.familyTableStatus = 2 AND cig.idA3B5 = ft.idA2A2 AND cii.idA3B5 = ft.idA2A2";
            preparedstatement6 = connection.prepareStatement(s4);
            String s5 = "SELECT gm.CADName FROM EPMDocument i, EPMContainedIn ci, EPMDocument g, EPMDocumentMaster gm WHERE i.idA3MasterReference = ? AND i.idA2A2 = ci.idA3A5 and ci.idA3B5 = g.idA2A2 and g.idA3MasterReference = gm.idA2A2";
            preparedstatement5 = connection.prepareStatement(s5);
            String s6 = "SELECT COUNT(*) FROM EPMDocumentMaster";
            preparedstatement3 = connection.prepareStatement(s6);
            ResultSet resultset = preparedstatement3.executeQuery();
            System.out.println(preparedstatement3 + " masters to check");
            resultset.next();
            HashMap hashmap = new HashMap(resultset.getInt(1));
            resultset.close();
            String s7 = "SELECT cadname, authoringapplication, documentnumber, ida2a2 ";
            if(flag2)
                s7 = s7 + ", classnamekeycontainerreferen, ida3containerreference ";
            s7 = s7 + "FROM EPMDocumentMaster";
            preparedstatement4 = connection.prepareStatement(s7);
            ResultSet resultset1 = preparedstatement4.executeQuery();
            Object obj3 = null;
            Object obj4 = null;
            Object obj5 = null;
            long l = 0L;
            String s11 = null;
            long l2 = 0L;
            do
            {
                if(!resultset1.next())
                    break;
                String s8 = resultset1.getString(1);
                String s9 = resultset1.getString(2);
                String s10 = resultset1.getString(3);
                long l1 = resultset1.getLong(4);
                System.out.println("master id = " + l1);
                if(flag2)
                {
                    s11 = resultset1.getString(5);
                    l2 = resultset1.getLong(6);
                } else
                if(flag1)
                {
                    preparedstatement2.setLong(1, l1);
                    ResultSet resultset2 = preparedstatement2.executeQuery();
                    if(resultset2.next())
                    {
                        s11 = resultset2.getString(1);
                        l2 = resultset2.getLong(2);
                    }
                    resultset2.close();
                }
                boolean flag5 = false;
                boolean flag6 = false;
                if(!"wt.projmgmt.admin.Project2".equals(s11))
                {
                    if(!flag4)
                    {
                        s11 = null;
                        l2 = 0L;
                    }
                } else
                {
                    flag5 = true;
                }
                if(s9.equals("IDEAS") && !flag3)
                {
                    preparedstatement1.setLong(1, l1);
                    ResultSet resultset3 = preparedstatement1.executeQuery();
                    flag6 = resultset3.next();
                    if(flag6)
                        s8 = parseTDM(resultset3.getString(1));
                    resultset3.close();
                }
                if(s8 == null)
                {
                    if(!flag6)
                    {
                        preparedstatement.setLong(1, l1);
                        ResultSet resultset4 = preparedstatement.executeQuery();
                        flag6 = resultset4.next();
                        if(flag6)
                        {
                            s8 = resultset4.getString(1);
                            if(flag5)
                                s8 = stripName(s8);
                        }
                        resultset4.close();
                    }
                    if(!flag6)
                        s8 = s10;
                }
                Object obj6 = null;
                if(s9.equals("CATIAV5") || s9.equals("SOLIDWORKS") || s9.equals("ACAD") || s9.equals("UG"))
                {
                    System.out.println("Master id for query is " + l1);
                    ResultSet resultset5;
                    if(flag3)
                    {
                        preparedstatement6.setLong(1, l1);
                        resultset5 = preparedstatement6.executeQuery();
                    } else
                    {
                        preparedstatement5.setLong(1, l1);
                        resultset5 = preparedstatement5.executeQuery();
                    }
                    if(resultset5.next())
                    {
                        String s12 = resultset5.getString(1);
                        if(!lastExtension(s8).equals(lastExtension(s12)))
                            s8 = s8 + lastExtension(s12);
                    }
                    resultset5.close();
                }
                CadNameProjKey cadnameprojkey = new CadNameProjKey(s8.toLowerCase(), l2);
                CadNameValue cadnamevalue = new CadNameValue(s10, s9, l1);
                CadNameValue cadnamevalue1 = (CadNameValue)hashmap.put(cadnameprojkey, cadnamevalue);
                if(cadnamevalue1 != null)
                {
                    String s13 = cadnamevalue1.docNumber;
                    String s14 = cadnamevalue1.authApp;
                    long l3 = cadnamevalue1.masterId;
                    filewriter.write("Conflict for CADName " + s8 + ":\n");
                    filewriter.write("  Document Number " + s10 + " with authoring application " + s9 + "    Database id " + l1 + "\n");
                    filewriter.write("  Document Number " + s13 + " with authoring application " + s14 + "    Database id " + l3 + "\n");
                    flag = true;
                }
            } while(true);
            if(flag)
                break MISSING_BLOCK_LABEL_1239;
            filewriter.write("No conflicts found\n");
            boolean3 = Boolean.TRUE;
            return boolean3;
            boolean3 = Boolean.FALSE;
            return boolean3;
            SQLException sqlexception;
            sqlexception;
            Boolean boolean2;
            sqlexception.printStackTrace();
            for(; sqlexception != null; sqlexception = sqlexception.getNextException())
            {
                filewriter.write("SQLState: " + sqlexception.getSQLState() + "\n");
                filewriter.write("Code:     " + sqlexception.getErrorCode() + "\n");
                filewriter.write("Message:  " + sqlexception.getMessage() + "\n");
            }

            boolean2 = Boolean.FALSE;
            return boolean2;
            local;
            filewriter.close();
            if(preparedstatement != null)
                preparedstatement.close();
            if(preparedstatement1 != null)
                preparedstatement1.close();
            if(preparedstatement2 != null)
                preparedstatement2.close();
            if(preparedstatement3 != null)
                preparedstatement3.close();
            if(preparedstatement4 != null)
                preparedstatement4.close();
            if(preparedstatement5 != null)
                preparedstatement5.close();
            if(preparedstatement6 != null)
                preparedstatement6.close();
            JVM INSTR ret 51;
            IOException ioexception;
            ioexception;
            ioexception.printStackTrace();
            return Boolean.FALSE;
        }

        private static String parseTDM(String s)
        {
            int i = s.lastIndexOf('$');
            int j = s.lastIndexOf('$', i - 1) + 1;
            String s1 = s.substring(j, i);
            if(s1.startsWith("."))
            {
                String s2 = s.substring(s.lastIndexOf("$", j - 2) + 1, j - 1);
                s1 = s2.concat(s1);
            }
            return s1;
        }

        protected static String stripName(String s)
        {
            if(isPackage(s))
            {
                int i = s.lastIndexOf(".");
                return s.substring(0, i);
            } else
            {
                return s;
            }
        }

        private static boolean isPackage(String s)
        {
            String s1 = lastExtension(s);
            if(isJar(s))
            {
                int i = s.lastIndexOf(".");
                String s2 = lastExtension(s.substring(0, i));
                return !s2.equals("");
            } else
            {
                return false;
            }
        }

        private static String lastExtension(String s)
        {
            String s1 = "";
            int i = s.lastIndexOf(".");
            if(i > 0)
                s1 = s.substring(i);
            return s1;
        }

        private static boolean isJar(String s)
        {
            String s1 = "";
            int i = s.lastIndexOf(".");
            if(i > 0)
                s1 = s.substring(i);
            return s1.equalsIgnoreCase(".jar");
        }

        public Server()
        {
        }
    }


    public EPMDocCADNameConflictCheck()
    {
    }

    public static void main(String args[])
        throws WTException
    {
        if(args.length > 0)
        {
            printUsage();
        } else
        {
            Boolean boolean1 = (Boolean)LightweightServiceHelper.invoke("EPMDocCADNameConflictCheck", "wt.epm.upgrade.EPMDocCADNameConflictCheck$Server", new Class[0], args);
            if(!boolean1.booleanValue())
                System.out.println("There are conflicts or errors!  Please check the EPMDocCADNameConflict log in the logs directory. ");
            else
                System.out.println("No conflicts found.  Please check details in the EPMDocCADNameConflict log in the logs directory");
        }
        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: EPMDocCADNameConflictCheck ");
        System.out.println("\n      This command takes no arguments");
    }


}
