
package ext;

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;

import wt.folder.CabinetBased;

import java.rmi.RemoteException;

import wt.iba.definition.AttributeDefinitionDataType;
import wt.iba.definition.litedefinition.*;
import wt.iba.definition.service.*;

import wt.iba.value.*;
import wt.iba.value.IBAHolder;
import wt.iba.value.litevalue.*;
import wt.iba.value.service.IBAValueHelper;
import wt.iba.value.service.IBAValueService;

import com.ptc.core.meta.server.IBAModel;
import com.ptc.core.meta.common.AttributeIdentifier;
import com.ptc.core.meta.common.impl.InstanceBasedAttributeIdentifier;
import com.ptc.core.meta.common.impl.InstanceBasedAttributeTypeIdentifier;
import com.ptc.core.meta.type.mgmt.common.TypeDefinitionDefaultView;

public class attr_05c {

	public static Enumeration FindEpmNumbered( String ObjType, String number ) throws WTException, WTPropertyVetoException {

		QuerySpec qs = new QuerySpec(EPMDocument.class);

		qs.appendWhere(new SearchCondition(EPMDocument.class,EPMDocument.NUMBER,SearchCondition.LIKE,number));
		qs.appendAnd();
		qs.appendWhere(new SearchCondition(EPMDocument.class, "iterationInfo.latest", "TRUE"));
		final QueryResult qr = PersistenceHelper.manager.find(qs);

		while (qr.hasMoreElements()) {
			EPMDocument epmm = (EPMDocument)qr.nextElement();
			System.out.println( "EPMDoc - Number: " + epmm.getNumber() + " - Name: " + epmm.getName()+ " - CadName: " + epmm.getCADName() );

			String tstStg = getIBA_Attr(epmm, "MATERIAL");

			System.out.println( "EPMDoc - MATERIAL: (tstStg): " + tstStg );

			if(tstStg != null) {
				System.out.println( "tstStg != null" );
				System.out.println( "tstStg: " + tstStg);

				EPMDocumentMaster epmdocmaster = (EPMDocumentMaster)epmm.getMaster();

				IdentificationObject identificationobject = ((Identified)epmdocmaster).getIdentificationObject();
				EPMDocumentMasterIdentity identity=(EPMDocumentMasterIdentity)identificationobject;
				identity.setName(tstStg);
				IdentityHelper.service.changeIdentity((Identified)epmdocmaster,identity);
				renCnt++;
			}
			queCnt++;
		}

		return new Enumeration() {
			public boolean hasMoreElements() {
				return qr.hasMoreElements();
			}
			public Object nextElement() throws NoSuchElementException {
				return ((Persistable[])qr.nextElement());
			}
		};
	}

	public static String getIBA_Attr(EPMDocument doc, String attrStg){
		String attrVal=null;
		String attrStgUp = attrStg.toUpperCase();

		try{
			IBAHolder ibaHolder=IBAValueHelper.service.refreshAttributeContainer((IBAHolder)doc, null, null, null);
			StandardIBADefinitionService defService=new StandardIBADefinitionService();
			DefaultAttributeContainer attributeContainer=(DefaultAttributeContainer)ibaHolder.getAttributeContainer();
			AttributeDefDefaultView attributeDefinition=defService.getAttributeDefDefaultViewByPath(attrStgUp);
			if(!(attributeContainer.getAttributeValues().length>0)){
				System.out.println("Error: "+attrStgUp+" Attribute Not Found");
				return null;
			}
			AbstractValueView attValue= attributeContainer.getAttributeValues(attributeDefinition)[0];
			attrVal=attValue.getLocalizedDisplayString();
			return attrVal;
		}
		catch(WTException ew){
			ew.printStackTrace();
		}
		catch (RemoteException er){
			er.printStackTrace();
		}
		catch (ArrayIndexOutOfBoundsException ea){
			ea.printStackTrace();
		}
		return null;
	}

	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")||OT.equals("-rae")||OT.equals("-rpe")) {
					Enumeration e = FindEpmNumbered(OT,QueStg);
					while (e.hasMoreElements()) {
						System.out.println(((EPMDocument)e.nextElement()).getIdentity());
					}
				}
				else {
					System.out.println(Usage);
					System.exit(0);
				}
				System.out.println(queCnt+" Objects Queried.");
				System.out.println(renCnt+" Objects Renamed.");
			}
		}
		catch (WTException wtee) {
			wtee.printStackTrace();
		}
		catch (WTPropertyVetoException wtpe) {
			wtpe.printStackTrace();
		}
		finally {
			System.exit(0);
		}
	}

	static String OT;
	static String QueStg;

	static int renCnt=0;
	static int queCnt=0;

	public static String Usage = "\nUsage:\n\njava/windchill [-mx1024m] "+
	"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"+
	"-rpw = rename .PRT to .P - wtparts only (query 1st)\n"+
	"-rpe = rename .PRT to .P - epmdocs only (query 1st)\n"+
	"-rpb = rename .PRT to .P - both (wtparts & epmdocs) (query 1st)\n\n"+
	"-raw = rename .ASM to .A - wtparts only (query 1st)\n"+
	"-rae = rename .ASM to .A - epmdocs only (query 1st)\n"+
	"-rab = rename .ASM to .A - 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";
}
