From f15022e2644e5e62071755664b9d1b4df4000337 Mon Sep 17 00:00:00 2001 From: cdtank Date: Mon, 13 Dec 2010 23:37:07 +0000 Subject: [PATCH] 1. Added license information in the recetly added freemarker templates. 2. Added Utilities vis to the freemarker branch. --- .../visualization/dummyVisClient.ftl | 1 + .../publicationSparklineContent.ftl | 1 + .../freemarker/visualization/utilities.ftl | 3 + .../freemarker/visualization/vistest.ftl | 0 .../visualization/visualizationError.ftl | 2 +- .../VisualizationFrameworkConstants.java | 2 + .../freemarker/VisualizationController.java | 3 - .../PersonPublicationCountRequestHandler.java | 2 - .../utilities/UtilitiesRequestHandler.java | 236 ++++++++++++++++++ .../visutils/AllPropertiesQueryRunner.java | 148 +++++++++++ .../visutils/GenericQueryRunner.java | 131 ++++++++++ .../freemarker/visutils/QueryRunner.java | 11 + 12 files changed, 534 insertions(+), 6 deletions(-) create mode 100644 productMods/templates/freemarker/visualization/utilities.ftl delete mode 100644 productMods/templates/freemarker/visualization/vistest.ftl create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/utilities/UtilitiesRequestHandler.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/AllPropertiesQueryRunner.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/GenericQueryRunner.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/QueryRunner.java diff --git a/productMods/templates/freemarker/visualization/dummyVisClient.ftl b/productMods/templates/freemarker/visualization/dummyVisClient.ftl index 454bd339..d17ecc1c 100644 --- a/productMods/templates/freemarker/visualization/dummyVisClient.ftl +++ b/productMods/templates/freemarker/visualization/dummyVisClient.ftl @@ -1,3 +1,4 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> ${headScripts.add("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js")} ${headScripts.add("http://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22visualization%22%2C%22version%22%3A%221%22%2C%22packages%22%3A%5B%22areachart%22%2C%22imagesparkline%22%5D%7D%5D%7D")} diff --git a/productMods/templates/freemarker/visualization/publicationSparklineContent.ftl b/productMods/templates/freemarker/visualization/publicationSparklineContent.ftl index 1285bfd0..c94c8fc8 100644 --- a/productMods/templates/freemarker/visualization/publicationSparklineContent.ftl +++ b/productMods/templates/freemarker/visualization/publicationSparklineContent.ftl @@ -1,3 +1,4 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> <#assign visContainerID = '${sparklineVO.visContainerDivID}'> <#if sparklineVO.shortVisMode> diff --git a/productMods/templates/freemarker/visualization/utilities.ftl b/productMods/templates/freemarker/visualization/utilities.ftl new file mode 100644 index 00000000..ce299794 --- /dev/null +++ b/productMods/templates/freemarker/visualization/utilities.ftl @@ -0,0 +1,3 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +${utilityResopnse} diff --git a/productMods/templates/freemarker/visualization/vistest.ftl b/productMods/templates/freemarker/visualization/vistest.ftl deleted file mode 100644 index e69de29b..00000000 diff --git a/productMods/templates/freemarker/visualization/visualizationError.ftl b/productMods/templates/freemarker/visualization/visualizationError.ftl index 00bd865f..06476959 100644 --- a/productMods/templates/freemarker/visualization/visualizationError.ftl +++ b/productMods/templates/freemarker/visualization/visualizationError.ftl @@ -1,4 +1,4 @@ -<%-- $This file is distributed under the terms of the license in /doc/license.txt$ --%> +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
diff --git a/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/VisualizationFrameworkConstants.java b/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/VisualizationFrameworkConstants.java index ef40004a..8f9db84c 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/VisualizationFrameworkConstants.java +++ b/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/VisualizationFrameworkConstants.java @@ -24,6 +24,8 @@ public class VisualizationFrameworkConstants { * Vis URL prefix that is seen by all the users */ public static final String VISUALIZATION_URL_PREFIX = "/visualization"; + public static final String FREEMARKERIZED_VISUALIZATION_URL_PREFIX = "/visualizationfm"; + public static final String INDIVIDUAL_URL_PREFIX = "/individual"; /* diff --git a/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/freemarker/VisualizationController.java b/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/freemarker/VisualizationController.java index 88c1bf5e..f57526b7 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/freemarker/VisualizationController.java +++ b/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/freemarker/VisualizationController.java @@ -60,14 +60,11 @@ public class VisualizationController extends FreemarkerHttpServlet { if (StringUtils.equalsIgnoreCase(renderMode, VisualizationFrameworkConstants.DYNAMIC_RENDER_MODE)) { - System.out.println("inside doing ajax request that is dynamic response"); - Configuration config = getConfig(vreq); TemplateResponseValues trv = (TemplateResponseValues) processRequest(vreq); writeTemplate(trv.getTemplateName(), trv.getMap(), config, request, response); } else { - System.out.println("inside doing super.doGet that is normal response"); super.doGet(request, response); } diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/personpubcount/PersonPublicationCountRequestHandler.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/personpubcount/PersonPublicationCountRequestHandler.java index cd381d58..c4fd228e 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/personpubcount/PersonPublicationCountRequestHandler.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/personpubcount/PersonPublicationCountRequestHandler.java @@ -142,8 +142,6 @@ public class PersonPublicationCountRequestHandler implements VisualizationReques sparklineData, yearToPublicationCount); -// requestDispatcher = request.getRequestDispatcher("/templates/page/blankPage.jsp"); - } else { return prepareStandaloneResponse(vitroRequest, sparklineData); diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/utilities/UtilitiesRequestHandler.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/utilities/UtilitiesRequestHandler.java new file mode 100644 index 00000000..82cbfb13 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/utilities/UtilitiesRequestHandler.java @@ -0,0 +1,236 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.utilities; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.logging.Log; + +import com.google.gson.Gson; +import com.hp.hpl.jena.query.DataSource; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.RDFNode; + +import edu.cornell.mannlib.vitro.webapp.beans.Portal; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues; +import edu.cornell.mannlib.vitro.webapp.controller.visualization.freemarker.VisualizationController; +import edu.cornell.mannlib.vitro.webapp.controller.visualization.VisualizationFrameworkConstants; +import edu.cornell.mannlib.vitro.webapp.filestorage.FileServingHelper; +import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryFieldLabels; +import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException; +import edu.cornell.mannlib.vitro.webapp.visualization.valueobjects.GenericQueryMap; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.AllPropertiesQueryRunner; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.GenericQueryRunner; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.QueryRunner; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.UtilityFunctions; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.VisualizationRequestHandler; + +/** + * This request handler is used when you need helpful information to add more context + * to the visualization. It does not have any code for generating the visualization, + * just fires sparql queries to get info for specific cases like, + * 1. thumbnail/image location for a particular individual + * 2. profile information for a particular individual like label, moniker etc + * 3. person level vis url for a particular individual + * etc. + * @author cdtank + */ +public class UtilitiesRequestHandler implements VisualizationRequestHandler { + + public ResponseValues generateVisualization(VitroRequest vitroRequest, + Log log, + DataSource dataSource) { + + String individualURI = vitroRequest.getParameter( + VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY); + + String visMode = vitroRequest.getParameter( + VisualizationFrameworkConstants.VIS_MODE_KEY); + + String preparedURL = ""; + + UrlBuilder urlBuilder = new UrlBuilder(vitroRequest.getPortal()); + + /* + * If the info being requested is about a profile which includes the name, moniker + * & image url. + * */ + if (VisualizationFrameworkConstants.PROFILE_INFO_UTILS_VIS_MODE + .equalsIgnoreCase(visMode)) { + + + String filterRule = "?predicate = j.2:mainImage " + + "|| ?predicate = vitro:moniker " + + "|| ?predicate = rdfs:label"; + + QueryRunner profileQueryHandler = + new AllPropertiesQueryRunner(individualURI, + filterRule, + dataSource, + log); + + try { + + GenericQueryMap profilePropertiesToValues = + profileQueryHandler.getQueryResult(); + + profilePropertiesToValues.addEntry("imageContextPath", + urlBuilder.getBaseUrl()); + + Gson profileInformation = new Gson(); + + return prepareUtilitiesResponse( + profileInformation.toJson(profilePropertiesToValues), + vitroRequest); + + + + } catch (MalformedQueryParametersException e) { + return UtilityFunctions.handleMalformedParameters( + "Visualization Query Error - Utilities Profile Info", + e.getMessage(), + vitroRequest); + } + } else if (VisualizationFrameworkConstants.IMAGE_UTILS_VIS_MODE + .equalsIgnoreCase(visMode)) { + /* + * If the url being requested is about a standalone image, which is used when we + * want to render an image & other info for a co-author OR ego for that matter. + * */ + + Map fieldLabelToOutputFieldLabel = new HashMap(); + fieldLabelToOutputFieldLabel.put("downloadLocation", + QueryFieldLabels.THUMBNAIL_LOCATION_URL); + fieldLabelToOutputFieldLabel.put("fileName", QueryFieldLabels.THUMBNAIL_FILENAME); + + String whereClause = "<" + individualURI + + "> j.2:thumbnailImage ?thumbnailImage . " + + "?thumbnailImage j.2:downloadLocation " + + "?downloadLocation ; j.2:filename ?fileName ."; + + + + QueryRunner imageQueryHandler = + new GenericQueryRunner(individualURI, + fieldLabelToOutputFieldLabel, + whereClause, + dataSource, + log); + + try { + + String thumbnailAccessURL = + getThumbnailInformation( + imageQueryHandler.getQueryResult(), + fieldLabelToOutputFieldLabel); + + return prepareUtilitiesResponse(thumbnailAccessURL, vitroRequest); + + } catch (MalformedQueryParametersException e) { + return UtilityFunctions.handleMalformedParameters( + "Visualization Query Error - Utilities Image Info", + e.getMessage(), + vitroRequest); + } + } else if (VisualizationFrameworkConstants.COAUTHOR_UTILS_VIS_MODE + .equalsIgnoreCase(visMode)) { + + /* + * By default we will be generating profile url else some specific url like + * coAuthorShip vis url for that individual. + * */ + ParamMap coAuthorProfileURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, + individualURI, + VisualizationFrameworkConstants.VIS_TYPE_KEY, + VisualizationFrameworkConstants.COAUTHORSHIP_VIS, + VisualizationFrameworkConstants.RENDER_MODE_KEY, + VisualizationFrameworkConstants.STANDALONE_RENDER_MODE); + + preparedURL = UrlBuilder.getUrl(VisualizationFrameworkConstants.FREEMARKERIZED_VISUALIZATION_URL_PREFIX, + coAuthorProfileURLParams); + + + return prepareUtilitiesResponse(preparedURL, vitroRequest); + + } else if (VisualizationFrameworkConstants.PERSON_LEVEL_UTILS_VIS_MODE + .equalsIgnoreCase(visMode)) { + /* + * By default we will be generating profile url else some specific url like + * coAuthorShip vis url for that individual. + * */ + ParamMap personLevelURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, + individualURI, + VisualizationFrameworkConstants.VIS_TYPE_KEY, + VisualizationFrameworkConstants.PERSON_LEVEL_VIS, + VisualizationFrameworkConstants.RENDER_MODE_KEY, + VisualizationFrameworkConstants.STANDALONE_RENDER_MODE); + + preparedURL = UrlBuilder.getUrl(VisualizationFrameworkConstants.FREEMARKERIZED_VISUALIZATION_URL_PREFIX, + personLevelURLParams); + + return prepareUtilitiesResponse(preparedURL, vitroRequest); + } else { + + ParamMap individualProfileURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, + individualURI); + + preparedURL = UrlBuilder.getUrl(VisualizationFrameworkConstants.INDIVIDUAL_URL_PREFIX, + individualProfileURLParams); + + return prepareUtilitiesResponse(preparedURL, vitroRequest); + } + + } + + private String getThumbnailInformation(ResultSet resultSet, + Map fieldLabelToOutputFieldLabel) { + + String finalThumbNailLocation = ""; + + while (resultSet.hasNext()) { + QuerySolution solution = resultSet.nextSolution(); + + + RDFNode downloadLocationNode = solution.get( + fieldLabelToOutputFieldLabel + .get("downloadLocation")); + RDFNode fileNameNode = solution.get(fieldLabelToOutputFieldLabel.get("fileName")); + + if (downloadLocationNode != null && fileNameNode != null) { + finalThumbNailLocation = + FileServingHelper + .getBytestreamAliasUrl(downloadLocationNode.toString(), + fileNameNode.toString()); + } + } + return finalThumbNailLocation; + } + + private TemplateResponseValues prepareUtilitiesResponse(String preparedContent, VitroRequest vreq) { + +// response.setContentType("text/plain"); + + String utilitiesTemplate = "/visualization/utilities.ftl"; + + Map body = new HashMap(); + body.put("utilityResopnse", preparedContent); + + return new TemplateResponseValues(utilitiesTemplate, body); + + } +} + diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/AllPropertiesQueryRunner.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/AllPropertiesQueryRunner.java new file mode 100644 index 00000000..6ab93ee7 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/AllPropertiesQueryRunner.java @@ -0,0 +1,148 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; + +import com.hp.hpl.jena.iri.IRI; +import com.hp.hpl.jena.iri.IRIFactory; +import com.hp.hpl.jena.iri.Violation; +import com.hp.hpl.jena.query.DataSource; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.query.Syntax; +import com.hp.hpl.jena.rdf.model.RDFNode; + +import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryConstants; +import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryFieldLabels; +import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException; +import edu.cornell.mannlib.vitro.webapp.visualization.valueobjects.GenericQueryMap; + + + +/** + * This query runner is used to execute a sparql query that will fetch all the + * properties available for the provided individual URI. + * + * @author cdtank + */ +public class AllPropertiesQueryRunner implements QueryRunner { + + protected static final Syntax SYNTAX = Syntax.syntaxARQ; + + private String filterRule, individualURI; + private DataSource dataSource; + + private Log log; + + public AllPropertiesQueryRunner(String individualURI, + String filterRule, + DataSource dataSource, + Log log) { + + this.individualURI = individualURI; + this.filterRule = filterRule; + this.dataSource = dataSource; + this.log = log; + + } + + private GenericQueryMap createJavaValueObjects(ResultSet resultSet) { + + GenericQueryMap queryResult = new GenericQueryMap(); + + while (resultSet.hasNext()) { + QuerySolution solution = resultSet.nextSolution(); + + + RDFNode predicateNode = solution.get(QueryFieldLabels.PREDICATE); + RDFNode objectNode = solution.get(QueryFieldLabels.OBJECT); + + if (predicateNode != null && objectNode != null) { + queryResult.addEntry(predicateNode.toString(), + objectNode.toString()); + } + + } + + return queryResult; + } + + private ResultSet executeQuery(String queryText, + DataSource dataSource) { + + QueryExecution queryExecution = null; +// try { + Query query = QueryFactory.create(queryText, SYNTAX); + +// QuerySolutionMap qs = new QuerySolutionMap(); +// qs.add("authPerson", queryParam); // bind resource to s + + queryExecution = QueryExecutionFactory.create(query, dataSource); + + // if (query.isSelectType()) { + return queryExecution.execSelect(); +// } +// } finally { +// if (queryExecution != null) { +// queryExecution.close(); +// } +// +// } +// return null; + } + + private String generateGenericSparqlQuery(String queryURI, String filterRule) { +// Resource uri1 = ResourceFactory.createResource(queryURI); + String filterClause; + + if (StringUtils.isNotBlank(filterRule)) { + filterClause = "FILTER ( " + filterRule + " ) . "; + } else { + filterClause = ""; + } + + String sparqlQuery = QueryConstants.getSparqlPrefixQuery() + + "SELECT " + + " (str(?predicate) as ?" + QueryFieldLabels.PREDICATE + ") " + + " (str(?object) as ?" + QueryFieldLabels.OBJECT + ") " + + "WHERE { " + + "<" + queryURI + "> ?predicate ?object. " + + filterClause + + "}"; + + return sparqlQuery; + } + + public GenericQueryMap getQueryResult() + throws MalformedQueryParametersException { + if (StringUtils.isNotBlank(this.individualURI)) { + /* + * To test for the validity of the URI submitted. + * */ + IRIFactory iRIFactory = IRIFactory.jenaImplementation(); + IRI iri = iRIFactory.create(this.individualURI); + if (iri.hasViolation(false)) { + String errorMsg = ((Violation) iri.violations(false).next()).getShortMessage(); + log.error("Generic Query " + errorMsg); + throw new MalformedQueryParametersException( + "URI provided for an individual is malformed."); + } + + } else { + throw new MalformedQueryParametersException("URI parameter is either null or empty."); + } + + ResultSet resultSet = executeQuery(generateGenericSparqlQuery( + this.individualURI, + this.filterRule), + this.dataSource); + + return createJavaValueObjects(resultSet); + } +} diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/GenericQueryRunner.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/GenericQueryRunner.java new file mode 100644 index 00000000..b9a52641 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/GenericQueryRunner.java @@ -0,0 +1,131 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils; + +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; + +import com.hp.hpl.jena.iri.IRI; +import com.hp.hpl.jena.iri.IRIFactory; +import com.hp.hpl.jena.iri.Violation; +import com.hp.hpl.jena.query.DataSource; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.query.Syntax; + +import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryConstants; +import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException; + + + +/** + * This query runner is used to run a generic sparql query based on the "select", + * "where" & "filter" rules provided to it. + * + * @author cdtank + */ +public class GenericQueryRunner implements QueryRunner { + + protected static final Syntax SYNTAX = Syntax.syntaxARQ; + + private String whereClause, individualURLParam; + private DataSource dataSource; + + private Log log; + + private Map fieldLabelToOutputFieldLabel; + + public GenericQueryRunner(String individualURLParam, + Map fieldLabelToOutputFieldLabel, + String whereClause, + DataSource dataSource, + Log log) { + + this.individualURLParam = individualURLParam; + this.fieldLabelToOutputFieldLabel = fieldLabelToOutputFieldLabel; + this.whereClause = whereClause; + this.dataSource = dataSource; + this.log = log; + + } + + private ResultSet executeQuery(String queryText, + DataSource dataSource) { + + QueryExecution queryExecution = null; +// try { + Query query = QueryFactory.create(queryText, SYNTAX); + +// QuerySolutionMap qs = new QuerySolutionMap(); +// qs.add("authPerson", queryParam); // bind resource to s + + queryExecution = QueryExecutionFactory.create(query, dataSource); + + + //remocve this if loop after knowing what is describe & construct sparql stuff. +// if (query.isSelectType()) { + return queryExecution.execSelect(); +// } +// } finally { +// if (queryExecution != null) { +// queryExecution.close(); +// } +// +// } +// return null; + } + + private String generateGenericSparqlQuery() { +// Resource uri1 = ResourceFactory.createResource(queryURI); + + StringBuilder sparqlQuery = new StringBuilder(); + sparqlQuery.append(QueryConstants.getSparqlPrefixQuery()); + + sparqlQuery.append("SELECT\n"); + + for (Map.Entry currentfieldLabelToOutputFieldLabel + : this.fieldLabelToOutputFieldLabel.entrySet()) { + + sparqlQuery.append("\t(str(?" + currentfieldLabelToOutputFieldLabel.getKey() + ") as ?" + + currentfieldLabelToOutputFieldLabel.getValue() + ")\n"); + + } + + sparqlQuery.append("WHERE {\n"); + + sparqlQuery.append(this.whereClause); + + sparqlQuery.append("}\n"); + + return sparqlQuery.toString(); + } + + public ResultSet getQueryResult() + throws MalformedQueryParametersException { + if (StringUtils.isNotBlank(this.individualURLParam)) { + /* + * To test for the validity of the URI submitted. + * */ + IRIFactory iRIFactory = IRIFactory.jenaImplementation(); + IRI iri = iRIFactory.create(this.individualURLParam); + if (iri.hasViolation(false)) { + String errorMsg = ((Violation) iri.violations(false).next()).getShortMessage(); + log.error("Generic Query " + errorMsg); + throw new MalformedQueryParametersException( + "URI provided for an individual is malformed."); + } + } else { + throw new MalformedQueryParametersException("URI parameter is either null or empty."); + } + + ResultSet resultSet = executeQuery(generateGenericSparqlQuery(), + this.dataSource); + + return resultSet; + } +} diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/QueryRunner.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/QueryRunner.java new file mode 100644 index 00000000..4c01a7f4 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/QueryRunner.java @@ -0,0 +1,11 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils; + +import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException; + +public interface QueryRunner { + + QueryResult getQueryResult() throws MalformedQueryParametersException; + +}