/*
* Copyright (C) 2004-2006 Paul Browne, http://www.firstpartners.net,
*/
package net.fp.rp.search.ui.command;
import java.util.List;
import java.util.Map;
import net.fp.rp.search.back.struct.DocumStruct;
import net.fp.rp.search.mid.global.Config;
import net.fp.rp.search.mid.global.KnowledgeSphereManager;
import net.fp.rp.search.mid.global.ModelView;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* Main point of entry into the system for the command line.
*
* @author brownpa
* Copyright @link www.firstpartners.net/red
*/
public class RpCommandLine {
// Logger
private static Log log = LogFactory.getLog(RpCommandLine.class);
/** Knowledge manager responsable for handling the requests */
private final KnowledgeSphereManager m_manager;
/**
* Creates a new RpCommandLine object.
*
* @param manager Knowledge manager responsable for the add/search
* operation
*/
public RpCommandLine(KnowledgeSphereManager manager) {
this.m_manager = manager;
}
/**
* Add the location to the system
*
* @param info Location to add
*/
public void processAdd(final String info) {
String message = m_manager.add(info);
log.info(message);
}
/**
* Search the query into system
*
* @param query Query to search
*/
public void processSearch(final String query) {
if (query.trim().length() != 0) {
Map model = m_manager.search(query,+1);
String modelError = (String) model.get(ModelView.ERROR);
if ("true".equals(modelError)) {
String modelStatus = (String) model.get(ModelView.STATUS);
log.info(modelStatus);
} else {
try {
StringBuffer text = new StringBuffer();
text.append(Config.NEW_LINE);
text.append(" Results for Search " + query + " ");
text.append(Config.NEW_LINE);
text.append(Config.NEW_LINE);
int nodocums = Integer.valueOf((String) model.get(
ModelView.DOCUMENTSNO)).intValue();
List list = (List) model.get(ModelView.DOCUMENTS);
for (int i = 0; i < nodocums; i++) {
DocumStruct docum = (DocumStruct) list.get(i);
text.append(Config.NEW_LINE);
text.append("Pos. " + i + " " + docum.getTitle() + " " +
docum.getTitle());
text.append(Config.NEW_LINE);
text.append(docum.getPath() + " " +
docum.getLastUpdate());
text.append(Config.NEW_LINE);
}
log.info("Search completed: " + nodocums + " documents found it ");
} catch (NumberFormatException e) {
log.info("Error in retrieving the documents ",e);
}
}
} else {
log.info("Please specify something in order to seach!!");
}
}
/**
* Called by the user from the command line to do an Add / Search /
* Feedback etc.
*
* @param args
*/
public static void main(String[] args) {
String contextDir = "";
String operation = null;
//arguments specified but not correctly
if (args.length < 3) {
System.out.println("Usage of the command line ");
System.out.println(
"RpCommandLine <context> <operation> <information> <information2> ... ");
System.out.println(
" <context> - running context (ex. '.' or d:/tmp ");
System.out.println(
" <operation> - required operation ('ADD'/'SEARCH') ");
System.out.println(
" <information> - information to be added or search for");
return;
}
if (!(Config.OPERATION_ADD.equals(args[1]) ||
Config.OPERATION_SEARCH.equals(args[1]))) {
System.out.println("Usage of the command line ");
System.out.println(
"RpCommandLine <context> <operation> <information> <information2> ... ");
System.out.println(
" <context> - running context (ex. '.' or d:/tmp ");
System.out.println(
" <operation> - required operation ('ADD'/'SEARCH') ");
System.out.println(
" <information> - information to be added or search for");
return;
}
//get the context
contextDir = args[0];
operation = args[1];
//set the application context root the current directory
//in case of web application this is already set in the web.xml (as webaprootkey)
System.setProperty(Config.RP_ROOT, contextDir);
//Commented out as the current version of log4j finds these ok
// //enable the log4j
// try {
//
//
// Log4jConfigurer.initLogging( Config.WEBINF + "/" +
// Config.LOG4J_XML);
// Log4jConfigurer.initLogging(Config.LOG4J_XML);
//
// }
// catch (FileNotFoundException e ){
// e.printStackTrace(System.out);
// }
//
//load the manager
ApplicationContext context = new FileSystemXmlApplicationContext(Config.WEBINF +
"/" + Config.APPCONTEXT);
//get the knowledgemanager
KnowledgeSphereManager manager = (KnowledgeSphereManager) context.getBean(Config.KNOWLEDGEMANAGER_PLUGINNAME);
RpCommandLine command = new RpCommandLine(manager);
//if the add operation was required
if (Config.OPERATION_ADD.equals(operation)) {
//iterate over the information and add it to the system
for (int i = 2; i < args.length; i++) {
command.processAdd(args[i]);
}
} else if (Config.OPERATION_SEARCH.equals(operation)) {
StringBuffer buf = new StringBuffer();
for (int i = 2; i < args.length; i++) {
buf.append(args[i]);
buf.append(" ");
}
command.processSearch(buf.toString().trim());
}
}
}