[caboto] r556 committed - merged multivalue changes

1 view
Skip to first unread message

codesite...@google.com

unread,
Jan 15, 2010, 6:41:44 AM1/15/10
to cabot...@googlegroups.com
Revision: 556
Author: mike.a.jones
Date: Fri Jan 15 03:37:41 2010
Log: merged multivalue changes
http://code.google.com/p/caboto/source/detail?r=556

Added:
/trunk/Caboto/caboto/src/main/java/META-INF
Modified:
/trunk/Caboto/caboto/src/main/java/org/caboto/CabotoJsonSupport.java
/trunk/Caboto/caboto/src/main/java/org/caboto/dao/AnnotationDaoImpl.java
/trunk/Caboto/caboto/src/main/java/org/caboto/domain/Annotation.java
/trunk/Caboto/caboto/src/main/java/org/caboto/domain/AnnotationFactory.java
/trunk/Caboto/caboto/src/main/java/org/caboto/filters

/trunk/Caboto/caboto/src/main/java/org/caboto/validation/AnnotationValidatorImpl.java

/trunk/Caboto/caboto/src/test/java/org/caboto/dao/AnnotationDaoImplTest.java
/trunk/Caboto/caboto/src/test/java/org/caboto/filters

/trunk/Caboto/caboto/src/test/java/org/caboto/rest/resources/AboutResourceTest.java

/trunk/Caboto/caboto/src/test/java/org/caboto/rest/resources/AbstractResourceTest.java

/trunk/Caboto/caboto/src/test/java/org/caboto/rest/resources/BasicAuthenticationClientFilter.java

/trunk/Caboto/caboto/src/test/java/org/caboto/validation/AnnotationValidationTest.java
/trunk/Caboto/caboto/src/test/resources/test-graph3.rdf

=======================================
--- /trunk/Caboto/caboto/src/main/java/org/caboto/CabotoJsonSupport.java
Tue Oct 14 07:21:33 2008
+++ /trunk/Caboto/caboto/src/main/java/org/caboto/CabotoJsonSupport.java
Fri Jan 15 03:37:41 2010
@@ -33,15 +33,16 @@
*/
package org.caboto;

+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
-import org.codehaus.jettison.json.JSONArray;
-import org.codehaus.jettison.json.JSONException;
-import org.codehaus.jettison.json.JSONObject;

/**
* A utility class used to convert RDF (Jena Model and Resource classes)
to JSON.
@@ -74,6 +75,10 @@
}

public JSONObject generateJsonObject(Resource resource) throws
JSONException {
+ return generateJsonObject(resource, false);
+ }
+
+ public JSONObject generateJsonObject(Resource resource, boolean
multiValued) throws JSONException {

JSONObject jsonObject = new JSONObject();
jsonObject.put("id", resource.getURI());
@@ -102,12 +107,18 @@
}

} else {
- jsonObject.put(key, generateJsonObject((Resource)
stmt.getObject()));
+ jsonObject.put(key, generateJsonObject((Resource)
stmt.getObject(), true));
}

} else if (stmt.getObject().isLiteral()) {
-
- jsonObject.put(key, (((Literal)
stmt.getObject()).getLexicalForm()));
+ if(multiValued) {
+ if(!jsonObject.has(key)) {
+ jsonObject.put(key, new JSONArray());
+ }
+ jsonObject.getJSONArray(key).put(((Literal)
stmt.getObject()).getLexicalForm());
+ } else {
+ jsonObject.put(key, ((Literal)
stmt.getObject()).getLexicalForm());
+ }
}

}
=======================================
---
/trunk/Caboto/caboto/src/main/java/org/caboto/dao/AnnotationDaoImpl.java
Wed Mar 25 06:25:30 2009
+++
/trunk/Caboto/caboto/src/main/java/org/caboto/dao/AnnotationDaoImpl.java
Fri Jan 15 03:37:41 2010
@@ -125,23 +125,25 @@

for (ProfileEntry entry : profile.getProfileEntries()) {

- String val = annotation.getBody().get(entry.getId());
- if ((val == null) && !entry.isRequired()){
+ List<String> values =
annotation.getBody().get(entry.getId());
+ if ((values == null) && !entry.isRequired()){
continue;
}

- Property prop =
model.createProperty(entry.getPropertyType());
-
- String dataType = entry.getObjectDatatype();
-
- if (dataType != null) {
-
- if (dataType.equals("String")) {
- bodyResource.addProperty(prop, val,
XSDDatatype.XSDstring);
- }
-
- } else {
- bodyResource.addProperty(prop, val);
+ for(String val: values) {
+ Property prop =
model.createProperty(entry.getPropertyType());
+
+ String dataType = entry.getObjectDatatype();
+
+ if (dataType != null) {
+
+ if (dataType.equals("String")) {
+ bodyResource.addProperty(prop, val,
XSDDatatype.XSDstring);
+ }
+
+ } else {
+ bodyResource.addProperty(prop, val);
+ }
}

}
=======================================
--- /trunk/Caboto/caboto/src/main/java/org/caboto/domain/Annotation.java
Wed Mar 25 06:25:30 2009
+++ /trunk/Caboto/caboto/src/main/java/org/caboto/domain/Annotation.java
Fri Jan 15 03:37:41 2010
@@ -34,10 +34,12 @@
package org.caboto.domain;

import java.text.ParseException;
+import java.util.ArrayList;
import java.util.Date;
+import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
-import java.util.HashMap;

import org.caboto.CabotoUtility;
import org.caboto.profile.Profile;
@@ -47,9 +49,9 @@

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
-import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;

/**
@@ -63,7 +65,7 @@
}

public Annotation(final String id, final String graphId, final String
annotates,
- final String author, final Date created, final
Map<String, String> body,
+ final String author, final Date created, final
Map<String, List<String>> body,
final String type) {
this.id = id;
this.graphId = graphId;
@@ -106,14 +108,20 @@
Iterator<ProfileEntry> profileIter =
profile.getProfileEntries().iterator();
while (profileIter.hasNext()){
profileEntry=profileIter.next();
-
bodyValue=bodyResource.getProperty(model.createProperty(profileEntry.getPropertyType()));
- if (bodyValue!=null) {
- body.put(profileEntry.getId(),bodyValue.getString());
- } else {
+ StmtIterator bodyValues =
bodyResource.listProperties(model.createProperty(profileEntry.getPropertyType()));
+ if(!bodyValues.hasNext()) {
if (profileEntry.isRequired()){
throw new AnnotationException("Annotation needs:" +
profileEntry.getId());
}
- }
+ } else {
+ while(bodyValues.hasNext()) {
+ bodyValue=bodyValues.nextStatement();
+ if(!body.containsKey(profileEntry.getId())) {
+ body.put(profileEntry.getId(),new ArrayList<String>());
+ }
+ body.get(profileEntry.getId()).add(bodyValue.getString());
+ }
+ }
}
}

@@ -158,11 +166,11 @@
this.created = created;
}

- public Map<String, String> getBody() {
+ public Map<String, List<String>> getBody() {
return body;
}

- public void setBody(final Map<String, String> body) {
+ public void setBody(final Map<String, List<String>> body) {
this.body = body;
}

@@ -191,6 +199,6 @@
private String annotates;
private String author;
private Date created;
- private Map<String, String> body = new HashMap<String, String>();
+ private Map<String, List<String>> body = new HashMap<String,
List<String>>();
private String type = "";
}
=======================================
---
/trunk/Caboto/caboto/src/main/java/org/caboto/domain/AnnotationFactory.java
Wed Mar 25 06:25:30 2009
+++
/trunk/Caboto/caboto/src/main/java/org/caboto/domain/AnnotationFactory.java
Fri Jan 15 03:37:41 2010
@@ -135,7 +135,7 @@

// copy the rest of map
for (String key : params.keySet()) {
- annotation.getBody().put(key, params.get(key).get(0));
+ annotation.getBody().put(key, params.get(key));
}

return annotation;
=======================================
---
/trunk/Caboto/caboto/src/main/java/org/caboto/validation/AnnotationValidatorImpl.java
Tue Oct 14 07:21:33 2008
+++
/trunk/Caboto/caboto/src/main/java/org/caboto/validation/AnnotationValidatorImpl.java
Fri Jan 15 03:37:41 2010
@@ -159,11 +159,23 @@
// (3) check that we have required values where
specified in the profile entries
for (ProfileEntry entry : profiles) {

- String bodyValue =
annotation.getBody().get(entry.getId());
+ List<String> bodyValues =
annotation.getBody().get(entry.getId());

if (entry.isRequired()) {

- if (bodyValue == null || bodyValue.length() ==
0) {
+ if (bodyValues == null || bodyValues.size() ==
0) {
+
errors.rejectValue("body", "annotation.body.missingRequiredVal",
+ new String[]{entry.getId()}, "");
+ }
+ // also check that there is at least one value
+ boolean passed = false;
+ for(String val: bodyValues) {
+ if(val != null && val.length() > 0) {
+ passed = true;
+ continue;
+ }
+ }
+ if(!passed) {

errors.rejectValue("body", "annotation.body.missingRequiredVal",
new String[]{entry.getId()}, "");
}
=======================================
---
/trunk/Caboto/caboto/src/test/java/org/caboto/dao/AnnotationDaoImplTest.java
Wed Mar 25 06:25:30 2009
+++
/trunk/Caboto/caboto/src/test/java/org/caboto/dao/AnnotationDaoImplTest.java
Fri Jan 15 03:37:41 2010
@@ -52,7 +52,9 @@
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.caboto.filters.AnnotationFilter;
import org.caboto.filters.PropValAnnotationFilter;
@@ -95,9 +97,11 @@
annotation.setAuthor("http://caboto.org/person/mikej/");
annotation.setType("SimpleComment");

- Map<String, String> map = new HashMap<String, String>();
- map.put("title", "A title to the annotation");
- map.put("description", "Some description of the annotation");
+ Map<String, List<String>> map = new HashMap<String,
List<String>>();
+ map.put("title", new ArrayList<String>());
+ map.get("title").add("A title to the annotation");
+ map.put("description", new ArrayList<String>());
+ map.get("description").add("Some description of the annotation");

annotation.setBody(map);

=======================================
---
/trunk/Caboto/caboto/src/test/java/org/caboto/rest/resources/AboutResourceTest.java
Thu Feb 12 07:02:40 2009
+++
/trunk/Caboto/caboto/src/test/java/org/caboto/rest/resources/AboutResourceTest.java
Fri Jan 15 03:37:41 2010
@@ -34,18 +34,21 @@

package org.caboto.rest.resources;

-import com.hp.hpl.jena.rdf.model.Model;
-import com.sun.jersey.api.client.ClientResponse;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+
import org.caboto.RdfMediaType;
import org.caboto.domain.Annotation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import static org.junit.Assert.*;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.sun.jersey.api.client.ClientResponse;

/**
*
@@ -70,8 +73,9 @@
@Override
public Annotation createTestAnnotation(String graphUri) {
Annotation annotation = super.createTestAnnotation(graphUri);
- Map<String, String> body = annotation.getBody();
- body.put("title", "title" + count);
+ Map<String, List<String>> body = annotation.getBody();
+ body.put("title", new ArrayList());
+ body.get("title").add("title" + count);
count++;
return annotation;
}
=======================================
---
/trunk/Caboto/caboto/src/test/java/org/caboto/rest/resources/AbstractResourceTest.java
Thu Jan 14 10:03:25 2010
+++
/trunk/Caboto/caboto/src/test/java/org/caboto/rest/resources/AbstractResourceTest.java
Fri Jan 15 03:37:41 2010
@@ -33,19 +33,17 @@
*/
package org.caboto.rest.resources;

