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.
This commit is contained in:
cdtank 2011-03-04 17:47:23 +00:00
parent d00685eee0
commit e14b399147
4 changed files with 279 additions and 294 deletions

View file

@ -79,13 +79,9 @@ public class DataVisualizationController extends VitroHttpServlet {
} catch (MalformedQueryParametersException e) { } catch (MalformedQueryParametersException e) {
UtilityFunctions.handleMalformedParameters("Visualization Query Error", UtilityFunctions.handleMalformedParameters(e.getMessage(),
e.getMessage(),
vreq,
request,
response, response,
log); log);
} }
return; return;
@ -93,10 +89,7 @@ public class DataVisualizationController extends VitroHttpServlet {
} else { } else {
UtilityFunctions.handleMalformedParameters( UtilityFunctions.handleMalformedParameters(
"Visualization Query Error",
"Inappropriate query parameters were submitted.", "Inappropriate query parameters were submitted.",
vreq,
request,
response, response,
log); log);

View file

@ -140,8 +140,6 @@ public class EntityComparisonUtilityFunctions {
Dataset dataset, VitroRequest vitroRequest) Dataset dataset, VitroRequest vitroRequest)
throws MalformedQueryParametersException { throws MalformedQueryParametersException {
String finalHighestLevelOrganizationURI = "";
String staffProvidedHighestLevelOrganization = ConfigurationProperties.getBean(vitroRequest) String staffProvidedHighestLevelOrganization = ConfigurationProperties.getBean(vitroRequest)
.getProperty("visualization.topLevelOrg"); .getProperty("visualization.topLevelOrg");
@ -157,13 +155,15 @@ public class EntityComparisonUtilityFunctions {
IRIFactory iRIFactory = IRIFactory.jenaImplementation(); IRIFactory iRIFactory = IRIFactory.jenaImplementation();
IRI iri = iRIFactory.create(staffProvidedHighestLevelOrganization); IRI iri = iRIFactory.create(staffProvidedHighestLevelOrganization);
if (iri.hasViolation(false)) {
finalHighestLevelOrganizationURI = EntityComparisonUtilityFunctions if (!iri.hasViolation(false)) {
.getHighestLevelOrganizationURI(log, dataset); return staffProvidedHighestLevelOrganization;
} else {
finalHighestLevelOrganizationURI = staffProvidedHighestLevelOrganization;
} }
} }
return finalHighestLevelOrganizationURI;
/*
* If the provided value was not proper compute it yourself.
* */
return EntityComparisonUtilityFunctions.getHighestLevelOrganizationURI(log, dataset);
} }
} }

View file

@ -51,6 +51,7 @@ public class EntityPublicationCountRequestHandler implements
log, log,
dataset, dataset,
vitroRequest); vitroRequest);
} }
return prepareStandaloneMarkupResponse(vitroRequest, entityURI); return prepareStandaloneMarkupResponse(vitroRequest, entityURI);
} }

View file

