
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_02 {

	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( "EPMDoc - 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;
	static String QueStg;

	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);
				System.exit(0);
			}
			else {
				OT = args[0];
				QueStg = args[1].toUpperCase();

				if ( OT.equals("-qe")||OT.equals("-re") ) {
					Enumeration e = FindEpmNumbered(OT,QueStg);
					while (e.hasMoreElements()) {
						System.out.println(((EPMDocument)e.nextElement()).getIdentity());
					}
				}
				else if ( OT.equals("-qw")||OT.equals("-rw") ) {
					Enumeration w = FindWtpNumbered(OT,QueStg);
					while (w.hasMoreElements()) {
						System.out.println(((WTPart)w.nextElement()).getIdentity());
					}
				}
				else if ( OT.equals("-qb")||OT.equals("-rb") ) {
					Enumeration e = FindEpmNumbered(OT,QueStg);
					while (e.hasMoreElements()) {
						System.out.println(((EPMDocument)e.nextElement()).getIdentity());
					}
					Enumeration w = FindWtpNumbered(OT,QueStg);
					while (w.hasMoreElements()) {
						System.out.println(((WTPart)w.nextElement()).getIdentity());
					}
				}
				else {
					System.out.println(Usage);
					System.exit(0);
				}
			}
		}
		catch (WTException wtee) {
			wtee.printStackTrace();
		}
		catch (WTPropertyVetoException wtpe) {
			wtpe.printStackTrace();
		}
		finally {
			System.exit(0);
		}
	}

	public static String Usage = "\nUsage: java/windchill lj_cust.ObjMan -<query/rename> %.<query/rename string>\n\n"+
	"1st Argument = <query/rename>:\n\n-qw = query wtparts only\n-qe = query epmdocs only\n"+
	"-qb = query both (wtparts & epmdocs)\n\n-rw = rename wtparts only (query 1st)\n"+
	"-re = rename epmdocs only (query 1st)\n-rb = rename both (wtparts & epmdocs) (query 1st)\n\n"+
	"1st Argument should be lower case.\n\n2nd Argument = %.<query/rename string>\n\n"+
	"Should include % & . where % = * = wild card.\nTypical = %.PRT or %.ASM\n";
}
