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 * Value: config topic 025 */ 026 private Map<String, TopicModelImpl> configTopics; 027 028 // ---------------------------------------------------------------------------------------------------- Constructors 029 030 /** 031 * @param configTopics must not be null 032 */ 033 ViewConfigurationModelImpl(Map<String, TopicModelImpl> configTopics) { 034 this.configTopics = configTopics; 035 } 036 037 // -------------------------------------------------------------------------------------------------- Public Methods 038 039 @Override 040 public Iterable<TopicModelImpl> getConfigTopics() { 041 return configTopics.values(); 042 } 043 044 @Override 045 public TopicModelImpl getConfigTopic(String configTypeUri) { 046 return configTopics.get(configTypeUri); 047 } 048 049 @Override 050 public void addConfigTopic(TopicModel configTopic) { 051 String configTypeUri = configTopic.getTypeUri(); 052 // error check 053 if (getConfigTopic(configTypeUri) != null) { 054 throw new RuntimeException("There is already a view configuration topic of type \"" + configTypeUri + "\""); 055 } 056 // 057 configTopics.put(configTypeUri, (TopicModelImpl) configTopic); 058 } 059 060 @Override 061 public void updateConfigTopic(TopicModel configTopic) { 062 String configTypeUri = configTopic.getTypeUri(); 063 TopicModel confTopic = getConfigTopic(configTypeUri); 064 // error check 065 if (confTopic == null) { 066 throw new RuntimeException("There is no view configuration topic of type \"" + configTypeUri + "\""); 067 } 068 // 069 confTopic.set(configTopic); 070 } 071 072 // --- 073 074 @Override 075 public Object getConfigValue(String configTypeUri, String childTypeUri) { 076 TopicModel configTopic = getConfigTopic(configTypeUri); 077 if (configTopic == null) { 078 return null; 079 } 080 return configTopic.getChildTopicsModel().getObject(childTypeUri, null); 081 } 082 083 // --- 084 085 @Override 086 public JSONArray toJSONArray() { 087 try { 088 JSONArray configTopics = new JSONArray(); 089 for (TopicModel configTopic : getConfigTopics()) { 090 configTopics.put(configTopic.toJSON()); 091 } 092 return configTopics; 093 } catch (Exception e) { 094 throw new RuntimeException("Serialization failed (" + this + ")", e); 095 } 096 } 097 098 @Override 099 public String toString() { 100 return "view configuration " + configTopics; 101 } 102}