From ce82c237a32a94f4d94a3f2a390e9fa67b9d5ccb Mon Sep 17 00:00:00 2001 From: Brian Lowe Date: Fri, 27 May 2022 17:55:08 +0300 Subject: [PATCH] address review comments and add additional cleanup --- .../setup/ConfigurationModelsSetup.java | 4 +- .../servlet/setup/ContentModelSetup.java | 4 +- .../webapp/servlet/setup/RDFFilesLoader.java | 117 +++++++++--------- .../servlet/setup/RDFFilesLoaderTest.java | 9 +- 4 files changed, 65 insertions(+), 69 deletions(-) diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/ConfigurationModelsSetup.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/ConfigurationModelsSetup.java index d2bbcc13a..75e660d7d 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/ConfigurationModelsSetup.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/ConfigurationModelsSetup.java @@ -148,7 +148,7 @@ public class ConfigurationModelsSetup implements ServletContextListener { log.debug("Difference for " + modelIdString + " (old -> new), these triples should be removed: " + out); // Check if the UI-changes Overlap with the changes made in the fristtime-files - RDFFilesLoader.checkUiChangesOverlapWithFileChanges(baseModel, userModel, difOldNew); + RDFFilesLoader.removeChangesThatConflictWithUIEdits(baseModel, userModel, difOldNew); // before we remove the triples, we need to compare values in back up firsttime with user's triplestore // if the triples which should be removed are still in user´s triplestore, remove them @@ -166,7 +166,7 @@ public class ConfigurationModelsSetup implements ServletContextListener { log.debug("Difference for " + modelIdString + " (new -> old), these triples should be added: " + out2); // Check if the UI-changes Overlap with the changes made in the fristtime-files - RDFFilesLoader.checkUiChangesOverlapWithFileChanges(baseModel, userModel, difNewOld); + RDFFilesLoader.removeChangesThatConflictWithUIEdits(baseModel, userModel, difNewOld); // before we add the triples, we need to compare values in back up firsttime with user's triplestore // if the triples which should be added are not already in user´s triplestore, add them diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/ContentModelSetup.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/ContentModelSetup.java index 44e1a4b84..c727bdb6e 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/ContentModelSetup.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/ContentModelSetup.java @@ -298,7 +298,7 @@ public class ContentModelSetup extends JenaDataSourceSetupBase log.debug("Difference for " + modelIdString + " (old -> new), these triples should be removed: " + out); // Check if the UI-changes Overlap with the changes made in the fristtime-files - RDFFilesLoader.checkUiChangesOverlapWithFileChanges(baseModel, userModel, difOldNew); + RDFFilesLoader.removeChangesThatConflictWithUIEdits(baseModel, userModel, difOldNew); // before we remove the triples, we need to compare values in back up firsttime with user's triplestore // if the triples which should be removed are still in user´s triplestore, remove them @@ -317,7 +317,7 @@ public class ContentModelSetup extends JenaDataSourceSetupBase log.debug("Difference for " + modelIdString + " (new -> old), these triples should be added: " + out2); // Check if the UI-changes Overlap with the changes made in the fristtime-files - RDFFilesLoader.checkUiChangesOverlapWithFileChanges(baseModel, userModel, difNewOld); + RDFFilesLoader.removeChangesThatConflictWithUIEdits(baseModel, userModel, difNewOld); // before we add the triples, we need to compare values in back up firsttime with user's triplestore // if the triples which should be added are not already in user´s triplestore, add them diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java index 36849f23b..c8535c76f 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java @@ -224,7 +224,7 @@ public class RDFFilesLoader { * @param userModel current state in the system (user/UI-model) * @param changesModel the changes between firsttime-files and firsttime-backup */ - public static void checkUiChangesOverlapWithFileChanges(Model baseModel, + public static void removeChangesThatConflictWithUIEdits(Model baseModel, Model userModel, Model changesModel) { log.debug("Check if subtractions from backup-firsttime model to" + " current state of firsttime-files were changed in user-model" @@ -245,74 +245,71 @@ public class RDFFilesLoader { log.debug("Diff of scoped user model against firsttime backup has " + changesUserModel.size() + " triples"); List changedInUIandFileStatements = new ArrayList(); - if(!changesUserModel.isEmpty()) { - removeBlankTriples(changesUserModel); - if(log.isDebugEnabled()) { - StringWriter out3 = new StringWriter(); - changesUserModel.write(out3, "TTL"); - log.debug("changesUserModel:\n" + out3); - } - log.debug("There were changes in the user-model via UI which have" - + " also changed in the firsttime files. The following" - + " triples will not be updated."); - // Iterate over all statements and check if the ones which should be - // removed were not changed via the UI - StmtIterator iter = changesUserModel.listStatements(); - while (iter.hasNext()) { - Statement stmt = iter.nextStatement(); - Resource subject = stmt.getSubject(); - Property predicate = stmt.getPredicate(); - RDFNode object = stmt.getObject(); - StmtIterator iter2 = changesModel.listStatements( - subject, predicate, (RDFNode) null); - while (iter2.hasNext()) { - Statement stmt2 = iter2.nextStatement(); - RDFNode object2 = stmt2.getObject(); - // If subject and predicate are equal but the object differs - // and the language tag is the same, do not update these triples. - // This case indicates an change in the UI, which should not - // be overwritten from the firsttime files. - if(!object.equals(object2) ) { - // if object is an literal, check the language tag - if (object.isLiteral() && object2.isLiteral()) { - // if the language tag is the same, remove this - // triple from the update list - if(object.asLiteral().getLanguage().equals( - object2.asLiteral().getLanguage())) { - log.debug("This two triples changed UI and" - + " files: \n UI: " + stmt - + " \n file: " +stmt2); - changedInUIandFileStatements.add(stmt2); - } - } else { - log.debug("This two triples changed UI and" - + " files: \n UI: " + stmt - + " \n file: " +stmt2); - changedInUIandFileStatements.add(stmt2); - } - } - } - } - // remove triples which were changed in the user model (UI) from the list - changesModel.remove(changedInUIandFileStatements); - } else { + if(changesUserModel.isEmpty()) { log.debug("There were no changes in the user-model via UI" - + " compared to the backup-firsttime-model"); + + " compared to the backup-firsttime-model"); + return; } + removeBlankTriples(changesUserModel); + if(log.isDebugEnabled()) { + StringWriter out3 = new StringWriter(); + changesUserModel.write(out3, "TTL"); + log.debug("changesUserModel:\n" + out3); + } + log.debug("There were changes in the user-model via UI which have" + + " also changed in the firsttime files. The following" + + " triples will not be updated."); + // Iterate over all statements and check if the ones which should be + // removed were not changed via the UI + StmtIterator userChanges = changesUserModel.listStatements(); + while (userChanges.hasNext()) { + Statement stmt = userChanges.nextStatement(); + Resource subject = stmt.getSubject(); + Property predicate = stmt.getPredicate(); + RDFNode object = stmt.getObject(); + StmtIterator firsttimeChanges = changesModel.listStatements( + subject, predicate, (RDFNode) null); + while (firsttimeChanges.hasNext()) { + Statement stmt2 = firsttimeChanges.nextStatement(); + RDFNode object2 = stmt2.getObject(); + // If subject and predicate are equal but the object differs + // and the language tag is the same, do not update these triples. + // This case indicates an change in the UI, which should not + // be overwritten from the firsttime files. + if(!object.equals(object2) ) { + // if object is an literal, check the language tag + if (object.isLiteral() && object2.isLiteral()) { + // if the language tag is the same, remove this + // triple from the update list + if(object.asLiteral().getLanguage().equals( + object2.asLiteral().getLanguage())) { + log.debug("This two triples changed UI and" + + " files: \n UI: " + stmt + + " \n file: " +stmt2); + changedInUIandFileStatements.add(stmt2); + } + } else { + log.debug("This two triples changed UI and" + + " files: \n UI: " + stmt + + " \n file: " +stmt2); + changedInUIandFileStatements.add(stmt2); + } + } + } + } + // remove triples which were changed in the user model (UI) from the list + changesModel.remove(changedInUIandFileStatements); } /** * Remove all triples where subject or object is blank (Anon) */ public static void removeBlankTriples(Model model) { - StmtIterator iter = model.listStatements(); List removeStatement = new ArrayList(); - while (iter.hasNext()) { - Statement stmt = iter.nextStatement(); // get next statement - Resource subject = stmt.getSubject(); // get the subject - RDFNode object = stmt.getObject(); // get the object - - if(subject.isAnon() || object.isAnon()) + StmtIterator stmts = model.listStatements(); + while (stmts.hasNext()) { + Statement stmt = stmts.nextStatement(); + if(stmt.getSubject().isAnon() || stmt.getObject().isAnon()) { removeStatement.add(stmt); } diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoaderTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoaderTest.java index 8476aaad8..b3a0028b6 100644 --- a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoaderTest.java +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoaderTest.java @@ -61,8 +61,8 @@ public class RDFFilesLoaderTest extends AbstractTestClass { Model additionsModel = fileModel.difference(backupModel); Model retractionsModel = backupModel.difference(fileModel); - RDFFilesLoader.checkUiChangesOverlapWithFileChanges(backupModel, userModel, additionsModel); - RDFFilesLoader.checkUiChangesOverlapWithFileChanges(backupModel, userModel, retractionsModel); + RDFFilesLoader.removeChangesThatConflictWithUIEdits(backupModel, userModel, additionsModel); + RDFFilesLoader.removeChangesThatConflictWithUIEdits(backupModel, userModel, retractionsModel); userModel.remove(retractionsModel); userModel.add(additionsModel); @@ -73,7 +73,6 @@ public class RDFFilesLoaderTest extends AbstractTestClass { // have the same language tag. assertTrue("expected: " + userModelExpected + " but was: " + userModel, userModelExpected.isIsomorphicWith(userModel)); - } @org.junit.Test @@ -118,8 +117,8 @@ public class RDFFilesLoaderTest extends AbstractTestClass { Model additionsModel = fileModel.difference(backupModel); Model retractionsModel = backupModel.difference(fileModel); - RDFFilesLoader.checkUiChangesOverlapWithFileChanges(backupModel, userModel, additionsModel); - RDFFilesLoader.checkUiChangesOverlapWithFileChanges(backupModel, userModel, retractionsModel); + RDFFilesLoader.removeChangesThatConflictWithUIEdits(backupModel, userModel, additionsModel); + RDFFilesLoader.removeChangesThatConflictWithUIEdits(backupModel, userModel, retractionsModel); userModel.remove(retractionsModel); userModel.add(additionsModel);