/**
*
*/
package net.fp.rp.search.simple;
import junit.framework.TestCase;
import java.io.File;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
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;
/**
* Todo - move this into servlet / spring and build
*
* @author Paul
*
* Notes on this simple search - just search & index , no feedback or add -
* simple spring configuration
*
*/
public class SimpleTest extends TestCase {
// Logger
private Log log = LogFactory.getLog(this.getClass());
/** Knowledge manager responsable for handling the requests */
private KnowledgeSphereManager knowledgeManager = null;
public void setUp() {
//This value is referenece in the spring config files
//It sets where we create the index and category information
System.setProperty("rp.root","red-adaptive-search/tmp");
File myFile = new File(".");
log.debug(myFile.getAbsolutePath());
ApplicationContext context = new FileSystemXmlApplicationContext(
"red-adaptive-search/war/WEB-INF/simple/appContext.xml");
// get the knowledgemanager
knowledgeManager = (KnowledgeSphereManager) context
.getBean(Config.KNOWLEDGEMANAGER_PLUGINNAME);
}
public void testCreateIndex() {
String info = "c://temp/searchdata//";
String message = knowledgeManager.add(info);
log.info(message);
}
public void testSearchIndex() {
String query = "org";
if (query.trim().length() != 0) {
Map model = knowledgeManager.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!!");
}
}
}