001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.http.webclient.repository.query;
007:
008: import java.util.HashMap;
009: import java.util.Map;
010: import java.util.TreeMap;
011:
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014:
015: import org.slf4j.Logger;
016: import org.slf4j.LoggerFactory;
017: import org.springframework.validation.BindException;
018: import org.springframework.web.servlet.ModelAndView;
019:
020: import org.openrdf.http.webclient.SessionKeys;
021: import org.openrdf.query.MalformedQueryException;
022: import org.openrdf.query.QueryEvaluationException;
023: import org.openrdf.query.QueryLanguage;
024: import org.openrdf.repository.RepositoryConnection;
025: import org.openrdf.repository.RepositoryException;
026: import org.openrdf.repository.http.HTTPBooleanQuery;
027: import org.openrdf.repository.http.HTTPQueryEvaluationException;
028: import org.openrdf.repository.http.HTTPRepository;
029:
030: public class BooleanQueryFormController extends QueryFormController {
031:
032: final Logger logger = LoggerFactory.getLogger(this .getClass());
033:
034: @Override
035: protected Map<String, Object> referenceData(
036: HttpServletRequest request) {
037: Map<String, Object> result = new HashMap<String, Object>();
038:
039: Map<String, QueryLanguage> queryLanguages = new TreeMap<String, QueryLanguage>();
040:
041: queryLanguages.put(
042: QueryLanguage.SPARQL.getName().toUpperCase(),
043: QueryLanguage.SPARQL);
044:
045: result.put("queryLanguages", queryLanguages);
046:
047: return result;
048: }
049:
050: @Override
051: public ModelAndView onSubmit(HttpServletRequest request,
052: HttpServletResponse response, Object command,
053: BindException errors) throws Exception {
054: ModelAndView result = null;
055:
056: HTTPRepository repo = (HTTPRepository) request.getSession()
057: .getAttribute(SessionKeys.REPOSITORY_KEY);
058:
059: BooleanQueryInfo qInfo = (BooleanQueryInfo) command;
060:
061: boolean queryResult = false;
062:
063: RepositoryConnection conn = null;
064: try {
065: conn = repo.getConnection();
066:
067: HTTPBooleanQuery query = (HTTPBooleanQuery) conn
068: .prepareBooleanQuery(qInfo.getQueryLanguage(),
069: qInfo.getQueryString());
070: query.setIncludeInferred(qInfo.isIncludeInferred());
071: try {
072: queryResult = query.evaluate();
073: } catch (HTTPQueryEvaluationException e) {
074: if (e.isCausedByMalformedQueryException()) {
075: throw e.getCauseAsMalformedQueryException();
076: } else if (e.isCausedByRepositoryException()) {
077: throw e.getCauseAsRepositoryException();
078: } else if (e.isCausedByIOException()) {
079: throw e.getCauseAsIOException();
080: } else {
081: throw e;
082: }
083: }
084: } catch (RepositoryException e) {
085: logger.info("Unable to process query", e);
086: errors.reject("repository.error");
087: } catch (MalformedQueryException e) {
088: errors.rejectValue("queryString",
089: "repository.query.error.malformed",
090: new String[] { e.getMessage() }, "Malformed query");
091: } catch (QueryEvaluationException e) {
092: errors.reject("repository.query.error.evaluation");
093: } finally {
094: if (conn != null) {
095: try {
096: conn.close();
097: } catch (RepositoryException e) {
098: e.printStackTrace();
099: }
100: }
101: }
102:
103: if (errors.hasErrors()) {
104: result = showForm(request, response, errors, errors
105: .getModel());
106: } else {
107: @SuppressWarnings("unchecked")
108: Map<String, Object> model = errors.getModel();
109:
110: model.put("answer", queryResult);
111:
112: result = new ModelAndView(getSuccessView(), model);
113: }
114:
115: return result;
116: }
117: }
|