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    // ---
068
069    @Override
070    public Topic getTopic(String roleTypeUri) {
071        TopicModelImpl topic = getModel().getTopic(roleTypeUri);
072        return topic != null ? topic.instantiate() : null;    // ### TODO: permission check?
073    }
074
075    @Override
076    public Topic getTopicByType(String topicTypeUri) {
077        TopicModelImpl topic = getModel().getTopicByType(topicTypeUri);
078        return topic != null ? topic.instantiate() : null;    // ### TODO: permission check?
079    }
080
081    // ---
082
083    // ### TODO: make use of model's getRole()
084    @Override
085    public Role getRole(RoleModel roleModel) {
086        if (getRole1().getModel().refsSameObject(roleModel)) {
087            return getRole1();
088        } else if (getRole2().getModel().refsSameObject(roleModel)) {
089            return getRole2();
090        }
091        throw new RuntimeException("Role is not part of association (role=" + roleModel + ", association=" + this);
092    }
093
094    @Override
095    public boolean isPlayer(TopicRoleModel roleModel) {
096        return filterRole(getRole1(), roleModel) != null || filterRole(getRole2(), roleModel) != null;
097    }
098
099    // ---
100
101    @Override
102    public void update(AssociationModel updateModel) {
103        model.update((AssociationModelImpl) updateModel);     // ### FIXME: call through pl for access control
104    }
105
106    // ---
107
108    @Override
109    public Association loadChildTopics() {
110        model.loadChildTopics();
111        return this;
112    }
113
114    @Override
115    public Association loadChildTopics(String assocDefUri) {
116        model.loadChildTopics(assocDefUri);
117        return this;
118    }
119
120    // ---
121
122    @Override
123    public AssociationModelImpl getModel() {
124        return (AssociationModelImpl) model;
125    }
126
127
128
129    // ***************************************
130    // *** DeepaMehtaObject Implementation ***
131    // ***************************************
132
133
134
135    // === Traversal ===
136
137    // ### TODO: move logic to model
138
139    // --- Topic Retrieval ---
140
141    @Override
142    public List<RelatedTopic> getRelatedTopics(List assocTypeUris, String myRoleTypeUri, String othersRoleTypeUri,
143                                                                                         String othersTopicTypeUri) {
144        List<RelatedTopicModelImpl> topics = pl.fetchAssociationRelatedTopics(getId(), assocTypeUris, myRoleTypeUri,
145            othersRoleTypeUri, othersTopicTypeUri);
146        return pl.checkReadAccessAndInstantiate(topics);
147    }
148
149    // --- Association Retrieval ---
150
151    @Override
152    public RelatedAssociation getRelatedAssociation(String assocTypeUri, String myRoleTypeUri,
153                                                    String othersRoleTypeUri, String othersAssocTypeUri) {
154        RelatedAssociationModelImpl assoc = pl.fetchAssociationRelatedAssociation(getId(),
155            assocTypeUri, myRoleTypeUri, othersRoleTypeUri, othersAssocTypeUri);
156        return assoc != null ? pl.<RelatedAssociation>checkReadAccessAndInstantiate(assoc) : null;
157    }
158
159    @Override
160    public List<RelatedAssociation> getRelatedAssociations(String assocTypeUri, String myRoleTypeUri,
161                                                           String othersRoleTypeUri, String othersAssocTypeUri) {
162        List<RelatedAssociationModelImpl> assocs = pl.fetchAssociationRelatedAssociations(getId(), assocTypeUri,
163            myRoleTypeUri, othersRoleTypeUri, othersAssocTypeUri);
164        return pl.checkReadAccessAndInstantiate(assocs);
165    }
166
167    // ---
168
169    @Override
170    public Association getAssociation(String assocTypeUri, String myRoleTypeUri, String othersRoleTypeUri,
171                                                                                 long othersTopicId) {
172        AssociationModelImpl assoc = pl.fetchAssociationBetweenTopicAndAssociation(assocTypeUri,
173            othersTopicId, getId(), othersRoleTypeUri, myRoleTypeUri);
174        return assoc != null ? pl.<Association>checkReadAccessAndInstantiate(assoc) : null;
175    }
176
177    @Override
178    public List<Association> getAssociations() {
179        return pl.checkReadAccessAndInstantiate(pl.fetchAssociationAssociations(getId()));
180    }
181
182
183
184    // ------------------------------------------------------------------------------------------------- Private Methods
185
186    // --- Helper ---
187
188    // ### TODO: move to model
189    private TopicRole filterRole(Role role, TopicRoleModel roleModel) {
190        return role instanceof TopicRole && role.getRoleTypeUri().equals(roleModel.getRoleTypeUri()) &&
191            role.getPlayerId() == roleModel.getPlayerId() ? (TopicRole) role : null;
192    }
193}