changeset 189:8aff920ec7c0

fix problem with making entities public when there are loops in the relations.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Thu, 08 Nov 2018 20:15:02 +0100
parents 34ac2e1b323a
children b36a57a452a6
files src/main/java/de/mpiwg/itgroup/ismi/browse/EntityDetailsBean.java src/main/java/de/mpiwg/itgroup/ismi/entry/utils/PrivacityUtils.java
diffstat 2 files changed, 57 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/ismi/browse/EntityDetailsBean.java	Tue Nov 06 20:05:49 2018 +0100
+++ b/src/main/java/de/mpiwg/itgroup/ismi/browse/EntityDetailsBean.java	Thu Nov 08 20:15:02 2018 +0100
@@ -260,13 +260,14 @@
 	 */
 	public String actionChangeRelatedEntitiesPrivacity() {
 		// set publication state
-		List<Entity> saveList = PrivacityUtils.setRelatedEntitiesPrivacity(this.entity, null, getWrapper(), null);
+		Map<Long, Entity> modified = new HashMap<Long,Entity>();
+		modified = PrivacityUtils.setRelatedEntitiesPrivacity(this.entity, null, getWrapper(), null, modified);
 		// hide related entities from display
 		sourceRelations = new HashMap<String, List<Relation>>();
 		targetRelations = new HashMap<String, List<Relation>>();
 		// save entities
 		try {
-			getWrapper().saveEntityListAsNodeWithoutContent(saveList, getUserName());
+			getWrapper().saveEntityListAsNodeWithoutContent(new ArrayList<Entity>(modified.values()), getUserName());
 		} catch (Exception e) {
 			printInternalError(e);
 			logger.error(e);
--- a/src/main/java/de/mpiwg/itgroup/ismi/entry/utils/PrivacityUtils.java	Tue Nov 06 20:05:49 2018 +0100
+++ b/src/main/java/de/mpiwg/itgroup/ismi/entry/utils/PrivacityUtils.java	Thu Nov 08 20:15:02 2018 +0100
@@ -2,8 +2,10 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.mpi.openmind.cache.WrapperService;
@@ -61,11 +63,12 @@
 	 * @param isPublic
 	 * @param wrapper
 	 * @param exceptedTypes
+	 * @param alreadyModified TODO
 	 * @return
 	 */
-    public static List<Entity> setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper,
-            Set<String> exceptedTypes) {
-        List<Entity> saveList = new ArrayList<Entity>();
+    public static Map<Long, Entity> setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper,
+            Set<String> exceptedTypes, Map<Long, Entity> alreadyModified) {
+        Map<Long,Entity> modified = new HashMap<Long,Entity>();
         // make sure relations are loaded
         if (entity.isLightweight()) {
             entity = wrapper.getEntityContent(entity);
@@ -80,21 +83,27 @@
         for (Relation rel : entity.getSourceRelations()) {
             if (!exceptedTypes.contains(rel.getTargetObjectClass())) {
                 Long entId = rel.getTargetId();
+                if (alreadyModified.containsKey(entId)) {
+                	continue;
+                }
                 Entity ent = wrapper.getEntityById(entId);
                 ent.setIsPublic(isPublic);
-                saveList.add(ent);
+                modified.put(entId, ent);
             }
         }
         // change target relations
         for (Relation rel : entity.getTargetRelations()) {
             if (!exceptedTypes.contains(rel.getSourceObjectClass())) {
                 Long entId = rel.getSourceId();
+                if (alreadyModified.containsKey(entId)) {
+                	continue;
+                }
                 Entity ent = wrapper.getEntityById(entId);
                 ent.setIsPublic(isPublic);
-                saveList.add(ent);
+                modified.put(entId, ent);
             }
         }
-        return saveList;
+        return modified;
     }
 	
 	/**
@@ -112,7 +121,7 @@
 	 */
     public static List<Entity> setTextAndMorePrivacity(Entity text, Boolean isPublic, List<String> report,
             WrapperService wrapper) throws Exception {
-        List<Entity> saveList = new ArrayList<Entity>();
+        Map<Long,Entity> modified = new HashMap<Long,Entity>();
         // make sure relations are loaded
         if (text.isLightweight()) {
             text = wrapper.getEntityContent(text);
@@ -127,71 +136,76 @@
          * mark text public
          */
         text.setIsPublic(isPublic);
-        saveList.add(text);
+        modified.put(text.getId(), text);
         report.add("Set public="+isPublic+" on "+text.getShortString()+"\n");
         
         /*
          * mark directly related objects except TEXT
          */
-        List<Entity> relatedEnts = setRelatedEntitiesPrivacity(text, isPublic, wrapper, TEXTexcepts);
-        saveList.addAll(relatedEnts);
-        report.add("Set public="+isPublic+" on related entities to "+text.getShortString()+" : ["+Entity.getShortStringList(relatedEnts)+"]\n");
+        Map<Long, Entity> relatedEnts = setRelatedEntitiesPrivacity(text, isPublic, wrapper, TEXTexcepts, modified);
+        modified.putAll(relatedEnts);
+        report.add("Set public="+isPublic+" on related entities to "+text.getShortString()+" : ["+Entity.getShortStringList(relatedEnts.values())+"]\n");
        
         /*
          * follow relations of related objects
          */
         int cnt = 0;
         do {
-            List<Entity> nextRelEnts = new ArrayList<Entity>();
-            for (Entity relEnt : relatedEnts) {
+        	Map<Long, Entity> nextRelEnts = new HashMap<Long,Entity>();
+            for (Entity relEnt : relatedEnts.values()) {
                 String entType = relEnt.getObjectClass();
                 if (entType.equals("PERSON")) {
                     // PERSON
-                    List<Entity> persRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, PERSONexcepts);
-                    saveList.addAll(persRelEnts);
-                    nextRelEnts.addAll(persRelEnts);
-                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(persRelEnts)+"]\n");
+                    Map<Long, Entity> persRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, PERSONexcepts, modified);
+                    modified.putAll(persRelEnts);
+                    nextRelEnts.putAll(persRelEnts);
+                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(persRelEnts.values())+"]\n");
                 } else if (entType.equals("WITNESS")) {
                     // WITNESS
-                    List<Entity> witRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, WITNESSexcepts);
-                    saveList.addAll(witRelEnts);
-                    nextRelEnts.addAll(witRelEnts);
-                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(witRelEnts)+"]\n");
+                	Map<Long, Entity> witRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, WITNESSexcepts, modified);
+                    modified.putAll(witRelEnts);
+                    nextRelEnts.putAll(witRelEnts);
+                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(witRelEnts.values())+"]\n");
                 } else if (entType.equals("CODEX")) {
                     // CODEX
-                    List<Entity> codRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, CODEXexcepts);
-                    saveList.addAll(codRelEnts);
-                    nextRelEnts.addAll(codRelEnts);
-                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(codRelEnts)+"]\n");
+                	Map<Long, Entity> codRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, CODEXexcepts, modified);
+                    modified.putAll(codRelEnts);
+                    nextRelEnts.putAll(codRelEnts);
+                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(codRelEnts.values())+"]\n");
                 } else if (entType.equals("COLLECTION")) {
                     // COLLECTION
-                    List<Entity> colRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, COLLECTIONexcepts);
-                    saveList.addAll(colRelEnts);
-                    nextRelEnts.addAll(colRelEnts);
-                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(colRelEnts)+"]\n");
+                	Map<Long, Entity> colRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, COLLECTIONexcepts, modified);
+                    modified.putAll(colRelEnts);
+                    nextRelEnts.putAll(colRelEnts);
+                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(colRelEnts.values())+"]\n");
                 } else if (entType.equals("REPOSITORY")) {
                     // REPOSITORY
-                    List<Entity> repRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, REPOSITORYexcepts);
-                    saveList.addAll(repRelEnts);
-                    nextRelEnts.addAll(repRelEnts);
-                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(repRelEnts)+"]\n");
+                	Map<Long, Entity> repRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, REPOSITORYexcepts, modified);
+                    modified.putAll(repRelEnts);
+                    nextRelEnts.putAll(repRelEnts);
+                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(repRelEnts.values())+"]\n");
                 } else if (entType.endsWith("_EVENT")) {
                     // *_EVENT: mark all related entities
-                    List<Entity> evRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, null);
-                    saveList.addAll(evRelEnts);
-                    nextRelEnts.addAll(evRelEnts);
-                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(evRelEnts)+"]\n");
+                	Map<Long, Entity> evRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, null, modified);
+                    modified.putAll(evRelEnts);
+                    nextRelEnts.putAll(evRelEnts);
+                    report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(evRelEnts.values())+"]\n");
                 } else {
                     // everything else?
                 }
             }
+            if (nextRelEnts.equals(relatedEnts)) {
+            	report.add("WARNING: had to break from loop!\n");
+            	break;
+            }
             // start with next level
             relatedEnts = nextRelEnts;
-        } while (!relatedEnts.isEmpty() && ++cnt < 5);
-        if (cnt == 5) {
+        } while (!relatedEnts.isEmpty() && ++cnt < 10);
+        if (cnt == 10) {
+        	report.add("ERROR: relation depth limit exceeded!");
             throw new Exception("Relation depth limit exceeded when marking text public!");
         }
-        return saveList;
+        return new ArrayList<Entity>(modified.values());
 	}