001package de.deepamehta.core.impl;
002
003import de.deepamehta.core.Topic;
004import de.deepamehta.core.ViewConfiguration;
005import de.deepamehta.core.model.ChildTopicsModel;
006import de.deepamehta.core.model.RoleModel;
007import de.deepamehta.core.model.TopicModel;
008import de.deepamehta.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 void addSetting(String configTypeUri, String settingUri, Object value) {
055        ChildTopicsModel childs = mf.newChildTopicsModel().put(settingUri, value);
056        TopicModelImpl configTopic = model.getConfigTopic(configTypeUri);
057        if (configTopic == null) {
058            configTopic = mf.newTopicModel(configTypeUri, childs);
059            model.addConfigTopic(configTopic);                                  // update memory
060            pl.typeStorage.storeViewConfigTopic(configurable, configTopic);     // update DB
061        } else {
062            configTopic.updateChildTopics(childs);                              // update memory + DB
063        }
064    }
065
066    @Override
067    public void updateConfigTopic(TopicModel configTopic) {
068        model.updateConfigTopic(configTopic);
069    }
070
071    // ---
072
073    @Override
074    public ViewConfigurationModel getModel() {
075        return model;
076    }
077}