001package de.deepamehta.core.impl;
002
003import de.deepamehta.core.Association;
004import de.deepamehta.core.RelatedAssociation;
005import de.deepamehta.core.RelatedTopic;
006import de.deepamehta.core.Topic;
007import de.deepamehta.core.model.TopicModel;
008
009import java.util.List;
010import java.util.logging.Logger;
011
012
013
014/**
015 * A topic that is attached to the {@link CoreService}.
016 */
017class TopicImpl extends DeepaMehtaObjectImpl implements Topic {
018
019    // ---------------------------------------------------------------------------------------------- Instance Variables
020
021    private final Logger logger = Logger.getLogger(getClass().getName());
022
023    // ---------------------------------------------------------------------------------------------------- Constructors
024
025    TopicImpl(TopicModelImpl model, PersistenceLayer pl) {
026        super(model, pl);
027    }
028
029    // -------------------------------------------------------------------------------------------------- Public Methods
030
031
032
033    // ****************************
034    // *** Topic Implementation ***
035    // ****************************
036
037
038
039    @Override
040    public final void update(TopicModel updateModel) {
041        pl.updateTopic(getModel(), (TopicModelImpl) updateModel);
042    }
043
044    @Override
045    public final void delete() {
046        pl.deleteTopic(getModel());
047    }
048
049    // ---
050
051    @Override
052    public final Topic findChildTopic(String topicTypeUri) {
053        TopicModelImpl topic = getModel().findChildTopic(topicTypeUri);
054        return topic != null ? topic.instantiate() : null;
055    }
056
057    // ---
058
059    @Override
060    public final Topic loadChildTopics() {
061        model.loadChildTopics();
062        return this;
063    }
064
065    @Override
066    public final Topic loadChildTopics(String assocDefUri) {
067        model.loadChildTopics(assocDefUri);
068        return this;
069    }
070
071    // ---
072
073    // Note: overridden by DeepaMehtaTypeImpl
074    @Override
075    public TopicModelImpl getModel() {
076        return (TopicModelImpl) model;
077    }
078
079
080
081    // ***************************************
082    // *** DeepaMehtaObject Implementation ***
083    // ***************************************
084
085
086
087    // === Traversal ===
088
089    // ### TODO: move logic to model
090
091    // --- Topic Retrieval ---
092
093    @Override
094    public final List<RelatedTopic> getRelatedTopics(List assocTypeUris, String myRoleTypeUri,
095                                                     String othersRoleTypeUri, String othersTopicTypeUri) {
096        List<RelatedTopicModelImpl> topics = pl.fetchTopicRelatedTopics(getId(), assocTypeUris, myRoleTypeUri,
097            othersRoleTypeUri, othersTopicTypeUri);
098        return pl.checkReadAccessAndInstantiate(topics);
099    }
100
101    // --- Association Retrieval ---
102
103    @Override
104    public final RelatedAssociation getRelatedAssociation(String assocTypeUri, String myRoleTypeUri,
105                                                          String othersRoleTypeUri, String othersAssocTypeUri) {
106        RelatedAssociationModelImpl assoc = pl.fetchTopicRelatedAssociation(getId(),
107            assocTypeUri, myRoleTypeUri, othersRoleTypeUri, othersAssocTypeUri);
108        return assoc != null ? pl.<RelatedAssociation>checkReadAccessAndInstantiate(assoc) : null;
109    }
110
111    @Override
112    public final List<RelatedAssociation> getRelatedAssociations(String assocTypeUri, String myRoleTypeUri,
113                                                                 String othersRoleTypeUri, String othersAssocTypeUri) {
114        List<RelatedAssociationModelImpl> assocs = pl.fetchTopicRelatedAssociations(getId(), assocTypeUri,
115            myRoleTypeUri, othersRoleTypeUri, othersAssocTypeUri);
116        return pl.checkReadAccessAndInstantiate(assocs);
117    }
118
119    // ---
120
121    @Override
122    public final Association getAssociation(String assocTypeUri, String myRoleTypeUri, String othersRoleTypeUri,
123                                                                                       long othersTopicId) {
124        AssociationModelImpl assoc = pl.fetchAssociation(assocTypeUri, getId(), othersTopicId, myRoleTypeUri,
125            othersRoleTypeUri);
126        return assoc != null ? pl.<Association>checkReadAccessAndInstantiate(assoc) : null;
127    }
128
129    @Override
130    public final List<Association> getAssociations() {
131        return pl.checkReadAccessAndInstantiate(pl.fetchTopicAssociations(getId()));
132    }
133}