001    package de.deepamehta.core.model;
002    
003    import org.codehaus.jettison.json.JSONObject;
004    import org.codehaus.jettison.json.JSONArray;
005    
006    import java.util.ArrayList;
007    import java.util.HashMap;
008    import java.util.List;
009    import java.util.Map;
010    import java.util.logging.Logger;
011    
012    
013    
014    /**
015     * @author <a href="mailto:jri@deepamehta.de">Jörg Richter</a>
016     */
017    public class ViewConfigurationModel {
018    
019        // ---------------------------------------------------------------------------------------------- Instance Variables
020    
021        /**
022         * Key: config topic type URI
023         */
024        private Map<String, TopicModel> viewConfig = new HashMap();
025    
026        // ---------------------------------------------------------------------------------------------------- Constructors
027    
028        public ViewConfigurationModel() {
029        }
030    
031        public ViewConfigurationModel(List<TopicModel> configTopics) {
032            for (TopicModel topic : configTopics) {
033                addConfigTopic(topic);
034            }
035        }
036    
037        /**
038         * @param   configurable    A topic type, an association type, or an association definition.
039         *                          ### FIXME: the sole JSONArray should be passed
040         */
041        public ViewConfigurationModel(JSONObject configurable) {
042            try {
043                JSONArray topics = configurable.optJSONArray("view_config_topics");
044                if (topics != null) {
045                    for (int i = 0; i < topics.length(); i++) {
046                        addConfigTopic(new TopicModel(topics.getJSONObject(i)));
047                    }
048                }
049            } catch (Exception e) {
050                throw new RuntimeException("Parsing ViewConfigurationModel failed (JSONObject=" + configurable + ")", e);
051            }
052        }
053    
054        // -------------------------------------------------------------------------------------------------- Public Methods
055    
056        public Iterable<TopicModel> getConfigTopics() {
057            return viewConfig.values();
058        }
059    
060        public void addConfigTopic(TopicModel configTopic) {
061            String configTypeUri = configTopic.getTypeUri();
062            // error check
063            if (getConfigTopic(configTypeUri) != null) {
064                throw new RuntimeException("There is already a view configuration topic of type \"" + configTypeUri + "\"");
065            }
066            //
067            viewConfig.put(configTypeUri, configTopic);
068        }
069    
070        public void updateConfigTopic(TopicModel configTopic) {
071            String configTypeUri = configTopic.getTypeUri();
072            TopicModel confTopic = getConfigTopic(configTypeUri);
073            // error check
074            if (confTopic == null) {
075                throw new RuntimeException("There is no view configuration topic of type \"" + configTypeUri + "\"");
076            }
077            //
078            confTopic.set(configTopic);
079        }
080    
081        // ---
082    
083        /**
084         * FIXME: to be dropped.
085         * <p>
086         * Read out a view configuration setting.
087         * <p>
088         * Compare to client-side counterpart: function get_view_config() in webclient.js
089         *
090         * @param   configTypeUri   The type URI of the configuration topic, e.g. "dm4.webclient.view_config"
091         * @param   settingUri      The setting URI, e.g. "dm4.webclient.icon"
092         *
093         * @return  The setting value, or <code>null</code> if there is no such setting
094         */
095        public Object getSetting(String configTypeUri, String settingUri) {
096            TopicModel configTopic = getConfigTopic(configTypeUri);
097            if (configTopic == null) {
098                return null;
099            }
100            ChildTopicsModel childTopics = configTopic.getChildTopicsModel();
101            return childTopics.has(settingUri) ? childTopics.getObject(settingUri) : null;
102        }
103    
104        // ---
105    
106        // ### FIXME: drop parameter, implement JSONEnabled
107        public void toJSON(JSONObject configurable) {
108            try {
109                List viewConfigTopics = new ArrayList();
110                for (TopicModel configTopic : getConfigTopics()) {
111                    viewConfigTopics.add(configTopic.toJSON());
112                }
113                configurable.put("view_config_topics", viewConfigTopics);
114            } catch (Exception e) {
115                throw new RuntimeException("Serialization failed (" + this + ")", e);
116            }
117        }
118    
119        @Override
120        public String toString() {
121            return "view configuration " + viewConfig;
122        }
123    
124        // ------------------------------------------------------------------------------------------------- Private Methods
125    
126        private TopicModel getConfigTopic(String configTypeUri) {
127            return viewConfig.get(configTypeUri);
128        }
129    }