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 last update date of this annotation. |
---|
101 | */ |
---|
102 | protected String updated; |
---|
103 | |
---|
104 | /** |
---|
105 | * The user or group that has admin permissions. |
---|
106 | * null means any user. |
---|
107 | */ |
---|
108 | protected Actor adminPermission; |
---|
109 | |
---|
110 | /** |
---|
111 | * The user or group that has delete permissions. |
---|
112 | * null means any user. |
---|
113 | */ |
---|
114 | protected Actor deletePermission; |
---|
115 | |
---|
116 | /** |
---|
117 | * The user or group that has update permissions. |
---|
118 | * null means any user. |
---|
119 | */ |
---|
120 | protected Actor updatePermission; |
---|
121 | |
---|
122 | /** |
---|
123 | * The user or group that has read permissions. |
---|
124 | * null means any user. |
---|
125 | */ |
---|
126 | protected Actor readPermission; |
---|
127 | |
---|
128 | /** |
---|
129 | * List of tags on this Annotation. |
---|
130 | */ |
---|
131 | protected Set<String> tags; |
---|
132 | |
---|
133 | /** |
---|
134 | * Returns if the requested action is allowed for the given user on this annotation. |
---|
135 | * |
---|
136 | * @param action |
---|
137 | * @param user |
---|
138 | * @param store AnnotationStore to check group membership |
---|
139 | * @return |
---|
140 | */ |
---|
141 | public boolean isActionAllowed(String action, Person user, AnnotationStore store) { |
---|
142 | if (action.equals("read")) { |
---|
143 | Actor reader = getReadPermission(); |
---|
144 | if (reader == null) { |
---|
145 | // if not specified then everybody is allowed |
---|
146 | return true; |
---|
147 | } else { |
---|
148 | return reader.isEquivalentWith(user, store); |
---|
149 | } |
---|
150 | } else if (action.equals("update")) { |
---|
151 | // require at least an authenticated user |
---|
152 | if (user == null) return false; |
---|
153 | Actor updater = getUpdatePermission(); |
---|
154 | if (updater == null) { |
---|
155 | // if not specified then everybody is allowed |
---|
156 | return true; |
---|
157 | } else { |
---|
158 | return updater.isEquivalentWith(user, store); |
---|
159 | } |
---|
160 | } else if (action.equals("delete")) { |
---|
161 | // require at least an authenticated user |
---|
162 | if (user == null) return false; |
---|
163 | Actor deleter = getDeletePermission(); |
---|
164 | if (deleter == null) { |
---|
165 | // if not specified then only creator is allowed |
---|
166 | deleter = creator; |
---|
167 | } |
---|
168 | return deleter.isEquivalentWith(user, store); |
---|
169 | } else if (action.equals("admin")) { |
---|
170 | // require at least an authenticated user |
---|
171 | if (user == null) return false; |
---|
172 | Actor admin = getAdminPermission(); |
---|
173 | if (admin == null) { |
---|
174 | // if not specified then only creator is allowed |
---|
175 | admin = creator; |
---|
176 | } |
---|
177 | return admin.isEquivalentWith(user, store); |
---|
178 | } |
---|
179 | return false; |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * @return the uri |
---|
184 | */ |
---|
185 | public String getUri() { |
---|
186 | return uri; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * @param uri the uri to set |
---|
191 | */ |
---|
192 | public void setUri(String uri) { |
---|
193 | this.uri = uri; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * Returns an URL-compatible id. |
---|
198 | * Currently the uri as base64 encoded string. |
---|
199 | * @return |
---|
200 | */ |
---|
201 | public String getUrlId() { |
---|
202 | if (uri == null) return null; |
---|
203 | try { |
---|
204 | return Base64.encodeBase64URLSafeString(uri.getBytes("UTF-8")); |
---|
205 | } catch (UnsupportedEncodingException e) { |
---|
206 | return null; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | public static String decodeId(String id) { |
---|
211 | if (id == null) return null; |
---|
212 | try { |
---|
213 | return new String(Base64.decodeBase64(id), "UTF-8"); |
---|
214 | } catch (UnsupportedEncodingException e) { |
---|
215 | return null; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * @return the bodyText |
---|
221 | */ |
---|
222 | public String getBodyText() { |
---|
223 | return bodyText; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * @param bodyText the bodyText to set |
---|
228 | */ |
---|
229 | public void setBodyText(String bodyText) { |
---|
230 | this.bodyText = bodyText; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * @return the bodyUri |
---|
235 | */ |
---|
236 | public String getBodyUri() { |
---|
237 | return bodyUri; |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * @param bodyUri the bodyUri to set |
---|
242 | */ |
---|
243 | public void setBodyUri(String bodyUri) { |
---|
244 | this.bodyUri = bodyUri; |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * @return the target |
---|
249 | */ |
---|
250 | public Target getTarget() { |
---|
251 | return target; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * @param target the target to set |
---|
256 | */ |
---|
257 | public void setTarget(Target target) { |
---|
258 | this.target = target; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * @return the targetBaseUri |
---|
263 | */ |
---|
264 | public String getTargetBaseUri() { |
---|
265 | if (target == null) return null; |
---|
266 | return target.getUri(); |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * @return the targetFragment |
---|
271 | */ |
---|
272 | public String getTargetFragment() { |
---|
273 | return targetFragment; |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * @param targetFragment the targetFragment to set |
---|
278 | */ |
---|
279 | public void setTargetFragment(String targetFragment) { |
---|
280 | this.targetFragment = targetFragment; |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | * @return the targetType |
---|
285 | */ |
---|
286 | public FragmentTypes getFragmentType() { |
---|
287 | return fragmentType; |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * @param fragmentType the fragmentType to set |
---|
292 | */ |
---|
293 | public void setFragmentType(FragmentTypes fragmentType) { |
---|
294 | this.fragmentType = fragmentType; |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * @return the quote |
---|
299 | */ |
---|
300 | public String getQuote() { |
---|
301 | return quote; |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * @param quote the quote to set |
---|
306 | */ |
---|
307 | public void setQuote(String quote) { |
---|
308 | this.quote = quote; |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * @return the resource |
---|
313 | */ |
---|
314 | public Resource getResource() { |
---|
315 | return resource; |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * @param resource the resource to set |
---|
320 | */ |
---|
321 | public void setResource(Resource resource) { |
---|
322 | this.resource = resource; |
---|
323 | } |
---|
324 | |
---|
325 | /** |
---|
326 | * @return the resourceUri |
---|
327 | */ |
---|
328 | public String getResourceUri() { |
---|
329 | if (resource == null) return null; |
---|
330 | return resource.getUri(); |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * @return the creator |
---|
335 | */ |
---|
336 | public Actor getCreator() { |
---|
337 | return creator; |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * @param creator the creator to set |
---|
342 | */ |
---|
343 | public void setCreator(Actor creator) { |
---|
344 | this.creator = creator; |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * @return the creatorUri |
---|
349 | */ |
---|
350 | public String getCreatorUri() { |
---|
351 | if (creator != null) { |
---|
352 | return creator.getUri(); |
---|
353 | } |
---|
354 | return null; |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | * @return the creatorName |
---|
359 | */ |
---|
360 | public String getCreatorName() { |
---|
361 | if (creator != null) { |
---|
362 | return creator.getName(); |
---|
363 | } |
---|
364 | return null; |
---|
365 | } |
---|
366 | |
---|
367 | /** |
---|
368 | * @return the created |
---|
369 | */ |
---|
370 | public String getCreated() { |
---|
371 | return created; |
---|
372 | } |
---|
373 | |
---|
374 | /** |
---|
375 | * @param created the created to set |
---|
376 | */ |
---|
377 | public void setCreated(String created) { |
---|
378 | this.created = created; |
---|
379 | } |
---|
380 | |
---|
381 | public String getUpdated() { |
---|
382 | return updated; |
---|
383 | } |
---|
384 | |
---|
385 | public void setUpdated(String updated) { |
---|
386 | this.updated = updated; |
---|
387 | } |
---|
388 | |
---|
389 | /** |
---|
390 | * @return the adminPermission |
---|
391 | */ |
---|
392 | public Actor getAdminPermission() { |
---|
393 | if (adminPermission != null) { |
---|
394 | return adminPermission; |
---|
395 | } else { |
---|
396 | // if not specified then only creator is allowed |
---|
397 | return this.creator; |
---|
398 | } |
---|
399 | } |
---|
400 | |
---|
401 | /** |
---|
402 | * @param adminPermission the adminPermission to set |
---|
403 | */ |
---|
404 | public void setAdminPermission(Actor adminPermission) { |
---|
405 | this.adminPermission = adminPermission; |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | * @return the deletePermission |
---|
410 | */ |
---|
411 | public Actor getDeletePermission() { |
---|
412 | if (deletePermission != null) { |
---|
413 | return deletePermission; |
---|
414 | } else { |
---|
415 | // if not specified then only creator is allowed |
---|
416 | return this.creator; |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | * @param deletePermission the deletePermission to set |
---|
422 | */ |
---|
423 | public void setDeletePermission(Actor deletePermission) { |
---|
424 | this.deletePermission = deletePermission; |
---|
425 | } |
---|
426 | |
---|
427 | /** |
---|
428 | * @return the updatePermission |
---|
429 | */ |
---|
430 | public Actor getUpdatePermission() { |
---|
431 | return updatePermission; |
---|
432 | } |
---|
433 | |
---|
434 | /** |
---|
435 | * @param updatePermission the updatePermission to set |
---|
436 | */ |
---|
437 | public void setUpdatePermission(Actor updatePermission) { |
---|
438 | this.updatePermission = updatePermission; |
---|
439 | } |
---|
440 | |
---|
441 | /** |
---|
442 | * @return the readPermission |
---|
443 | */ |
---|
444 | public Actor getReadPermission() { |
---|
445 | return readPermission; |
---|
446 | } |
---|
447 | |
---|
448 | /** |
---|
449 | * @param readPermission the readPermission to set |
---|
450 | */ |
---|
451 | public void setReadPermission(Actor readPermission) { |
---|
452 | this.readPermission = readPermission; |
---|
453 | } |
---|
454 | |
---|
455 | /** |
---|
456 | * @return the tags |
---|
457 | */ |
---|
458 | public Set<String> getTags() { |
---|
459 | return tags; |
---|
460 | } |
---|
461 | |
---|
462 | /** |
---|
463 | * @param tags the tags to set |
---|
464 | */ |
---|
465 | public void setTags(Set<String> tags) { |
---|
466 | this.tags = tags; |
---|
467 | } |
---|
468 | |
---|
469 | |
---|
470 | } |
---|