
package lj_cust;

import java.util.*;
import wt.util.*;
import wt.fc.*;
import wt.vc.*;
import wt.query.*;
import wt.part.*;
import wt.epm.*;
import wt.httpgw.GatewayAuthenticator;
import wt.method.RemoteMethodServer;

public class ObjMan_01 {

	public static Enumeration FindEpmNumbered( String ObjType, String number ) throws WTException, WTPropertyVetoException {
		QuerySpec qs = new QuerySpec(EPMDocumentMaster.class);
		qs.appendSearchCondition(new SearchCondition(EPMDocumentMaster.class,EPMDocumentMaster.NUMBER,SearchCondition.LIKE,number) );
		final QueryResult qr = PersistenceHelper.manager.find(qs);

		while (qr.hasMoreElements()) {
			EPMDocumentMaster epmm = (EPMDocumentMaster)qr.nextElement();
			System.out.println( "EPM - Number: " + epmm.getNumber() + " - Name: " + epmm.getName() );

			if ( ObjType.equals("-re") || ObjType.equals("-rb") ) {
				String newNo=epmm.getNumber().substring(0, epmm.getNumber().lastIndexOf("."));

				IdentificationObject identificationobject = ((Identified)epmm).getIdentificationObject();
				EPMDocumentMasterIdentity identity=(EPMDocumentMasterIdentity)identificationobject;
				identity.setNumber(newNo);
				IdentityHelper.service.changeIdentity((Identified)epmm,identity);
			}
		}

		return new Enumeration() {
			public boolean hasMoreElements() {
				return qr.hasMoreElements();
			}
			public Object nextElement() throws NoSuchElementException {
				return ((Persistable[])qr.nextElement());
			}
		};
	}

	public static Enumeration FindWtpNumbered( String ObjType, String number ) throws WTException, WTPropertyVetoException {
		QuerySpec qs = new QuerySpec(WTPartMaster.class);
		qs.appendSearchCondition(new SearchCondition(WTPartMaster.class,WTPartMaster.NUMBER,SearchCondition.LIKE,number) );
		final QueryResult qr = PersistenceHelper.manager.find(qs);

		while (qr.hasMoreElements()) {
			WTPartMaster wtpm = (WTPartMaster)qr.nextElement();
			System.out.println( "WtPart - Number: " + wtpm.getNumber() + " - Name: " + wtpm.getName() );

			if ( ObjType.equals("-rw") || ObjType.equals("-rb") ) {
				String newNo=wtpm.getNumber().substring(0, wtpm.getNumber().lastIndexOf("."));

				IdentificationObject identificationobject = ((Identified)wtpm).getIdentificationObject();
				WTPartMasterIdentity identity=(WTPartMasterIdentity)identificationobject;
				identity.setNumber(newNo);
				IdentityHelper.service.changeIdentity((Identified)wtpm,identity);
			}
		}

		return new Enumeration() {
			public boolean hasMoreElements() {
				return qr.hasMoreElements();
			}
			public Object nextElement() throws NoSuchElementException {
				return ((Persistable[])qr.nextElement());
			}
		};
	}

	static String OT = "-qw";
	static String QueStg = "%.ASM";

	public static void main(String[] args) {
		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		GatewayAuthenticator auth = new GatewayAuthenticator();
		auth.setRemoteUser("wcadmin");
		rms.setAuthenticator(auth);

		try {
			if ( args.length < 2 || args.length > 2 ) {
				System.out.println("Usage: java/windchill lj_cust.ObjMan -<query/rename> %.<query/rename string>");
				System.out.println("1st Argument = <query/rename>:");
				System.out.println(" ");
				System.out.println("-qw = query wtparts only");
				System.out.println("-qe = query epmdocs only");
				System.out.println("-qb = query both (wtparts & epmdocs)");
				System.out.println(" ");
				System.out.println("-rw = rename wtparts only (query 1st)");
				System.out.println("-re = rename epmdocs only (query 1st)");
				System.out.println("-rb = rename both (wtparts & epmdocs) (query 1st)");
				System.out.println(" ");
				System.out.println("2nd Argument = %.<query/rename string>");
				System.out.println(" ");
				System.out.println("Should include % & . where % = * = wild card.");
				System.out.println("Typical = %.PRT or %.ASM");
			}
			else {
				OT = args[0];

				if ( OT.equals("-qe")||OT.equals("-re") ) {
					QueStg = args[1].toLowerCase();
					Enumeration e = FindEpmNumbered(OT,QueStg);
					while (e.hasMoreElements()) {
						System.out.println(((EPMDocument)e.nextElement()).getIdentity());
					}
				}
				else if ( OT.equals("-qw")||OT.equals("-rw") ) {
					QueStg = args[1].toUpperCase();
					Enumeration w = FindWtpNumbered(OT,QueStg);
					while (w.hasMoreElements()) {
						System.out.println(((WTPart)w.nextElement()).getIdentity());
					}
				}
				else if ( OT.equals("-qb")||OT.equals("-rb") ) {
					QueStg = args[1].toLowerCase();
					Enumeration e = FindEpmNumbered(OT,QueStg);
					while (e.hasMoreElements()) {
						System.out.println(((EPMDocument)e.nextElement()).getIdentity());
					}

					QueStg = args[1].toUpperCase();
					Enumeration w = FindWtpNumbered(OT,QueStg);
					while (w.hasMoreElements()) {
						System.out.println(((WTPart)w.nextElement()).getIdentity());
					}
				}
			}
		}
		catch (WTException wtee) {
			wtee.printStackTrace();
		}
		catch (WTPropertyVetoException wtpe) {
			wtpe.printStackTrace();
		}
		finally {
			System.exit(0);
		}
	}
}
