/**
*
*/
package crrasolrindexer;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Properties;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
/**
* @author slittle2
*
* A simple class to facilitate searches and managing the Indexer & data.
*
*/
public class TextUICRRASI {
/**
* @param args
*/
// Paths to various objects. These are read in from a parameters file passed at runtime.
private String pathEAD;
private String pathMARC;
private String pathIndex;
private String defaultEAD;
private String defaultMARC;
private String defaultIndex;
private String pathEadSchema;
Properties prop = new Properties();
private class MenuItem {
private int selectionNumber;
private String selectionDescription;
MenuItem(String description, int number) {
selectionNumber = number; // Note that no error-checking occurs to make sure that there is only one item with each number!
selectionDescription = description;
}
}
LinkedHashSet<MenuItem> menu = new LinkedHashSet<MenuItem>();
public static void main(String[] args) throws IOException {
// Loads properties file from arguments passed to it
FileInputStream propFile = new FileInputStream(args[0]);
TextUICRRASI currentMenu = new TextUICRRASI();
currentMenu.prop.load(propFile);
currentMenu.pathIndex = currentMenu.prop.getProperty("solr.url");
currentMenu.pathEAD = currentMenu.prop.getProperty("solr.ead");
currentMenu.pathMARC = currentMenu.prop.getProperty("solr.marc");
currentMenu.defaultIndex = currentMenu.prop.getProperty("solr.url");
currentMenu.defaultEAD = currentMenu.prop.getProperty("solr.ead");
currentMenu.defaultMARC = currentMenu.prop.getProperty("solr.marc");
currentMenu.pathEadSchema = currentMenu.prop.getProperty("crra.schema");
CRRA_EADRetriever.schema_filename = currentMenu.pathEadSchema;
CRRA_EADRetriever.schema_presets = currentMenu.prop.getProperty("crra.presets");
// Initialize menu options
currentMenu.initialize();
// Ask for input & execute commands -- call another function
currentMenu.run();
}
// Displays menu, asks for input, and executes command
private void run() {
while (true) {
String command = " ";
char commandChar;
// Print the menu
System.out.println(this.toString());
// Get a command
command = getStringCommand();
commandChar = command.charAt(0);
// Carry out command
switch (commandChar) {
// Searching the Solr database
case '1':
this.searchDatabase();
break;
// Importing MARC data & indexing it
case '2':
this.indexMARC();
break;
// Importing EAD data & indexing it
case '3':
this.indexEAD();
break;
// Setting the file paths for all of the above
case '4':
this.setMARCpath();
break;
case '5':
this.setEADpath();
break;
// Deletes data
case '6':
this.deleteQuery();
break;
case '7':
this.setIndexPath();
break;
case 'Q':
case 'q':
return; // Quit returns to the method that called
default:
System.out.println("No such command");
}
}
}
private void setIndexPath() {
// Print out current setting
System.out.println("Current path to index is: " + pathIndex);
// Set new setting
pathIndex = getStringCommand();
// Easy way to restore default
if(pathIndex.equalsIgnoreCase("D")){
pathIndex = defaultIndex;
}
}
private void deleteQuery() {
System.out.println("Enter query to delete:");
String query = getStringCommand();
Indexer.deleteRecord(query,pathIndex);
}
private void setEADpath() {
// Print out current setting
System.out.println("Current path to EAD files is: " + pathEAD);
// Set new setting
pathEAD = getStringCommand();
// Easy way to restore default
if(pathEAD.equalsIgnoreCase("D")){
pathEAD = defaultEAD;
}
}
private void setMARCpath() {
// Print out current setting
System.out.println("Current path to MARC file is: " + pathMARC);
// Set new setting
pathMARC = getStringCommand();
// Easy way to restore default
if(pathMARC.equalsIgnoreCase("D")){
pathMARC = defaultMARC;
}
}
private void indexEAD() {
try {
// Read in EAD files
// Pass retrieved records to Index
Indexer.indexCD(CRRA_EADRetriever.eadLoader(pathEAD), pathIndex);
// EADDataRetriever.eadLoader(pathEAD);
} catch (SolrServerException e) {
System.err.println("*** SolrServerException while indexing EAD records ***");
e.printStackTrace();
} catch (IOException e) {
System.err.println("*** IO Exception while indexing EAD records ***");
e.printStackTrace();
}
}
// Calls methods in package to extract data from MARC records in MARC path,
// then index them on the default Solr implementation
private void indexMARC() {
try {
Indexer.indexID(MarcDataRetriever.getMarcData(pathMARC), pathIndex);
} catch (SolrServerException e) {
System.err.println("*** SolrServerException while indexing MARC records ***");
e.printStackTrace();
} catch (IOException e) {
System.err.println("*** IO Exception while indexing MARC records ***");
e.printStackTrace();
}
}
// TODO UNFINISHED: This method was to query Solr. It does NOT work yet.
private void searchDatabase() {
/*SolrServer server = getSolrServer();
String command = getStringCommand();
SolrQuery query = new SolrQuery(command);
QueryResponse rsp = server.query( query );
// SolrDocumentList docs = rsp.getResults();
// List<Item> beans = rsp.getBeans(Item.class);
*/
}
/* These two methods were initially to be used to retrieve commands from the keyboard,
* but have proved unnecessary. They are included in case anyone wants to play with them.
*
// Read a single character from the keyboard. Characters are read until the newline is reached, but only
// the first one is returned.
private static int getIntCommand() {
// BufferedReader commandLine = null;
int bufferChar = 0;
try {
// Get command selection
bufferChar = System.in.read();
} catch (IOException e) {
System.err.println("*** Can't get command ***");
e.printStackTrace();
}
return bufferChar;
}
// Read a single character from the keyboard. Characters are read until the newline is reached, but only
// the first one is returned.
private static char getCharCommand() {
// BufferedReader commandLine = null;
int bufferChar = 0;
try {
// Get command selection
bufferChar = System.in.read();
} catch (IOException e) {
System.err.println("*** Can't get command ***");
e.printStackTrace();
}
return (char) bufferChar;
} */
// Read an entire string from the keyboard, up until a '\n' is reached
private static String getStringCommand() {
BufferedReader commandLine = null;
String command = "";
try {
commandLine = new BufferedReader(new InputStreamReader((System.in)));
try {
command = commandLine.readLine();
} catch (IOException e) {
System.err.println("*** Can't get command *** : " + command);
e.printStackTrace();
}
System.out.println(command);
} finally {
// Don't think there's anything that has to be done at this point...
}
return command;
}
// To add more menu items, add to the list below, and associate a
// method with each one in the run() method.
private void initialize() {
// Menu options include:
//
// Searching the Solr database
// Importing MARC data & indexing it
// Importing EAD data & indexing it
// Setting the file paths for all of the above
// Deleting records according to a query
// Set index path
menu.add(new MenuItem("Search the Solr database", 1));
menu.add(new MenuItem("Import MARC data & index it", 2));
menu.add(new MenuItem("Import EAD data & index it", 3));
menu.add(new MenuItem("Set MARC file path", 4));
menu.add(new MenuItem("Set EAD file path", 5));
menu.add(new MenuItem("Delete records in database", 6));
menu.add(new MenuItem("Set index path", 7));
}
public String toString(){
String description = "";
Iterator<MenuItem> iter = menu.iterator();
// Menu should look like:
//
// -------------------------------
// TextUICRRASI Menu
//
// (list of menu items)
//
// Q - Quit
// -------------------------------
description += "-------------------------------\nTextUICRRASI Menu\n\n";
while(iter.hasNext()){
MenuItem temp = iter.next();
description += temp.selectionNumber + " - " + temp.selectionDescription + "\n";
}
description += "Q - Quit\n-------------------------------\n";
return description;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment