<?xml version="1.0" standalone="yes"?>
<%@taglib uri="http://www.ptc.com/infoengine/taglib/core" prefix="ie"%>
<%@ page import="com.infoengine.object.factory.Att"%>
<%@ page import="com.infoengine.object.factory.Element"%>
<!DOCTYPE PROCESS>

<PROCESS name="dca-tree-used-by">
  <description>
	Tree of related objects
	
	
	
	todo:
		build where clause for object ref
		build querytree 1 level format
  </description>

<!--switching debug on & off, degug of input groups-->
<%
	boolean VERBOSE = false;
	Group groupIn = getGroupIn();
//	groupIn.setName("jdbcPdmInput");
	if(VERBOSE){
		System.out.println("_______________in AccessPDM Query Task");
		//getContextGroup("FORM").toXML(new java.io.PrintWriter(System.out),true) ;
		getContextGroup("FORM").printTree();
		System.out.println(" AccessPDM Query Task, groupIn.getName()="+groupIn.getName());
		//groupIn.toXML(new java.io.PrintWriter(System.out),true) ;
		groupIn.printTree() ;
	}
	String thisRepository = "test";
	if(groupIn.getElementCount()>0){
		thisRepository = ext.adapter.federation.UfidHelper.getRepository( groupIn.getElementAt(0).getUfid() );
	}
%>
<%
 //build where clause from ufid
String ufidIn = (String)getParam("dca_object_ref");
if(ufidIn==null) ufidIn = groupIn.getElementAt(0).getUfid();
if(ufidIn==null) return;
// ext.devtools.dca.config:search.table.2:dcaRepos.mvonhasselbach.ptc.com@dcaRepos.mvonhasselbach.ptc.com
String localId = ext.adapter.federation.UfidHelper.getShortOid(ufidIn);
int sepIdx1 = localId.indexOf(":");
String pckg = localId.substring( 0, sepIdx1);
if("ANY".equalsIgnoreCase(pckg) ) pckg ="*";
int sepIdx2 = localId.indexOf("__");
String id = localId.substring( sepIdx1+1, sepIdx2);
String context = localId.substring( sepIdx2+2);
if(VERBOSE) System.out.println("pckg: "+pckg+" id: "+id+" context: "+context);
// (ID=&apos;*&apos;)&amp;(PACKAGE=&apos;*ext*&apos;)&amp;(TAG=&apos;Simp*&apos;)
String where = "(ID='"+id+"')&(PACKAGE='"+pckg+"')&(CONTEXT='"+context+"')";
if(VERBOSE) System.out.println("where: "+where);
%>
	<ie:webject name="Query-Objects" type="OBJ">
		<ie:param name="instance"  data="${@FORM[]supporting-adapter[*]}" valueSeparator=";" delim=";" default="windchill"/>
		<ie:param name="AUTHORIZATION" data="${SERVER[]AUTHORIZATION[0]}"/>
		<ie:param name="ATTRIBUTE" data="*"/>
		<ie:param name="CLASS"   data="repository.csv"/>
		<ie:param name="WHERE"   data="<%=where%>"/>
		<ie:param name="group_out"  data="baseElem"/>
	</ie:webject>
	<ie:webject name="Set-Metadata" type="GRP">
		<ie:param name="group_in"  data="baseElem"/>
		<ie:param name="SCOPE"  data="ELEMENT"/>
		<ie:param name="ELEMENT"  data="*"/>
		<ie:param name="NAME"   data="com.infoengine.object.type"/>
		<ie:param name="VALUE"  data="DCAElement"/>
		<ie:param name="NAME"   data="com.infoengine.depth"/>
		<ie:param name="VALUE"  data="0"/>
		<ie:param name="NAME"   data="com.infoengine.object.id"/>
		<ie:param name="VALUE"  data="${baseElem[0]PACKAGE[]}:${baseElem[0]ID[]}__${baseElem[0]CONTEXT[]}:<%=thisRepository%>@<%=thisRepository%>"/>
	</ie:webject>
	<ie:webject name="Copy-Group" type="GRP">
		<ie:param name="GROUP_IN" data="baseElem"/>
		<ie:param name="group_out"  data="${@FORM[]group_out[]}" default="output"/>
	</ie:webject>

<%
//mvh: add ref_by_context here ??
where = "(REF_BY_ID='"+id+"')&(REF_BY_PACKAGE='"+pckg+"')";	//&(REF_BY_CONTEXT='"+context+"')";
String parentTag = (String)getGroup("baseElem").getAttributeValue( 0, "TAG");
if(VERBOSE) System.out.println("where: "+where);
%>
	<ie:webject name="Query-Objects" type="OBJ">
		<ie:param name="instance"  data="${@FORM[]supporting-adapter[*]}" valueSeparator=";" delim=";" default="windchill"/>
		<ie:param name="AUTHORIZATION" data="${SERVER[]AUTHORIZATION[0]}"/>
		<ie:param name="ATTRIBUTE" data="*"/>
		<ie:param name="CLASS"   data="repository_xref.csv"/>
		<ie:param name="WHERE"   data="<%=where%>"/>
		<ie:param name="group_out"  data="result"/>
	</ie:webject>
