Skip to content

Commit

Permalink
Creating author entities based on keycloak data
Browse files Browse the repository at this point in the history
  • Loading branch information
hvarg committed Feb 10, 2024
1 parent 89535d3 commit 156a267
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.diskproject.server.api.impl;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -106,7 +107,13 @@ public String reloadVocabularies() {

//--
private void addAuthorFromRequest (DISKResource obj, HttpServletRequest request) {
String username = (String) request.getAttribute("username");
//Enumeration attr = request.getAttributeNames();
//while (attr.hasMoreElements()) {
// String value = attr.nextElement().toString();
// System.out.println(value + " = " + request.getAttribute(value));
//}

String username = (String) request.getAttribute("username"); //username is an email.
if (username != null) {
Entity author = this.repo.getOrCreateEntity(username);
obj.setAuthor(author);
Expand Down
68 changes: 41 additions & 27 deletions server/src/main/java/org/diskproject/server/db/DiskDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private void loadKB () {
this.diskKB = this.rdf.getFactory().getKB(KBConstants.DISK_URI, OntSpec.PELLET, false, true);
} catch (Exception e) {
System.out.println("Error reading KB: " + KBConstants.DISK_URI);
e.printStackTrace();
return;
}
//This maps the terms of the ontology to use for creating new DISK resources.
Expand Down Expand Up @@ -249,29 +250,39 @@ private Endpoint loadEndpoint (KBObject endpoint) {
return new Endpoint(name.getValueAsString(), url.getValueAsString());
}

public KBObject findOrWriteEntity (Entity src) {
// All entities should be unique
int id = src.getEmail().hashCode();
KBObject KBEntity = domainKB.getIndividual(createEntityURI(id));
//Check if this entity exists
Entity entity = loadEntity(domainKB, KBEntity);
if (entity != null) {
System.out.println("Entity " + src.getEmail() + " already exists.");
} else {
domainKB.setPropertyValue(KBEntity, DISKOnt.getProperty(DISK.HAS_NAME), domainKB.createLiteral(src.getName()));
domainKB.setPropertyValue(KBEntity, DISKOnt.getProperty(DISK.HAS_EMAIL), domainKB.createLiteral(src.getEmail()));
}
private KBObject writeEntity (Entity src) {
// This assumes the entity does not exists
KBObject KBEntity = domainKB.createObjectOfClass(src.getId(), DISKOnt.getClass(DISK.ENTITY));
domainKB.setPropertyValue(KBEntity, DISKOnt.getProperty(DISK.HAS_NAME), domainKB.createLiteral(src.getName()));
domainKB.setPropertyValue(KBEntity, DISKOnt.getProperty(DISK.HAS_EMAIL), domainKB.createLiteral(src.getEmail()));
return KBEntity;
}

private Entity loadEntity (KBAPI kb, KBObject author) {
KBObject name = kb.getPropertyValue(author, DISKOnt.getProperty(DISK.HAS_NAME));
KBObject email = kb.getPropertyValue(author, DISKOnt.getProperty(DISK.HAS_EMAIL));
private Entity loadEntity (KBObject author) {
KBObject name = domainKB.getPropertyValue(author, DISKOnt.getProperty(DISK.HAS_NAME));
KBObject email = domainKB.getPropertyValue(author, DISKOnt.getProperty(DISK.HAS_EMAIL));
if (name == null || email == null)
return null;
return new Entity(author.getID(), name.getValueAsString(), email.getValueAsString());
}

public Entity loadOrRegisterEntity (String email) {
int id = email.hashCode();
this.rdf.startRead();
KBObject KBEntity = domainKB.getIndividual(createEntityURI(id));
Entity entity = loadEntity(KBEntity);
this.rdf.end();
if (entity == null) {
String name = email.split("@")[0];
entity = new Entity(String.valueOf(id), name, email);
this.rdf.startWrite();
this.writeEntity(entity);
this.rdf.save(domainKB);
this.rdf.end();
}
return entity;
}

private KBObject writeCommonResource (DISKResource obj, String uri, KBObject cls) {
//This writes name, description, notes, date created and date modified. The object is of class `cls`
KBObject kbObj = domainKB.createObjectOfClass(uri, cls);
Expand All @@ -289,8 +300,9 @@ private KBObject writeCommonResource (DISKResource obj, String uri, KBObject cls
domainKB.setPropertyValue(kbObj, DISKOnt.getProperty(DISK.HAS_USAGE_NOTES),
domainKB.createLiteral(obj.getNotes()));
if (obj.getAuthor() != null) {
// At this point entity already exists
domainKB.setPropertyValue(kbObj, DISKOnt.getProperty(DISK.HAS_AUTHOR),
findOrWriteEntity(obj.getAuthor()));
domainKB.getIndividual(obj.getAuthor().getId()));
}
return kbObj;
}
Expand All @@ -304,7 +316,7 @@ private DISKResource loadCommonResource (KBObject item) {
if (created != null) current.setDateCreated(created.getValueAsString());
if (updated != null) current.setDateModified(updated.getValueAsString());
if (notes != null) current.setNotes(notes.getValueAsString());
if (author != null) current.setAuthor(loadEntity(domainKB, author));
if (author != null) current.setAuthor(loadEntity(author));
return current;
}

Expand Down Expand Up @@ -346,8 +358,8 @@ public Goal AddOrUpdateGoal (Goal goal, String id) {

public boolean writeGoal(Goal goal) {
Boolean newGoal = goal.getId() == null || goal.getId().equals("");
if (newGoal) goal.setId(GUID.randomId("Goal"));
String fullId = createGoalURI(goal.getId());
if (newGoal) goal.setId(createGoalURI(GUID.randomId("Goal")));
String fullId = goal.getId();
//if (domainKB == null) return false;
this.rdf.startWrite();
KBObject goalItem = this.writeCommonResource(goal, fullId, DISKOnt.getClass(DISK.GOAL));
Expand Down Expand Up @@ -460,7 +472,8 @@ public boolean deleteGoal(String id) {
public List<Goal> listGoals () {
List<Goal> list = new ArrayList<Goal>();
List<String> goalIds = listObjectIdPerClass(DISKOnt.getClass(DISK.GOAL));
for (String id: goalIds) {
for (String fullId: goalIds) {
String id = fullId.replaceAll("^.*\\/", "");
list.add(this.loadGoal(id));
}
return list;
Expand Down Expand Up @@ -549,9 +562,8 @@ public LineOfInquiry AddOrUpdateLOI (LineOfInquiry loi, String id) {

public boolean writeLOI(LineOfInquiry loi) {
Boolean newLOI = loi.getId() == null || loi.getId().equals("");
if (newLOI) loi.setId(GUID.randomId("LOI"));

String loiId = createLoiURI(loi.getId());
if (newLOI) loi.setId(createLoiURI(GUID.randomId("LOI")));
String loiId = loi.getId();
//if (domainKB == null) return false;
this.rdf.startWrite();

Expand Down Expand Up @@ -663,7 +675,8 @@ public List<LineOfInquiry> listLOIPreviews() {
List<LineOfInquiry> list = new ArrayList<LineOfInquiry>();
List<String> ids = listObjectIdPerClass(DISKOnt.getClass(DISK.LINE_OF_INQUIRY));

for (String id: ids) {
for (String fullId: ids) {
String id = fullId.replaceAll("^.*\\/", "");
list.add(this.loadLOI(id));
}
return list;
Expand Down Expand Up @@ -1008,8 +1021,8 @@ public TriggeredLOI addOrUpdateTLOI (TriggeredLOI tloi, String id) {

public boolean writeTLOI(TriggeredLOI tloi) {
Boolean newTLOI = tloi.getId() == null || tloi.getId().equals("");
if (newTLOI) tloi.setId(GUID.randomId("TriggeredLOI"));
String tloiId = createTloiURI(tloi.getId());
if (newTLOI) tloi.setId(createTloiURI(GUID.randomId("TriggeredLOI")));
String tloiId = tloi.getId();
//if (domainKB == null) return false;

this.rdf.startWrite();
Expand Down Expand Up @@ -1123,7 +1136,8 @@ public boolean deleteTLOI(String id) {
public List<TriggeredLOI> listTLOIs() {
List<TriggeredLOI> list = new ArrayList<TriggeredLOI>();
List<String> ids = listObjectIdPerClass(DISKOnt.getClass(DISK.TRIGGERED_LINE_OF_INQUIRY));
for (String id: ids) {
for (String fullId: ids) {
String id = fullId.replaceAll("^.*\\/", "");
list.add(this.loadTLOI(id));
}
//TriggeredLOI tloi = loadTLOI(username, tloiId.replaceAll("^.*\\/", ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,6 @@ public void processWorkflowOutputs (TriggeredLOI tloi, LineOfInquiry loi, Workfl
}

public Entity getOrCreateEntity(String username) {
return null;
return this.diskDB.loadOrRegisterEntity(username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Goal extends DISKResource {
List<VariableBinding> questionBindings;
Graph graph;

public Goal () {}; //TODO: remove me
public Goal () {};

public Goal (String id) {
this.setId(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class Question {
QuestionCategory category;
List<QuestionVariable> variables;

public Question () {};

public Question (String id) {
this.id = id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

public class KBConstants {
// Internal URI
public static final String DISK_URI = "https://knowledgecaptureanddiscovery.github.io/DISK-Ontologies/disk/release/1.2.5/ontology.ttl";
//public static final String DISK_URI = "https://knowledgecaptureanddiscovery.github.io/DISK-Ontologies/disk/release/1.2.5/ontology.ttl";
public static final String DISK_URI = "https://raw.githubusercontent.com/KnowledgeCaptureAndDiscovery/DISK-Ontologies/1.3/disk/release/1.3.0/ontology.xml";
public static final String HYP_URI = "https://knowledgecaptureanddiscovery.github.io/DISK-Ontologies/hypothesis/release/0.0.3/ontology.xml";
public static final String QUESTION_URI = "https://knowledgecaptureanddiscovery.github.io/QuestionOntology/release/v1.3.1/ontology.ttl";

Expand Down

0 comments on commit 156a267

Please sign in to comment.