001package systems.dmx.core.impl; 002 003import systems.dmx.core.model.RelatedTopicModel; 004import systems.dmx.core.model.TopicModel; 005import systems.dmx.core.model.ViewConfigurationModel; 006 007import org.codehaus.jettison.json.JSONArray; 008 009import java.util.Map; 010import java.util.logging.Logger; 011 012 013 014class ViewConfigurationModelImpl implements ViewConfigurationModel { 015 016 // ---------------------------------------------------------------------------------------------- Instance Variables 017 018 /** 019 * Key: config topic type URI 020 * Value: config topic 021 */ 022 private Map<String, TopicModelImpl> configTopics; 023 024 private ModelFactoryImpl mf; 025 026 // ---------------------------------------------------------------------------------------------------- Constructors 027 028 /** 029 * @param configTopics must not be null 030 */ 031 ViewConfigurationModelImpl(Map<String, TopicModelImpl> configTopics, PersistenceLayer pl) { 032 this.configTopics = configTopics; 033 this.mf = pl.mf; 034 } 035 036 // -------------------------------------------------------------------------------------------------- Public Methods 037 038 @Override 039 public Iterable<TopicModelImpl> getConfigTopics() { 040 return configTopics.values(); 041 } 042 043 @Override 044 public TopicModelImpl getConfigTopic(String configTypeUri) { 045 return configTopics.get(configTypeUri); 046 } 047 048 @Override 049 public void addConfigTopic(TopicModel configTopic) { 050 String configTypeUri = configTopic.getTypeUri(); 051 // error check 052 if (getConfigTopic(configTypeUri) != null) { 053 throw new RuntimeException("There is already a view configuration topic of type \"" + configTypeUri + "\""); 054 } 055 // 056 configTopics.put(configTypeUri, (TopicModelImpl) configTopic); 057 } 058 059 @Override 060 public ViewConfigurationModel setConfigValue(String configTypeUri, String childTypeUri, Object value) { 061 TopicModel configTopic = getConfigTopic(configTypeUri); 062 if (configTopic == null) { 063 addConfigTopic(mf.newTopicModel(configTypeUri, mf.newChildTopicsModel().put(childTypeUri, value))); 064 } else { 065 configTopic.getChildTopicsModel().put(childTypeUri, value); 066 } 067 return this; 068 } 069 070 @Override 071 public ViewConfigurationModel setConfigValueRef(String configTypeUri, String childTypeUri, Object topicIdOrUri) { 072 TopicModel configTopic = getConfigTopic(configTypeUri); 073 RelatedTopicModel valueRef = mf.newTopicReferenceModel(topicIdOrUri); 074 if (configTopic == null) { 075 // Note: valueRef must be known to be of type RelatedTopicModel *at compile time*. 076 // Otherwise the wrong put() method would be invoked. 077 // In Java method overloading involves NO dynamic dispatch. See JavaAPITest in dmx-test. 078 addConfigTopic(mf.newTopicModel(configTypeUri, mf.newChildTopicsModel().put(childTypeUri, valueRef))); 079 } else { 080 configTopic.getChildTopicsModel().put(childTypeUri, valueRef); 081 } 082 return this; 083 } 084 085 @Override 086 public void updateConfigTopic(TopicModel configTopic) { 087 String configTypeUri = configTopic.getTypeUri(); 088 TopicModel confTopic = getConfigTopic(configTypeUri); 089 // error check 090 if (confTopic == null) { 091 throw new RuntimeException("There is no view configuration topic of type \"" + configTypeUri + "\""); 092 } 093 // 094 confTopic.set(configTopic); 095 } 096 097 // --- 098 099 @Override 100 public Object getConfigValue(String configTypeUri, String childTypeUri) { 101 TopicModel configTopic = getConfigTopic(configTypeUri); 102 if (configTopic == null) { 103 return null; 104 } 105 return configTopic.getChildTopicsModel().getObject(childTypeUri, null); 106 } 107 108 // --- 109 110 @Override 111 public JSONArray toJSONArray() { 112 try { 113 JSONArray configTopics = new JSONArray(); 114 for (TopicModel configTopic : getConfigTopics()) { 115 configTopics.put(configTopic.toJSON()); 116 } 117 return configTopics; 118 } catch (Exception e) { 119 throw new RuntimeException("Serialization failed", e); 120 } 121 } 122 123 @Override 124 public String toString() { 125 return "view configuration " + configTopics; 126 } 127}