-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.ModelFactory;
-import com.hp.hpl.jena.sdb.SDBFactory;
-import com.hp.hpl.jena.sdb.Store;
-import com.hp.hpl.jena.sdb.StoreDesc;
-import com.hp.hpl.jena.sdb.sql.JDBC;
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.config.ClientConfig;
-import com.sun.jersey.api.client.config.DefaultClientConfig;
-import com.sun.jersey.api.core.PackagesResourceConfig;
-import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
+import java.net.Authenticator;
+import java.net.PasswordAuthentication;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import junit.framework.TestCase;
+
import org.caboto.dao.AnnotationDao;
import org.caboto.dao.AnnotationDaoImpl;
import org.caboto.domain.Annotation;
@@ -62,12 +60,18 @@
import org.mortbay.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderServlet;

-import java.net.Authenticator;
-import java.net.PasswordAuthentication;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.util.HashMap;
-import java.util.Map;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.sdb.SDBFactory;
+import com.hp.hpl.jena.sdb.Store;
+import com.hp.hpl.jena.sdb.StoreDesc;
+import com.hp.hpl.jena.sdb.sql.JDBC;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+import com.sun.jersey.api.core.PackagesResourceConfig;
+import com.sun.jersey.spi.spring.container.servlet.SpringServlet;

