You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by fc...@apache.org on 2011/11/04 14:43:50 UTC
svn commit: r1197564 - in /incubator/stanbol/trunk:
factstore/src/main/java/org/apache/stanbol/factstore/api/
factstore/src/main/java/org/apache/stanbol/factstore/derby/
factstore/src/main/java/org/apache/stanbol/factstore/model/
factstore/src/main/jav...
Author: fchrist
Date: Fri Nov 4 13:43:50 2011
New Revision: 1197564
URL: http://svn.apache.org/viewvc?rev=1197564&view=rev
Log:
STANBOL-372 Implemented GET for facts by ID. Added two fact schema examples from schema.org.
Added:
incubator/stanbol/trunk/factstore/src/test/resources/factschemas/iks_personslikespage-schema.json
incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_Person.json
incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_WebPage.json
Modified:
incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/api/FactStore.java
incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/derby/DerbyFactStore.java
incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/Fact.java
incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/FactSchema.java
incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/web/resource/FactsResource.java
incubator/stanbol/trunk/factstore/src/test/java/org/apache/stanbol/factstore/FactStoreMock.java
incubator/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/factstore/FactStoreTest.java
Modified: incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/api/FactStore.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/api/FactStore.java?rev=1197564&r1=1197563&r2=1197564&view=diff
==============================================================================
--- incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/api/FactStore.java (original)
+++ incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/api/FactStore.java Fri Nov 4 13:43:50 2011
@@ -33,6 +33,8 @@ public interface FactStore {
public void createFactSchema(FactSchema factSchema) throws Exception;
+ public Fact getFact(int factId, String factSchemaURN) throws Exception;
+
public int addFact(Fact fact) throws Exception;
public void addFacts(Set<Fact> factSet) throws Exception;
Modified: incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/derby/DerbyFactStore.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/derby/DerbyFactStore.java?rev=1197564&r1=1197563&r2=1197564&view=diff
==============================================================================
--- incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/derby/DerbyFactStore.java (original)
+++ incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/derby/DerbyFactStore.java Fri Nov 4 13:43:50 2011
@@ -377,8 +377,85 @@ public class DerbyFactStore implements F
}
}
}
+
+ @Override
+ public Fact getFact(int factId, String factSchemaURN) throws Exception {
+ Fact fact = null;
+ Connection con = null;
+ try {
+ con = DriverManager.getConnection(DB_URL);
+ FactSchema factSchema = this.loadFactSchema(factSchemaURN, con);
+
+ if (factSchema != null) {
+ fact = this.getFact(factId, factSchema, con);
+ }
+
+ } catch (Exception e) {
+ throw e;
+ } finally {
+ try {
+ con.close();
+ } catch (Throwable t) { /* ignore */
+ }
+ }
+
+ return fact;
+ }
+
+ private Fact getFact(int factId, FactSchema factSchema, Connection con) throws Exception {
+ Fact fact = null;
+
+ String factSchemaB64 = Base64.encodeBase64URLSafeString(factSchema.getFactSchemaURN().getBytes());
+ logger.info("Loading fact {} from fact table {}", factId, factSchemaB64);
+
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ try {
+ StringBuilder selectBuilder = new StringBuilder("SELECT ");
+ boolean firstRole = true;
+ for (String role : factSchema.getRoles()) {
+ if (!firstRole) {
+ selectBuilder.append(",");
+ }
+ selectBuilder.append(role);
+ firstRole = false;
+ }
+ selectBuilder.append(" FROM " + factSchemaB64 + " WHERE id=?");
+ ps = con.prepareStatement(selectBuilder.toString());
+ ps.setInt(1, factId);
+ rs = ps.executeQuery();
+
+ if (rs.next()) {
+ fact = new Fact();
+ fact.setFactSchemaURN(factSchema.getFactSchemaURN());
+ for (int col=1; col<=rs.getMetaData().getColumnCount(); col++) {
+ String role = factSchema.fixSpellingOfRole(rs.getMetaData().getColumnName(col));
+ fact.addRole(role, rs.getString(col));
+ }
+ }
+ } catch (SQLException e) {
+ throw new Exception("Error while selecting fact", e);
+ } finally {
+ if (rs != null) {
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ /* ignore */
+ }
+ }
+ if (ps != null) {
+ try {
+ ps.close();
+ } catch (SQLException e) {
+ /* ignore */
+ }
+ }
+ }
+
+ return fact;
+ }
- @Override
+ @Override
public int addFact(Fact fact) throws Exception {
int factId = -1;
Connection con = null;
@@ -411,20 +488,23 @@ public class DerbyFactStore implements F
StringBuilder insertFact = new StringBuilder("INSERT INTO ").append(factSchemaB64).append('(');
StringBuilder valueSB = new StringBuilder(" VALUES (");
+
Map<String,Integer> roleIndexMap = new HashMap<String,Integer>();
boolean firstRole = true;
int roleIndex = 0;
for (String role : factSchema.getRoles()) {
- if (!firstRole) {
- insertFact.append(',');
- valueSB.append(',');
+ if (fact.getRoles().contains(role)) {
+ if (!firstRole) {
+ insertFact.append(',');
+ valueSB.append(',');
+ }
+ insertFact.append(role);
+ valueSB.append('?');
+ firstRole = false;
+
+ roleIndex++;
+ roleIndexMap.put(role, roleIndex);
}
- insertFact.append(role);
- valueSB.append('?');
- firstRole = false;
-
- roleIndex++;
- roleIndexMap.put(role, roleIndex);
}
insertFact.append(')').append(valueSB).append(')');
@@ -503,7 +583,7 @@ public class DerbyFactStore implements F
try {
con = DriverManager.getConnection(DB_URL);
- validateQuery(query, con);
+ FactSchema schema = validateQuery(query, con);
// from here on we have valid data
@@ -549,7 +629,7 @@ public class DerbyFactStore implements F
if (rs != null) {
List<String> header = new ArrayList<String>();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
- header.add(rs.getMetaData().getColumnName(i));
+ header.add(schema.fixSpellingOfRole(rs.getMetaData().getColumnName(i)));
}
frs = new FactResultSet();
@@ -598,7 +678,7 @@ public class DerbyFactStore implements F
return frs;
}
- private void validateQuery(Query query, Connection con) throws Exception {
+ private FactSchema validateQuery(Query query, Connection con) throws Exception {
FactSchema schema = this.loadFactSchema(query.getFromSchemaURN(), con);
if (schema == null) {
@@ -621,6 +701,8 @@ public class DerbyFactStore implements F
+ query.getFromSchemaURN() + "': "
+ unknownRoles.toString());
}
+
+ return schema;
}
}
Modified: incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/Fact.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/Fact.java?rev=1197564&r1=1197563&r2=1197564&view=diff
==============================================================================
--- incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/Fact.java (original)
+++ incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/Fact.java Fri Nov 4 13:43:50 2011
@@ -100,15 +100,10 @@ public class Fact {
JsonLdProperty jldProperty = propMap.get(role);
if (jldProperty.isSingleValued()) {
JsonLdPropertyValue jldValue = jldProperty.getValues().get(0);
- if (jldValue.getType().equals(JsonLdCommon.IRI)) {
+ if (jldValue.getType() != null && jldValue.getType().equals(JsonLdCommon.IRI)) {
fact.addRole(role, jsonLd.unCURIE(jldValue.getLiteralValue()));
- }
- else if (jldValue.getValue() instanceof String) {
- String strType = (String) jldValue.getValue();
- fact.addRole(role, strType);
- }
- else {
- fact.addRole(role, propMap.get(role).toString());
+ } else {
+ fact.addRole(role, jldValue.getValue().toString());
}
}
else {
@@ -121,5 +116,18 @@ public class Fact {
return fact;
}
+
+ public JsonLd factToJsonLd() {
+ JsonLd jsonLd = new JsonLd();
+
+ JsonLdResource subject = new JsonLdResource();
+ subject.setProfile(this.factSchemaURN);
+ for (String role : this.roleMap.keySet()) {
+ subject.putProperty(role, this.roleMap.get(role));
+ }
+ jsonLd.put(subject);
+
+ return jsonLd;
+ }
}
Modified: incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/FactSchema.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/FactSchema.java?rev=1197564&r1=1197563&r2=1197564&view=diff
==============================================================================
--- incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/FactSchema.java (original)
+++ incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/model/FactSchema.java Fri Nov 4 13:43:50 2011
@@ -87,5 +87,15 @@ public class FactSchema {
public boolean hasRole(String role) {
return this.roleMap.keySet().contains(role);
}
+
+ public String fixSpellingOfRole(String role) {
+ for (String correctSpelledRole : this.roleMap.keySet()) {
+ if (role.equalsIgnoreCase(correctSpelledRole)) {
+ return correctSpelledRole;
+ }
+ }
+
+ return role;
+ }
}
Modified: incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/web/resource/FactsResource.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/web/resource/FactsResource.java?rev=1197564&r1=1197563&r2=1197564&view=diff
==============================================================================
--- incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/web/resource/FactsResource.java (original)
+++ incubator/stanbol/trunk/factstore/src/main/java/org/apache/stanbol/factstore/web/resource/FactsResource.java Fri Nov 4 13:43:50 2011
@@ -152,6 +152,38 @@ public class FactsResource extends BaseF
return Response.status(Status.CREATED).type(MediaType.TEXT_PLAIN).build();
}
+
+ @GET
+ @Path("/{factSchemaURN}/{factId}")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getFact(@PathParam("factId") int factId, @PathParam("factSchemaURN") String factSchemaURN) {
+ Response validationResponse = standardValidation(factSchemaURN);
+ if (validationResponse != null) {
+ return validationResponse;
+ }
+
+ logger.info("Request for getting fact {} of schema {}", factId, factSchemaURN);
+
+ Fact fact = null;
+ try {
+ fact = this.factStore.getFact(factId, factSchemaURN);
+ } catch (Exception e) {
+ logger.error("Error while loading fact {}", factId, e);
+ return Response.status(Status.INTERNAL_SERVER_ERROR).entity(
+ "Error while loading fact " + factId + " of fact schema " + factSchemaURN + " from database")
+ .type(MediaType.TEXT_PLAIN).build();
+ }
+ if (fact == null) {
+ logger.debug("Fact {} for fact schema {} not found", factId, factSchemaURN);
+ return Response.status(Status.NOT_FOUND).entity(
+ "Could not find fact with ID " + factId + " for fact schema " + factSchemaURN).build();
+ }
+ else {
+ JsonLd factAsJsonLd = fact.factToJsonLd();
+ return Response.status(Status.OK).entity(factAsJsonLd.toString())
+ .type(MediaType.APPLICATION_JSON).build();
+ }
+ }
@POST
@Consumes(MediaType.APPLICATION_JSON)
Modified: incubator/stanbol/trunk/factstore/src/test/java/org/apache/stanbol/factstore/FactStoreMock.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/factstore/src/test/java/org/apache/stanbol/factstore/FactStoreMock.java?rev=1197564&r1=1197563&r2=1197564&view=diff
==============================================================================
--- incubator/stanbol/trunk/factstore/src/test/java/org/apache/stanbol/factstore/FactStoreMock.java (original)
+++ incubator/stanbol/trunk/factstore/src/test/java/org/apache/stanbol/factstore/FactStoreMock.java Fri Nov 4 13:43:50 2011
@@ -67,4 +67,10 @@ public class FactStoreMock implements Fa
return null;
}
+ @Override
+ public Fact getFact(int factId, String factSchemaURN) throws Exception {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
}
Added: incubator/stanbol/trunk/factstore/src/test/resources/factschemas/iks_personslikespage-schema.json
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/factstore/src/test/resources/factschemas/iks_personslikespage-schema.json?rev=1197564&view=auto
==============================================================================
--- incubator/stanbol/trunk/factstore/src/test/resources/factschemas/iks_personslikespage-schema.json (added)
+++ incubator/stanbol/trunk/factstore/src/test/resources/factschemas/iks_personslikespage-schema.json Fri Nov 4 13:43:50 2011
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Schema to describe that a person likes a web page.
+ * Schema ID: http://iks-project.eu/ont/like
+ */
+{
+ "@context" : {
+ "schema" : "http://schema.org/",
+ "@types" : {
+ "person" : "schema:Person",
+ "page" : "schema:WebPage"
+ }
+ }
+}
\ No newline at end of file
Added: incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_Person.json
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_Person.json?rev=1197564&view=auto
==============================================================================
--- incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_Person.json (added)
+++ incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_Person.json Fri Nov 4 13:43:50 2011
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Schema to describe a person.
+ * Schema ID: http://schema.org/Person
+ */
+{
+ "@context" : {
+ "schema" : "http://schema.org/",
+ "xsd" : "http://www.w3.org/2001/XMLSchema#",
+ "@types" : {
+ "description" : "xsd:string",
+ "image" : "xsd:anyURI",
+ "name" : "xsd:string",
+ "url" : "xsd:anyURI",
+ "additionalName" : "xsd:string",
+ "address" : "schema:PostalAddress",
+ "affiliation" : "schema:Organization",
+ "alumniOf" : "schema:EducationalOrganization",
+ "awards" : "xsd:string",
+ "birthDate" : "xsd:date",
+ "children" : "schema:Person",
+ "colleagues" : "schema:Person",
+ "contactPoints" : "schema:ContactPoint",
+ "deathDate" : "xsd:date",
+ "email" : "xsd:string",
+ "familyName" : "xsd:string",
+ "faxNumber" : "xsd:string",
+ "follows" : "schema:Person",
+ "gender" : "xsd:string",
+ "givenName" : "xsd:string",
+ "homeLocation" : ["schema:Place", "schema:ContactPoint"],
+ "honorificPrefix" : "xsd:string",
+ "honorificSuffix" : "xsd:string",
+ "interactionCount": "xsd:string",
+ "jobTitle" : "xsd:string",
+ "knows" : "schema:Person",
+ "memberOf" : "schema:Organization",
+ "nationality" : "schema:Country",
+ "parents" : "schema:Person",
+ "performerIn" : "schema:Event",
+ "relatedTo" : "schema:Person",
+ "siblings" : "schema:Person",
+ "spouse" : "schema:Person",
+ "telephone" : "xsd:string",
+ "workLocation" : ["schema:Place", "schema:ContactPoint"],
+ "worksFor" : "schema:Organization"
+ }
+ }
+}
\ No newline at end of file
Added: incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_WebPage.json
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_WebPage.json?rev=1197564&view=auto
==============================================================================
--- incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_WebPage.json (added)
+++ incubator/stanbol/trunk/factstore/src/test/resources/factschemas/schemaorg_WebPage.json Fri Nov 4 13:43:50 2011
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Schema to describe a person.
+ * Schema ID: http://schema.org/WebPage
+ */
+{
+ "@context" : {
+ "schema" : "http://schema.org/",
+ "xsd" : "http://www.w3.org/2001/XMLSchema#",
+ "@types" : {
+ "description" : "xsd:string",
+ "image" : "xsd:anyURI",
+ "name" : "xsd:string",
+ "url" : "xsd:anyURI",
+ "about" : "schema:Thing",
+ "accountablePerson" : "schema:Person",
+ "aggregateRating" : "schema:AggregateRating",
+ "alternativeHeadline" : "xsd:string",
+ "associatedMedia" : "schema:MediaObject",
+ "audio" : "schema:AudioObject",
+ "author" : ["schema:Person", "schema:Organization"],
+ "awards" : "xsd:string",
+ "comment" : "schema:UserComments",
+ "contentLocation" : "schema:Place",
+ "contentRating" : "xsd:string",
+ "contributor" : ["schema:Person", "schema:Organization"],
+ "copyrightHolder" : ["schema:Person", "schema:Organization"],
+ "copyrightYear" : "xsd:nonNegativeInteger",
+ "creator" : ["schema:Person", "schema:Organization"],
+ "dateCreated" : "xsd:dateTime",
+ "dateModified" : "xsd:dateTime",
+ "datePublished" : "xsd:dateTime",
+ "discussionUrl" : "xsd:anyURI",
+ "editor" : "schema:Person",
+ "encodings" : "schema:MediaObject",
+ "genre" : "xsd:string",
+ "headline" : "xsd:string",
+ "inLanguage" : "xsd:string",
+ "interactionCount": "xsd:string",
+ "isFamilyFriendly": "xsd:boolean",
+ "keywords" : "xsd:string",
+ "mentions" : "schema:Thing",
+ "offers" : "schema:Offer",
+ "provider" : ["schema:Person", "schema:Organization"],
+ "publisher" : "schema:Organization",
+ "publishingPrinicples" : "xsd:anyURI",
+ "reviews" : "schema:Review",
+ "sourceOrganization" : "schema:Organization",
+ "thumbnailUrl" : "xsd:anyURI",
+ "version" : "xsd:nonNegativeInteger",
+ "video" : "schema:VideoObject",
+ "breadcrump" : "xsd:string",
+ "isPartOf" : "schema:CollectionPage",
+ "mainContentOfPage" : "xsd:string",
+ "primaryImageOfPage" : "schema:ImageObject",
+ "significantLinks": "xsd:anyURI"
+ }
+ }
+}
\ No newline at end of file
Modified: incubator/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/factstore/FactStoreTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/factstore/FactStoreTest.java?rev=1197564&r1=1197563&r2=1197564&view=diff
==============================================================================
--- incubator/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/factstore/FactStoreTest.java (original)
+++ incubator/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/factstore/FactStoreTest.java Fri Nov 4 13:43:50 2011
@@ -300,7 +300,7 @@ public class FactStoreTest extends Stanb
.withHeader("Content-Type", "application/json")
.withHeader("Accept", "application/json");
- String expected = "{\"PERSON\":\"http://upb.de/persons/bnagel\"}";
+ String expected = "{\"person\":\"http://upb.de/persons/bnagel\"}";
String actual = executor.execute(q).assertStatus(200).getContent();
Assert.assertEquals(expected, actual);
}