view src/main/java/de/mpiwg/itgroup/annotations/Actor.java @ 58:f5c0e6df7e88

made uri prefixes in store configurable.
author casties
date Tue, 20 Nov 2012 18:23:52 +0100
parents aafa3884b2c4
children 2b1e6df5e21a
line wrap: on
line source

/**
 * 
 */
package de.mpiwg.itgroup.annotations;

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
            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;
    }

}