
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 PA_Rename_04 {

	public static Enumeration FindEpmNumbered( String ObjType ) throws WTException, WTPropertyVetoException {

		number = "%.ASM";
		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()+ " - CadName: " + epmm.getCADName() );

			if (ObjType.equals("-re")||ObjType.equals("-rb")||ObjType.equals("-rpe")
				||ObjType.equals("-rpb")||ObjType.equals("-rae")||ObjType.equals("-rab")) {

				String newNo=epmm.getNumber().substring(0, epmm.getNumber().lastIndexOf("."));

				if ( number.endsWith(".DRW") ) { newNo = newNo+".D"; }

				if ( ObjType.equals("-rpe")||ObjType.equals("-rpb") ) {
					if ( number.endsWith(".PRT") ) { newNo = newNo+".P"; }
				}

				if ( ObjType.equals("-rae")||ObjType.equals("-rab") ) {
					if ( number.endsWith(".ASM") ) { newNo = newNo+".A"; }
				}

				IdentificationObject identificationobject = ((Identified)epmm).getIdentificationObject();
				EPMDocumentMasterIdentity identity=(EPMDocumentMasterIdentity)identificationobject;
				identity.setNumber(newNo);
				IdentityHelper.service.changeIdentity((Identified)epmm,identity);
				renCnt++;
			}
			queCnt++;
		}

		return new Enumeration() {
			public boolean hasMoreElements() {
				return qr.hasMoreElements();
			}
			public Object nextElement() throws NoSuchElementException {
				return ((Persistable[])qr.nextElement());
			}
		};
	}

	public static Enumeration FindWtpNumbered( String ObjType ) throws WTException, WTPropertyVetoException {

		number = "%.ASM";
		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() );

			String subNo = wtpm.getNumber().substring(0, wtpm.getNumber().lastIndexOf("."));

			String fullNo = subNo+",PRT";
			String fullNoP = subNo+",P";

			Enumeration w2 = FindWtpNo(fullNoP);
			while (w2.hasMoreElements()) {
				System.out.println(((WTPart)w2.nextElement()).getIdentity());
			}
		}

		return new Enumeration() {
			public boolean hasMoreElements() {
				return qr.hasMoreElements();
			}
			public Object nextElement() throws NoSuchElementException {
				return ((Persistable[])qr.nextElement());
			}
		};
	}

	public static Enumeration FindWtpNo( String fullNoP2 ) throws WTException, WTPropertyVetoException {
		QuerySpec qs2 = new QuerySpec(WTPartMaster.class);
		qs2.appendSearchCondition(new SearchCondition(WTPartMaster.class,WTPartMaster.NUMBER,SearchCondition.LIKE,fullNo2) );
		final QueryResult qr2 = PersistenceHelper.manager.find(qs2);

		while (qr2.hasMoreElements()) {
			WTPartMaster wtpm2 = (WTPartMaster)qr2.nextElement();
			System.out.println( "WtPart - Number2: " + wtpm2.getNumber() + " - Name2: " + wtpm2.getName() );

				IdentificationObject identificationobject = ((Identified)wtpm2).getIdentificationObject();
				WTPartMasterIdentity identity=(WTPartMasterIdentity)identificationobject;
				identity.setNumber(fullNoP);
				IdentityHelper.service.changeIdentity((Identified)wtpm2,identity);
			}
		}

		return new Enumeration() {
			public boolean hasMoreElements() {
				return qr2.hasMoreElements();
			}
			public Object nextElement() throws NoSuchElementException {
				return ((Persistable[])qr2.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 < 1 || args.length > 1 ) {
				System.out.println(Usage);
				System.exit(0);
			}
			else {
				OT = args[0];

				//Enumeration e = FindEpmNumbered(OT);
				//while (e.hasMoreElements()) {
				//	System.out.println(((EPMDocument)e.nextElement()).getIdentity());
				//}

				Enumeration w = FindWtpNumbered(OT);
				while (w.hasMoreElements()) {
					System.out.println(((WTPart)w.nextElement()).getIdentity());
				}
			}
		}
		catch (WTException wtee) {
			wtee.printStackTrace();
		}
		catch (WTPropertyVetoException wtpe) {
			wtpe.printStackTrace();
		}
		finally {
			System.exit(0);
		}
	}

	static String OT;

	public static String Usage = "\nUsage:\n\njava/windchill [-mx1024m] "+
	"lj_cust.PA_Rename -<query/rename>\n\n"+
	"Argument = <query/rename>: -qo = query only OR -qr = query & rename\n";
}
