001package systems.dmx.core.impl;
002
003import systems.dmx.core.Role;
004import systems.dmx.core.model.RoleModel;
005
006
007
008abstract class RoleModelImpl implements RoleModel {
009
010    // ---------------------------------------------------------------------------------------------- Instance Variables
011
012    long playerId;                  // id of the player (a topic, or an association)
013    String roleTypeUri;             // is never null
014
015    PersistenceLayer pl;
016    ModelFactoryImpl mf;
017
018    // ---------------------------------------------------------------------------------------------------- Constructors
019
020    // ### TODO: drop this?
021    RoleModelImpl() {
022    }
023
024    RoleModelImpl(long playerId, String roleTypeUri, PersistenceLayer pl) {
025        setPlayerId(playerId);
026        setRoleTypeUri(roleTypeUri);
027        this.pl = pl;
028        this.mf = pl.mf;
029    }
030
031    // -------------------------------------------------------------------------------------------------- Public Methods
032
033    @Override
034    public long getPlayerId() {
035        return playerId;
036    }
037
038    @Override
039    public final String getRoleTypeUri() {
040        return roleTypeUri;
041    }
042
043    // ---
044
045    // ### TODO: to be dropped?
046    @Override
047    public void setPlayerId(long playerId) {
048        this.playerId = playerId;
049    }
050
051    @Override
052    public final void setRoleTypeUri(String roleTypeUri) {
053        if (roleTypeUri == null) {
054            throw new IllegalArgumentException("\"roleTypeUri\" must not be null");
055        }
056        //
057        this.roleTypeUri = roleTypeUri;
058    }
059
060    // ---
061
062    // Note: refsSameObject() remain abstract
063
064
065
066    // === Java API ===
067
068    @Override
069    public RoleModel clone() {
070        try {
071            return (RoleModel) super.clone();
072        } catch (Exception e) {
073            throw new RuntimeException("Cloning a RoleModel failed", e);
074        }
075    }
076
077    // TODO: copy in DMXObjectModelImpl
078    // Can we use Java 8 and put this in the JSONEnabled interface?
079    @Override
080    public String toString() {
081        try {
082            return getClass().getSimpleName() + " " + toJSON().toString(4);
083        } catch (Exception e) {
084            throw new RuntimeException("Prettyprinting failed", e);
085        }
086    }
087
088    // ----------------------------------------------------------------------------------------- Package Private Methods
089
090    /**
091     * @param   assoc   the association this role is involved in
092     */
093    abstract Role instantiate(AssociationModelImpl assoc);
094
095    /**
096     * @param   assoc   the association this role is involved in
097     */
098    abstract DMXObjectModelImpl getPlayer(AssociationModelImpl assoc);
099}