
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 Obj_Rename_04 {

	public static Enumeration FindEpmNumbered( 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( "Number: " + epmm.getNumber() + " - Name: " + epmm.getName() );

			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 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( "Number: " + wtpm.getNumber() + " - Name: " + wtpm.getName() );

			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());
			}
		};
	}

	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) {
				System.out.println("Usage: java/windchill lj_cust.Obj_Rename <object type [epm or wtp]> - <query string>");
			}
			else {
				String ObjType = args[0];
				String QueStg;

				if ( args[0].equals("epm") ) {
					QueStg = args[1].toLowerCase();
					Enumeration e = FindEpmNumbered(QueStg);
					while (e.hasMoreElements()) {
						System.out.println(((EPMDocument)e.nextElement()).getIdentity());
					}
				}
				else if ( args[0].equals("wtp") ) {
					QueStg = args[1].toUpperCase();
					Enumeration e = FindWtpNumbered(QueStg);
					while (e.hasMoreElements()) {
						System.out.println(((WTPart)e.nextElement()).getIdentity());
					}
				}
			}
		}
		catch (WTException wtee) {
			wtee.printStackTrace();
		}
		catch (WTPropertyVetoException wtpe) {
			wtpe.printStackTrace();
		}
		finally {
			System.exit(0);
		}
	}
}
