/* * SearchAllCollections.java * * Copyright (C) 2001 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ // the package we're in package org.nzdl.gsdl; // long live GNU ... import gnu.getopt.Getopt; import gnu.getopt.LongOpt; // java libraries we're using import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.Serializable; import java.net.URL; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.Vector; // local libraries import org.nzdl.gsdl.service.NzdlCollectionInfo; import org.nzdl.gsdl.service.NzdlQuery; import org.nzdl.gsdl.service.NzdlRequest; import org.nzdl.gsdl.service.NzdlResponse; import org.nzdl.gsdl.service.NzdlResultSet; import org.nzdl.gsdl.service.NzdlService; import org.nzdl.gsdl.service.NzdlServiceClient; import org.nzdl.gsdl.util.NzdlServiceFactory; /** * Class SearchAllCollections * * Send a query to each collection and report the number of results. * * @author Gordon Paynter (paynter@cs.waikato.ac.nz) * @author Stuart Yeates (say1@cs.waikato.ac.nz) * @author Dave Nichols (daven@cs.waikato.ac.nz) * @version $Revision: 2232 $ * @see org.nzdl.gsdl.service.NzdlCollectionInfo * @see org.nzdl.gsdl.service.NzdlQuery * @see org.nzdl.gsdl.service.NzdlRequest * @see org.nzdl.gsdl.service.NzdlResponse * @see org.nzdl.gsdl.service.NzdlResultSet * @see org.nzdl.gsdl.service.NzdlService * @see org.nzdl.gsdl.service.NzdlServiceClient * @see org.nzdl.gsdl.util.NzdlServiceFactory * @see gnu.getopt.Getopt * @see gnu.getopt.LongOpt */ public class SearchAllCollections implements Cloneable, Serializable { /** The underlying CORBA inferface */ NzdlService nzdl = null; /** Look for an IOR in the file first */ boolean useFileFirst = false; /** The IOR that was specified on the commandline */ String IORFromCommandLine = null; /** The URL that was specified on the commandline */ String URLFromCommandLine = null; /** The name of the file we look for IOR's in */ static String IORFileName = "/tmp/localcorba.objid"; /** The query to use */ String queryText = "the"; /** How verbose do we want to be ? */ int verbosity = 0; SearchAllCollections() { nzdl = NzdlServiceFactory.createNzdlService(null, System.getProperties(), URLFromCommandLine, IORFileName, IORFromCommandLine); if (nzdl == null) throw new Error("URK! unable to find an IOR..."); } /** * Normal constructor. the args are passed into the orb * initialisation code so they can extract obscure args ... */ SearchAllCollections(String [] args) { // parseArgs(args); nzdl = NzdlServiceFactory.createNzdlService(args, System.getProperties(), URLFromCommandLine, IORFileName, IORFromCommandLine); if (nzdl == null) throw new Error("Unable to find an IOR."); } public void finalize(){ } /** * Pre: Accept SourceOfID as either: URL or IOR string or Filename * Post: extract localcorba.objid from source of ID */ private String getIorKey(String sourceOfID) { String ior = null; try { sourceOfID = sourceOfID.trim(); System.err.println("Reading IOR from: " + sourceOfID); String firstChars = sourceOfID.substring(0, Math.min(4,sourceOfID.length())).toUpperCase(); // if sourceOfID is a URL --------------------------------------- if (sourceOfID.length() > 4 && firstChars.equalsIgnoreCase("HTTP")) { URL myUrl = new URL(sourceOfID); BufferedReader input =new BufferedReader(new InputStreamReader(myUrl.openStream())); ior = input.readLine(); } // if sourceOfID is an IOR -------------------------------------- else if (sourceOfID.length() > 4 && firstChars.equalsIgnoreCase("IOR:")) { ior = sourceOfID; } // else assume sourceOfID is a file name ------------------------ else { BufferedReader input = new BufferedReader(new FileReader(sourceOfID)); ior = input.readLine(); } } //end of try catch (java.io.IOException e) { System.err.println("Error reading IOR key:\n" + e); System.err.println("Aborting service..."); System.exit(1); } return ior; } // end of getIorKey static String getHelpText() { String s = ""; s += "SearchAllCollections - a Java client to the Greenstone Digital Library"; s += "\n"; s += "Arguments -\n"; s += "Short form Long form Explaination\n"; s += "-h --help Print this help text\n"; s += "-I --IOR Use to connect\n"; s += "-f --file Look for an IOR if \n"; s += "-q --query Use \n"; s += "-v --verbose Use verbosity level of \n"; s += " 0 = silent except fro error reporting\n"; s += " 1 = reporting of operations\n"; s += " 2 = verbose reporting of operations \n"; s += " 3 = reporting of results of operations\n"; s += " 4 = showing all the documents retrieved\n"; s += "\n"; return s; } private boolean parseArgs(String [] args) { LongOpt[] longopts = new LongOpt[9]; int i = 0; StringBuffer sb = new StringBuffer(); longopts[i++] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'); longopts[i++] = new LongOpt("IOR", LongOpt.REQUIRED_ARGUMENT, null, 'I'); longopts[i++] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f'); longopts[i++] = new LongOpt("query", LongOpt.REQUIRED_ARGUMENT, null, 'q'); longopts[i++] = new LongOpt("verbose", LongOpt.REQUIRED_ARGUMENT, null, 'v'); Getopt g = new Getopt("org.nzdl.gsdl.SearchAllCollections", args, "hI:f:c:q:v:", longopts); int c; String arg; try { while ((c = g.getopt()) != -1) { switch(c) { case 'h': System.out.println(getHelpText()); return false; case 'I': IORFromCommandLine = g.getOptarg(); break; case 'f': IORFileName = g.getOptarg(); useFileFirst = true; break; case 'q': queryText = g.getOptarg(); break; case 'v': arg = g.getOptarg(); verbosity = Integer.parseInt(arg); break; default: System.err.print(getHelpText()); System.exit(1); } } } catch (Throwable t) { System.out.println("Exception thrown while processing arguments \n"); t.printStackTrace(System.out); return false; } return true; } void queryAllCollections() { // set of collection titles Set collectionSet = nzdl.getCollectionSet(); System.out.println("Searching for \"" + queryText + "\" in up to " + collectionSet.size() + " collections."); Iterator collectionSetIterator = collectionSet.iterator(); while (collectionSetIterator.hasNext() ) { String collectionName = (String) collectionSetIterator.next(); if (nzdl.hasCollection(collectionName) ) { // if (nzdl.pingCollection(collectionName) ) { // ping is broken, so use builddate to decide whether this // collection is valid NzdlCollectionInfo info = nzdl.getCollectionInfo(collectionName); if (info != null && info.getBuildDate() != 0) { queryCollection(collectionName, queryText); } // } } } } void queryCollection(String collectionName, String queryText) { NzdlQuery query = new NzdlQuery(queryText); query.setMaxDocs( 10 ); NzdlRequest request = new NzdlRequest( query ); NzdlResponse response = new NzdlResponse( ); nzdl.service( collectionName, request, response ); long frequency = response.getResultSet().getNumOfDocs(); if (frequency > 0) { System.out.println("\n------------------------------"); System.out.println(frequency + "\t" + collectionName); describeCollection(collectionName); } } void describeCollection(String collectionName) { // collection name Set n = nzdl.getMetaData(collectionName, "collection", "collectionname"); if (n.size() == 1) { System.out.println("\nName: " + n.toArray()[0]); } // collection description Set d = nzdl.getMetaData(collectionName, "collection", "collectionextra"); if (d.size() == 1) { String value = (String) d.toArray()[0]; if (value.length() > 0) { System.out.println("Description: " + value); } } // basic collection information NzdlCollectionInfo info = nzdl.getCollectionInfo(collectionName); System.out.println("Info: " + info); } public static final void main( String [] args ) { SearchAllCollections client = new SearchAllCollections(args); client.queryAllCollections(); } //main } // class