001package de.deepamehta.core.impl; 002 003import de.deepamehta.core.Role; 004import de.deepamehta.core.model.RoleModel; 005import de.deepamehta.core.model.SimpleValue; 006import de.deepamehta.core.model.TopicModel; 007import de.deepamehta.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("topic_uri", topicUri); 082 } else { 083 o.put("topic_id", playerId); 084 } 085 o.put("role_type_uri", roleTypeUri); 086 return o; 087 } catch (Exception e) { 088 throw new RuntimeException("Serialization failed (" + this + ")", e); 089 } 090 } 091 092 093 094 // === Java API === 095 096 @Override 097 public String toString() { 098 String player = topicIdentifiedByUri ? "topicUri=\"" + topicUri + "\"" : "playerId=" + playerId; 099 return "\n topic role (roleTypeUri=\"" + roleTypeUri + "\", " + player + ")"; 100 } 101 102 // ----------------------------------------------------------------------------------------- Package Private Methods 103 104 105 106 // === Implementation of abstract RoleModelImpl methods === 107 108 @Override 109 Role instantiate(AssociationModelImpl assoc) { 110 return new TopicRoleImpl(this, assoc, pl); 111 } 112 113 @Override 114 TopicModelImpl getPlayer() { 115 if (topicIdentifiedByUri) { 116 return pl.fetchTopic("uri", new SimpleValue(topicUri)); 117 } else { 118 return pl.fetchTopic(playerId); 119 } 120 } 121}