001package de.deepamehta.core.impl; 002 003import de.deepamehta.core.DeepaMehtaObject; 004import de.deepamehta.core.Topic; 005import de.deepamehta.core.model.ChildTopicsModel; 006import de.deepamehta.core.model.TopicModel; 007import de.deepamehta.core.model.ViewConfigurationModel; 008 009import org.codehaus.jettison.json.JSONArray; 010 011import java.util.ArrayList; 012import java.util.Collection; 013import java.util.Map; 014import java.util.logging.Logger; 015 016 017 018class ViewConfigurationModelImpl implements ViewConfigurationModel { 019 020 // ---------------------------------------------------------------------------------------------- Instance Variables 021 022 /** 023 * Key: config topic type URI 024 */ 025 private Map<String, TopicModelImpl> configTopics; 026 027 // ---------------------------------------------------------------------------------------------------- Constructors 028 029 ViewConfigurationModelImpl(Map<String, TopicModelImpl> configTopics) { 030 this.configTopics = configTopics; 031 } 032 033 // -------------------------------------------------------------------------------------------------- Public Methods 034 035 @Override 036 public Iterable<TopicModelImpl> getConfigTopics() { 037 return configTopics.values(); 038 } 039 040 @Override 041 public TopicModelImpl getConfigTopic(String configTypeUri) { 042 return configTopics.get(configTypeUri); 043 } 044 045 @Override 046 public void addConfigTopic(TopicModel configTopic) { 047 String configTypeUri = configTopic.getTypeUri(); 048 // error check 049 if (getConfigTopic(configTypeUri) != null) { 050 throw new RuntimeException("There is already a view configuration topic of type \"" + configTypeUri + "\""); 051 } 052 // 053 configTopics.put(configTypeUri, (TopicModelImpl) configTopic); 054 } 055 056 @Override 057 public void updateConfigTopic(TopicModel configTopic) { 058 String configTypeUri = configTopic.getTypeUri(); 059 TopicModel confTopic = getConfigTopic(configTypeUri); 060 // error check 061 if (confTopic == null) { 062 throw new RuntimeException("There is no view configuration topic of type \"" + configTypeUri + "\""); 063 } 064 // 065 confTopic.set(configTopic); 066 } 067 068 // --- 069 070 @Override 071 public Object getSetting(String configTypeUri, String settingUri) { 072 TopicModel configTopic = getConfigTopic(configTypeUri); 073 if (configTopic == null) { 074 return null; 075 } 076 return configTopic.getChildTopicsModel().getObject(settingUri, null); 077 } 078 079 // --- 080 081 @Override 082 public JSONArray toJSONArray() { 083 try { 084 JSONArray configTopics = new JSONArray(); 085 for (TopicModel configTopic : getConfigTopics()) { 086 configTopics.put(configTopic.toJSON()); 087 } 088 return configTopics; 089 } catch (Exception e) { 090 throw new RuntimeException("Serialization failed (" + this + ")", e); 091 } 092 } 093 094 @Override 095 public String toString() { 096 return "view configuration " + configTopics; 097 } 098}