001package de.deepamehta.core.model;
002
003import org.codehaus.jettison.json.JSONArray;
004import org.codehaus.jettison.json.JSONObject;
005
006import java.util.HashMap;
007import java.util.List;
008import java.util.Map;
009
010
011
012/**
013 * The role an association plays in an association.
014 * <p>
015 * A AssociationRoleModel object is a pair of an association ID and a role type URI.
016 * <p>
017 * Assertion: both, the association ID and the role type URI are set.
018 * <p>
019 * In the database a role type is represented by a topic of type "dm4.core.role_type".
020 */
021public class AssociationRoleModel extends RoleModel {
022
023    // ---------------------------------------------------------------------------------------------------- Constructors
024
025    public AssociationRoleModel(long assocId, String roleTypeUri) {
026        super(assocId, roleTypeUri);
027    }
028
029    public AssociationRoleModel(JSONObject assocRoleModel) {
030        try {
031            this.playerId = assocRoleModel.getLong("assoc_id");
032            this.roleTypeUri = assocRoleModel.getString("role_type_uri");
033        } catch (Exception e) {
034            throw new RuntimeException("Parsing AssociationRoleModel failed (JSONObject=" + assocRoleModel + ")", e);
035        }
036    }
037
038    // -------------------------------------------------------------------------------------------------- Public Methods
039
040    // === Implementation of abstract RoleModel methods ===
041
042    @Override
043    public boolean refsSameObject(RoleModel model) {
044        if (model instanceof AssociationRoleModel) {
045            AssociationRoleModel assocRole = (AssociationRoleModel) model;
046            return assocRole.playerId == playerId;
047        }
048        return false;
049    }
050
051    @Override
052    public JSONObject toJSON() {
053        try {
054            JSONObject o = new JSONObject();
055            o.put("assoc_id", playerId);
056            o.put("role_type_uri", roleTypeUri);
057            return o;
058        } catch (Exception e) {
059            throw new RuntimeException("Serialization failed (" + this + ")", e);
060        }
061    }
062
063    // === Java API ===
064
065    @Override
066    public String toString() {
067        return "\n        association role (roleTypeUri=\"" + roleTypeUri + "\", playerId=" + playerId + ")";
068    }
069}