/**
* @author Mike Jones (mike.a...@bristol.ac.uk)
@@ -238,9 +242,11 @@
Annotation createTestAnnotation(String graphUri) {

// body of the annotation
- Map<String, String> body = new HashMap<String, String>();
- body.put("title", "A title");
- body.put("description", "A description");
+ Map<String, List<String>> body = new HashMap<String,
List<String>>();
+ body.put("title", new ArrayList<String>());
+ body.get("title").add("A title");
+ body.put("description", new ArrayList<String>());
+ body.get("description").add("A description");

// main bits of the annotation
Annotation annotation = new Annotation();
=======================================
---
/trunk/Caboto/caboto/src/test/java/org/caboto/validation/AnnotationValidationTest.java
Tue Oct 14 07:21:33 2008
+++
/trunk/Caboto/caboto/src/test/java/org/caboto/validation/AnnotationValidationTest.java
Fri Jan 15 03:37:41 2010
@@ -33,19 +33,21 @@
*/
package org.caboto.validation;

+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import junit.framework.TestCase;
+
import org.caboto.domain.Annotation;
import org.caboto.profile.MockProfileRepositoryImpl;
+import org.junit.Before;
+import org.junit.Test;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;

/**
* @author Mike Jones (mike.a...@bristol.ac.uk)
@@ -67,9 +69,11 @@
annotation.setGraphId("http://caboto.org/person/MikeJ/public/");

// the body of the annotation
- Map<String, String> body = new HashMap<String, String>();
- body.put("title", "This is a test annotation");
- body.put("description", "Lorem ipsum dolor sit amet, consectetur
adipisicing elit, " +
+ Map<String, List<String>> body = new HashMap<String,
List<String>>();
+ body.put("title", new ArrayList<String>());
+ body.get("title").add("This is a test annotation");
+ body.put("description", new ArrayList<String>());
+ body.get("description").add("Lorem ipsum dolor sit amet,
consectetur adipisicing elit, " +
"sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua.");
annotation.setBody(body);

@@ -170,7 +174,8 @@
@Test
public void testIncorrectBody() {

- annotation.getBody().put("extraField", "Some value or other");
+ annotation.getBody().put("extraField", new ArrayList<String>());
+ annotation.getBody().get("extraField").add("Some value or other");

validator.validate(annotation, errors);

@@ -194,7 +199,8 @@
@Test
public void testMissingBodyValue() {

- annotation.getBody().put("title", "");
+ annotation.getBody().put("title", new ArrayList<String>());
+ annotation.getBody().get("title").add("");

validator.validate(annotation, errors);

Reply all
Reply to author
Forward
0 new messages