1 | /** |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.mpiwg.itgroup.annotations; |
---|
5 | |
---|
6 | /* |
---|
7 | * #%L |
---|
8 | * AnnotationManager |
---|
9 | * %% |
---|
10 | * Copyright (C) 2012 - 2014 MPIWG Berlin |
---|
11 | * %% |
---|
12 | * This program is free software: you can redistribute it and/or modify |
---|
13 | * it under the terms of the GNU Lesser General Public License as |
---|
14 | * published by the Free Software Foundation, either version 3 of the |
---|
15 | * License, or (at your option) any later version. |
---|
16 | * |
---|
17 | * This program is distributed in the hope that it will be useful, |
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | * GNU General Lesser Public License for more details. |
---|
21 | * |
---|
22 | * You should have received a copy of the GNU General Lesser Public |
---|
23 | * License along with this program. If not, see |
---|
24 | * <http://www.gnu.org/licenses/lgpl-3.0.html>. |
---|
25 | * #L% |
---|
26 | */ |
---|
27 | |
---|
28 | import java.io.UnsupportedEncodingException; |
---|
29 | import java.util.Set; |
---|
30 | |
---|
31 | import org.apache.commons.codec.binary.Base64; |
---|
32 | |
---|
33 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
34 | |
---|
35 | /** |
---|
36 | * @author casties |
---|
37 | * |
---|
38 | */ |
---|
39 | public class Annotation { |
---|
40 | /** |
---|
41 | * The URI of this annotation. |
---|
42 | */ |
---|
43 | protected String uri; |
---|
44 | |
---|
45 | /** |
---|
46 | * The annotation (body) text. |
---|
47 | */ |
---|
48 | protected String bodyText; |
---|
49 | |
---|
50 | /** |
---|
51 | * The URI of the annotation text |
---|
52 | */ |
---|
53 | protected String bodyUri; |
---|
54 | |
---|
55 | /** |
---|
56 | * The annotation target. |
---|
57 | */ |
---|
58 | protected Target target; |
---|
59 | |
---|
60 | /** |
---|
61 | * The fragment part of the annotation target. |
---|
62 | */ |
---|
63 | protected String targetFragment; |
---|
64 | |
---|
65 | /** |
---|
66 | * The types of annotation target fragments. |
---|
67 | * |
---|
68 | */ |
---|
69 | public static enum FragmentTypes { |
---|
70 | XPOINTER, AREA, WKT |
---|
71 | }; |
---|
72 | |
---|
73 | /** |
---|
74 | * The type of the annotation target fragment. |
---|
75 | */ |
---|
76 | protected FragmentTypes fragmentType; |
---|
77 | |
---|
78 | /** |
---|
79 | * The selected text of the annotation target. |
---|
80 | */ |
---|
81 | protected String quote; |
---|
82 | |
---|
83 | /** |
---|
84 | * The Resource that is annotated e.g. a book. |
---|
85 | * The target is part of this resource e.g. a page of a book. |
---|
86 | */ |
---|
87 | protected Resource resource; |
---|
88 | |
---|
89 | /** |
---|
90 | * The creator of this annotation. |
---|
91 | */ |
---|
92 | protected Actor creator; |
---|
93 | |
---|
94 | /** |
---|
95 | * The creation date of this annotation. |
---|
96 | */ |
---|
97 | protected String created; |
---|
98 | |
---|
99 | /** |
---|
100 | * The user or group that has admin permissions. |
---|
101 | * null means any user. |
---|
102 | */ |
---|
103 | protected Actor adminPermission; |
---|
104 | |
---|
105 | /** |
---|
106 | * The user or group that has delete permissions. |
---|
107 | * null means any user. |
---|
108 | */ |
---|
109 | protected Actor deletePermission; |
---|
110 | |
---|
111 | /** |
---|
112 | * The user or group that has update permissions. |
---|
113 | * null means any user. |
---|
114 | */ |
---|
115 | protected Actor updatePermission; |
---|
116 | |
---|
117 | /** |
---|
118 | * The user or group that has read permissions. |
---|
119 | * null means any user. |
---|
120 | */ |
---|
121 | protected Actor readPermission; |
---|
122 | |
---|
123 | /** |
---|
124 | * List of tags on this Annotation. |
---|
125 | */ |
---|
126 | protected Set<String> tags; |
---|
127 | |
---|
128 | /** |
---|
129 | * Returns if the requested action is allowed for the given user on this annotation. |
---|
130 | * |
---|
131 | * @param action |
---|
132 | * @param user |
---|
133 | * @param store AnnotationStore to check group membership |
---|
134 | * @return |
---|
135 | */ |
---|
136 | public boolean isActionAllowed(String action, Person user, AnnotationStore store) { |
---|
137 | if (action.equals("read")) { |
---|
138 | Actor reader = getReadPermission(); |
---|
139 | if (reader == null) { |
---|
140 | // if not specified then everybody is allowed |
---|
141 | return true; |
---|
142 | } else { |
---|
143 | return reader.isEquivalentWith(user, store); |
---|
144 | } |
---|
145 | } else if (action.equals("update")) { |
---|
146 | // require at least an authenticated user |
---|
147 | if (user == null) return false; |
---|
148 | Actor updater = getUpdatePermission(); |
---|
149 | if (updater == null) { |
---|
150 | // if not specified then everybody is allowed |
---|
151 | return true; |
---|
152 | } else { |
---|
153 | return updater.isEquivalentWith(user, store); |
---|
154 | } |
---|
155 | } else if (action.equals("delete")) { |
---|
156 | // require at least an authenticated user |
---|
157 | if (user == null) return false; |
---|
158 | Actor deleter = getDeletePermission(); |
---|
159 | if (deleter == null) { |
---|
160 | // if not specified then only creator is allowed |
---|
161 | deleter = creator; |
---|
162 | } |
---|
163 | return deleter.isEquivalentWith(user, store); |
---|
164 | } else if (action.equals("admin")) { |
---|
165 | // require at least an authenticated user |
---|
166 | if (user == null) return false; |
---|
167 | Actor admin = getAdminPermission(); |
---|
168 | if (admin == null) { |
---|
169 | // if not specified then only creator is allowed |
---|
170 | admin = creator; |
---|
171 | } |
---|
172 | return admin.isEquivalentWith(user, store); |
---|
173 | } |
---|
174 | return false; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * @return the uri |
---|
179 | */ |
---|
180 | public String getUri() { |
---|
181 | return uri; |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * @param uri the uri to set |
---|
186 | */ |
---|
187 | public void setUri(String uri) { |
---|
188 | this.uri = uri; |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Returns an URL-compatible id. |
---|
193 | * Currently the uri as base64 encoded string. |
---|
194 | * @return |
---|
195 | */ |
---|
196 | public String getUrlId() { |
---|
197 | if (uri == null) return null; |
---|
198 | try { |
---|
199 | return Base64.encodeBase64URLSafeString(uri.getBytes("UTF-8")); |
---|
200 | } catch (UnsupportedEncodingException e) { |
---|
201 | return null; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | public static String decodeId(String id) { |
---|
206 | if (id == null) return null; |
---|
207 | try { |
---|
208 | return new String(Base64.decodeBase64(id), "UTF-8"); |
---|
209 | } catch (UnsupportedEncodingException e) { |
---|
210 | return null; |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * @return the bodyText |
---|
216 | */ |
---|
217 | public String getBodyText() { |
---|
218 | return bodyText; |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * @param bodyText the bodyText to set |
---|
223 | */ |
---|
224 | public void setBodyText(String bodyText) { |
---|
225 | this.bodyText = bodyText; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * @return the bodyUri |
---|
230 | */ |
---|
231 | public String getBodyUri() { |
---|
232 | return bodyUri; |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * @param bodyUri the bodyUri to set |
---|
237 | */ |
---|
238 | public void setBodyUri(String bodyUri) { |
---|
239 | this.bodyUri = bodyUri; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * @return the target |
---|
244 | */ |
---|
245 | public Target getTarget() { |
---|
246 | return target; |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * @param target the target to set |
---|
251 | */ |
---|
252 | public void setTarget(Target target) { |
---|
253 | this.target = target; |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | * @return the targetBaseUri |
---|
258 | */ |
---|
259 | public String getTargetBaseUri() { |
---|
260 | if (target == null) return null; |
---|
261 | return target.getUri(); |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * @return the targetFragment |
---|
266 | */ |
---|
267 | public String getTargetFragment() { |
---|
268 | return targetFragment; |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * @param targetFragment the targetFragment to set |
---|
273 | */ |
---|
274 | public void setTargetFragment(String targetFragment) { |
---|
275 | this.targetFragment = targetFragment; |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * @return the targetType |
---|
280 | */ |
---|
281 | public FragmentTypes getFragmentType() { |
---|
282 | return fragmentType; |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | * @param fragmentType the fragmentType to set |
---|
287 | */ |
---|
288 | public void setFragmentType(FragmentTypes fragmentType) { |
---|
289 | this.fragmentType = fragmentType; |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * @return the quote |
---|
294 | */ |
---|
295 | public String getQuote() { |
---|
296 | return quote; |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | * @param quote the quote to set |
---|
301 | */ |
---|
302 | public void setQuote(String quote) { |
---|
303 | this.quote = quote; |
---|
304 | } |
---|
305 | |
---|
306 | /** |
---|
307 | * @return the resource |
---|
308 | */ |
---|
309 | public Resource getResource() { |
---|
310 | return resource; |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | * @param resource the resource to set |
---|
315 | */ |
---|
316 | public void setResource(Resource resource) { |
---|
317 | this.resource = resource; |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * @return the resourceUri |
---|
322 | */ |
---|
323 | public String getResourceUri() { |
---|
324 | if (resource == null) return null; |
---|
325 | return resource.getUri(); |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * @return the creator |
---|
330 | */ |
---|
331 | public Actor getCreator() { |
---|
332 | return creator; |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | * @param creator the creator to set |
---|
337 | */ |
---|
338 | public void setCreator(Actor creator) { |
---|
339 | this.creator = creator; |
---|
340 | } |
---|
341 | |
---|
342 | /** |
---|
343 | * @return the creatorUri |
---|
344 | */ |
---|
345 | public String getCreatorUri() { |
---|
346 | if (creator != null) { |
---|
347 | return creator.getUri(); |
---|
348 | } |
---|
349 | return null; |
---|
350 | } |
---|
351 | |
---|
352 | /** |
---|
353 | * @return the creatorName |
---|
354 | */ |
---|
355 | public String getCreatorName() { |
---|
356 | if (creator != null) { |
---|
357 | return creator.getName(); |
---|
358 | } |
---|
359 | return null; |
---|
360 | } |
---|
361 | |
---|
362 | /** |
---|
363 | * @return the created |
---|
364 | */ |
---|
365 | public String getCreated() { |
---|
366 | return created; |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | * @param created the created to set |
---|
371 | */ |
---|
372 | public void setCreated(String created) { |
---|
373 | this.created = created; |
---|
374 | } |
---|
375 | |
---|
376 | /** |
---|
377 | * @return the adminPermission |
---|
378 | */ |
---|
379 | public Actor getAdminPermission() { |
---|
380 | if (adminPermission != null) { |
---|
381 | return adminPermission; |
---|
382 | } else { |
---|
383 | // if not specified then only creator is allowed |
---|
384 | return this.creator; |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | /** |
---|
389 | * @param adminPermission the adminPermission to set |
---|
390 | */ |
---|
391 | public void setAdminPermission(Actor adminPermission) { |
---|
392 | this.adminPermission = adminPermission; |
---|
393 | } |
---|
394 | |
---|
395 | /** |
---|
396 | * @return the deletePermission |
---|
397 | */ |
---|
398 | public Actor getDeletePermission() { |
---|
399 | if (deletePermission != null) { |
---|
400 | return deletePermission; |
---|
401 | } else { |
---|
402 | // if not specified then only creator is allowed |
---|
403 | return this.creator; |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | /** |
---|
408 | * @param deletePermission the deletePermission to set |
---|
409 | */ |
---|
410 | public void setDeletePermission(Actor deletePermission) { |
---|
411 | this.deletePermission = deletePermission; |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | * @return the updatePermission |
---|
416 | */ |
---|
417 | public Actor getUpdatePermission() { |
---|
418 | return updatePermission; |
---|
419 | } |
---|
420 | |
---|
421 | /** |
---|
422 | * @param updatePermission the updatePermission to set |
---|
423 | */ |
---|
424 | public void setUpdatePermission(Actor updatePermission) { |
---|
425 | this.updatePermission = updatePermission; |
---|
426 | } |
---|
427 | |
---|
428 | /** |
---|
429 | * @return the readPermission |
---|
430 | */ |
---|
431 | public Actor getReadPermission() { |
---|
432 | return readPermission; |
---|
433 | } |
---|
434 | |
---|
435 | /** |
---|
436 | * @param readPermission the readPermission to set |
---|
437 | */ |
---|
438 | public void setReadPermission(Actor readPermission) { |
---|
439 | this.readPermission = readPermission; |
---|
440 | } |
---|
441 | |
---|
442 | /** |
---|
443 | * @return the tags |
---|
444 | */ |
---|
445 | public Set<String> getTags() { |
---|
446 | return tags; |
---|
447 | } |
---|
448 | |
---|
449 | /** |
---|
450 | * @param tags the tags to set |
---|
451 | */ |
---|
452 | public void setTags(Set<String> tags) { |
---|
453 | this.tags = tags; |
---|
454 | } |
---|
455 | |
---|
456 | |
---|
457 | } |
---|