Last active
November 11, 2016 13:09
-
-
Save PatrickKwinten/b044da5ba2a6a460a93c0a7e28978be7 to your computer and use it in GitHub Desktop.
Class to migrate IBM Notes documents into a Graph DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wordpress.quintessens.graph.teamroom.controller; | |
import java.io.Serializable; | |
import org.openntf.domino.Database; | |
import org.openntf.domino.Document; | |
import org.openntf.domino.DocumentCollection; | |
import org.openntf.domino.View; | |
import org.openntf.domino.graph2.impl.DFramedTransactionalGraph; | |
import org.openntf.domino.graph2.impl.DGraph; | |
import org.openntf.domino.utils.Factory; | |
import org.openntf.domino.xsp.XspOpenLogUtil; | |
//add our graph data modelling classes | |
import com.wordpress.quintessens.graph.teamroom.GraphHelper; | |
import com.wordpress.quintessens.graph.teamroom.Profile; | |
import com.wordpress.quintessens.graph.teamroom.Post; | |
import com.wordpress.quintessens.graph.teamroom.Response; | |
/** | |
* @author Patrick Kwinten - http://quintessens.wordpress.com | |
* | |
*/ | |
public class MigrationController implements Serializable { | |
private static final long serialVersionUID = 1L; | |
public void reset(){ | |
GraphHelper.clearGraphData(); | |
} | |
public boolean migrateData() { | |
DFramedTransactionalGraph<DGraph> profilesGraph = GraphHelper.getObjectsGraph(); | |
boolean result = false; | |
DocumentCollection profiles = Factory.getSession().getCurrentDatabase().search("SELECT Form=\"ParticipantProfile\""); | |
for (Document profile : profiles) { | |
result = migrateProfile(profile); | |
} | |
if (result == true){ | |
migrateResponses(profilesGraph); | |
} | |
return result; | |
} | |
private boolean migrateProfile(Document doc) { | |
boolean result = false; | |
try { | |
DFramedTransactionalGraph<DGraph> profilesGraph = GraphHelper.getObjectsGraph(); | |
Profile profile = profilesGraph.addVertex(doc.getItemValueString("enterWho"), Profile.class); | |
profile.setName(doc.getItemValueString("enterWho")); | |
profile.setDepartment(doc.getItemValueString("Department")); | |
profile.setLocation(doc.getItemValueString("Location")); | |
profile.setJob(doc.getItemValueString("JobTitle")); | |
profilesGraph.commit(); | |
migrateTopics(doc, profilesGraph, profile); | |
result = true; | |
} catch (Throwable t) { | |
XspOpenLogUtil.logError(t); | |
} | |
return result; | |
} | |
private void migrateTopics(Document doc, DFramedTransactionalGraph<DGraph> profilesGraph, Profile profile) { | |
try { | |
Database db = Factory.getSession().getCurrentDatabase(); | |
View view = db.getView("postsbyAuthor"); | |
String author = doc.getItemValueString("GetAlternateName"); | |
DocumentCollection col = view.getAllDocumentsByKey(author, true); | |
for (Document post : col) { | |
Post vertexPost = profilesGraph.addVertex(post.getUniversalID(), Post.class); | |
vertexPost.setSubject(post.getItemValueString("Subject")); | |
vertexPost.setAbstract(post.getItemValueString("Abstract")); | |
profile.addTopic(vertexPost); | |
} | |
profilesGraph.commit(); | |
} catch (Throwable t) { | |
XspOpenLogUtil.logError(t); | |
} | |
} | |
private boolean migrateResponses(DFramedTransactionalGraph<DGraph> profilesGraph) { | |
boolean result = false; | |
try { | |
Database db = Factory.getSession().getCurrentDatabase(); | |
View view = db.getView("responsesOnly"); | |
DocumentCollection col = view.getAllDocuments(); | |
for (Document response : col) { | |
org.openntf.domino.ext.Document parent = response.getParentDocument(); | |
if (parent.getFormName().equalsIgnoreCase( "MainTopic")){ | |
Post post = profilesGraph.getElement(response.getParentDocumentUNID(), Post.class); | |
Response vertexResponse = profilesGraph.addVertex(response.getUniversalID(), Response.class); | |
vertexResponse.setSubject(response.getItemValueString("Subject")); | |
vertexResponse.setAbstract(response.getItemValueString("Abstract")); | |
vertexResponse.setParent(response.getParentDocumentUNID()); | |
post.addResponse(vertexResponse); | |
} | |
else{ | |
Response prevResponse = profilesGraph.getElement(response.getParentDocumentUNID(), Response.class); | |
Response vertexResponse = profilesGraph.addVertex(response.getUniversalID(), Response.class); | |
vertexResponse.setSubject(response.getItemValueString("Subject")); | |
prevResponse.addResponse(vertexResponse); | |
} | |
profilesGraph.commit(); | |
} | |
result = true; | |
} catch (Throwable t) { | |
XspOpenLogUtil.logError(t); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment