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