
package ext;

import java.util.*;
import java.rmi.RemoteException;

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 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 attrList01 {

	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() );

			String tstStg = getIBA_EPM_Attr(epmm, peAttr);
			System.out.println( "EPMDoc - IBA ProE Param: "+ peAttr+ " & Value: " + tstStg );

			String tstNm = epmm.getName();

			if ( oldEpmObj == epmdocmaster ) { continue; }

			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);

						oldEpmObj = epmdocmaster;
						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( "\nWtPart - Number: " + wtpm.getNumber() + " - Name: " + wtpm.getName() );

			String tstStg = getIBA_WTP_Attr(wtpm, peAttr);
			System.out.println( "WtPart - IBA ProE Param: "+ peAttr+ " & Value: " + tstStg );

			String tstNm = wtpm.getName();

			if ( oldWtpObj == wtpartmaster ) { continue; }

			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);

						oldWtpObj = wtpartmaster;
						renCnt++;
					}
				}
			}
			queCnt++;
		}

		return new Enumeration() {
			public boolean hasMoreElements() {
				return qr.hasMoreElements();
			}
			public Object nextElement() throws NoSuchElementException {
				return ((Persistable[])qr.nextElement());
			}
		};
	}

		AbstractValueView aabstractvalueview[];
		aabstractvalueview = null;

		IBAHolder ibaholder = (IBAHolder)obj;
		ibaholder = IBAValueHelper.service.refreshAttributeContainerWithoutConstraints(ibaholder);
		DefaultAttributeContainer defaultattributecontainer = (DefaultAttributeContainer)ibaholder.getAttributeContainer();
		if(defaultattributecontainer != null)
			aabstractvalueview = getAttributeHolder(defaultattributecontainer);

	public static String getIBA_EPM_Attr(EPMDocument doc){

		String attrVal=null;
		String attrStgUp = attrStg.toUpperCase();

		try{
			IBAHolder ibaHolder=IBAValueHelper.service.refreshAttributeContainer((IBAHolder)wtp, 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;
		}

		String attrStgUp = attrStg.toUpperCase();

		try{

			AbstractValueView aabstractvalueview[];

			aabstractvalueview = null;

            DefaultAttributeContainer defaultattributecontainer = (DefaultAttributeContainer)ibaholder.getAttributeContainer();
            if(defaultattributecontainer != null)
                aabstractvalueview = getAttributeHolder(defaultattributecontainer);

			IBAHolder ibaHolder=IBAValueHelper.service.refreshAttributeContainer((IBAHolder)doc, null, null, null);
			StandardIBADefinitionService defService=new StandardIBADefinitionService();

			DefaultAttributeContainer attributeContainer=(DefaultAttributeContainer)ibaHolder.getAttributeContainer();

			aabstractvalueview = getAttributeHolder(defaultattributecontainer);

			if(!(attributeContainer.getAttributeValues().length>0)){
				System.out.println("NO Attributes Found");
				return null;
			}
			AbstractValueView attValue[]= attributeContainer.getAttributeValues(attributeDefinition);
			return attValue;
		}
		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);
			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.exit(0);
		}
	}

	static String OT;
	static String QueStg;
	static String pe_attr;

	static WTPartMaster oldWtpObj = null;
	static EPMDocumentMaster oldEpmObj = null;

	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\n(like TITLE, DESCRIPTION & upper case)\n\n";
}
