view src/main/java/de/mpiwg/itgroup/annotations/Actor.java @ 88:b406507a953d

upped version to 0.5. can use display name and groups from auth token.
author casties
date Tue, 03 Feb 2015 19:01:27 +0100
parents 2b1e6df5e21a
children f83eb8b335b1
line wrap: on
line source

/**
 * 
 */
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
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;

/**
 * @author casties
 * 
 */
public abstract class Actor {

    public String uri;
    public String name;
    public String id;

    /**
     * @return if this Actor is a Group
     */
    public abstract boolean isGroup();

    /**
     * Returns if this Actor is equivalent to Person person. If this is
     * a Group returns true when the Person is in the Group.
     * 
     * @param person
     * @param store AnnotationStore to check group membership
     * @return
     */
    public boolean isEquivalentWith(Person person, AnnotationStore store) {
        if (person == null) return false;
        if (person.equals(this)) return true;
        if (person.getIdString().equals(this.getIdString())) return true;
        if (isGroup() && store != null) {
            // check if person in group
            if (person.groups != null) {
                // check person's groups
                if (person.groups.contains(this.id)) {
                    return true;
                }
            }
            // check in store
            return store.isPersonInGroup(person, (Group) this);            
        }
        return false;
    }

    /**
     * @return the uri
     */
    public String getUri() {
        return uri;
    }

    /**
     * Returns the uri (uses id if empty).
     * 
     * @return the uri
     */
    public String getUriString() {
        if (uri == null) {
            return getUriFromId(id, isGroup());
        }
        return uri;
    }

    /**
     * @param uri
     *            the uri to set
     */
    public void setUri(String uri) {
        this.uri = uri;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * Returns id as a String starting with "group:" for groups.
     * 
     * @return
     */
    public abstract String getIdString();

    /**
     * @param id
     *            the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Returns a short id from an uri.
     * 
     * @param uri
     * @return
     */
    public static String getIdFromUri(String uri, boolean isGroup) {
        String id = null;
        String prefix = BaseRestlet.PERSONS_URI_PREFIX;
        if (isGroup) {
            prefix = BaseRestlet.GROUPS_URI_PREFIX;
        }
        if (uri != null && uri.startsWith(prefix)) {
            id = uri.replace(prefix, "");
        }
        return id;
    }

    /**
     * Returns an uri from a short id.
     * 
     * @param id
     * @return
     */
    public static String getUriFromId(String id, boolean isGroup) {
        String uri = null;
        String prefix = BaseRestlet.PERSONS_URI_PREFIX;
        if (isGroup) {
            prefix = BaseRestlet.GROUPS_URI_PREFIX;
        }
        if (id != null && !id.startsWith("http://")) {
            uri = prefix + id;
        }
        return uri;
    }

}