# HG changeset patch # User Robert Casties # Date 1541704502 -3600 # Node ID 8aff920ec7c00ba0b9dcfacfd558cb5b0e549a7c # Parent 34ac2e1b323aa822bcc051c5176493d3ae47756b fix problem with making entities public when there are loops in the relations. diff -r 34ac2e1b323a -r 8aff920ec7c0 src/main/java/de/mpiwg/itgroup/ismi/browse/EntityDetailsBean.java --- 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 saveList = PrivacityUtils.setRelatedEntitiesPrivacity(this.entity, null, getWrapper(), null); + Map modified = new HashMap(); + modified = PrivacityUtils.setRelatedEntitiesPrivacity(this.entity, null, getWrapper(), null, modified); // hide related entities from display sourceRelations = new HashMap>(); targetRelations = new HashMap>(); // save entities try { - getWrapper().saveEntityListAsNodeWithoutContent(saveList, getUserName()); + getWrapper().saveEntityListAsNodeWithoutContent(new ArrayList(modified.values()), getUserName()); } catch (Exception e) { printInternalError(e); logger.error(e); diff -r 34ac2e1b323a -r 8aff920ec7c0 src/main/java/de/mpiwg/itgroup/ismi/entry/utils/PrivacityUtils.java --- 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 setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper, - Set exceptedTypes) { - List saveList = new ArrayList(); + public static Map setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper, + Set exceptedTypes, Map alreadyModified) { + Map modified = new HashMap(); // 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 setTextAndMorePrivacity(Entity text, Boolean isPublic, List report, WrapperService wrapper) throws Exception { - List saveList = new ArrayList(); + Map modified = new HashMap(); // 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 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 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 nextRelEnts = new ArrayList(); - for (Entity relEnt : relatedEnts) { + Map nextRelEnts = new HashMap(); + for (Entity relEnt : relatedEnts.values()) { String entType = relEnt.getObjectClass(); if (entType.equals("PERSON")) { // PERSON - List 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 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 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 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 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 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 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 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 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 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 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 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(modified.values()); }