// 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:   EPMDocNumberConflictCheck.java

package wt.epm.upgrade;

import java.io.*;
import java.sql.*;
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 EPMDocNumberConflictCheck
{
    public static class Server
        implements RemoteAccess
    {

        public static Boolean EPMDocNumberConflictCheck()
        {
            FileWriter filewriter;
            Connection connection;
            File file = new File(WTProperties.getLocalProperties().getProperty("wt.logs.dir"));
            File file1 = new File(file, "EPMDocNumberConflict.log");
            filewriter = new FileWriter(file1);
            filewriter.write("Checking for EPMDocument number conflicts when the number becomes uppercase...\n");
            try
            {
                WTConnection wtconnection = (WTConnection)MethodContext.getContext().getConnection();
                connection = wtconnection.getConnection();
                break MISSING_BLOCK_LABEL_78;
            }
            catch(Exception exception)
            {
                filewriter.write("Exception in getting connection");
            }
            return Boolean.FALSE;
            Boolean boolean2;
            boolean flag = false;
            boolean flag1 = false;
            int k = 0;
            int l = 2;
            boolean flag2 = EPMDocNumberConflictCheck.columnExists(connection, "EPMDOCUMENTMASTERKEY", "IDA3ORGANIZATIONREFERENCE");
            boolean flag3 = EPMDocNumberConflictCheck.columnExists(connection, "EPMDOCUMENTMASTERKEY", "IDA3NAMESPACEREFERENCE");
            boolean flag4 = EPMDocNumberConflictCheck.columnExists(connection, "EPMDOCUMENTMASTERKEY", "IDA3A4");
            String s = "SELECT a0.wtKey, ";
            if(flag2)
            {
                s = s + "a0.ida3organizationreference, ";
                int i = l;
                l++;
            }
            if(flag3)
            {
                s = s + "a0.ida3namespacereference, ";
                int j = l;
                l++;
            }
            if(!flag2 && !flag3 && flag4)
            {
                s = s + "a0.classnamekeya4, a0.ida3a4, a1.classnamekeya4, a1.ida3a4,";
                k = l;
                l += 4;
            } else
            {
                flag4 = false;
            }
            s = s + "a1.wtkey " + "FROM epmdocumentmasterkey a0, epmdocumentmasterkey a1 " + "WHERE upper(a0.wtkey) = upper(a1.wtkey) ";
            if(flag2)
                s = s + "AND a0.ida3organizationreference = a1.ida3organizationreference ";
            if(flag3)
                s = s + "AND a0.ida3namespacereference = a1.ida3namespacereference ";
            s = s + "AND a0.ida2a2 <> a1.ida2a2";
            PreparedStatement preparedstatement = connection.prepareStatement(s);
            ResultSet resultset = preparedstatement.executeQuery();
            boolean flag5 = false;
            do
            {
                if(!resultset.next())
                    break;
                if(flag4)
                {
                    String s1 = resultset.getString(k);
                    String s2 = resultset.getString(k + 2);
                    if("wt.projmgmt.admin.Project2".equals(s1) && resultset.getLong(k + 1) != resultset.getLong(k + 3))
                        continue;
                }
                flag5 = true;
                filewriter.write("Conflict:\n");
                filewriter.write("  DocumentNumber " + resultset.getString(1));
                filewriter.write("     and " + resultset.getString(l) + "\n");
            } while(true);
            if(!flag5)
                filewriter.write("No conflicts found");
            resultset.close();
            preparedstatement.close();
            if(!flag5)
                break MISSING_BLOCK_LABEL_580;
            boolean2 = Boolean.FALSE;
            filewriter.close();
            return boolean2;
            boolean2 = Boolean.TRUE;
            filewriter.close();
            return boolean2;
            SQLException sqlexception;
            sqlexception;
            Boolean boolean1;
            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");
            }

            boolean1 = Boolean.FALSE;
            filewriter.close();
            return boolean1;
            Exception exception1;
            exception1;
            filewriter.close();
            throw exception1;
            IOException ioexception;
            ioexception;
            ioexception.printStackTrace();
            return Boolean.FALSE;
        }

        public Server()
        {
        }
    }


    public EPMDocNumberConflictCheck()
    {
    }

    public static void main(String args[])
        throws WTException
    {
        Boolean boolean1 = (Boolean)LightweightServiceHelper.invoke("EPMDocNumberConflictCheck", "wt.epm.upgrade.EPMDocNumberConflictCheck$Server", new Class[0], args);
        if(!boolean1.booleanValue())
            System.out.println("There are conflicts or errors!  Please check the EPMDocNumberConflict log in the logs directory. ");
        else
            System.out.println("No conflicts found.  Please check details in the EPMDocNumberConflict 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;
    }

}
