source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/Annotation.java @ 50:64aa756c60cc

Last change on this file since 50:64aa756c60cc was 50:64aa756c60cc, checked in by casties, 12 years ago

annotations ui can show and delete annotations now.

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