comparison src/main/java/de/mpiwg/itgroup/ismi/entry/utils/PrivacityUtils.java @ 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 3d8b31508128
children c7fec83ab69a
comparison
equal deleted inserted replaced
188:34ac2e1b323a 189:8aff920ec7c0
1 package de.mpiwg.itgroup.ismi.entry.utils; 1 package de.mpiwg.itgroup.ismi.entry.utils;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.Arrays; 4 import java.util.Arrays;
5 import java.util.HashMap;
5 import java.util.HashSet; 6 import java.util.HashSet;
6 import java.util.List; 7 import java.util.List;
8 import java.util.Map;
7 import java.util.Set; 9 import java.util.Set;
8 10
9 import org.mpi.openmind.cache.WrapperService; 11 import org.mpi.openmind.cache.WrapperService;
10 import org.mpi.openmind.repository.bo.Entity; 12 import org.mpi.openmind.repository.bo.Entity;
11 import org.mpi.openmind.repository.bo.Relation; 13 import org.mpi.openmind.repository.bo.Relation;
59 * 61 *
60 * @param entity 62 * @param entity
61 * @param isPublic 63 * @param isPublic
62 * @param wrapper 64 * @param wrapper
63 * @param exceptedTypes 65 * @param exceptedTypes
66 * @param alreadyModified TODO
64 * @return 67 * @return
65 */ 68 */
66 public static List<Entity> setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper, 69 public static Map<Long, Entity> setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper,
67 Set<String> exceptedTypes) { 70 Set<String> exceptedTypes, Map<Long, Entity> alreadyModified) {
68 List<Entity> saveList = new ArrayList<Entity>(); 71 Map<Long,Entity> modified = new HashMap<Long,Entity>();
69 // make sure relations are loaded 72 // make sure relations are loaded
70 if (entity.isLightweight()) { 73 if (entity.isLightweight()) {
71 entity = wrapper.getEntityContent(entity); 74 entity = wrapper.getEntityContent(entity);
72 } 75 }
73 76
78 81
79 // change source relations 82 // change source relations
80 for (Relation rel : entity.getSourceRelations()) { 83 for (Relation rel : entity.getSourceRelations()) {
81 if (!exceptedTypes.contains(rel.getTargetObjectClass())) { 84 if (!exceptedTypes.contains(rel.getTargetObjectClass())) {
82 Long entId = rel.getTargetId(); 85 Long entId = rel.getTargetId();
86 if (alreadyModified.containsKey(entId)) {
87 continue;
88 }
83 Entity ent = wrapper.getEntityById(entId); 89 Entity ent = wrapper.getEntityById(entId);
84 ent.setIsPublic(isPublic); 90 ent.setIsPublic(isPublic);
85 saveList.add(ent); 91 modified.put(entId, ent);
86 } 92 }
87 } 93 }
88 // change target relations 94 // change target relations
89 for (Relation rel : entity.getTargetRelations()) { 95 for (Relation rel : entity.getTargetRelations()) {
90 if (!exceptedTypes.contains(rel.getSourceObjectClass())) { 96 if (!exceptedTypes.contains(rel.getSourceObjectClass())) {
91 Long entId = rel.getSourceId(); 97 Long entId = rel.getSourceId();
98 if (alreadyModified.containsKey(entId)) {
99 continue;
100 }
92 Entity ent = wrapper.getEntityById(entId); 101 Entity ent = wrapper.getEntityById(entId);
93 ent.setIsPublic(isPublic); 102 ent.setIsPublic(isPublic);
94 saveList.add(ent); 103 modified.put(entId, ent);
95 } 104 }
96 } 105 }
97 return saveList; 106 return modified;
98 } 107 }
99 108
100 /** 109 /**
101 * Change public state of a TEXT and all meaningfully related Entities. 110 * Change public state of a TEXT and all meaningfully related Entities.
102 * 111 *
110 * @return 119 * @return
111 * @throws Exception 120 * @throws Exception
112 */ 121 */
113 public static List<Entity> setTextAndMorePrivacity(Entity text, Boolean isPublic, List<String> report, 122 public static List<Entity> setTextAndMorePrivacity(Entity text, Boolean isPublic, List<String> report,
114 WrapperService wrapper) throws Exception { 123 WrapperService wrapper) throws Exception {
115 List<Entity> saveList = new ArrayList<Entity>(); 124 Map<Long,Entity> modified = new HashMap<Long,Entity>();
116 // make sure relations are loaded 125 // make sure relations are loaded
117 if (text.isLightweight()) { 126 if (text.isLightweight()) {
118 text = wrapper.getEntityContent(text); 127 text = wrapper.getEntityContent(text);
119 } 128 }
120 129
125 134
126 /* 135 /*
127 * mark text public 136 * mark text public
128 */ 137 */
129 text.setIsPublic(isPublic); 138 text.setIsPublic(isPublic);
130 saveList.add(text); 139 modified.put(text.getId(), text);
131 report.add("Set public="+isPublic+" on "+text.getShortString()+"\n"); 140 report.add("Set public="+isPublic+" on "+text.getShortString()+"\n");
132 141
133 /* 142 /*
134 * mark directly related objects except TEXT 143 * mark directly related objects except TEXT
135 */ 144 */
136 List<Entity> relatedEnts = setRelatedEntitiesPrivacity(text, isPublic, wrapper, TEXTexcepts); 145 Map<Long, Entity> relatedEnts = setRelatedEntitiesPrivacity(text, isPublic, wrapper, TEXTexcepts, modified);
137 saveList.addAll(relatedEnts); 146 modified.putAll(relatedEnts);
138 report.add("Set public="+isPublic+" on related entities to "+text.getShortString()+" : ["+Entity.getShortStringList(relatedEnts)+"]\n"); 147 report.add("Set public="+isPublic+" on related entities to "+text.getShortString()+" : ["+Entity.getShortStringList(relatedEnts.values())+"]\n");
139 148
140 /* 149 /*
141 * follow relations of related objects 150 * follow relations of related objects
142 */ 151 */
143 int cnt = 0; 152 int cnt = 0;
144 do { 153 do {
145 List<Entity> nextRelEnts = new ArrayList<Entity>(); 154 Map<Long, Entity> nextRelEnts = new HashMap<Long,Entity>();
146 for (Entity relEnt : relatedEnts) { 155 for (Entity relEnt : relatedEnts.values()) {
147 String entType = relEnt.getObjectClass(); 156 String entType = relEnt.getObjectClass();
148 if (entType.equals("PERSON")) { 157 if (entType.equals("PERSON")) {
149 // PERSON 158 // PERSON
150 List<Entity> persRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, PERSONexcepts); 159 Map<Long, Entity> persRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, PERSONexcepts, modified);
151 saveList.addAll(persRelEnts); 160 modified.putAll(persRelEnts);
152 nextRelEnts.addAll(persRelEnts); 161 nextRelEnts.putAll(persRelEnts);
153 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(persRelEnts)+"]\n"); 162 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(persRelEnts.values())+"]\n");
154 } else if (entType.equals("WITNESS")) { 163 } else if (entType.equals("WITNESS")) {
155 // WITNESS 164 // WITNESS
156 List<Entity> witRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, WITNESSexcepts); 165 Map<Long, Entity> witRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, WITNESSexcepts, modified);
157 saveList.addAll(witRelEnts); 166 modified.putAll(witRelEnts);
158 nextRelEnts.addAll(witRelEnts); 167 nextRelEnts.putAll(witRelEnts);
159 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(witRelEnts)+"]\n"); 168 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(witRelEnts.values())+"]\n");
160 } else if (entType.equals("CODEX")) { 169 } else if (entType.equals("CODEX")) {
161 // CODEX 170 // CODEX
162 List<Entity> codRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, CODEXexcepts); 171 Map<Long, Entity> codRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, CODEXexcepts, modified);
163 saveList.addAll(codRelEnts); 172 modified.putAll(codRelEnts);
164 nextRelEnts.addAll(codRelEnts); 173 nextRelEnts.putAll(codRelEnts);
165 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(codRelEnts)+"]\n"); 174 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(codRelEnts.values())+"]\n");
166 } else if (entType.equals("COLLECTION")) { 175 } else if (entType.equals("COLLECTION")) {
167 // COLLECTION 176 // COLLECTION
168 List<Entity> colRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, COLLECTIONexcepts); 177 Map<Long, Entity> colRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, COLLECTIONexcepts, modified);
169 saveList.addAll(colRelEnts); 178 modified.putAll(colRelEnts);
170 nextRelEnts.addAll(colRelEnts); 179 nextRelEnts.putAll(colRelEnts);
171 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(colRelEnts)+"]\n"); 180 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(colRelEnts.values())+"]\n");
172 } else if (entType.equals("REPOSITORY")) { 181 } else if (entType.equals("REPOSITORY")) {
173 // REPOSITORY 182 // REPOSITORY
174 List<Entity> repRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, REPOSITORYexcepts); 183 Map<Long, Entity> repRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, REPOSITORYexcepts, modified);
175 saveList.addAll(repRelEnts); 184 modified.putAll(repRelEnts);
176 nextRelEnts.addAll(repRelEnts); 185 nextRelEnts.putAll(repRelEnts);
177 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(repRelEnts)+"]\n"); 186 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(repRelEnts.values())+"]\n");
178 } else if (entType.endsWith("_EVENT")) { 187 } else if (entType.endsWith("_EVENT")) {
179 // *_EVENT: mark all related entities 188 // *_EVENT: mark all related entities
180 List<Entity> evRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, null); 189 Map<Long, Entity> evRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, null, modified);
181 saveList.addAll(evRelEnts); 190 modified.putAll(evRelEnts);
182 nextRelEnts.addAll(evRelEnts); 191 nextRelEnts.putAll(evRelEnts);
183 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(evRelEnts)+"]\n"); 192 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(evRelEnts.values())+"]\n");
184 } else { 193 } else {
185 // everything else? 194 // everything else?
186 } 195 }
187 } 196 }
197 if (nextRelEnts.equals(relatedEnts)) {
198 report.add("WARNING: had to break from loop!\n");
199 break;
200 }
188 // start with next level 201 // start with next level
189 relatedEnts = nextRelEnts; 202 relatedEnts = nextRelEnts;
190 } while (!relatedEnts.isEmpty() && ++cnt < 5); 203 } while (!relatedEnts.isEmpty() && ++cnt < 10);
191 if (cnt == 5) { 204 if (cnt == 10) {
205 report.add("ERROR: relation depth limit exceeded!");
192 throw new Exception("Relation depth limit exceeded when marking text public!"); 206 throw new Exception("Relation depth limit exceeded when marking text public!");
193 } 207 }
194 return saveList; 208 return new ArrayList<Entity>(modified.values());
195 } 209 }
196 210
197 211
198 212
199 public static List<Entity> changePrivacity4Person(Entity person, Boolean isPublic, WrapperService wrapper){ 213 public static List<Entity> changePrivacity4Person(Entity person, Boolean isPublic, WrapperService wrapper){