package edu.indiana.dde.catalog.catalogtester; import java.io.File; import edu.indiana.dde.metadata.catalog.types.*; import edu.indiana.dde.catalog.catalogclient.*; /** * This method is used to test the context query (the query operation). * The host and port default to 8080 on localhost. * The content and hierarchy filters can be set. If the property content * filter is used, the properties returned are status and theme keywords. * If the element content filter is used, the elements returned are the * progress (within status) and the keywords (within theme keywords). * The delivery methods for the result and error status can both be configured * to either DIRECT or STREAMING (independently). * The target criteria (and optionally a context) are provided as files. * * @author Scott Jensen scjensen@cs.indiana.edu * */ public class TestContextQuery extends TesterBase { /** * @param args */ public static void main(String[] args) throws Exception { int option = 0; String hostName = "localhost"; String portNum = "8080"; String myDn = null; String hFilter = "TARGET"; String cFilter = "ID_ONLY"; String resultDelivery = "STREAMING"; String errorDelivery = "DIRECT"; boolean applyAttrFilter = false; boolean applyElementFilter = false; String targetFileName = null; //file containing the queryTarget element String contextFileName = null; //file containing the queryContext element (optional) for (int j = 0; j < args.length; j++) { String argVal = args[j]; if ( argVal.compareToIgnoreCase("--help") == 0 || argVal.compareToIgnoreCase("-help") == 0) { usage(); return; } else if (argVal.startsWith("-")) { if (argVal.compareToIgnoreCase("-target") == 0) option = 1; //target file else if (argVal.compareToIgnoreCase("-context") == 0) option = 2; //context file else if (argVal.compareToIgnoreCase("-hf") == 0) option = 6; //Hierarchy Filter else if (argVal.compareToIgnoreCase("-cf") == 0) option = 7; //Content Filter else if (argVal.compareToIgnoreCase("-rd") == 0) option = 8; // Result Delivery else if (argVal.compareToIgnoreCase("-ed") == 0) option = 9; // Error Delivery else if (argVal.compareToIgnoreCase("-attr") == 0) applyAttrFilter = true; // apply the attr filter - no parameter to follow else if (argVal.compareToIgnoreCase("-element") == 0) applyElementFilter = true; // apply the element filter - no parameter to follow // only applies if applyAttrFilter is false else if (argVal.compareToIgnoreCase("-dn") == 0) option = 3; //distinguished name else if (argVal.compareToIgnoreCase("-h") == 0) option = 4; //host name else if (argVal.compareToIgnoreCase("-p") == 0) option = 5; //port else //invalid option = 0; } else if (option > 0) { if (option == 1) targetFileName = argVal; else if (option == 2) contextFileName = argVal; else if (option == 3) myDn = argVal; else if (option == 4) hostName = argVal; else if (option == 5) portNum = argVal; else if (option == 6) hFilter = argVal; else if (option == 7) cFilter = argVal; else if (option == 8) resultDelivery = argVal; else if (option == 9) errorDelivery = argVal; option = 0; //reset } //end of option setting } //loop through parameters if (myDn == null || targetFileName == null) { usage(); return; } CatalogServiceStub stub = getStub(hostName, portNum, myDn); ContextQueryRequestDocument doc = ContextQueryRequestDocument.Factory.newInstance(); ContextQueryRequestDocument.ContextQueryRequest request = doc.addNewContextQueryRequest(); //The context query consists of: // 1) a user ID // 2) queryTarget // 3) contextQuery (optional) // 4) queryResultFormat // build the target File targetFile = new File(targetFileName); QueryTargetDocument targetDoc = QueryTargetDocument.Factory.parse(targetFile); request.setQueryTarget(targetDoc.getQueryTarget() ); if (contextFileName != null) { File context = new File(contextFileName); ContextQueryDocument contextDoc = ContextQueryDocument.Factory.parse(context); request.setContextQuery(contextDoc.getContextQuery() ); } // build the result format QueryResultFormatType resultFormat = request.addNewQueryResultFormat(); resultFormat.setOffset(0); resultFormat.setCount(0); resultFormat.setHierarchyFilter(HierarchyFilterType.Enum.forString(hFilter) ); if (applyAttrFilter) { System.out.println("Applying attribute filter"); PropertyFilterType filterOne = resultFormat.addNewPropertyFilter(); filterOne.setPropertyName("status"); filterOne.setPropertySource("LEAD"); PropertyFilterType filterTwo = resultFormat.addNewPropertyFilter(); filterTwo.setPropertyName("keywordTheme"); filterTwo.setPropertySource("LEAD"); } else if (applyElementFilter) { System.out.println("Applying element filter"); ElementFilterType filterOne = resultFormat.addNewElementFilter(); filterOne.setPropertyName("status"); filterOne.setPropertySource("LEAD"); filterOne.setPropertyName("progress"); filterOne.setElementSource("LEAD"); filterOne.setElementFilter("HowAmIDoin"); ElementFilterType filterTwo = resultFormat.addNewElementFilter(); filterTwo.setPropertyName("keywordTheme"); filterTwo.setPropertySource("LEAD"); filterTwo.setElementName("themeKeyword"); filterTwo.setElementSource("LEAD"); filterTwo.setElementFilter("SomeTheme"); } else { System.out.println("Applying content filter: " + cFilter); resultFormat.setContentFilter(ContentFilterType.Enum.forString(cFilter) ); } resultFormat.setResultDeliveryMethod(CatalogDeliveryType.Enum.forString(resultDelivery) ); resultFormat.setErrorDeliveryMethod(CatalogDeliveryType.Enum.forString(errorDelivery) ); System.out.println(doc.toString() ); //invoke service QueryResponseDocument myResponse = stub.query(doc); System.out.println(myResponse.getQueryResponse().getCatalogOperationStatus().getStatus().toString() ); System.out.println("\nResponse Document:\n******************\n" + myResponse.toString() ); if (myResponse.validate() ) System.out.println("Valid"); else System.out.println("Sorry Charlie - Not Valid"); stub.cleanup(); return; } //end of main private static void usage() { System.out.println("Usage: TestContextQuery -dn -hf -cf -target -context -rd -ed -attr -element -h -p \n" + "Defaults:\n" + "hf = TARGET\n" + "cf = ID_ONLY\n" + "rd = STREAMING\n" + "ed = DIRECT\n" + "host = localhost\n" + "port = 8080\n" + "Hierarchy Filter Options: TARGET, SUBTREE\n" + "Content Filter Options: ID_ONLY, FULL_SCHEMA\n" + " -attr option will apply attribute filters\n" + " -element option will apply an element filter (unless -attr is used)\n" + "Result and Error Delivery Options: STREAMING, DIRECT\n" + "The target file is required, but the context file is optional"); return; } //end of usage } //end of TestContextQuery