From e14b399147c707ce54edcd33d62cacd1651fd70d Mon Sep 17 00:00:00 2001 From: cdtank Date: Fri, 4 Mar 2011 17:47:23 +0000 Subject: [PATCH] 1. Fix for incorrect error response in case of DataVisualization request. see http://issues.library.cornell.edu/browse/NIHVIVO-2200 2. Also fix for incorrect logic flow in resolving the highest level org. --- .../DataVisualizationController.java | 9 +- .../EntityComparisonUtilityFunctions.java | 18 +- .../EntityPublicationCountRequestHandler.java | 1 + .../freemarker/visutils/UtilityFunctions.java | 545 +++++++++--------- 4 files changed, 279 insertions(+), 294 deletions(-) diff --git a/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/freemarker/DataVisualizationController.java b/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/freemarker/DataVisualizationController.java index a18f8ae5..88cc9bf2 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/freemarker/DataVisualizationController.java +++ b/src/edu/cornell/mannlib/vitro/webapp/controller/visualization/freemarker/DataVisualizationController.java @@ -79,13 +79,9 @@ public class DataVisualizationController extends VitroHttpServlet { } catch (MalformedQueryParametersException e) { - UtilityFunctions.handleMalformedParameters("Visualization Query Error", - e.getMessage(), - vreq, - request, + UtilityFunctions.handleMalformedParameters(e.getMessage(), response, log); - } return; @@ -93,10 +89,7 @@ public class DataVisualizationController extends VitroHttpServlet { } else { UtilityFunctions.handleMalformedParameters( - "Visualization Query Error", "Inappropriate query parameters were submitted.", - vreq, - request, response, log); diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java index 455c0a36..1ba6c8ff 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java @@ -140,8 +140,6 @@ public class EntityComparisonUtilityFunctions { Dataset dataset, VitroRequest vitroRequest) throws MalformedQueryParametersException { - String finalHighestLevelOrganizationURI = ""; - String staffProvidedHighestLevelOrganization = ConfigurationProperties.getBean(vitroRequest) .getProperty("visualization.topLevelOrg"); @@ -157,13 +155,15 @@ public class EntityComparisonUtilityFunctions { IRIFactory iRIFactory = IRIFactory.jenaImplementation(); IRI iri = iRIFactory.create(staffProvidedHighestLevelOrganization); - if (iri.hasViolation(false)) { - finalHighestLevelOrganizationURI = EntityComparisonUtilityFunctions - .getHighestLevelOrganizationURI(log, dataset); - } else { - finalHighestLevelOrganizationURI = staffProvidedHighestLevelOrganization; + + if (!iri.hasViolation(false)) { + return staffProvidedHighestLevelOrganization; } - } - return finalHighestLevelOrganizationURI; + } + + /* + * If the provided value was not proper compute it yourself. + * */ + return EntityComparisonUtilityFunctions.getHighestLevelOrganizationURI(log, dataset); } } diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java index 39fe9e4d..42738cdc 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java @@ -51,6 +51,7 @@ public class EntityPublicationCountRequestHandler implements log, dataset, vitroRequest); + } return prepareStandaloneMarkupResponse(vitroRequest, entityURI); } diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/UtilityFunctions.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/UtilityFunctions.java index 2a7a8ecb..3125cba8 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/UtilityFunctions.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/UtilityFunctions.java @@ -1,277 +1,268 @@ -/* $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.io.IOException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; - -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.joda.time.DateTime; -import org.joda.time.format.DateTimeFormatter; - -import edu.cornell.mannlib.vitro.webapp.beans.Portal; -import edu.cornell.mannlib.vitro.webapp.controller.Controllers; -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.VisualizationFrameworkConstants; -import edu.cornell.mannlib.vitro.webapp.visualization.constants.VOConstants; -import edu.cornell.mannlib.vitro.webapp.visualization.constants.VisConstants; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.collaborationutils.CollaborationData; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Activity; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Collaborator; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.SubEntity; - -public class UtilityFunctions { - - public static Map getYearToActivityCount( - Set activities) { - - /* - * Create a map from the year to number of publications. Use the BiboDocument's - * or Grant's parsedPublicationYear or parsedGrantYear to populate the data passed - * via Activity's getParsedActivityYear. - * */ - Map yearToActivityCount = new TreeMap(); - - for (Activity currentActivity : activities) { - - /* - * Increment the count because there is an entry already available for - * that particular year. - * */ - String activityYear = currentActivity.getParsedActivityYear(); - - if (yearToActivityCount.containsKey(activityYear)) { - yearToActivityCount.put(activityYear, - yearToActivityCount - .get(activityYear) + 1); - - } else { - yearToActivityCount.put(activityYear, 1); - } - - } - - return yearToActivityCount; - } - - /** - * This method is used to return a mapping between activity year & all the collaborators - * that published with ego in that year. - * @param collaborationData - * @return - */ - public static Map> getActivityYearToCollaborators( - CollaborationData collaborationData) { - - Map> yearToCollaborators = new TreeMap>(); - - Collaborator egoCollaborator = collaborationData.getEgoCollaborator(); - - for (Collaborator currNode : collaborationData.getCollaborators()) { - - /* - * We have already printed the Ego Node info. - * */ - if (currNode != egoCollaborator) { - - for (String year : currNode.getYearToActivityCount().keySet()) { - - Set collaboratorNodes; - - if (yearToCollaborators.containsKey(year)) { - - collaboratorNodes = yearToCollaborators.get(year); - collaboratorNodes.add(currNode); - - } else { - - collaboratorNodes = new HashSet(); - collaboratorNodes.add(currNode); - yearToCollaborators.put(year, collaboratorNodes); - } - - } - - } - } - return yearToCollaborators; - } - - /** - * Currently the approach for slugifying filenames is naive. In future if there is need, - * we can write more sophisticated method. - * @param textToBeSlugified - * @return - */ - public static String slugify(String textToBeSlugified) { - String textBlockSeparator = "-"; - return StringUtils.removeEnd(StringUtils.substring(textToBeSlugified.toLowerCase().trim() - .replaceAll("[^a-zA-Z0-9-]+", textBlockSeparator), - 0, - VisConstants.MAX_NAME_TEXT_LENGTH), - textBlockSeparator); - } - - - public static ResponseValues handleMalformedParameters(String errorPageTitle, - String errorMessage, - VitroRequest vitroRequest) { - - Portal portal = vitroRequest.getPortal(); - - Map body = new HashMap(); - body.put("portalBean", portal); - body.put("error", errorMessage); - body.put("title", errorPageTitle); - - return new TemplateResponseValues(VisualizationFrameworkConstants.ERROR_TEMPLATE, body); - } - - public static void handleMalformedParameters(String errorPageTitle, - String errorMessage, - VitroRequest vitroRequest, - HttpServletRequest request, - HttpServletResponse response, - Log log) - throws ServletException, IOException { - - Portal portal = vitroRequest.getPortal(); - - request.setAttribute("error", errorMessage); - - RequestDispatcher requestDispatcher = request.getRequestDispatcher(Controllers.BASIC_JSP); - request.setAttribute("bodyJsp", "/templates/visualization/visualization_error.jsp"); - request.setAttribute("portalBean", portal); - request.setAttribute("title", errorPageTitle); - - try { - requestDispatcher.forward(request, response); - } catch (Exception e) { - log.error("EntityEditController could not forward to view."); - log.error(e.getMessage()); - log.error(e.getStackTrace()); - } - } - - public static DateTime getValidParsedDateTimeObject(String unparsedDateTime) { - - for (DateTimeFormatter currentFormatter : VOConstants.POSSIBLE_DATE_TIME_FORMATTERS) { - - try { - - DateTime dateTime = currentFormatter.parseDateTime(unparsedDateTime); - return dateTime; - - } catch (Exception e2) { - /* - * The current date-time formatter did not pass the muster. - * */ - } - } - - /* - * This means that none of the date time formatters worked. - * */ - return null; - } - - /** - * This method will be called to get the inferred end year for the entity. - * The 2 choices, in order, are, - * 1. parsed year from core:DateTime object saved in core:dateTimeValue - * 2. Default Entity Year - * @return - */ - public static String getValidYearFromCoreDateTimeString(String inputDate, - String defaultYearInCaseOfError) { - /* - * Always return default year identifier in case of an illegal parsed year. - * */ - String parsedGrantYear = defaultYearInCaseOfError; - - if (inputDate != null) { - - DateTime validParsedDateTimeObject = UtilityFunctions - .getValidParsedDateTimeObject(inputDate); - - if (validParsedDateTimeObject != null) { - return String.valueOf(validParsedDateTimeObject.getYear()); - } - } - - return parsedGrantYear; - } - - public static String getCSVDownloadURL(String individualURI, String visType, String visMode) { - - ParamMap csvDownloadURLParams = null; - - if (StringUtils.isBlank(visMode)) { - - csvDownloadURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, - individualURI, - VisualizationFrameworkConstants.VIS_TYPE_KEY, - visType); - - } else { - - csvDownloadURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, - individualURI, - VisualizationFrameworkConstants.VIS_TYPE_KEY, - visType, - VisualizationFrameworkConstants.VIS_MODE_KEY, - visMode); - - } - - String csvDownloadLink = UrlBuilder.getUrl( - VisualizationFrameworkConstants - .DATA_VISUALIZATION_SERVICE_URL_PREFIX, - csvDownloadURLParams); - - return csvDownloadLink != null ? csvDownloadLink : "" ; - - } - - public static String getCollaboratorshipNetworkLink(String individualURI, - String visType, - String visMode) { - - ParamMap collaboratorshipNetworkURLParams = new ParamMap( - VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, - individualURI, - VisualizationFrameworkConstants.VIS_TYPE_KEY, - visType, - VisualizationFrameworkConstants.VIS_MODE_KEY, - visMode); - - String collaboratorshipNetworkURL = UrlBuilder.getUrl( - VisualizationFrameworkConstants.FREEMARKERIZED_VISUALIZATION_URL_PREFIX, - collaboratorshipNetworkURLParams); - - return collaboratorshipNetworkURL != null ? collaboratorshipNetworkURL : "" ; - } - - public static boolean isEntityAPerson(VitroRequest vreq, SubEntity subentity) { - return vreq.getWebappDaoFactory() - .getIndividualDao() - .getIndividualByURI(subentity.getIndividualURI()) - .isVClass("http://xmlns.com/foaf/0.1/Person"); - } - -} +/* $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.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormatter; + +import com.google.gson.Gson; + +import edu.cornell.mannlib.vitro.webapp.beans.Portal; +import edu.cornell.mannlib.vitro.webapp.controller.Controllers; +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.DataVisualizationController; +import edu.cornell.mannlib.vitro.webapp.controller.visualization.freemarker.VisualizationFrameworkConstants; +import edu.cornell.mannlib.vitro.webapp.visualization.constants.VOConstants; +import edu.cornell.mannlib.vitro.webapp.visualization.constants.VisConstants; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.collaborationutils.CollaborationData; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Activity; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Collaborator; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.GenericQueryMap; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.SubEntity; + +public class UtilityFunctions { + + public static Map getYearToActivityCount( + Set activities) { + + /* + * Create a map from the year to number of publications. Use the BiboDocument's + * or Grant's parsedPublicationYear or parsedGrantYear to populate the data passed + * via Activity's getParsedActivityYear. + * */ + Map yearToActivityCount = new TreeMap(); + + for (Activity currentActivity : activities) { + + /* + * Increment the count because there is an entry already available for + * that particular year. + * */ + String activityYear = currentActivity.getParsedActivityYear(); + + if (yearToActivityCount.containsKey(activityYear)) { + yearToActivityCount.put(activityYear, + yearToActivityCount + .get(activityYear) + 1); + + } else { + yearToActivityCount.put(activityYear, 1); + } + + } + + return yearToActivityCount; + } + + /** + * This method is used to return a mapping between activity year & all the collaborators + * that published with ego in that year. + * @param collaborationData + * @return + */ + public static Map> getActivityYearToCollaborators( + CollaborationData collaborationData) { + + Map> yearToCollaborators = new TreeMap>(); + + Collaborator egoCollaborator = collaborationData.getEgoCollaborator(); + + for (Collaborator currNode : collaborationData.getCollaborators()) { + + /* + * We have already printed the Ego Node info. + * */ + if (currNode != egoCollaborator) { + + for (String year : currNode.getYearToActivityCount().keySet()) { + + Set collaboratorNodes; + + if (yearToCollaborators.containsKey(year)) { + + collaboratorNodes = yearToCollaborators.get(year); + collaboratorNodes.add(currNode); + + } else { + + collaboratorNodes = new HashSet(); + collaboratorNodes.add(currNode); + yearToCollaborators.put(year, collaboratorNodes); + } + + } + + } + } + return yearToCollaborators; + } + + /** + * Currently the approach for slugifying filenames is naive. In future if there is need, + * we can write more sophisticated method. + * @param textToBeSlugified + * @return + */ + public static String slugify(String textToBeSlugified) { + String textBlockSeparator = "-"; + return StringUtils.removeEnd(StringUtils.substring(textToBeSlugified.toLowerCase().trim() + .replaceAll("[^a-zA-Z0-9-]+", textBlockSeparator), + 0, + VisConstants.MAX_NAME_TEXT_LENGTH), + textBlockSeparator); + } + + + public static ResponseValues handleMalformedParameters(String errorPageTitle, + String errorMessage, + VitroRequest vitroRequest) { + + Portal portal = vitroRequest.getPortal(); + + Map body = new HashMap(); + body.put("portalBean", portal); + body.put("error", errorMessage); + body.put("title", errorPageTitle); + + return new TemplateResponseValues(VisualizationFrameworkConstants.ERROR_TEMPLATE, body); + } + + public static void handleMalformedParameters(String errorMessage, + HttpServletResponse response, + Log log) + throws IOException { + + GenericQueryMap errorDataResponse = new GenericQueryMap(); + errorDataResponse.addEntry("error", errorMessage); + + Gson jsonErrorResponse = new Gson(); + + response.setContentType("application/octet-stream"); + response.getWriter().write(jsonErrorResponse.toJson(errorDataResponse)); + } + + public static DateTime getValidParsedDateTimeObject(String unparsedDateTime) { + + for (DateTimeFormatter currentFormatter : VOConstants.POSSIBLE_DATE_TIME_FORMATTERS) { + + try { + + DateTime dateTime = currentFormatter.parseDateTime(unparsedDateTime); + return dateTime; + + } catch (Exception e2) { + /* + * The current date-time formatter did not pass the muster. + * */ + } + } + + /* + * This means that none of the date time formatters worked. + * */ + return null; + } + + /** + * This method will be called to get the inferred end year for the entity. + * The 2 choices, in order, are, + * 1. parsed year from core:DateTime object saved in core:dateTimeValue + * 2. Default Entity Year + * @return + */ + public static String getValidYearFromCoreDateTimeString(String inputDate, + String defaultYearInCaseOfError) { + /* + * Always return default year identifier in case of an illegal parsed year. + * */ + String parsedGrantYear = defaultYearInCaseOfError; + + if (inputDate != null) { + + DateTime validParsedDateTimeObject = UtilityFunctions + .getValidParsedDateTimeObject(inputDate); + + if (validParsedDateTimeObject != null) { + return String.valueOf(validParsedDateTimeObject.getYear()); + } + } + + return parsedGrantYear; + } + + public static String getCSVDownloadURL(String individualURI, String visType, String visMode) { + + ParamMap csvDownloadURLParams = null; + + if (StringUtils.isBlank(visMode)) { + + csvDownloadURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, + individualURI, + VisualizationFrameworkConstants.VIS_TYPE_KEY, + visType); + + } else { + + csvDownloadURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, + individualURI, + VisualizationFrameworkConstants.VIS_TYPE_KEY, + visType, + VisualizationFrameworkConstants.VIS_MODE_KEY, + visMode); + + } + + String csvDownloadLink = UrlBuilder.getUrl( + VisualizationFrameworkConstants + .DATA_VISUALIZATION_SERVICE_URL_PREFIX, + csvDownloadURLParams); + + return csvDownloadLink != null ? csvDownloadLink : "" ; + + } + + public static String getCollaboratorshipNetworkLink(String individualURI, + String visType, + String visMode) { + + ParamMap collaboratorshipNetworkURLParams = new ParamMap( + VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, + individualURI, + VisualizationFrameworkConstants.VIS_TYPE_KEY, + visType, + VisualizationFrameworkConstants.VIS_MODE_KEY, + visMode); + + String collaboratorshipNetworkURL = UrlBuilder.getUrl( + VisualizationFrameworkConstants.FREEMARKERIZED_VISUALIZATION_URL_PREFIX, + collaboratorshipNetworkURLParams); + + return collaboratorshipNetworkURL != null ? collaboratorshipNetworkURL : "" ; + } + + public static boolean isEntityAPerson(VitroRequest vreq, SubEntity subentity) { + return vreq.getWebappDaoFactory() + .getIndividualDao() + .getIndividualByURI(subentity.getIndividualURI()) + .isVClass("http://xmlns.com/foaf/0.1/Person"); + } + +} \ No newline at end of file