package gt.ext;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.Iterator;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.collections.WTHashSet;
import wt.fc.collections.WTSet;
import wt.iba.definition.StringDefinition;
import wt.iba.value.StringValue;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.pds.StatementSpec;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.type.TypeDefinitionReference;
import wt.type.TypedUtility;
import wt.util.WTException;
import wt.vc.VersionControlHelper;
public class FilterWtpartAccordingtocondition implements RemoteAccess{
public static void main(String[] args) throws RemoteException, InvocationTargetException {
Class[] args1 = { String.class, String.class,boolean.class, String.class, String.class };
Object[] args2 = { "00**", "com.galaxy.gt034.PTL_Part",false,"Helical/Emill","ZUNT - Unit Sales" };
//WTPart part = null;
// String number;
RemoteMethodServer rms = RemoteMethodServer.getDefault();
rms.setUserName("wcadmin");
rms.setPassword("itcadmin");
System.out.println("Windchill loged in ");
rms.invoke("searchWTPart", gt.ext.FilterWtpartAccordingtocondition.class.getName(),
null, args1, args2);
// TODO Auto-generated method stub
}
@SuppressWarnings("deprecation")
public static WTSet searchWTPart(String partNumber, String type, boolean exactMatch ,String productfamilyforjsp,String ordertypeformjsp) throws WTException {
System.out.println("Method partNumber "+partNumber);
long branchId = 0;
int iPartMaster, iPart;
String criteria;
QueryResult qr = null;
//wt.pds.PartialResultException qr1 = new PartialResultException(arg0);
QuerySpec qs= new QuerySpec();
iPartMaster = qs.addClassList(WTPartMaster.class,true);
int defindex = qs.addClassList(StringDefinition.class, false);
int valueindex = qs.addClassList(StringValue.class, false);
//int valueindex1 = qs.addClassList(StringValue.class, false);
// search product Family as per jsp product family Input
if(productfamilyforjsp.equalsIgnoreCase("Helical/Emill"))
{
productfamilyforjsp="Helical/Emill";
}
if(productfamilyforjsp.equalsIgnoreCase("Fluid Coupling"))
{
productfamilyforjsp="Fluid Coupling";
}
if(productfamilyforjsp.equalsIgnoreCase("Worm/Altra"))
{
productfamilyforjsp="Worm";
}
if(productfamilyforjsp.equalsIgnoreCase("Gear Motor"))
{
productfamilyforjsp="Geared motor";
}
if(productfamilyforjsp.equalsIgnoreCase("Planetary"))
{
productfamilyforjsp="Planetary";
}
if(productfamilyforjsp.equalsIgnoreCase("Lift"))
{
productfamilyforjsp="Lift";
}
if(partNumber.contains("*")){
System.out.println("Leng: "+partNumber.length());
partNumber=partNumber.substring(0, (partNumber.length()-1));
partNumber=partNumber+"%";
partNumber=partNumber.toUpperCase();
System.out.println("Key: "+partNumber);
}
else{
partNumber="%"+partNumber+"%";
partNumber=partNumber.toUpperCase();
}
System.out.println("Process part NO :-"+partNumber);
iPart=qs.appendClassList(WTPart.class,false);
if(exactMatch){criteria = SearchCondition.EQUAL;}
else{criteria = SearchCondition.LIKE;}
System.out.println("criteria for check :-"+criteria);
SearchCondition sc_number=new SearchCondition(WTPartMaster.class, WTPartMaster.NUMBER, criteria, partNumber);
SearchCondition sc_master=new SearchCondition(WTPart.class, "masterReference.key.id", WTPartMaster.class, "thePersistInfo.theObjectIdentifier.id");
TypeDefinitionReference tref=TypedUtility.getTypeDefinitionReference(type);
branchId = tref.getKey().getBranchId();
SearchCondition sc_branch=new SearchCondition(WTPart.class,"typeDefinitionReference.key.branchId",SearchCondition.EQUAL,branchId);
qs.appendWhere(sc_master,new int []{iPart,iPartMaster});
qs.appendAnd();
qs.appendWhere(sc_number, new int[]{iPartMaster});
qs.appendAnd();
qs.appendWhere(sc_branch, new int[]{iPart});
qs.appendAnd();
qs.appendWhere(new SearchCondition(StringDefinition.class,
StringDefinition.NAME, SearchCondition.EQUAL, "ProductFamily"),
defindex);
qs.appendAnd();
qs.appendWhere(new SearchCondition(StringValue.class,
StringValue.VALUE, SearchCondition.EQUAL, productfamilyforjsp),
valueindex);
qs.appendAnd();
// to check value and name is equal
qs.appendWhere(new SearchCondition(StringDefinition.class,
"thePersistInfo.theObjectIdentifier.id", StringValue.class,
"definitionReference.key.id"),
new int[] { defindex, valueindex });
qs.appendAnd();
if(ordertypeformjsp.equalsIgnoreCase("ZUNT - Unit Sales"))
{
qs.appendWhere(new SearchCondition(StringDefinition.class,
StringDefinition.NAME, SearchCondition.EQUAL, "MTART"),
defindex);
qs.appendAnd();
qs.appendWhere(new SearchCondition(StringValue.class,
StringValue.VALUE, SearchCondition.EQUAL, "ZFRT"),
valueindex);
qs.appendAnd();
// to check value and name is equal
qs.appendWhere(new SearchCondition(StringDefinition.class,
"thePersistInfo.theObjectIdentifier.id", StringValue.class,
"definitionReference.key.id"),
new int[] { defindex, valueindex });
//qs.appendHaving(new SearchCondition(StringDefinition.class,StringDefinition.NAME, SearchCondition.EQUAL, "MTART"),valueindex );
}
if(ordertypeformjsp.equalsIgnoreCase("ZEXP - Export Sales"))
{
qs.appendWhere(new SearchCondition(StringDefinition.class,
StringDefinition.NAME, SearchCondition.EQUAL, "MTART"),
defindex);
qs.appendAnd();
qs.appendWhere(new SearchCondition(StringValue.class,
StringValue.VALUE, SearchCondition.EQUAL, "ZFRT"),
valueindex);
qs.appendAnd();
// to check value and name is equal
qs.appendWhere(new SearchCondition(StringDefinition.class,
"thePersistInfo.theObjectIdentifier.id", StringValue.class,
"definitionReference.key.id"),
new int[] { defindex, valueindex });
qs.appendOr();
qs.appendWhere(new SearchCondition(StringDefinition.class,
StringDefinition.NAME, SearchCondition.EQUAL, "MTART"),
defindex);
qs.appendAnd();
qs.appendWhere(new SearchCondition(StringValue.class,
StringValue.VALUE, SearchCondition.EQUAL, "ZSPR"),
valueindex);
qs.appendAnd();
// to check value and name is equal
qs.appendWhere(new SearchCondition(StringDefinition.class,
"thePersistInfo.theObjectIdentifier.id", StringValue.class,
"definitionReference.key.id"),
new int[] { defindex, valueindex });
}
if(ordertypeformjsp.equalsIgnoreCase("ZDEX - Deemed Exports"))
{
qs.appendWhere(new SearchCondition(StringDefinition.class,
StringDefinition.NAME, SearchCondition.EQUAL, "MTART"),
defindex);
qs.appendAnd();
qs.appendWhere(new SearchCondition(StringValue.class,
StringValue.VALUE, SearchCondition.EQUAL, "ZFRT"),
valueindex);
qs.appendAnd();
// to check value and name is equal
qs.appendWhere(new SearchCondition(StringDefinition.class,
"thePersistInfo.theObjectIdentifier.id", StringValue.class,
"definitionReference.key.id"),
new int[] { defindex, valueindex });
qs.appendOr();
qs.appendWhere(new SearchCondition(StringDefinition.class,
StringDefinition.NAME, SearchCondition.EQUAL, "MTART"),
defindex);
qs.appendAnd();
qs.appendWhere(new SearchCondition(StringValue.class,
StringValue.VALUE, SearchCondition.EQUAL, "ZSPR"),
valueindex);
qs.appendAnd();
// to check value and name is equal
qs.appendWhere(new SearchCondition(StringDefinition.class,
"thePersistInfo.theObjectIdentifier.id", StringValue.class,
"definitionReference.key.id"),
new int[] { defindex, valueindex });
}
if(ordertypeformjsp.equalsIgnoreCase("ZSPA - Spares Sales"))
{
qs.appendWhere(new SearchCondition(StringDefinition.class,
StringDefinition.NAME, SearchCondition.EQUAL, "MTART"),
defindex);
qs.appendAnd();
qs.appendWhere(new SearchCondition(StringValue.class,
StringValue.VALUE, SearchCondition.EQUAL, "ZSPR"),
valueindex);
qs.appendAnd();
// to check value and name is equal
qs.appendWhere(new SearchCondition(StringDefinition.class,
"thePersistInfo.theObjectIdentifier.id", StringValue.class,
"definitionReference.key.id"),
new int[] { defindex, valueindex });
}
if(ordertypeformjsp.equalsIgnoreCase("ZTRD - Third Party Sales"))
{
qs.appendWhere(new SearchCondition(StringDefinition.class,
StringDefinition.NAME, SearchCondition.EQUAL, "MTART"),
defindex);
qs.appendAnd();
qs.appendWhere(new SearchCondition(StringValue.class,
StringValue.VALUE, SearchCondition.EQUAL, "ZSPR"),
valueindex);
qs.appendAnd();
// to check value and name is equal
qs.appendWhere(new SearchCondition(StringDefinition.class,
"thePersistInfo.theObjectIdentifier.id", StringValue.class,
"definitionReference.key.id"),
new int[] { defindex, valueindex });
}
System.out.println("\n\n ======== QUERY =======\n" + qs.toString() + "\n====================\n");
qr = new QueryResult();
qr = PersistenceHelper.manager.find((StatementSpec) qs);
qs.setQueryLimit(-1);
// qs.setQueryLimit(-1);
System.out.println("searchWTPart");
System.out.println("Query size : " + qr.size());
WTSet set = new WTHashSet(qr);
System.out.println("Found " + set.size() + " objects!");
//System.out.println("PART querystring: \n "+qs.toString());
System.out.println("size"+set.size());
Iterator<?> it = null;
it = set.persistableIterator();
String number="";
WTPart part =null;
while(it.hasNext())
{
WTPartMaster partMaster = (WTPartMaster) it.next();
part = (WTPart) VersionControlHelper.service.allIterationsOf(partMaster).nextElement();
number=part.getNumber();
System.out.println(number);
}
/* if(part!=null)
{
MaterialTYpe=IbaUtil.retriveValue(part, "MTART");
// System.out.println("MATNR="+MaterialTYpe);
productFamily=IbaUtil.retriveValue(part, "WERKS"); //change internal name as "WERKS" for test server
plant= IbaUtil.retriveValue(part, "ProductFamily"); //change internal name as "ProductFamily" for test server
//System.out.println("Product Family :- "+productFamily);
//System.out.println("Plant"+plant);
//Actualsetofpart.add(part);
//System.out.println("ordertypeactualvalue==="+ordertypeactualvalue);
//System.out.println("productFamily:-"+productFamily+"\n productfamilyforjsp:-"+productfamilyforjsp);
//System.out.println(ordertypeformjsp.equalsIgnoreCase("ZUNT - Unit Sales") );
if(productFamily.equalsIgnoreCase(productfamilyforjsp))
{
//System.out.println("In if loop to if both product family matched ");
if(ordertypeformjsp.equalsIgnoreCase("ZUNT - Unit Sales"))
{
System.out.println("In if loop for ordertype=ZUNT"+ordertypeactualvalue);
if(MaterialTYpe.equalsIgnoreCase("ZFRT"))
{
System.out.println("In if loop for ordertype=ZUNT and material type=ZFTR" +ordertypeactualvalue);
Actualsetofpart.add(part);
}
}
if(ordertypeformjsp.equalsIgnoreCase("ZEXP - Export Sales"))
{
//System.out.println("In if loop for ordertype=ZEXP"+ordertypeactualvalue);
if(MaterialTYpe.equalsIgnoreCase("ZSPR")|| MaterialTYpe.equalsIgnoreCase("ZFRT"))
{
//System.out.println("In if loop for ordertype=ZUNT and material type=ZSPR or type=ZFRT " +ordertypeactualvalue);
Actualsetofpart.add(part);
}
}
if(ordertypeformjsp.equals("ZDEX - Deemed Exports"))
{
//System.out.println("In if loop for ordertype=ZDEX"+ordertypeactualvalue);
if(MaterialTYpe.equalsIgnoreCase("ZSPR")|| MaterialTYpe.equalsIgnoreCase("ZFRT"))
{
//System.out.println("In if loop for ordertype=ZUNT and material type=ZSPR or type=ZFRT " +ordertypeactualvalue);
Actualsetofpart.add(part);
}
}
if(ordertypeformjsp.equalsIgnoreCase("ZSPA - Spares Sales"))
{
//System.out.println("In if loop for ordertype=ZSPA"+ordertypeactualvalue);
if(MaterialTYpe.equalsIgnoreCase("ZSPR"))
{
//System.out.println("In if loop for ordertype=ZUNT and material type=ZSPR or type=ZFRT " +ordertypeactualvalue);
Actualsetofpart.add(part);
}
}
if(ordertypeformjsp.equalsIgnoreCase("ZTRD - Third Party Sales"))
{
//System.out.println("In if loop for ordertype=ZSPA"+ordertypeactualvalue);
if(MaterialTYpe.equalsIgnoreCase("ZSPR"))
{
//System.out.println("In if loop for ordertype=ZUNT and material type=ZSPR or type=ZFRT " +ordertypeactualvalue);
Actualsetofpart.add(part);
}
}
}
}
}
System.out.println("size"+Actualsetofpart.size());
Iterator<?> it1 = null;
it1 = Actualsetofpart.persistableIterator();
while(it1.hasNext())
{
Actualpart = (WTPart)it1.next();
if(Actualpart!=null)
{
number=Actualpart.getNumber();
// System.out.println(number);
productFamily=IbaUtil.retriveValue(Actualpart, "ProductFamily");
//System.out.println("Producytfamily :-"+productFamily);
}
}*/
return set;
}
}