diff --git a/server/src/main/java/org/diskproject/server/api/impl/DiskResource.java b/server/src/main/java/org/diskproject/server/api/impl/DiskResource.java index ac0b8df..a063d71 100644 --- a/server/src/main/java/org/diskproject/server/api/impl/DiskResource.java +++ b/server/src/main/java/org/diskproject/server/api/impl/DiskResource.java @@ -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; @@ -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); diff --git a/server/src/main/java/org/diskproject/server/db/DiskDB.java b/server/src/main/java/org/diskproject/server/db/DiskDB.java index 018724a..96a3192 100644 --- a/server/src/main/java/org/diskproject/server/db/DiskDB.java +++ b/server/src/main/java/org/diskproject/server/db/DiskDB.java @@ -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. @@ -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); @@ -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; } @@ -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; } @@ -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)); @@ -460,7 +472,8 @@ public boolean deleteGoal(String id) { public List listGoals () { List list = new ArrayList(); List 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; @@ -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(); @@ -663,7 +675,8 @@ public List listLOIPreviews() { List list = new ArrayList(); List 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; @@ -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(); @@ -1123,7 +1136,8 @@ public boolean deleteTLOI(String id) { public List listTLOIs() { List list = new ArrayList(); List 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("^.*\\/", "")); diff --git a/server/src/main/java/org/diskproject/server/repository/DiskRepository.java b/server/src/main/java/org/diskproject/server/repository/DiskRepository.java index b53b7ed..3d12418 100644 --- a/server/src/main/java/org/diskproject/server/repository/DiskRepository.java +++ b/server/src/main/java/org/diskproject/server/repository/DiskRepository.java @@ -1062,6 +1062,6 @@ public void processWorkflowOutputs (TriggeredLOI tloi, LineOfInquiry loi, Workfl } public Entity getOrCreateEntity(String username) { - return null; + return this.diskDB.loadOrRegisterEntity(username); } } diff --git a/shared/src/main/java/org/diskproject/shared/classes/hypothesis/Goal.java b/shared/src/main/java/org/diskproject/shared/classes/hypothesis/Goal.java index 99b137f..f215f14 100644 --- a/shared/src/main/java/org/diskproject/shared/classes/hypothesis/Goal.java +++ b/shared/src/main/java/org/diskproject/shared/classes/hypothesis/Goal.java @@ -12,7 +12,7 @@ public class Goal extends DISKResource { List questionBindings; Graph graph; - public Goal () {}; //TODO: remove me + public Goal () {}; public Goal (String id) { this.setId(id); diff --git a/shared/src/main/java/org/diskproject/shared/classes/question/Question.java b/shared/src/main/java/org/diskproject/shared/classes/question/Question.java index 87d88bb..eb010b1 100644 --- a/shared/src/main/java/org/diskproject/shared/classes/question/Question.java +++ b/shared/src/main/java/org/diskproject/shared/classes/question/Question.java @@ -11,6 +11,8 @@ public class Question { QuestionCategory category; List variables; + public Question () {}; + public Question (String id) { this.id = id; } diff --git a/shared/src/main/java/org/diskproject/shared/classes/util/KBConstants.java b/shared/src/main/java/org/diskproject/shared/classes/util/KBConstants.java index 189974a..54a5fe5 100644 --- a/shared/src/main/java/org/diskproject/shared/classes/util/KBConstants.java +++ b/shared/src/main/java/org/diskproject/shared/classes/util/KBConstants.java @@ -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";