Mercurial > hg > ismi-richfaces
comparison 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 |
comparison
equal
deleted
inserted
replaced
174:4961820373d0 | 175:3d8b31508128 |
---|---|
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; | |
5 import java.util.HashSet; | |
4 import java.util.List; | 6 import java.util.List; |
7 import java.util.Set; | |
5 | 8 |
6 import org.mpi.openmind.cache.WrapperService; | 9 import org.mpi.openmind.cache.WrapperService; |
7 import org.mpi.openmind.repository.bo.Entity; | 10 import org.mpi.openmind.repository.bo.Entity; |
8 import org.mpi.openmind.repository.bo.Relation; | 11 import org.mpi.openmind.repository.bo.Relation; |
9 | 12 |
10 public class PrivacityUtils { | 13 public class PrivacityUtils { |
11 | 14 |
15 public static final Set<String> TEXTexcepts = new HashSet<String>(Arrays.asList("TEXT")); | |
16 public static final Set<String> PERSONexcepts = new HashSet<String>(Arrays.asList("TEXT", "PERSON", "WITNESS", | |
17 "CODEX", "TRANSFER_EVENT", "STUDY_EVENT", "COPY_EVENT", "MISATTRIBUTION", "MISIDENTIFICATION")); | |
18 public static final Set<String> WITNESSexcepts = new HashSet<String>(Arrays.asList("WITNESS", "TEXT")); | |
19 public static final Set<String> CODEXexcepts = new HashSet<String>(Arrays.asList("WITNESS")); | |
20 public static final Set<String> COLLECTIONexcepts = new HashSet<String>(Arrays.asList("CODEX")); | |
21 public static final Set<String> REPOSITORYexcepts = new HashSet<String>(Arrays.asList("COLLECTION", "STUDY_EVENT", "COPY_EVENT")); | |
22 | |
23 | |
12 /** | 24 /** |
13 * Change public state of the given entity. | 25 * Change public state of the given entity. |
14 * | 26 * |
15 * Toggles public state if isPublic == null. | 27 * Toggles public state if isPublic == null. |
28 * | |
29 * Returns the changed Entity (that needs to be saved). | |
16 * | 30 * |
17 * @param entity | 31 * @param entity |
18 * @param isPublic | 32 * @param isPublic |
19 * @param wrapper | 33 * @param wrapper |
20 * @return | 34 * @return |
32 entity.setIsPublic(isPublic); | 46 entity.setIsPublic(isPublic); |
33 return entity; | 47 return entity; |
34 } | 48 } |
35 | 49 |
36 /** | 50 /** |
37 * Change public state of all entities related to the given entity. | 51 * Change public state of all entities directly related to the given entity |
52 * (does not change the given entity). | |
38 * | 53 * |
39 * Sets public state to given entity's state if isPublic == null. | 54 * Sets public state to given entity's state if isPublic == null. |
55 * | |
56 * Does not touch Entities of the types listed in exceptedTypes. | |
57 * | |
58 * Returns a List of Entities that have been changed (and need to be saved). | |
40 * | 59 * |
41 * @param entity | 60 * @param entity |
42 * @param isPublic | 61 * @param isPublic |
43 * @param wrapper | 62 * @param wrapper |
63 * @param exceptedTypes | |
44 * @return | 64 * @return |
45 */ | 65 */ |
46 public static List<Entity> setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper) { | 66 public static List<Entity> setRelatedEntitiesPrivacity(Entity entity, Boolean isPublic, WrapperService wrapper, |
47 // make sure relations are loaded | 67 Set<String> exceptedTypes) { |
48 if (entity.isLightweight()) { | 68 List<Entity> saveList = new ArrayList<Entity>(); |
49 entity = wrapper.getEntityContent(entity); | 69 // make sure relations are loaded |
50 } | 70 if (entity.isLightweight()) { |
51 | 71 entity = wrapper.getEntityContent(entity); |
52 // use entity's public if isPublic == null | 72 } |
53 if (isPublic == null) { | 73 |
54 isPublic = entity.getIsPublic(); | 74 // use entity's public if isPublic == null |
55 } | 75 if (isPublic == null) { |
56 List<Entity> saveList = new ArrayList<Entity>(); | 76 isPublic = entity.getIsPublic(); |
57 | 77 } |
58 // change source relations | 78 |
59 for (Relation rel : entity.getSourceRelations()) { | 79 // change source relations |
60 Long entId = rel.getTargetId(); | 80 for (Relation rel : entity.getSourceRelations()) { |
61 Entity ent = wrapper.getEntityById(entId); | 81 if (!exceptedTypes.contains(rel.getTargetObjectClass())) { |
62 ent.setIsPublic(isPublic); | 82 Long entId = rel.getTargetId(); |
63 saveList.add(ent); | 83 Entity ent = wrapper.getEntityById(entId); |
64 } | 84 ent.setIsPublic(isPublic); |
65 // change target relations | 85 saveList.add(ent); |
66 for (Relation rel : entity.getTargetRelations()) { | 86 } |
67 Long entId = rel.getSourceId(); | 87 } |
68 Entity ent = wrapper.getEntityById(entId); | 88 // change target relations |
69 ent.setIsPublic(isPublic); | 89 for (Relation rel : entity.getTargetRelations()) { |
70 saveList.add(ent); | 90 if (!exceptedTypes.contains(rel.getSourceObjectClass())) { |
71 } | 91 Long entId = rel.getSourceId(); |
72 return saveList; | 92 Entity ent = wrapper.getEntityById(entId); |
73 } | 93 ent.setIsPublic(isPublic); |
94 saveList.add(ent); | |
95 } | |
96 } | |
97 return saveList; | |
98 } | |
99 | |
100 /** | |
101 * Change public state of a TEXT and all meaningfully related Entities. | |
102 * | |
103 * Sets public state to given entity's state if isPublic == null. | |
104 * | |
105 * Returns a List of Entities that have been changed (and need to be saved). | |
106 * | |
107 * @param text | |
108 * @param isPublic | |
109 * @param wrapper | |
110 * @return | |
111 * @throws Exception | |
112 */ | |
113 public static List<Entity> setTextAndMorePrivacity(Entity text, Boolean isPublic, List<String> report, | |
114 WrapperService wrapper) throws Exception { | |
115 List<Entity> saveList = new ArrayList<Entity>(); | |
116 // make sure relations are loaded | |
117 if (text.isLightweight()) { | |
118 text = wrapper.getEntityContent(text); | |
119 } | |
120 | |
121 // use entity's public if isPublic == null | |
122 if (isPublic == null) { | |
123 isPublic = text.getIsPublic(); | |
124 } | |
125 | |
126 /* | |
127 * mark text public | |
128 */ | |
129 text.setIsPublic(isPublic); | |
130 saveList.add(text); | |
131 report.add("Set public="+isPublic+" on "+text.getShortString()+"\n"); | |
132 | |
133 /* | |
134 * mark directly related objects except TEXT | |
135 */ | |
136 List<Entity> relatedEnts = setRelatedEntitiesPrivacity(text, isPublic, wrapper, TEXTexcepts); | |
137 saveList.addAll(relatedEnts); | |
138 report.add("Set public="+isPublic+" on related entities to "+text.getShortString()+" : ["+Entity.getShortStringList(relatedEnts)+"]\n"); | |
139 | |
140 /* | |
141 * follow relations of related objects | |
142 */ | |
143 int cnt = 0; | |
144 do { | |
145 List<Entity> nextRelEnts = new ArrayList<Entity>(); | |
146 for (Entity relEnt : relatedEnts) { | |
147 String entType = relEnt.getObjectClass(); | |
148 if (entType.equals("PERSON")) { | |
149 // PERSON | |
150 List<Entity> persRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, PERSONexcepts); | |
151 saveList.addAll(persRelEnts); | |
152 nextRelEnts.addAll(persRelEnts); | |
153 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(persRelEnts)+"]\n"); | |
154 } else if (entType.equals("WITNESS")) { | |
155 // WITNESS | |
156 List<Entity> witRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, WITNESSexcepts); | |
157 saveList.addAll(witRelEnts); | |
158 nextRelEnts.addAll(witRelEnts); | |
159 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(witRelEnts)+"]\n"); | |
160 } else if (entType.equals("CODEX")) { | |
161 // CODEX | |
162 List<Entity> codRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, CODEXexcepts); | |
163 saveList.addAll(codRelEnts); | |
164 nextRelEnts.addAll(codRelEnts); | |
165 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(codRelEnts)+"]\n"); | |
166 } else if (entType.equals("COLLECTION")) { | |
167 // COLLECTION | |
168 List<Entity> colRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, COLLECTIONexcepts); | |
169 saveList.addAll(colRelEnts); | |
170 nextRelEnts.addAll(colRelEnts); | |
171 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(colRelEnts)+"]\n"); | |
172 } else if (entType.equals("REPOSITORY")) { | |
173 // REPOSITORY | |
174 List<Entity> repRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, REPOSITORYexcepts); | |
175 saveList.addAll(repRelEnts); | |
176 nextRelEnts.addAll(repRelEnts); | |
177 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(repRelEnts)+"]\n"); | |
178 } else if (entType.endsWith("_EVENT")) { | |
179 // *_EVENT: mark all related entities | |
180 List<Entity> evRelEnts = setRelatedEntitiesPrivacity(relEnt, isPublic, wrapper, null); | |
181 saveList.addAll(evRelEnts); | |
182 nextRelEnts.addAll(evRelEnts); | |
183 report.add("Set public="+isPublic+" on related entities to "+relEnt.getShortString()+" : ["+Entity.getShortStringList(evRelEnts)+"]\n"); | |
184 } else { | |
185 // everything else? | |
186 } | |
187 } | |
188 // start with next level | |
189 relatedEnts = nextRelEnts; | |
190 } while (!relatedEnts.isEmpty() && ++cnt < 5); | |
191 if (cnt == 5) { | |
192 throw new Exception("Relation depth limit exceeded when marking text public!"); | |
193 } | |
194 return saveList; | |
195 } | |
196 | |
197 | |
74 | 198 |
75 public static List<Entity> changePrivacity4Person(Entity person, Boolean isPublic, WrapperService wrapper){ | 199 public static List<Entity> changePrivacity4Person(Entity person, Boolean isPublic, WrapperService wrapper){ |
76 List<Entity> saveList = new ArrayList<Entity>(); | 200 List<Entity> saveList = new ArrayList<Entity>(); |
77 | 201 |
78 if(person.isLightweight()){ | 202 if(person.isLightweight()){ |