<?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);
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+"')";
//mvh: check if REF_BY_CONTEXT Where clause is needed/makes sense
String where2 = "(ID='"+id+"')&(PACKAGE='"+pckg+"')&(REF_BY_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="output"/>
	</ie:webject>
	<ie:webject name="Set-Metadata" type="GRP">
		<ie:param name="group_in"  data="output"/>
		<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="${output[0]PACKAGE[]}:${output[0]ID[]}:<%=thisRepository%>@<%=thisRepository%>"/>
		<ie:param name="NAME"   data="com.infoengine.element.id"/>
		<ie:param name="VALUE"  data="${output[0]PACKAGE[]}:${output[0]ID[]}:<%=thisRepository%>@<%=thisRepository%>"/>
		<ie:param name="group_out"  data="output"/>
	</ie:webject>

	<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="<%=where2%>"/>
		<ie:param name="group_out"  data="result"/>
	</ie:webject>
	<%
	if( getGroup("result").getElementCount()==0){
		addGroup(new Group("output") );
		return;
	}
	String parentTag = (String)getGroup("output").getAttributeValue( 0, "TAG");	
	%>
	<ie:webject name="Extract-Group" type="GRP">
		<ie:param name="GROUP_IN" data="result"/>
		<ie:param name="EXTRACT" data="REF_BY_ID"/>
		<ie:param name="EXTRACT" data="REF_BY_PACKAGE"/>
		<ie:param name="EXTRACT" data="REF_BY_TAG"/>
		<ie:param name="EXTRACT" data="REF_BY_CONTEXT"/>
		<ie:param name="EXTRACT" data="FILE"/>
		<ie:param name="GROUP_OUT" data="result"/>
	</ie:webject>
	<ie:webject name="Change-Group" type="GRP">
		<ie:param name="GROUP_IN" data="result"/>
		<ie:param name="RENAME" data="'REF_BY_ID'='ID'"/>
		<ie:param name="RENAME" data="'REF_BY_PACKAGE'='PACKAGE'"/>
		<ie:param name="RENAME" data="'REF_BY_TAG'='TAG'"/>
		<ie:param name="RENAME" data="'REF_BY_CONTEXT'='CONTEXT'"/>
		<ie:param name="GROUP_OUT" data="result"/>
   </ie:webject>

	<ie:forEach groupIn="result" groupOut="elem">
		<%
		Element baseEl = getGroup("elem").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="elem"/>
			<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="${elem[0]PACKAGE[]}:${elem[0]ID[]}__${elem[0]CONTEXT[]}:<%=thisRepository%>@<%=thisRepository%>"/>
			<ie:param name="NAME"   data="com.infoengine.element.id"/>
			<ie:param name="VALUE"  data="${elem[0]PACKAGE[]}:${elem[0]ID[]}__${elem[0]CONTEXT[]}:<%=thisRepository%>@<%=thisRepository%>"/>
			<ie:param name="group_out"  data="elem"/>
		</ie:webject>
		<ie:webject name="Concat-Groups" type="GRP">
			<ie:param name="GROUP_IN" data="output"/>
			<ie:param name="GROUP_IN" data="elem"/>
			<ie:param name="GROUP_OUT" data="output"/>
      </ie:webject>
	</ie:forEach>
	<%
		if(VERBOSE)getGroup("output").printTree() ;
	%>

</PROCESS>
