diff src/main/java/de/mpiwg/itgroup/ismi/entry/utils/PrivacityUtils.java @ 175:3d8b31508128

PublicByAuthor feature works now.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Thu, 07 Jun 2018 18:47:18 +0200
parents 29c4b64caad0
children 8aff920ec7c0
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/ismi/entry/utils/PrivacityUtils.java	Wed Jun 06 21:02:30 2018 +0200
+++ b/src/main/java/de/mpiwg/itgroup/ismi/entry/utils/PrivacityUtils.java	Thu Jun 07 18:47:18 2018 +0200
@@ -1,7 +1,10 @@
 package de.mpiwg.itgroup.ismi.entry.utils;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.mpi.openmind.cache.WrapperService;
 import org.mpi.openmind.repository.bo.Entity;
@@ -9,11 +12,22 @@
 
 public class PrivacityUtils {
 
+    public static final Set<String> TEXTexcepts = new HashSet<String>(Arrays.asList("TEXT"));
+    public static final Set<String> PERSONexcepts = new HashSet<String>(Arrays.asList("TEXT", "PERSON", "WITNESS",
+            "CODEX", "TRANSFER_EVENT", "STUDY_EVENT", "COPY_EVENT", "MISATTRIBUTION", "MISIDENTIFICATION"));
+    public static final Set<String> WITNESSexcepts = new HashSet<String>(Arrays.asList("WITNESS", "TEXT"));
+    public static final Set<String> CODEXexcepts = new HashSet<String>(Arrays.asList("WITNESS"));
+    public static final Set<String> COLLECTIONexcepts = new HashSet<String>(Arrays.asList("CODEX"));
+    public static final Set<String> REPOSITORYexcepts = new HashSet<String>(Arrays.asList("COLLECTION", "STUDY_EVENT", "COPY_EVENT"));
+
+    
 	/**
 	 * Change public state of the given entity.
 	 * 
 	 * Toggles public state if isPublic == null.
 	 * 
+	 * Returns the changed Entity (that needs to be saved).
+	 * 
 	 * @param entity
 	 * @param isPublic
 	 * @param wrapper
@@ -34,44 +48,154 @@
 	}
 	
 	/**
-	 * Change public state of all entities related to the given entity.
+	 * Change public state of all entities directly related to the given entity 
+	 * (does not change the given entity).
 	 * 
 	 * Sets public state to given entity's state if isPublic == null.
 	 * 
+	 * Does not touch Entities of the types listed in exceptedTypes.
+	 * 
+	 * Returns a List of Entities that have been changed (and need to be saved).
+	 * 
 	 * @param entity
 	 * @param isPublic
 	 * @param wrapper
+	 * @param exceptedTypes
 	 * @return
 	 */
-	public static List<Entity> setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper) {
-		// make sure relations are loaded
-		if (entity.isLightweight()) {
-			entity = wrapper.getEntityContent(entity);
-		}
+    public static List<Entity> setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper,
+            Set<String> exceptedTypes) {
+        List<Entity> saveList = new ArrayList<Entity>();
+        // make sure relations are loaded
+        if (entity.isLightweight()) {
+            entity = wrapper.getEntityContent(entity);
+        }
+
+        // use entity's public if isPublic == null
+        if (isPublic == null) {
+            isPublic = entity.getIsPublic();
+        }
 
-		// use entity's public if isPublic == null
-		if (isPublic == null) {
-			isPublic = entity.getIsPublic();
-		}
-		List<Entity> saveList = new ArrayList<Entity>();
+        // change source relations
+        for (Relation rel : entity.getSourceRelations()) {
+            if (!exceptedTypes.contains(rel.getTargetObjectClass())) {
+                Long entId = rel.getTargetId();
+                Entity ent = wrapper.getEntityById(entId);
+                ent.setIsPublic(isPublic);
+                saveList.add(ent);
+            }
+        }
+        // change target relations
+        for (Relation rel : entity.getTargetRelations()) {
+            if (!exceptedTypes.contains(rel.getSourceObjectClass())) {
+                Long entId = rel.getSourceId();
+                Entity ent = wrapper.getEntityById(entId);
+                ent.setIsPublic(isPublic);
+                saveList.add(ent);
+            }
+        }
+        return saveList;
+    }
+	
+	/**
+	 * Change public state of a TEXT and all meaningfully related Entities.
+	 * 
+     * Sets public state to given entity's state if isPublic == null.
+     * 
+     * Returns a List of Entities that have been changed (and need to be saved).
+     * 
+	 * @param text
+	 * @param isPublic
+	 * @param wrapper
+	 * @return
+	 * @throws Exception 
+	 */
+    public static List<Entity> setTextAndMorePrivacity(Entity text, Boolean isPublic, List<String> report,
+            WrapperService wrapper) throws Exception {
+        List<Entity> saveList = new ArrayList<Entity>();
+        // make sure relations are loaded
+        if (text.isLightweight()) {
+            text = wrapper.getEntityContent(text);
+        }
+
+        // use entity's public if isPublic == null
+        if (isPublic == null) {
+            isPublic = text.getIsPublic();
+        }
 
-		// change source relations
-		for (Relation rel : entity.getSourceRelations()) {
-			Long entId = rel.getTargetId();
-			Entity ent = wrapper.getEntityById(entId);
-			ent.setIsPublic(isPublic);
-			saveList.add(ent);
-		}
-		// change target relations
-		for (Relation rel : entity.getTargetRelations()) {
-			Long entId = rel.getSourceId();
-			Entity ent = wrapper.getEntityById(entId);
-			ent.setIsPublic(isPublic);
-			saveList.add(ent);
-		}
-		return saveList;
+        /*
+         * mark text public
+         */
+        text.setIsPublic(isPublic);
+        saveList.add(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");
+       
+        /*
+         * follow relations of related objects
+         */
+        int cnt = 0;
+        do {
+            List<Entity> nextRelEnts = new ArrayList<Entity>();
+            for (Entity relEnt : relatedEnts) {
+                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");
+                } 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");
+                } 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");
+                } 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");
+                } 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");
+                } 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");
+                } else {
+                    // everything else?
+                }
+            }
+            // start with next level
+            relatedEnts = nextRelEnts;
+        } while (!relatedEnts.isEmpty() && ++cnt < 5);
+        if (cnt == 5) {
+            throw new Exception("Relation depth limit exceeded when marking text public!");
+        }
+        return saveList;
 	}
 	
+	
+	
 	public static List<Entity> changePrivacity4Person(Entity person, Boolean isPublic, WrapperService wrapper){
 		List<Entity> saveList = new ArrayList<Entity>();