/*
* Copyright (C) 2004 Paul Browne, http://www.firstpartners.net,
* built with the help of Fast-Soft (fastsoftdev@yahoo.com)
*
* released under terms of the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This product includes software developed by the
* Apache Software Foundation (http://www.apache.org)."
*
* This product includes software developed by the
* Spring Framework Project (http://www.springframework.org)."
*
*/
package net.fp.rp.search.ui.web;
import net.fp.rp.search.back.extractor.util.Bookmarks;
import net.fp.rp.search.back.extractor.util.History;
import net.fp.rp.search.common.util.MessageUtil;
import net.fp.rp.search.common.util.Util;
import net.fp.rp.search.mid.feedback.BaseFeedback;
import net.fp.rp.search.mid.feedback.CategoryFeedback;
import net.fp.rp.search.mid.feedback.DocumentFeedback;
import net.fp.rp.search.mid.global.KnowledgeSphereManager;
import net.fp.rp.search.mid.global.ModelView;
import net.fp.rp.search.mid.global.PluginManager;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Main point of entry into the system for the web interface. This class will
* be responsable for dispatching the request and translate the request to
* be interpreted by the KnowledgeSpherreManager-controller
*
* @author Firstpartners.net
* @version 1.4
* Copyright @link www.firstpartners.net/red
*/
public class RpServlet implements Controller {
/** Get method name */
public static final String METHOD_GET = "GET";
/** The result view */
private String resultPage = "result";
/** The search view */
private String searchPage = "search";
/** The error view */
public static final String ERROR_PAGE = "error";
/** The model name */
public static final String MODEL_NAME = "model";
/** Query parameter */
public static final String PARAM_QUERY = "q";
/** Operation parameter */
public static final String PARAM_OPERATION = "operation";
/** Import favorites operation */
public static final String OP_IMPORT_FAVORITES = "import_favorites";
/** Import history operation */
public static final String OP_IMPORT_HISTORY = "import_history";
/** Document feedback operation */
public static final String OP_DOCUMENT_FEEDBACK = "document_feedback";
/** Category feedback operation */
public static final String OP_CATEGORY_FEEDBACK = "category_feedback";
/** Add operation */
public static final String OP_ADD = "add";
/** Search operation */
public static final String OP_SEARCH = "search";
/** Search term */
public static final String PARAM_SEARCHTERM = "searchterm";
/** Logger for this class and subclasses */
protected final Logger logger = Logger.getLogger(getClass());
/** KnowledgeManager responsable for adding / searching information */
private KnowledgeSphereManager knowledgeManager;
/**
* Process the request and return a ModelAndView object which the
* DispatcherServlet will render.
*
* @param request current HTTP request
* @param response current HTTP response
*
* @return a ModelAndView to render, or null if handled directly
*
* @throws Exception in case of errors
*/
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//Local Variables
String result;
String message;
//define the default model;
Map model = new HashMap();
BaseFeedback feedback = null;
//validate the request mode
String method = request.getMethod();
//allow only GET method requests
if (!METHOD_GET.equals(method)) {
logger.info("Illegal access of the RP System (non-GET)");
return new ModelAndView(ERROR_PAGE);
}
//validate the operation
String operation = request.getParameter(PARAM_OPERATION);
//if the operation is not specified (send the search page back)
if (Util.isEmpty(operation)) {
logger.info("No operation was specified - back to search page");
return new ModelAndView(searchPage);
}
//convert to lower case the operation
operation = operation.toLowerCase();
//validate the operation
logger.info("Operation is :" + operation);
//get the query parameters
String query = request.getParameter(PARAM_QUERY);
if ((OP_SEARCH.equalsIgnoreCase(operation)) && (!Util.isEmpty(query))) {
logger.info("Search process for : " + query);
//fill the model data
model = knowledgeManager.search(query, +1);
} else if (operation.startsWith(OP_ADD) && (!Util.isEmpty(query))) {
//define a model and fill the data
logger.info("Add process for : " + query);
//add the information to the system
result = knowledgeManager.add(query);
//add the status to the model
model.put(ModelView.STATUS, result);
} else if (OP_IMPORT_FAVORITES.equalsIgnoreCase(operation)) {
//define a model and fill the data
logger.info("Import favorites");
//get the message from resource message
message = MessageUtil.getMessage("app.add.status.ok",
new Object[] { " < my Favorites > " });
//add the status to the model
model.put(ModelView.STATUS, message);
//get the bookmarks list (IE and Mozzila)
Bookmarks bookmarks = new Bookmarks();
LinkedList list = bookmarks.getBookmarks();
for (int i = 0; i < list.size(); i++) {
final String location = (String) list.get(i);
//add the information to the system
Thread t = new Thread(new Runnable() {
public void run() {
knowledgeManager.add(location);
}
});
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}
} else if (OP_IMPORT_HISTORY.equals(operation)) {
//define a model and fill the data
logger.info("Import history");
//get the message from resource message
message = MessageUtil.getMessage("app.add.status.ok",
new Object[] { " < my history > " });
//add the status to the model
model.put(ModelView.STATUS, message);
//get the history list (IE and Mozzila)
History history = new History();
LinkedList list = history.getHistoryList();
for (int i = 0; i < list.size(); i++) {
final String location = (String) list.get(i);
//add the information to the system
Thread t = new Thread(new Runnable() {
public void run() {
knowledgeManager.add(location);
}
});
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}
}
else if (OP_DOCUMENT_FEEDBACK.equals(operation)) {
logger.info("Document feedback process");
//read the other params
String score = request.getParameter(DocumentFeedback.SCORE);
String id = request.getParameter(DocumentFeedback.ID);
String categname = request.getParameter(DocumentFeedback.CATEGORY_NAME);
String summary = request.getParameter(DocumentFeedback.DESCRIPTION);
feedback = new DocumentFeedback(Integer.valueOf(score).intValue(),
id, categname, summary);
String searchterm = request.getParameter(PARAM_SEARCHTERM);
//fill the model with data
model = knowledgeManager.feedback(feedback, searchterm);
} else if (OP_CATEGORY_FEEDBACK.equals(operation)) {
logger.info("Category feedback process");
//read the other params
String score = request.getParameter(CategoryFeedback.SCORE);
String categname = request.getParameter(CategoryFeedback.CATEGORY_NAME);
String location = request.getParameter(CategoryFeedback.CATEGORY_LOCATION);
feedback = new CategoryFeedback(Integer.valueOf(score).intValue(),
categname, location);
String searchterm = request.getParameter(PARAM_SEARCHTERM);
//fill the model with data
model = knowledgeManager.feedback(feedback, searchterm);
} else {
logger.info("No valid operation was specified ");
return new ModelAndView(searchPage);
}
//add to the model also the available categories from the system
model.put(ModelView.CATEGORIES,
PluginManager.getCategoryManager().getCategoryList());
//return the model
return new ModelAndView(getResultPage(), MODEL_NAME, model);
}
/**
* Get the knowledge-manager
*
* @return Returns the knowledgeManager.
*/
public KnowledgeSphereManager getKnowledgeManager() {
return knowledgeManager;
}
/**
* Set the knowledge-manager
*
* @param knowledgeManager KnowledgeManager to set.
*/
public void setKnowledgeManager(KnowledgeSphereManager knowledgeManager) {
this.knowledgeManager = knowledgeManager;
}
/**
* @param resultPage The resultPage to set.
*/
public void setResultPage(String resultPage) {
this.resultPage = resultPage;
}
/**
* @return Returns the resultPage.
*/
public String getResultPage() {
return resultPage;
}
/**
* @param searchPage The searchPage to set.
*/
public void setSearchPage(String searchPage) {
this.searchPage = searchPage;
}
/**
* @return Returns the searchPage.
*/
public String getSearchPage() {
return searchPage;
}
}