001package systems.dmx.core.impl; 002 003import systems.dmx.core.Role; 004import systems.dmx.core.model.RoleModel; 005import systems.dmx.core.model.SimpleValue; 006import systems.dmx.core.model.TopicModel; 007import systems.dmx.core.model.TopicRoleModel; 008 009import org.codehaus.jettison.json.JSONObject; 010 011 012 013class TopicRoleModelImpl extends RoleModelImpl implements TopicRoleModel { 014 015 // ---------------------------------------------------------------------------------------------- Instance Variables 016 017 private String topicUri; 018 private boolean topicIdentifiedByUri; 019 020 // ---------------------------------------------------------------------------------------------------- Constructors 021 022 TopicRoleModelImpl(long topicId, String roleTypeUri, PersistenceLayer pl) { 023 super(topicId, roleTypeUri, pl); 024 this.topicUri = null; 025 this.topicIdentifiedByUri = false; 026 } 027 028 TopicRoleModelImpl(String topicUri, String roleTypeUri, PersistenceLayer pl) { 029 super(-1, roleTypeUri, pl); 030 this.topicUri = topicUri; 031 this.topicIdentifiedByUri = true; 032 } 033 034 // -------------------------------------------------------------------------------------------------- Public Methods 035 036 @Override 037 public long getPlayerId() { 038 if (topicIdentifiedByUri) { 039 throw new IllegalStateException("The topic is not identified by ID but by URI (" + this + ")"); 040 } 041 return super.getPlayerId(); 042 } 043 044 @Override 045 public String getTopicUri() { 046 if (!topicIdentifiedByUri) { 047 throw new IllegalStateException("The topic is not identified by URI but by ID (" + this + ")"); 048 } 049 return topicUri; 050 } 051 052 @Override 053 public boolean topicIdentifiedByUri() { 054 return topicIdentifiedByUri; 055 } 056 057 058 059 // === Implementation of abstract RoleModel methods === 060 061 @Override 062 public boolean refsSameObject(RoleModel model) { 063 if (model instanceof TopicRoleModel) { 064 TopicRoleModel topicRole = (TopicRoleModel) model; 065 if (topicRole.topicIdentifiedByUri() == topicIdentifiedByUri) { 066 if (topicIdentifiedByUri) { 067 return topicRole.getTopicUri().equals(topicUri); 068 } else { 069 return topicRole.getPlayerId() == playerId; 070 } 071 } 072 } 073 return false; 074 } 075 076 @Override 077 public JSONObject toJSON() { 078 try { 079 JSONObject o = new JSONObject(); 080 if (topicIdentifiedByUri) { 081 o.put("topicUri", topicUri); 082 } else { 083 o.put("topicId", playerId); 084 } 085 o.put("roleTypeUri", roleTypeUri); 086 return o; 087 } catch (Exception e) { 088 throw new RuntimeException("Serialization failed", e); 089 } 090 } 091 092 // ----------------------------------------------------------------------------------------- Package Private Methods 093 094 095 096 // === Implementation of abstract RoleModelImpl methods === 097 098 @Override 099 Role instantiate(AssociationModelImpl assoc) { 100 return new TopicRoleImpl(this, assoc); 101 } 102 103 @Override 104 RelatedTopicModelImpl getPlayer(AssociationModelImpl assoc) { 105 TopicModel topic; 106 if (topicIdentifiedByUri) { 107 topic = pl.fetchTopic("uri", new SimpleValue(topicUri)); 108 } else { 109 topic = pl.fetchTopic(playerId); 110 } 111 return mf.newRelatedTopicModel(topic, assoc); 112 } 113}