001package systems.dmx.core.impl;
002
003import systems.dmx.core.Role;
004import systems.dmx.core.model.AssociationModel;
005import systems.dmx.core.model.AssociationRoleModel;
006import systems.dmx.core.model.RoleModel;
007
008import org.codehaus.jettison.json.JSONObject;
009
010
011
012class AssociationRoleModelImpl extends RoleModelImpl implements AssociationRoleModel {
013
014    // ---------------------------------------------------------------------------------------------------- Constructors
015
016    AssociationRoleModelImpl(long assocId, String roleTypeUri, PersistenceLayer pl) {
017        super(assocId, roleTypeUri,  pl);
018    }
019
020    // -------------------------------------------------------------------------------------------------- Public Methods
021
022
023
024    // === Implementation of abstract RoleModel methods ===
025
026    @Override
027    public boolean refsSameObject(RoleModel model) {
028        if (model instanceof AssociationRoleModel) {
029            AssociationRoleModel assocRole = (AssociationRoleModel) model;
030            return assocRole.getPlayerId() == playerId;
031        }
032        return false;
033    }
034
035    @Override
036    public JSONObject toJSON() {
037        try {
038            return new JSONObject()
039                .put("assocId", playerId)
040                .put("roleTypeUri", roleTypeUri);
041        } catch (Exception e) {
042            throw new RuntimeException("Serialization failed", e);
043        }
044    }
045
046    // ----------------------------------------------------------------------------------------- Package Private Methods
047
048
049
050    // === Implementation of abstract RoleModelImpl methods ===
051
052    @Override
053    Role instantiate(AssociationModelImpl assoc) {
054        return new AssociationRoleImpl(this, assoc);
055    }
056
057    @Override
058    RelatedAssociationModelImpl getPlayer(AssociationModelImpl assoc) {
059        return mf.newRelatedAssociationModel(pl.fetchAssociation(playerId), assoc);
060    }
061}