NIHVIVO-202 Modify validation of frontend vitro namespace property edits to allow both the validator "nonempty" and a range datatype validator.

This commit is contained in:
rjy7 2010-03-19 19:47:38 +00:00
parent 1081648126
commit ea25ff4ffb
4 changed files with 142 additions and 13 deletions

View file

@ -1,9 +1,45 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.utils; package edu.cornell.mannlib.vitro.webapp.utils;
import java.util.List;
public class StringUtils { public class StringUtils {
public static String capitalize(String s) { public static String capitalize(String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1); return s.substring(0, 1).toUpperCase() + s.substring(1);
} }
public static String quote(String s) {
return s.isEmpty() ? "" : '"' + s + '"';
}
public static String join(List<?> list, boolean quote, String glue) {
if (glue == null) {
glue = ",";
}
String joinedList = "";
int count = 0;
for (Object o : list) {
String s = o.toString();
if (count > 0) {
joinedList += glue;
}
count++;
joinedList += quote ? quote(s) : s;
}
return joinedList;
}
public static String join(List<?> list) {
return join(list, false, ",");
}
public static String quotedList(List<?> list, String glue) {
return join(list, true, glue);
}
} }

View file

@ -188,7 +188,7 @@ public class RdfLiteralHashTest {
} }
@Test @Test
public void testgetVitroNsPropertyStatement(){ public void testGetVitroNsPropertyStatement(){
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + "@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" +

View file

@ -0,0 +1,74 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.utils;
import java.util.List;
import java.util.ArrayList;
import org.junit.Test;
import junit.framework.Assert;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
public class StringUtilsTest extends AbstractTestClass {
protected static List<String> stringList = new ArrayList<String>();
protected static List<Integer> intList = new ArrayList<Integer>();
static {
stringList.add("apple");
stringList.add("banana");
stringList.add("orange");
intList.add(1);
intList.add(2);
intList.add(3);
}
@Test
public void testCapitalize() {
String s1 = "cat";
Assert.assertEquals("Cat", StringUtils.capitalize(s1));
String s2 = "Cat";
Assert.assertEquals(s2, StringUtils.capitalize(s2));
String s3 = "CAT";
Assert.assertEquals(s3, StringUtils.capitalize(s3));
}
@Test
public void testQuote() {
String s1 = "cat";
Assert.assertEquals("\"cat\"", StringUtils.quote(s1));
String s2 = "";
Assert.assertEquals("", StringUtils.quote(s2));
}
@Test
public void testJoinNoArgs() {
Assert.assertEquals("apple,banana,orange", StringUtils.join(stringList));
Assert.assertEquals("1,2,3", StringUtils.join(intList));
}
@Test
public void testJoinArgs() {
Assert.assertEquals("apple:banana:orange", StringUtils.join(stringList, false, ":"));
Assert.assertEquals("\"apple\"|\"banana\"|\"orange\"", StringUtils.join(stringList, true, "|"));
Assert.assertEquals("\"apple\",\"banana\",\"orange\"", StringUtils.join(stringList, true, null));
Assert.assertEquals("apple,banana,orange", StringUtils.join(stringList, false, null));
Assert.assertEquals("apple...banana...orange", StringUtils.join(stringList, false, "..."));
}
@Test
public void testQuotedList() {
Assert.assertEquals("\"apple\"|\"banana\"|\"orange\"", StringUtils.quotedList(stringList, "|"));
Assert.assertEquals("\"apple\",\"banana\",\"orange\"", StringUtils.quotedList(stringList, null));
}
}

View file

@ -1,5 +1,8 @@
<%-- $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$ --%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Arrays"%>
<%@ page import="com.hp.hpl.jena.rdf.model.Literal"%> <%@ page import="com.hp.hpl.jena.rdf.model.Literal"%>
<%@ page import="com.hp.hpl.jena.rdf.model.Model"%> <%@ page import="com.hp.hpl.jena.rdf.model.Model"%>
@ -11,6 +14,7 @@
<%@ page import="edu.cornell.mannlib.vitro.webapp.controller.VitroRequest"%> <%@ page import="edu.cornell.mannlib.vitro.webapp.controller.VitroRequest"%>
<%@ page import="edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement" %> <%@ page import="edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement" %>
<%@page import="edu.cornell.mannlib.vitro.webapp.web.MiscWebUtils"%> <%@page import="edu.cornell.mannlib.vitro.webapp.web.MiscWebUtils"%>
<%@page import="edu.cornell.mannlib.vitro.webapp.utils.StringUtils" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
@ -42,7 +46,21 @@
//String rangeDatatypeUri = vreq.getWebappDaoFactory().getDataPropertyDao().getRequiredDatatypeURI(subject, prop); //String rangeDatatypeUri = vreq.getWebappDaoFactory().getDataPropertyDao().getRequiredDatatypeURI(subject, prop);
//String rangeDatatypeUri = prop.getRangeDatatypeURI(); //String rangeDatatypeUri = prop.getRangeDatatypeURI();
String rangeDatatypeUri = "http://www.w3.org/2001/XMLSchema#string"; String rangeDatatypeUri = "http://www.w3.org/2001/XMLSchema#string";
vreq.setAttribute("rangeDatatypeUriJson", MiscWebUtils.escape(rangeDatatypeUri)); String rangeDatatypeUriJson = MiscWebUtils.escape(rangeDatatypeUri);
vreq.setAttribute("rangeDatatypeUriJson", rangeDatatypeUriJson);
ArrayList<String> validatorList = new ArrayList<String>();
if (predicateUri.equals(VitroVocabulary.LABEL) || predicateUri.equals(VitroVocabulary.RDF_TYPE)) {
validatorList.add("nonempty");
}
if (!rangeDatatypeUriJson.isEmpty()) {
validatorList.add("datatype:" + rangeDatatypeUriJson);
}
String validators = StringUtils.quotedList(validatorList, ",");
System.out.println("VALIDATORS: " + validators);
vreq.setAttribute("validators", StringUtils.quotedList(validatorList, ","));
%> %>
@ -65,15 +83,12 @@
?subject <${predicate}> ?${propertyName} . ?subject <${predicate}> ?${propertyName} .
</v:jsonset> </v:jsonset>
<%
%>
<%-- RY Add other validation cases here. --%> <c:if test="${propertyName == 'label' || propertyName == 'type'}">
<c:choose> <c:set var="validator" value="nonempty" scope="request" />
<c:when test="${propertyName == 'label' || propertyName == 'type'}"> </c:if>
<c:set var="validator" value="nonempty" />
</c:when>
</c:choose>
<c:set var="editjson" scope="request"> <c:set var="editjson" scope="request">
{ {
@ -101,7 +116,7 @@
"fields" : { "fields" : {
"${propertyName}" : { "${propertyName}" : {
"newResource" : "false", "newResource" : "false",
"validators" : [ <c:if test="${!empty validator}">"${validator}"</c:if> ], "validators" : [ ${validators} ],
"optionsType" : "UNDEFINED", "optionsType" : "UNDEFINED",
"literalOptions" : [ ], "literalOptions" : [ ],
"predicateUri" : "", "predicateUri" : "",
@ -114,7 +129,11 @@
} }
</c:set> </c:set>
<% <%
System.out.println(request.getAttribute("editjson"));
if( log.isDebugEnabled()) log.debug(request.getAttribute("editjson"));
EditConfiguration editConfig = EditConfiguration.getConfigFromSession(session,request); EditConfiguration editConfig = EditConfiguration.getConfigFromSession(session,request);
if (editConfig == null) { if (editConfig == null) {