package ora.jwsnut.chapter7.jaxr;
import java.net.PasswordAuthentication;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import javax.security.auth.x500.X500PrivateCredential;
import javax.xml.registry.BulkResponse;
import javax.xml.registry.Connection;
import javax.xml.registry.ConnectionFactory;
import javax.xml.registry.DeclarativeQueryManager;
import javax.xml.registry.FindQualifier;
import javax.xml.registry.JAXRException;
import javax.xml.registry.Query;
import javax.xml.registry.RegistryService;
import javax.xml.registry.infomodel.Classification;
import javax.xml.registry.infomodel.Organization;
import ora.jwsnut.chapter7.util.Utils;
public class SQLQuery {
// The DeclarativeQueryManager
private static DeclarativeQueryManager dqm;
public static void main(String[] args) {
// Validate arguments
if (args.length != 1) {
usage();
}
String queryURL = args[0];
try {
// Get the ConnectionFactory
Properties props = new Properties();
props.put("javax.xml.registry.queryManagerURL", queryURL);
ConnectionFactory cf = ConnectionFactory.newInstance();
cf.setProperties(props);
// Get and initialize the connection
Connection conn = cf.createConnection();
conn.setSynchronous(true);
// Get the RegistryService and the DeclarativeQueryManager
RegistryService registry = conn.getRegistryService();
dqm = registry.getDeclarativeQueryManager();
// Run the example code
doQuery();
// Close connection
conn.close();
} catch (Throwable t) {
System.out.println(t);
t.printStackTrace(System.out);
System.exit(1);
}
System.exit(0);
}
private static void doQuery() throws JAXRException {
// BUG ALERT: according to the spec, we should say "SELECT o.id",
// but the implementation currently requires us to also get the objectType...
Query query = dqm.createQuery(Query.QUERY_TYPE_SQL,
"SELECT * FROM Organization o, Name_ nm " +
"WHERE nm.value LIKE '%ei%' AND " +
"nm.parent = o.id");
BulkResponse res = dqm.executeQuery(query);
// Process exceptions (if any)
Collection exceptions = res.getExceptions();
if (exceptions != null && !exceptions.isEmpty()){
System.out.println("Error during query");
Iterator iter = exceptions.iterator();
while (iter.hasNext()) {
((Exception)iter.next()).printStackTrace(System.out);
}
}
// Process results (if any)
Collection coll = res.getCollection();
System.out.println("Query found " + coll.size());
Iterator iter = coll.iterator();
while (iter.hasNext()) {
Organization org = (Organization)iter.next();
System.out.println("\t" + org.getName().getValue());
// Get the classifications for this organization
Query classQuery = dqm.createQuery(Query.QUERY_TYPE_SQL,
"SELECT * FROM Classification c WHERE id IN " +
"(RegistryObject_classifications('" +
org.getKey().getId() + "'))");
BulkResponse cRes = dqm.executeQuery(classQuery);
// Process exceptions (if any)
exceptions = cRes.getExceptions();
if (exceptions != null && !exceptions.isEmpty()){
System.out.println("Error during classification query");
iter = exceptions.iterator();
while (iter.hasNext()) {
((Exception)iter.next()).printStackTrace(System.out);
}
}
// Process results (if any)
Collection classColl = cRes.getCollection();
Iterator classIter = classColl .iterator();
while (classIter.hasNext()) {
Classification cls = (Classification)classIter.next();
System.out.print(" " + cls.getName().getValue());
}
System.out.println();
}
}
public static void usage() {
System.out.println("Usage:\tSQLQuery queryURL");
System.exit(1);
}
}
|