
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 attr2nameA9d3 {

	public static Enumeration FindEpmNumbered( String ObjType, String number, String peAttr ) 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();
			EPMDocumentMaster epmdocmaster = (EPMDocumentMaster)epmm.getMaster();

			System.out.println( "\nEPMDoc - Number: " + epmm.getNumber() + " - Name: " + epmm.getName()+ " - CadName: " + epmm.getCADName() );
			System.out.println( "EPMDoc - epmm.getDocType(): "+epmm.getDocType());

			String tstStg = getIBA_EPM_Attr(epmm, peAttr);
			System.out.println( "EPMDoc - IBA ProE Param: "+ peAttr+ " & Value: " + tstStg );

			int tstInt = epmm.getName().compareTo(tstStg);
			System.out.println( "tstInt: " + tstInt);

			int tstLg = tstStg.length();
			System.out.println( "tstStg.length(): " + tstStg.length());

			if(tstInt == 0) {
				System.out.println( "SAME: tstStg: " + tstStg + " == epmm.getName(): " + epmm.getName());
			} else {
				System.out.println( "NOT SAME: tstStg: " + tstStg + " != epmm.getName(): " + epmm.getName());
			}

			if ( tstStg == null || epmm.getName().compareTo(tstStg) == 0 ) {
				continue;
			} else {
				if(tstStg.length() > 0) {
					if (ObjType.equals("-re")||ObjType.equals("-rb")) {
						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 Enumeration FindWtpNumbered( String ObjType, String number, String peAttr ) throws WTException, WTPropertyVetoException {
		QuerySpec qs = new QuerySpec(WTPart.class);
		qs.appendWhere(new SearchCondition(WTPart.class,WTPart.NUMBER,SearchCondition.LIKE,number));
		qs.appendAnd();
		qs.appendWhere(new SearchCondition(WTPart.class, "iterationInfo.latest", "TRUE"));
		final QueryResult qr = PersistenceHelper.manager.find(qs);

		while (qr.hasMoreElements()) {
			WTPart wtpm = (WTPart)qr.nextElement();
			WTPartMaster wtpartmaster = (WTPartMaster)wtpm.getMaster();

			System.out.println( "WtPart - Number: " + wtpm.getNumber() + " - Name: " + wtpm.getName() );

			String tstStg = getIBA_WTP_Attr(wtpm, peAttr);
			System.out.println( "WtPart - IBA ProE Param: "+ peAttr+ " & Value: " + tstStg );

			int tstInt = wtpm.getName().compareTo(tstStg);
			System.out.println( "tstInt: " + tstInt);

			int tstLg = tstStg.length();
			System.out.println( "tstStg.length(): " + tstStg.length());

			if(tstInt == 0) {
				System.out.println( "SAME: tstStg: " + tstStg + " != wtpm.getName(): " + wtpm.getName());
			} else {
				System.out.println( "NOT SAME: tstStg: " + tstStg + " == wtpm.getName(): " + wtpm.getName());
			}

			if ( tstStg == null || wtpm.getName().compareTo(tstStg) == 0 ) {
				continue;
			} else {
				if(tstStg.length() > 0) {
					if (ObjType.equals("-rw")||ObjType.equals("-rb")) {
						IdentificationObject identificationobject = ((Identified)wtpartmaster).getIdentificationObject();
						WTPartMasterIdentity identity=(WTPartMasterIdentity)identificationobject;
						identity.setName(tstStg);
						IdentityHelper.service.changeIdentity((Identified)wtpartmaster,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_EPM_Attr(EPMDocument doc, String attrStg){
		String attrVal=null;
		String attrStgUp = attrStg.toUpperCase();

		try{
			IBAHolder ibaHolder=IBAValueHelper.service.refreshAttributeContainer((IBAHolder)doc, null, null, null);
			//IBADefinitionService defService = IBADefinitionHelper.service;
			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 String getIBA_WTP_Attr(WTPart wtp, String attrStg){
		String attrVal=null;
		String attrStgUp = attrStg.toUpperCase();

		try{
			IBAHolder ibaHolder=IBAValueHelper.service.refreshAttributeContainer((IBAHolder)wtp, null, null, null);
			//IBADefinitionService defService = IBADefinitionHelper.service;
			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 < 3 || args.length > 3 ) {
				System.out.println(Usage);
				System.exit(0);
			}
			else {
				OT = args[0];
				QueStg = args[1].toUpperCase();
				pe_attr = args[2].toUpperCase();

				if ( OT.equals("-qe")||OT.equals("-re")) {
					Enumeration e = FindEpmNumbered(OT,QueStg,pe_attr);
					while (e.hasMoreElements()) {
						System.out.println(((EPMDocument)e.nextElement()).getIdentity());
					}
				}
				else if ( OT.equals("-qw")||OT.equals("-rw")) {
					Enumeration w = FindWtpNumbered(OT,QueStg,pe_attr);
					while (w.hasMoreElements()) {
						System.out.println(((WTPart)w.nextElement()).getIdentity());
					}
				}
				else if ( OT.equals("-qb")||OT.equals("-rb")) {
					Enumeration e = FindEpmNumbered(OT,QueStg,pe_attr);
					while (e.hasMoreElements()) {
						System.out.println(((EPMDocument)e.nextElement()).getIdentity());
					}
					Enumeration w = FindWtpNumbered(OT,QueStg,pe_attr);
					while (w.hasMoreElements()) {
						System.out.println(((WTPart)w.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.out.println("Got to Finally");
			System.exit(0);
		}
	}

	static String OT;
	static String QueStg;
	static String pe_attr;

	static int renCnt=0;
	static int queCnt=0;

	public static String Usage = "\nUsage:\n\njava or windchill [-mx1024m] "+
	"ext.attr2name -<query/rename> %<query/rename string>% <proe parameter/attribute>\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 % (front, back or middle or all 3) where % = * = wild card.\nTypical = 1% or 2%\n\n"+
	"3rd argument should be ProE Parameter/Attribute/IBA (like TITLE, DESCRIPTION & upper case)\n\n";
}
