
package ext;

import java.io.*;
import java.util.StringTokenizer;
import org.apache.log4j.*;
import wt.doc.WTDocumentMaster;
import wt.doc.WTDocumentMasterIdentity;
import wt.epm.EPMDocumentMaster;
import wt.epm.EPMDocumentMasterIdentity;
import wt.fc.*;
import wt.log4j.LogR;
import wt.part.WTPartMaster;
import wt.part.WTPartMasterIdentity;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;

public class objChangeIdentity
{
    public objChangeIdentity() { }

    public void chgIdWTPart(String number, String name)
    {
        try
        {
            QuerySpec queryspec = new QuerySpec(wt/part/WTPartMaster);
            queryspec.appendWhere(new SearchCondition(wt/part/WTPartMaster, "number", "=", number.toUpperCase()));
            QueryResult result = PersistenceHelper.manager.find(queryspec);
            if(result.size() == 1)
            {
                System.out.println((new StringBuilder()).append("Found master of ").append(number).toString());
                WTPartMaster master = (WTPartMaster)result.nextElement();
                wt.fc.IdentificationObject identificationobject = master.getIdentificationObject();
                WTPartMasterIdentity identity = (WTPartMasterIdentity)identificationobject;
                identity.setName(name);
                IdentityHelper.service.changeIdentity(master, identity);
                System.out.println("Identity Changed");
            }
        }
        catch(WTException exc)
        {
            exc.printStackTrace();
        }
        catch(WTPropertyVetoException eVeto)
        {
            eVeto.printStackTrace();
        }
    }

    public void chgIdWTDocument(String number, String name)
    {
        try
        {
            QuerySpec queryspec = new QuerySpec(wt/doc/WTDocumentMaster);
            queryspec.appendWhere(new SearchCondition(wt/doc/WTDocumentMaster, "number", "=", number.toUpperCase()));
            QueryResult result = PersistenceHelper.manager.find(queryspec);
            if(result.size() == 1)
            {
                System.out.println((new StringBuilder()).append("Found master of ").append(number).toString());
                WTDocumentMaster master = (WTDocumentMaster)result.nextElement();
                wt.fc.IdentificationObject identificationobject = master.getIdentificationObject();
                WTDocumentMasterIdentity identity = (WTDocumentMasterIdentity)identificationobject;
                identity.setName(name);
                IdentityHelper.service.changeIdentity(master, identity);
                System.out.println("Identity Changed");
            }
        }
        catch(WTException exc)
        {
            exc.printStackTrace();
        }
        catch(WTPropertyVetoException eVeto)
        {
            eVeto.printStackTrace();
        }
    }

    public void chgIdEPMDocument(String number, String name)
    {
        try
        {
            QuerySpec queryspec = new QuerySpec(wt/epm/EPMDocumentMaster);
            queryspec.appendWhere(new SearchCondition(wt/epm/EPMDocumentMaster, "number", "=", number.toUpperCase()));
            QueryResult result = PersistenceHelper.manager.find(queryspec);
            if(result.size() == 1)
            {
                System.out.println((new StringBuilder()).append("Found master of ").append(number).toString());
                EPMDocumentMaster master = (EPMDocumentMaster)result.nextElement();
                wt.fc.IdentificationObject identificationobject = master.getIdentificationObject();
                EPMDocumentMasterIdentity identity = (EPMDocumentMasterIdentity)identificationobject;
                identity.setName(name);
                IdentityHelper.service.changeIdentity(master, identity);
                System.out.println("Identity Changed");
            }
        }
        catch(WTException exc)
        {
            exc.printStackTrace();
        }
        catch(WTPropertyVetoException eVeto)
        {
            eVeto.printStackTrace();
        }
    }

    public static void main(String args[])
        throws WTException, WTPropertyVetoException, ClassNotFoundException
    {
        objChangeIdentity prj;
label0:
        {
            prj = new objChangeIdentity();

            if(args.length < 2 )
            {
                System.out.println("Incorrect arguments provided.");
                System.out.println("objChangeIdentity <object type> <input file>");
                System.exit(0);
            }

            int objType = 0;

            if(args[0].equalsIgnoreCase("part"))
                objType = 1;
            else if(args[0].equalsIgnoreCase("doc"))
                objType = 2;
            else if(args[0].equalsIgnoreCase("caddoc"))
                objType = 3;

            if(objType == 0)
            {
                System.out.println("Incorrect object type provided");
                System.out.println("Please use Part, Doc, or CadDoc only as first arguement");
                System.exit(0);
            }
            try
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(args[1])));
                do
                {
                    String line;
                    if((line = br.readLine()) != null)
                    {
                        String number = null;
                        String name = null;

                        StringTokenizer token = new StringTokenizer(line, "\t");

                        number = token.nextToken();
                        System.out.println((new StringBuilder()).append("Read ").append(number).toString());

                        name = token.nextToken();
                        System.out.println((new StringBuilder()).append("Read ").append(name).toString());

                        if(name != null && number != null)
                        {
                            switch(objType)
                            {
                            case 1: // '\001'
                                prj.chgIdWTPart(number, name);
                                // fall through

                            case 2: // '\002'
                                prj.chgIdWTDocument(number, name);
                                // fall through

                            case 3: // '\003'
                                prj.chgIdEPMDocument(number, name);
                                break;
                            }
                        } else
                        {
                            System.out.println("Error reading line from file.");
                            System.out.println((new StringBuilder()).append("number:").append(number).toString());
                            System.out.println((new StringBuilder()).append("name:").append(name).toString());
                            System.out.println("Continuing to next line");
                        }
                        continue;
                    }
                    System.out.println("End of file");
                    break label0;
                } while(true);
            }
            catch(IOException ioe)
            {
				ioe.printStackTrace();
                System.out.println("Error loading data file", ioe);
            }
        }
        System.out.println("Exiting");
        System.exit(0);
    }
}
