1 56 package org.objectstyle.cayenne.conf; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 63 import org.apache.commons.collections.Transformer; 64 import org.apache.commons.collections.iterators.TransformIterator; 65 import org.objectstyle.cayenne.access.DataDomain; 66 import org.objectstyle.cayenne.access.DataNode; 67 import org.objectstyle.cayenne.dba.DbAdapter; 68 import org.objectstyle.cayenne.map.DataMap; 69 70 75 public class RuntimeSaveDelegate implements ConfigSaverDelegate { 76 protected Configuration config; 77 78 public RuntimeSaveDelegate(Configuration config) { 79 this.config = config; 80 } 81 82 85 public RuntimeSaveDelegate() { 86 super(); 87 } 88 89 protected DataDomain findDomain(String domainName) { 90 DataDomain domain = config.getDomain(domainName); 91 if (domain == null) { 92 throw new IllegalArgumentException ("Can't find DataDomain: " + domainName); 93 } 94 95 return domain; 96 } 97 98 protected DataNode findNode(String domainName, String nodeName) { 99 DataDomain domain = findDomain(domainName); 100 DataNode node = domain.getNode(nodeName); 101 if (node == null) { 102 throw new IllegalArgumentException ( 103 "Can't find DataNode: " + domainName + "." + nodeName); 104 } 105 106 return node; 107 } 108 109 112 public String projectVersion() { 113 return config.getProjectVersion(); 114 } 115 116 public Iterator domainNames() { 117 Transformer tr = new Transformer() { 118 public Object transform(Object input) { 119 return ((DataDomain) input).getName(); 120 } 121 }; 122 return new TransformIterator(config.getDomains().iterator(), tr); 123 } 124 125 128 public Iterator viewNames() { 129 return config.getDataViewLocations().keySet().iterator(); 130 } 131 132 135 public String viewLocation(String dataViewName) { 136 return (String ) config.getDataViewLocations().get(dataViewName); 137 } 138 139 public Iterator propertyNames(String domainName) { 140 return findDomain(domainName).getProperties().keySet().iterator(); 141 } 142 143 public String propertyValue(String domainName, String propertyName) { 144 return (String ) findDomain(domainName).getProperties().get(propertyName); 145 } 146 147 public String mapLocation(String domainName, String mapName) { 148 return findDomain(domainName).getMap(mapName).getLocation(); 149 } 150 151 public Iterator mapNames(String domainName) { 152 Transformer tr = new Transformer() { 153 public Object transform(Object input) { 154 return ((DataMap) input).getName(); 155 } 156 }; 157 158 List maps = new ArrayList (findDomain(domainName).getDataMaps()); 159 return new TransformIterator(maps.iterator(), tr); 160 } 161 162 public String nodeAdapterName(String domainName, String nodeName) { 163 DbAdapter adapter = findNode(domainName, nodeName).getAdapter(); 164 return (adapter != null) ? adapter.getClass().getName() : null; 165 } 166 167 public String nodeDataSourceName(String domainName, String nodeName) { 168 return findNode(domainName, nodeName).getDataSourceLocation(); 169 } 170 171 public String nodeFactoryName(String domainName, String nodeName) { 172 return findNode(domainName, nodeName).getDataSourceFactory(); 173 } 174 175 public Iterator nodeNames(String domainName) { 176 Transformer tr = new Transformer() { 177 public Object transform(Object input) { 178 return ((DataNode) input).getName(); 179 } 180 }; 181 182 Collection nodes = findDomain(domainName).getDataNodes(); 183 return new TransformIterator(nodes.iterator(), tr); 184 } 185 186 public Iterator linkedMapNames(String domainName, String nodeName) { 187 Transformer tr = new Transformer() { 188 public Object transform(Object input) { 189 return ((DataMap) input).getName(); 190 } 191 }; 192 193 Collection maps = findNode(domainName, nodeName).getDataMaps(); 194 return new TransformIterator(maps.iterator(), tr); 195 } 196 } 197 | Popular Tags |