001package systems.dmx.core.impl; 002 003import systems.dmx.core.Association; 004import systems.dmx.core.DMXObject; 005import systems.dmx.core.RelatedAssociation; 006import systems.dmx.core.RelatedObject; 007import systems.dmx.core.RelatedTopic; 008import systems.dmx.core.Role; 009import systems.dmx.core.Topic; 010import systems.dmx.core.TopicRole; 011import systems.dmx.core.model.AssociationModel; 012import systems.dmx.core.model.RoleModel; 013import systems.dmx.core.model.TopicRoleModel; 014 015import java.util.List; 016 017import java.util.logging.Logger; 018 019 020 021/** 022 * An association model that is attached to the DB. 023 */ 024class AssociationImpl extends DMXObjectImpl implements Association { 025 026 // ---------------------------------------------------------------------------------------------- Instance Variables 027 028 private final Logger logger = Logger.getLogger(getClass().getName()); 029 030 // ---------------------------------------------------------------------------------------------------- Constructors 031 032 AssociationImpl(AssociationModelImpl model, PersistenceLayer pl) { 033 super(model, pl); 034 } 035 036 // -------------------------------------------------------------------------------------------------- Public Methods 037 038 039 040 // ********************************** 041 // *** Association Implementation *** 042 // ********************************** 043 044 045 046 @Override 047 public final Role getRole1() { 048 return getModel().getRoleModel1().instantiate(getModel()); 049 } 050 051 @Override 052 public final Role getRole2() { 053 return getModel().getRoleModel2().instantiate(getModel()); 054 } 055 056 // --- 057 058 @Override 059 public final DMXObject getPlayer1() { 060 return getRole1().getPlayer(); 061 } 062 063 @Override 064 public final DMXObject getPlayer2() { 065 return getRole2().getPlayer(); 066 } 067 068 // --- Convenience Methods --- 069 070 @Override 071 public final Role getRole(String roleTypeUri) { 072 return getModel().getRoleModel(roleTypeUri).instantiate(getModel()); 073 } 074 075 @Override 076 public final boolean hasSameRoleTypeUris() { 077 return getModel().hasSameRoleTypeUris(); 078 } 079 080 @Override 081 public final boolean matches(String roleTypeUri1, long playerId1, String roleTypeUri2, long playerId2) { 082 return getModel().matches(roleTypeUri1, playerId1, roleTypeUri2, playerId2); 083 } 084 085 @Override 086 public final long getOtherPlayerId(long id) { 087 return getModel().getOtherPlayerId(id); 088 } 089 090 // --- 091 092 @Override 093 public final RelatedObject getPlayer(String roleTypeUri) { 094 DMXObjectModelImpl object = getModel().getPlayer(roleTypeUri); 095 return object != null ? (RelatedObject) object.instantiate() : null; // ### TODO: permission check? 096 } 097 098 @Override 099 public final Topic getTopicByType(String topicTypeUri) { 100 TopicModelImpl topic = getModel().getTopicByType(topicTypeUri); 101 return topic != null ? topic.instantiate() : null; // ### TODO: permission check? 102 } 103 104 // --- 105 106 // ### TODO: make use of model's getRole() 107 @Override 108 public final Role getRole(RoleModel roleModel) { 109 if (getRole1().getModel().refsSameObject(roleModel)) { 110 return getRole1(); 111 } else if (getRole2().getModel().refsSameObject(roleModel)) { 112 return getRole2(); 113 } 114 throw new RuntimeException("Role is not part of association (role=" + roleModel + ", association=" + this); 115 } 116 117 @Override 118 public final boolean isPlayer(TopicRoleModel roleModel) { 119 return filterRole(getRole1(), roleModel) != null || filterRole(getRole2(), roleModel) != null; 120 } 121 122 // --- 123 124 @Override 125 public final void update(AssociationModel updateModel) { 126 pl.updateAssociation(getModel(), (AssociationModelImpl) updateModel); 127 } 128 129 @Override 130 public final void delete() { 131 pl.deleteAssociation(getModel()); 132 } 133 134 // --- 135 136 @Override 137 public final Association loadChildTopics() { 138 super.loadChildTopics(); 139 return this; 140 } 141 142 @Override 143 public final Association loadChildTopics(String assocDefUri) { 144 super.loadChildTopics(assocDefUri); 145 return this; 146 } 147 148 // --- 149 150 // Note: overridden by RelatedAssociationImpl 151 @Override 152 public AssociationModelImpl getModel() { 153 return (AssociationModelImpl) model; 154 } 155 156 157 158 // *************************************** 159 // *** DMXObject Implementation *** 160 // *************************************** 161 162 163 164 // === Traversal === 165 166 // ### TODO: consider adding model convenience, would require model renamings (get -> fetch) 167 168 // --- Topic Retrieval --- 169 170 @Override 171 public final List<RelatedTopic> getRelatedTopics(List assocTypeUris, String myRoleTypeUri, 172 String othersRoleTypeUri, String othersTopicTypeUri) { 173 return pl.instantiate(pl.getAssociationRelatedTopics(getId(), assocTypeUris, myRoleTypeUri, othersRoleTypeUri, 174 othersTopicTypeUri)); 175 } 176 177 // --- Association Retrieval --- 178 179 @Override 180 public final RelatedAssociation getRelatedAssociation(String assocTypeUri, String myRoleTypeUri, 181 String othersRoleTypeUri, String othersAssocTypeUri) { 182 RelatedAssociationModelImpl assoc = pl.getAssociationRelatedAssociation(getId(), assocTypeUri, myRoleTypeUri, 183 othersRoleTypeUri, othersAssocTypeUri); 184 return assoc != null ? assoc.instantiate() : null; 185 } 186 187 @Override 188 public final List<RelatedAssociation> getRelatedAssociations(String assocTypeUri, String myRoleTypeUri, 189 String othersRoleTypeUri, String othersAssocTypeUri) { 190 return pl.instantiate(pl.getAssociationRelatedAssociations(getId(), assocTypeUri, myRoleTypeUri, 191 othersRoleTypeUri, othersAssocTypeUri)); 192 } 193 194 // --- 195 196 @Override 197 public final Association getAssociation(String assocTypeUri, String myRoleTypeUri, String othersRoleTypeUri, 198 long othersTopicId) { 199 return pl.getAssociationBetweenTopicAndAssociation(assocTypeUri, othersTopicId, getId(), othersRoleTypeUri, 200 myRoleTypeUri); 201 } 202 203 @Override 204 public final List<Association> getAssociations() { 205 return pl.instantiate(pl.getAssociationAssociations(getId())); 206 } 207 208 209 210 // ------------------------------------------------------------------------------------------------- Private Methods 211 212 // --- Helper --- 213 214 // ### TODO: move to model 215 private final TopicRole filterRole(Role role, TopicRoleModel roleModel) { 216 return role instanceof TopicRole && role.getRoleTypeUri().equals(roleModel.getRoleTypeUri()) && 217 role.getPlayerId() == roleModel.getPlayerId() ? (TopicRole) role : null; 218 } 219}