/** * */ package de.mpiwg.itgroup.annotations; /* * #%L * AnnotationManager * %% * Copyright (C) 2012 - 2014 MPIWG Berlin * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. * * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% */ import java.io.UnsupportedEncodingException; import java.util.Set; import org.apache.commons.codec.binary.Base64; import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; /** * @author casties * */ public class Annotation { /** * The URI of this annotation. */ protected String uri; /** * The annotation (body) text. */ protected String bodyText; /** * The URI of the annotation text */ protected String bodyUri; /** * The annotation target. */ protected Target target; /** * The fragment part of the annotation target. */ protected String targetFragment; /** * The types of annotation target fragments. * */ public static enum FragmentTypes { XPOINTER, AREA, WKT }; /** * The type of the annotation target fragment. */ protected FragmentTypes fragmentType; /** * The selected text of the annotation target. */ protected String quote; /** * The Resource that is annotated e.g. a book. * The target is part of this resource e.g. a page of a book. */ protected Resource resource; /** * The creator of this annotation. */ protected Actor creator; /** * The creation date of this annotation. */ protected String created; /** * The last update date of this annotation. */ protected String updated; /** * The user or group that has admin permissions. * null means any user. */ protected Actor adminPermission; /** * The user or group that has delete permissions. * null means any user. */ protected Actor deletePermission; /** * The user or group that has update permissions. * null means any user. */ protected Actor updatePermission; /** * The user or group that has read permissions. * null means any user. */ protected Actor readPermission; /** * List of tags on this Annotation. */ protected Set tags; /** * Enum of actions (for permissions). */ public static enum Action { read, update, create, delete, admin } /** * Returns if the requested action is allowed for the given user on this annotation. * * @param action * @param user * @param store AnnotationStore to check group membership * @return */ public boolean isActionAllowed(Action action, Person user, AnnotationStore store) { if (action == Action.read) { Actor reader = getReadPermission(); if (reader == null) { // if not specified then everybody is allowed return true; } else { return reader.isEquivalentWith(user, store); } } else if (action == Action.update) { // require at least an authenticated user if (user == null) return false; Actor updater = getUpdatePermission(); if (updater == null) { // if not specified then everybody is allowed return true; } else { return updater.isEquivalentWith(user, store); } } else if (action == Action.delete) { // require at least an authenticated user if (user == null) return false; Actor deleter = getDeletePermission(); if (deleter == null) { // if not specified then only creator is allowed deleter = creator; } return deleter.isEquivalentWith(user, store); } else if (action == Action.admin) { // require at least an authenticated user if (user == null) return false; Actor admin = getAdminPermission(); if (admin == null) { // if not specified then only creator is allowed admin = creator; } return admin.isEquivalentWith(user, store); } return false; } /** * @return the uri */ public String getUri() { return uri; } /** * @param uri the uri to set */ public void setUri(String uri) { this.uri = uri; } /** * Returns an URL-compatible id. * Currently the uri as base64 encoded string. * @return */ public String getUrlId() { if (uri == null) return null; try { return Base64.encodeBase64URLSafeString(uri.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { return null; } } public static String decodeId(String id) { if (id == null) return null; try { return new String(Base64.decodeBase64(id), "UTF-8"); } catch (UnsupportedEncodingException e) { return null; } } /** * @return the bodyText */ public String getBodyText() { return bodyText; } /** * @param bodyText the bodyText to set */ public void setBodyText(String bodyText) { this.bodyText = bodyText; } /** * @return the bodyUri */ public String getBodyUri() { return bodyUri; } /** * @param bodyUri the bodyUri to set */ public void setBodyUri(String bodyUri) { this.bodyUri = bodyUri; } /** * @return the target */ public Target getTarget() { return target; } /** * @param target the target to set */ public void setTarget(Target target) { this.target = target; } /** * @return the targetBaseUri */ public String getTargetBaseUri() { if (target == null) return null; return target.getUri(); } /** * @return the targetFragment */ public String getTargetFragment() { return targetFragment; } /** * @param targetFragment the targetFragment to set */ public void setTargetFragment(String targetFragment) { this.targetFragment = targetFragment; } /** * @return the targetType */ public FragmentTypes getFragmentType() { return fragmentType; } /** * @param fragmentType the fragmentType to set */ public void setFragmentType(FragmentTypes fragmentType) { this.fragmentType = fragmentType; } /** * @return the quote */ public String getQuote() { return quote; } /** * @param quote the quote to set */ public void setQuote(String quote) { this.quote = quote; } /** * @return the resource */ public Resource getResource() { return resource; } /** * @param resource the resource to set */ public void setResource(Resource resource) { this.resource = resource; } /** * @return the resourceUri */ public String getResourceUri() { if (resource == null) return null; return resource.getUri(); } /** * @return the creator */ public Actor getCreator() { return creator; } /** * @param creator the creator to set */ public void setCreator(Actor creator) { this.creator = creator; } /** * @return the creatorUri */ public String getCreatorUri() { if (creator != null) { return creator.getUri(); } return null; } /** * @return the creatorName */ public String getCreatorName() { if (creator != null) { return creator.getName(); } return null; } /** * @return the created */ public String getCreated() { return created; } /** * @param created the created to set */ public void setCreated(String created) { this.created = created; } public String getUpdated() { return updated; } public void setUpdated(String updated) { this.updated = updated; } /** * @return the adminPermission */ public Actor getAdminPermission() { if (adminPermission != null) { return adminPermission; } else { // if not specified then only creator is allowed return this.creator; } } /** * @param adminPermission the adminPermission to set */ public void setAdminPermission(Actor adminPermission) { this.adminPermission = adminPermission; } /** * @return the deletePermission */ public Actor getDeletePermission() { if (deletePermission != null) { return deletePermission; } else { // if not specified then only creator is allowed return this.creator; } } /** * @param deletePermission the deletePermission to set */ public void setDeletePermission(Actor deletePermission) { this.deletePermission = deletePermission; } /** * @return the updatePermission */ public Actor getUpdatePermission() { return updatePermission; } /** * @param updatePermission the updatePermission to set */ public void setUpdatePermission(Actor updatePermission) { this.updatePermission = updatePermission; } /** * @return the readPermission */ public Actor getReadPermission() { return readPermission; } /** * @param readPermission the readPermission to set */ public void setReadPermission(Actor readPermission) { this.readPermission = readPermission; } /** * @return the tags */ public Set getTags() { return tags; } /** * @param tags the tags to set */ public void setTags(Set tags) { this.tags = tags; } }