001package systems.dmx.core.impl;
002
003import systems.dmx.core.Topic;
004import systems.dmx.core.ViewConfiguration;
005import systems.dmx.core.model.ChildTopicsModel;
006import systems.dmx.core.model.RoleModel;
007import systems.dmx.core.model.TopicModel;
008import systems.dmx.core.model.ViewConfigurationModel;
009
010
011
012/**
013 * A view configuration that is attached to the {@link PersistenceLayer}.
014 */
015class ViewConfigurationImpl implements ViewConfiguration {
016
017    // ---------------------------------------------------------------------------------------------- Instance Variables
018
019    /**
020     * The underlying model.
021     */
022    private ViewConfigurationModelImpl model;
023
024    /**
025     * A role that points to the object this view configuration applies to.
026     * This is either a type (topic role) or an association definition (association role).
027     */
028    private RoleModel configurable;
029
030    private PersistenceLayer pl;
031    private ModelFactoryImpl mf;
032
033    // ---------------------------------------------------------------------------------------------------- Constructors
034
035    ViewConfigurationImpl(RoleModel configurable, ViewConfigurationModelImpl model, PersistenceLayer pl) {
036        this.configurable = configurable;
037        this.model = model;
038        this.pl = pl;
039        this.mf = pl.mf;
040    }
041
042    // -------------------------------------------------------------------------------------------------- Public Methods
043
044
045
046    // === ViewConfiguration Implementation ===
047
048    @Override
049    public Iterable<Topic> getConfigTopics() {
050        return pl.instantiate(model.getConfigTopics());
051    }
052
053    @Override
054    public Topic getConfigTopic(String configTypeUri) {
055        TopicModelImpl configTopic = model.getConfigTopic(configTypeUri);
056        return configTopic != null ? configTopic.instantiate() : null;
057    }
058
059    @Override
060    public Topic addConfigTopic(TopicModel configTopic) {
061        TopicModelImpl _configTopic = (TopicModelImpl) configTopic;
062        _addConfigTopic(_configTopic);                      // update memory + DB
063        return _configTopic.instantiate();
064    }
065
066    @Override
067    public ViewConfiguration setConfigValue(String configTypeUri, String childTypeUri, Object value) {
068        _setConfigValue(configTypeUri, mf.newChildTopicsModel()
069            .put(childTypeUri, value));
070        return this;
071    }
072
073    @Override
074    public ViewConfiguration setConfigValueRef(String configTypeUri, String childTypeUri, Object topicIdOrUri) {
075        _setConfigValue(configTypeUri, mf.newChildTopicsModel()
076            .put(childTypeUri, mf.newTopicReferenceModel(topicIdOrUri)));
077        return this;
078    }
079
080    // ---
081
082    @Override
083    public ViewConfigurationModel getModel() {
084        return model;
085    }
086
087    // ------------------------------------------------------------------------------------------------- Private Methods
088
089    private void _setConfigValue(String configTypeUri, ChildTopicsModel childs) {
090        TopicModelImpl configTopic = model.getConfigTopic(configTypeUri);
091        if (configTopic == null) {
092            configTopic = mf.newTopicModel(configTypeUri, childs);
093            _addConfigTopic(configTopic);               // update memory + DB
094        } else {
095            configTopic.updateChildTopics(childs);      // update memory + DB
096        }
097    }
098
099    private void _addConfigTopic(TopicModelImpl configTopic) {
100        model.addConfigTopic(configTopic);                                  // update memory
101        pl.typeStorage.storeViewConfigTopic(configurable, configTopic);     // update DB
102    }
103}