@ -1,277 +1,268 @@
/* $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$ */
package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils; package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log;
import org.apache.commons.logging.Log; import org.joda.time.DateTime;
import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormatter;
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.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.visualization.freemarker.VisualizationFrameworkConstants; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.VOConstants; import edu.cornell.mannlib.vitro.webapp.controller.visualization.freemarker.DataVisualizationController;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.VisConstants; import edu.cornell.mannlib.vitro.webapp.controller.visualization.freemarker.VisualizationFrameworkConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.collaborationutils.CollaborationData; import edu.cornell.mannlib.vitro.webapp.visualization.constants.VOConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Activity; import edu.cornell.mannlib.vitro.webapp.visualization.constants.VisConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Collaborator; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.collaborationutils.CollaborationData;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.SubEntity; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Activity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Collaborator;
public class UtilityFunctions { import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.GenericQueryMap;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.SubEntity;
public static Map<String, Integer> getYearToActivityCount(
Set<Activity> activities) { public class UtilityFunctions {
/* public static Map<String, Integer> getYearToActivityCount(
* Create a map from the year to number of publications. Use the BiboDocument's Set<Activity> activities) {
* or Grant's parsedPublicationYear or parsedGrantYear to populate the data passed
* via Activity's getParsedActivityYear. /*
* */ * Create a map from the year to number of publications. Use the BiboDocument's
Map<String, Integer> yearToActivityCount = new TreeMap<String, Integer>(); * or Grant's parsedPublicationYear or parsedGrantYear to populate the data passed
* via Activity's getParsedActivityYear.
for (Activity currentActivity : activities) { * */
Map<String, Integer> yearToActivityCount = new TreeMap<String, Integer>();
/*
* Increment the count because there is an entry already available for for (Activity currentActivity : activities) {
* that particular year.
* */ /*
String activityYear = currentActivity.getParsedActivityYear(); * Increment the count because there is an entry already available for
* that particular year.
if (yearToActivityCount.containsKey(activityYear)) { * */
yearToActivityCount.put(activityYear, String activityYear = currentActivity.getParsedActivityYear();
yearToActivityCount
.get(activityYear) + 1); if (yearToActivityCount.containsKey(activityYear)) {
yearToActivityCount.put(activityYear,
} else { yearToActivityCount
yearToActivityCount.put(activityYear, 1); .get(activityYear) + 1);
}
} else {
} yearToActivityCount.put(activityYear, 1);
}
return yearToActivityCount;
} }
/** 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 * This method is used to return a mapping between activity year & all the collaborators
*/ * that published with ego in that year.
public static Map<String, Set<Collaborator>> getActivityYearToCollaborators( * @param collaborationData
CollaborationData collaborationData) { * @return
*/
Map<String, Set<Collaborator>> yearToCollaborators = new TreeMap<String, public static Map<String, Set<Collaborator>> getActivityYearToCollaborators(
Set<Collaborator>>(); CollaborationData collaborationData) {
Collaborator egoCollaborator = collaborationData.getEgoCollaborator(); Map<String, Set<Collaborator>> yearToCollaborators = new TreeMap<String,
Set<Collaborator>>();
for (Collaborator currNode : collaborationData.getCollaborators()) {
Collaborator egoCollaborator = collaborationData.getEgoCollaborator();
/*
* We have already printed the Ego Node info. for (Collaborator currNode : collaborationData.getCollaborators()) {
* */
if (currNode != egoCollaborator) { /*
* We have already printed the Ego Node info.
for (String year : currNode.getYearToActivityCount().keySet()) { * */
if (currNode != egoCollaborator) {
Set<Collaborator> collaboratorNodes;
for (String year : currNode.getYearToActivityCount().keySet()) {
if (yearToCollaborators.containsKey(year)) {
Set<Collaborator> collaboratorNodes;
collaboratorNodes = yearToCollaborators.get(year);
collaboratorNodes.add(currNode); if (yearToCollaborators.containsKey(year)) {
} else { collaboratorNodes = yearToCollaborators.get(year);
collaboratorNodes.add(currNode);
collaboratorNodes = new HashSet<Collaborator>();
collaboratorNodes.add(currNode); } else {
yearToCollaborators.put(year, collaboratorNodes);
} collaboratorNodes = new HashSet<Collaborator>();
collaboratorNodes.add(currNode);
} yearToCollaborators.put(year, collaboratorNodes);
}
}
} }
return yearToCollaborators;
} }
}
/** 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 * Currently the approach for slugifying filenames is naive. In future if there is need,
*/ * we can write more sophisticated method.
public static String slugify(String textToBeSlugified) { * @param textToBeSlugified
String textBlockSeparator = "-"; * @return
return StringUtils.removeEnd(StringUtils.substring(textToBeSlugified.toLowerCase().trim() */
.replaceAll("[^a-zA-Z0-9-]+", textBlockSeparator), public static String slugify(String textToBeSlugified) {
0, String textBlockSeparator = "-";
VisConstants.MAX_NAME_TEXT_LENGTH), return StringUtils.removeEnd(StringUtils.substring(textToBeSlugified.toLowerCase().trim()
textBlockSeparator); .replaceAll("[^a-zA-Z0-9-]+", textBlockSeparator),
} 0,
VisConstants.MAX_NAME_TEXT_LENGTH),
textBlockSeparator);
public static ResponseValues handleMalformedParameters(String errorPageTitle, }
String errorMessage,
VitroRequest vitroRequest) {
public static ResponseValues handleMalformedParameters(String errorPageTitle,
Portal portal = vitroRequest.getPortal(); String errorMessage,
VitroRequest vitroRequest) {
Map<String, Object> body = new HashMap<String, Object>();
body.put("portalBean", portal); Portal portal = vitroRequest.getPortal();
body.put("error", errorMessage);
body.put("title", errorPageTitle); Map<String, Object> body = new HashMap<String, Object>();
body.put("portalBean", portal);
return new TemplateResponseValues(VisualizationFrameworkConstants.ERROR_TEMPLATE, body); body.put("error", errorMessage);
} body.put("title", errorPageTitle);
public static void handleMalformedParameters(String errorPageTitle, return new TemplateResponseValues(VisualizationFrameworkConstants.ERROR_TEMPLATE, body);
String errorMessage, }
VitroRequest vitroRequest,
HttpServletRequest request, public static void handleMalformedParameters(String errorMessage,
HttpServletResponse response, HttpServletResponse response,
Log log) Log log)
throws ServletException, IOException { throws IOException {
Portal portal = vitroRequest.getPortal(); GenericQueryMap errorDataResponse = new GenericQueryMap();
errorDataResponse.addEntry("error", errorMessage);
request.setAttribute("error", errorMessage);
Gson jsonErrorResponse = new Gson();
RequestDispatcher requestDispatcher = request.getRequestDispatcher(Controllers.BASIC_JSP);
request.setAttribute("bodyJsp", "/templates/visualization/visualization_error.jsp"); response.setContentType("application/octet-stream");
request.setAttribute("portalBean", portal); response.getWriter().write(jsonErrorResponse.toJson(errorDataResponse));
request.setAttribute("title", errorPageTitle); }
try { public static DateTime getValidParsedDateTimeObject(String unparsedDateTime) {
requestDispatcher.forward(request, response);
} catch (Exception e) { for (DateTimeFormatter currentFormatter : VOConstants.POSSIBLE_DATE_TIME_FORMATTERS) {
log.error("EntityEditController could not forward to view.");
log.error(e.getMessage()); try {
log.error(e.getStackTrace());
} DateTime dateTime = currentFormatter.parseDateTime(unparsedDateTime);
} return dateTime;
public static DateTime getValidParsedDateTimeObject(String unparsedDateTime) { } catch (Exception e2) {
/*
for (DateTimeFormatter currentFormatter : VOConstants.POSSIBLE_DATE_TIME_FORMATTERS) { * The current date-time formatter did not pass the muster.
* */
try { }
}
DateTime dateTime = currentFormatter.parseDateTime(unparsedDateTime);
return dateTime; /*
* This means that none of the date time formatters worked.
} catch (Exception e2) { * */
/* return null;
* The current date-time formatter did not pass the muster. }
* */
} /**
} * 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
* This means that none of the date time formatters worked. * 2. Default Entity Year
* */ * @return
return null; */
} public static String getValidYearFromCoreDateTimeString(String inputDate,
String defaultYearInCaseOfError) {
/** /*
* This method will be called to get the inferred end year for the entity. * Always return default year identifier in case of an illegal parsed year.
* The 2 choices, in order, are, * */
* 1. parsed year from core:DateTime object saved in core:dateTimeValue String parsedGrantYear = defaultYearInCaseOfError;
* 2. Default Entity Year
* @return if (inputDate != null) {
*/
public static String getValidYearFromCoreDateTimeString(String inputDate, DateTime validParsedDateTimeObject = UtilityFunctions
String defaultYearInCaseOfError) { .getValidParsedDateTimeObject(inputDate);
/*
* Always return default year identifier in case of an illegal parsed year. if (validParsedDateTimeObject != null) {
* */ return String.valueOf(validParsedDateTimeObject.getYear());
String parsedGrantYear = defaultYearInCaseOfError; }
}
if (inputDate != null) {
return parsedGrantYear;
DateTime validParsedDateTimeObject = UtilityFunctions }
.getValidParsedDateTimeObject(inputDate);
public static String getCSVDownloadURL(String individualURI, String visType, String visMode) {
if (validParsedDateTimeObject != null) {
return String.valueOf(validParsedDateTimeObject.getYear()); ParamMap csvDownloadURLParams = null;
}
} if (StringUtils.isBlank(visMode)) {
return parsedGrantYear; csvDownloadURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY,
} individualURI,
VisualizationFrameworkConstants.VIS_TYPE_KEY,
public static String getCSVDownloadURL(String individualURI, String visType, String visMode) { visType);
ParamMap csvDownloadURLParams = null; } else {
if (StringUtils.isBlank(visMode)) { csvDownloadURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY,
individualURI,
csvDownloadURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, VisualizationFrameworkConstants.VIS_TYPE_KEY,
individualURI, visType,
VisualizationFrameworkConstants.VIS_TYPE_KEY, VisualizationFrameworkConstants.VIS_MODE_KEY,
visType); visMode);
} else { }
csvDownloadURLParams = new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, String csvDownloadLink = UrlBuilder.getUrl(
individualURI, VisualizationFrameworkConstants
VisualizationFrameworkConstants.VIS_TYPE_KEY, .DATA_VISUALIZATION_SERVICE_URL_PREFIX,
visType, csvDownloadURLParams);
VisualizationFrameworkConstants.VIS_MODE_KEY,
visMode); return csvDownloadLink != null ? csvDownloadLink : "" ;
} }
String csvDownloadLink = UrlBuilder.getUrl( public static String getCollaboratorshipNetworkLink(String individualURI,
VisualizationFrameworkConstants String visType,
.DATA_VISUALIZATION_SERVICE_URL_PREFIX, String visMode) {
csvDownloadURLParams);
ParamMap collaboratorshipNetworkURLParams = new ParamMap(
return csvDownloadLink != null ? csvDownloadLink : "" ; VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY,
individualURI,
} VisualizationFrameworkConstants.VIS_TYPE_KEY,
visType,
public static String getCollaboratorshipNetworkLink(String individualURI, VisualizationFrameworkConstants.VIS_MODE_KEY,
String visType, visMode);
String visMode) {
String collaboratorshipNetworkURL = UrlBuilder.getUrl(
ParamMap collaboratorshipNetworkURLParams = new ParamMap( VisualizationFrameworkConstants.FREEMARKERIZED_VISUALIZATION_URL_PREFIX,
VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, collaboratorshipNetworkURLParams);
individualURI,
VisualizationFrameworkConstants.VIS_TYPE_KEY, return collaboratorshipNetworkURL != null ? collaboratorshipNetworkURL : "" ;
visType, }
VisualizationFrameworkConstants.VIS_MODE_KEY,
visMode); public static boolean isEntityAPerson(VitroRequest vreq, SubEntity subentity) {
return vreq.getWebappDaoFactory()
String collaboratorshipNetworkURL = UrlBuilder.getUrl( .getIndividualDao()
VisualizationFrameworkConstants.FREEMARKERIZED_VISUALIZATION_URL_PREFIX, .getIndividualByURI(subentity.getIndividualURI())
collaboratorshipNetworkURLParams); .isVClass("http://xmlns.com/foaf/0.1/Person");
}
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");
}
}