source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/Annotation.java @ 70:2b1e6df5e21a

Last change on this file since 70:2b1e6df5e21a was 70:2b1e6df5e21a, checked in by casties, 10 years ago

added lgpl_v3 license information.

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