Skip to content

Commit

Permalink
fix RDF preview
Browse files Browse the repository at this point in the history
  • Loading branch information
armisael committed Nov 25, 2014
1 parent d66b996 commit a6d8367
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 54 deletions.
65 changes: 30 additions & 35 deletions src/org/deri/grefine/rdf/commands/PreviewRdfCommand.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package org.deri.grefine.rdf.commands;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.refine.browsing.Engine;
import com.google.refine.commands.Command;
import com.google.refine.model.Project;
import com.google.refine.model.Row;
import com.google.refine.util.ParsingUtilities;
import org.deri.grefine.rdf.Node;
import org.deri.grefine.rdf.RdfSchema;
import org.deri.grefine.rdf.exporters.RdfExporter;
import org.deri.grefine.rdf.exporters.RdfExporter.RdfRowVisitor;
import org.deri.grefine.rdf.vocab.Vocabulary;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFWriter;
import org.openrdf.rio.Rio;

import com.google.refine.browsing.Engine;
import com.google.refine.commands.Command;
import com.google.refine.model.Project;
import com.google.refine.model.Row;
import com.google.refine.util.ParsingUtilities;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.StringWriter;

public class PreviewRdfCommand extends Command {

Expand All @@ -43,8 +39,10 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
String jsonString = request.getParameter("schema");
JSONObject json = ParsingUtilities.evaluateJsonStringToObject(jsonString);
final RdfSchema schema = RdfSchema.reconstruct(json);

RdfRowVisitor visitor = new RdfRowVisitor(schema) {

StringWriter sw = new StringWriter();
RDFWriter w = Rio.createWriter(RDFFormat.TURTLE, sw);
RdfRowVisitor visitor = new RdfRowVisitor(schema, w) {
final int limit = 10;
int _count;
@Override
Expand All @@ -56,29 +54,26 @@ public boolean visit(Project project, int rowIndex, Row row) {
root.createNode(baseUri, factory, con, project, row, rowIndex,blanks);
}
_count +=1;

try {
flushStatements();
} catch (RepositoryException e) {
e.printStackTrace();
return true;
} catch (RDFHandlerException e) {
e.printStackTrace();
return true;
}

return false;
}
};

Repository model = RdfExporter.buildModel(project, engine, visitor);
StringWriter sw = new StringWriter();
try{
RepositoryConnection con = model.getConnection();
try{
RDFWriter w = Rio.createWriter(RDFFormat.TURTLE, sw);
for(Vocabulary v:schema.getPrefixesMap().values()){
w.handleNamespace(v.getName(), v.getUri());
}
con.export(w);
}finally{
con.close();
}
}catch(RepositoryException ex){
throw new RuntimeException(ex);
}catch(RDFHandlerException ex){
throw new RuntimeException(ex);
}

for(Vocabulary v:schema.getPrefixesMap().values()){
w.handleNamespace(v.getName(), v.getUri());
}
RdfExporter.buildModel(project, engine, visitor);

JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("v");
Expand Down
41 changes: 22 additions & 19 deletions src/org/deri/grefine/rdf/exporters/RdfExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,13 @@ public boolean visit(Project project, int rowIndex, Row row) {
for(Node root:roots){
root.createNode(baseUri, factory, con, project, row, rowIndex,blanks);
}
try {
List<Resource> resourceList = con.getContextIDs().asList();
Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]);

// Export statements
CloseableIteration<? extends Statement, RepositoryException> stIter =
con.getStatements(null, null, null, false, resources);

try {
while (stIter.hasNext()) {
this.writer.handleStatement(stIter.next());
}
} finally {
stIter.close();
}

// empty the repository
con.clear();
try {
flushStatements();
} catch (RepositoryException e) {
e.printStackTrace();
return true;
} catch (RDFHandlerException e) {
e.printStackTrace();
return true;
}

return false;
Expand Down Expand Up @@ -218,6 +201,26 @@ public void start(Project project) {
}
}

protected void flushStatements() throws RepositoryException, RDFHandlerException{
List<Resource> resourceList = con.getContextIDs().asList();
Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]);

// Export statements
CloseableIteration<? extends Statement, RepositoryException> stIter =
con.getStatements(null, null, null, false, resources);

try {
while (stIter.hasNext()) {
this.writer.handleStatement(stIter.next());
}
} finally {
stIter.close();
}

// empty the repository
con.clear();
}

abstract public boolean visit(Project project, int rowIndex, Row row);
public RdfSchema getRdfSchema(){
return schema;
Expand Down

0 comments on commit a6d8367

Please sign in to comment.