<%
	if(VERBOSE) System.out.println("getGroup(result).getElementCount(): "+getGroup("result").getElementCount());
	if( getGroup("result").getElementCount()==0){
		addGroup(new Group("output") );
		return;
	}
%>	
	<ie:forEach groupIn="result" groupOut="elem">
		<%
		id = (String)getGroup("elem").getAttributeValue( 0,"ID");
		pckg = (String)getGroup("elem").getElementAt(0).getValue("PACKAGE");
		if("ANY".equalsIgnoreCase(pckg) ) pckg ="*";
		//context = (String)getGroup("elem").getElementAt(0).getValue("CONTEXT");
		//there is no context info in repository_xref so you have to use the 'parent' context
		where = "(ID='"+id+"')&(PACKAGE='"+pckg+"')";
		String where2 = "(ID='"+id+"')&(PACKAGE='"+pckg+"')&(CONTEXT='"+context+"')";
		if(VERBOSE) System.out.println("where xref: "+where);
		%>	
		<ie:webject name="Query-Objects" type="OBJ">
			<ie:param name="instance"  data="${@FORM[]supporting-adapter[*]}" valueSeparator=";" delim=";" default="windchill"/>
			<ie:param name="AUTHORIZATION" data="${SERVER[]AUTHORIZATION[0]}"/>
			<ie:param name="ATTRIBUTE" data="*"/>
			<ie:param name="CLASS"   data="repository.csv"/>
	  	    <ie:param name="WHERE"   data="<%=where%>"/>
			<ie:param name="group_out"  data="baseElem"/>
		</ie:webject>
		<%
		int resC = getGroup("baseElem").getElementCount();
		if(resC==0) throw new IEException("no result for where clause: "+where+" in repository.csv");
		if(resC>1){
			System.out.println("ambiguous results for where clause: "+where+" in repository.csv");
			%>
			<ie:webject name="Query-Objects" type="OBJ">
				<ie:param name="instance"  data="${@FORM[]supporting-adapter[*]}" valueSeparator=";" delim=";" default="windchill"/>
				<ie:param name="AUTHORIZATION" data="${SERVER[]AUTHORIZATION[0]}"/>
				<ie:param name="ATTRIBUTE" data="*"/>
				<ie:param name="CLASS"   data="repository.csv"/>
		  	    <ie:param name="WHERE"   data="<%=where2%>"/>
				<ie:param name="group_out"  data="baseElem"/>
			</ie:webject>
			<%
			int resC2 = getGroup("baseElem").getElementCount();
			if(resC2>1) throw new IEException("ambiguous results for where clause: "+where+" in repository.csv");
			if(resC2==0) throw new IEException("no result for where clause: "+where2+" in repository.csv\n "+resC+" results found for where clause: "+where);
		}
		Element baseEl = getGroup("baseElem").getElementAt(0);
		String childTag = (String)baseEl.getValue("TAG");
		String refType = "Insert";
		if( parentTag.equalsIgnoreCase( childTag) ) refType = "Extend";
		//add Att for icon
		//baseEl.addAtt( new Att("usageIcon", "<img src='wtcore/images/ext/devtools/dca/config/"+refType+".gif' alt='Reference by "+refType+"'>"));
		baseEl.addAtt( new Att("usageIcon", refType));
		String previewable = "false";
		if( "SimpleFrame".equalsIgnoreCase( childTag) || "CompositeFrame".equalsIgnoreCase( childTag) ){ 
			baseEl.addAtt( new Att("previewable", "true"));
			baseEl.addAtt( new Att("previewLocation", baseEl.getValue("PACKAGE")+":"+baseEl.getValue("ID")));
		}
		%>
		<ie:webject name="Set-Metadata" type="GRP">
			<ie:param name="group_in"  data="baseElem"/>
			<ie:param name="SCOPE"  data="ELEMENT"/>
			<ie:param name="ELEMENT"  data="*"/>
			<ie:param name="NAME"   data="com.infoengine.object.type"/>
			<ie:param name="VALUE"  data="DCAElement"/>
			<ie:param name="NAME"   data="com.infoengine.depth"/>
			<ie:param name="VALUE"  data="1"/>
			<ie:param name="NAME"   data="com.infoengine.object.id"/>
			<ie:param name="VALUE"  data="${baseElem[0]PACKAGE[]}:${baseElem[0]ID[]}__${baseElem[0]CONTEXT[]}:<%=thisRepository%>@<%=thisRepository%>"/>
	   </ie:webject>
	   <ie:webject name="Concat-Groups" type="GRP">
			<ie:param name="GROUP_IN" data="${@FORM[]group_out[]}" default="output"/>
			<ie:param name="GROUP_IN" data="baseElem"/>
			<ie:param name="GROUP_OUT" data="${@FORM[]group_out[]}" default="output"/>
       </ie:webject>
	</ie:forEach>
	<%
		if(VERBOSE) getGroup("output").printTree() ;
	%>

</PROCESS>
