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.Map;
009:
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletResponse;
012:
013: import org.slf4j.Logger;
014: import org.slf4j.LoggerFactory;
015: import org.springframework.validation.BindException;
016: import org.springframework.web.servlet.ModelAndView;
017:
018: import org.openrdf.http.webclient.SessionKeys;
019: import org.openrdf.query.MalformedQueryException;
020: import org.openrdf.query.QueryEvaluationException;
021: import org.openrdf.query.TupleQueryResult;
022: import org.openrdf.repository.RepositoryConnection;
023: import org.openrdf.repository.RepositoryException;
024: import org.openrdf.repository.http.HTTPQueryEvaluationException;
025: import org.openrdf.repository.http.HTTPRepository;
026: import org.openrdf.repository.http.HTTPTupleQuery;
027:
028: public class SelectQueryFormController extends QueryFormController {
029:
030: final Logger logger = LoggerFactory.getLogger(this .getClass());
031:
032: @Override
033: public ModelAndView onSubmit(HttpServletRequest request,
034: HttpServletResponse response, Object command,
035: BindException errors) throws Exception {
036: ModelAndView result = null;
037:
038: HTTPRepository repo = (HTTPRepository) request.getSession()
039: .getAttribute(SessionKeys.REPOSITORY_KEY);
040:
041: SelectQueryInfo qInfo = (SelectQueryInfo) command;
042:
043: TupleQueryResult queryResult = null;
044:
045: RepositoryConnection conn = null;
046: try {
047: conn = repo.getConnection();
048:
049: HTTPTupleQuery query = (HTTPTupleQuery) conn
050: .prepareTupleQuery(qInfo.getQueryLanguage(), qInfo
051: .getQueryString());
052: query.setIncludeInferred(qInfo.isIncludeInferred());
053: try {
054: queryResult = query.evaluate();
055: } catch (HTTPQueryEvaluationException e) {
056: if (e.isCausedByMalformedQueryException()) {
057: throw e.getCauseAsMalformedQueryException();
058: } else if (e.isCausedByRepositoryException()) {
059: throw e.getCauseAsRepositoryException();
060: } else if (e.isCausedByIOException()) {
061: throw e.getCauseAsIOException();
062: } else {
063: throw e;
064: }
065: }
066: } catch (RepositoryException e) {
067: logger.info("Unable to process query", e);
068: errors.reject("repository.error");
069: } catch (MalformedQueryException e) {
070: errors.rejectValue("queryString",
071: "repository.query.error.malformed",
072: new String[] { e.getMessage() }, "Malformed query");
073: } catch (QueryEvaluationException e) {
074: errors.reject("repository.query.error.evaluation");
075: } finally {
076: // FIXME: check to see where this connection gets closed
077: // if (conn != null) {
078: // try {
079: // conn.close();
080: // }
081: // catch (RepositoryException e) {
082: // e.printStackTrace();
083: // }
084: // }
085: }
086:
087: if (errors.hasErrors()) {
088: result = showForm(request, response, errors, errors
089: .getModel());
090: } else {
091: @SuppressWarnings("unchecked")
092: Map<String, Object> model = errors.getModel();
093:
094: model.put("bindingNames", queryResult.getBindingNames());
095: model.put("solutions", queryResult);
096:
097: result = new ModelAndView(getSuccessView(), model);
098: }
099:
100: return result;
101: }
102: }
|