1 19 20 package org.apache.cayenne.conf; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.apache.commons.collections.Transformer; 30 import org.apache.commons.collections.iterators.TransformIterator; 31 import org.apache.cayenne.access.DataDomain; 32 import org.apache.cayenne.access.DataNode; 33 import org.apache.cayenne.dba.AutoAdapter; 34 import org.apache.cayenne.dba.DbAdapter; 35 import org.apache.cayenne.map.DataMap; 36 import org.apache.cayenne.util.Util; 37 38 43 public class RuntimeSaveDelegate implements ConfigSaverDelegate { 44 45 protected Configuration config; 46 47 public RuntimeSaveDelegate(Configuration config) { 48 this.config = config; 49 } 50 51 54 public RuntimeSaveDelegate() { 55 super(); 56 } 57 58 protected DataDomain findDomain(String domainName) { 59 DataDomain domain = config.getDomain(domainName); 60 if (domain == null) { 61 throw new IllegalArgumentException ("Can't find DataDomain: " + domainName); 62 } 63 64 return domain; 65 } 66 67 protected DataNode findNode(String domainName, String nodeName) { 68 DataDomain domain = findDomain(domainName); 69 DataNode node = domain.getNode(nodeName); 70 if (node == null) { 71 throw new IllegalArgumentException ("Can't find DataNode: " 72 + domainName 73 + "." 74 + nodeName); 75 } 76 77 return node; 78 } 79 80 83 public String projectVersion() { 84 return config.getProjectVersion(); 85 } 86 87 public Iterator domainNames() { 88 Transformer tr = new Transformer() { 89 90 public Object transform(Object input) { 91 return ((DataDomain) input).getName(); 92 } 93 }; 94 return new TransformIterator(config.getDomains().iterator(), tr); 95 } 96 97 100 public Iterator viewNames() { 101 return config.getDataViewLocations().keySet().iterator(); 102 } 103 104 107 public String viewLocation(String dataViewName) { 108 return (String ) config.getDataViewLocations().get(dataViewName); 109 } 110 111 public Iterator propertyNames(String domainName) { 112 return findDomain(domainName).getProperties().keySet().iterator(); 113 } 114 115 public String propertyValue(String domainName, String propertyName) { 116 return (String ) findDomain(domainName).getProperties().get(propertyName); 117 } 118 119 public String mapLocation(String domainName, String mapName) { 120 return findDomain(domainName).getMap(mapName).getLocation(); 121 } 122 123 public Iterator mapNames(String domainName) { 124 125 List maps = new ArrayList (findDomain(domainName).getDataMaps()); 127 Collections.sort(maps, new Comparator () { 128 129 public int compare(Object o1, Object o2) { 130 String name1 = (o1 != null) ? ((DataMap) o1).getName() : null; 131 String name2 = (o1 != null) ? ((DataMap) o2).getName() : null; 132 return Util.nullSafeCompare(true, name1, name2); 133 } 134 }); 135 136 Transformer tr = new Transformer() { 137 138 public Object transform(Object input) { 139 return ((DataMap) input).getName(); 140 } 141 }; 142 return new TransformIterator(maps.iterator(), tr); 143 } 144 145 public String nodeAdapterName(String domainName, String nodeName) { 146 DbAdapter adapter = findNode(domainName, nodeName).getAdapter(); 147 return (adapter != null && adapter.getClass() != AutoAdapter.class) ? adapter 148 .getClass() 149 .getName() : null; 150 } 151 152 public String nodeDataSourceName(String domainName, String nodeName) { 153 return findNode(domainName, nodeName).getDataSourceLocation(); 154 } 155 156 public String nodeFactoryName(String domainName, String nodeName) { 157 return findNode(domainName, nodeName).getDataSourceFactory(); 158 } 159 160 public Iterator nodeNames(String domainName) { 161 Transformer tr = new Transformer() { 162 163 public Object transform(Object input) { 164 return ((DataNode) input).getName(); 165 } 166 }; 167 168 List nodes = new ArrayList (findDomain(domainName).getDataNodes()); 170 Collections.sort(nodes, new Comparator () { 171 172 public int compare(Object o1, Object o2) { 173 String name1 = (o1 != null) ? ((DataNode) o1).getName() : null; 174 String name2 = (o1 != null) ? ((DataNode) o2).getName() : null; 175 return Util.nullSafeCompare(true, name1, name2); 176 } 177 }); 178 179 return new TransformIterator(nodes.iterator(), tr); 180 } 181 182 public Iterator linkedMapNames(String domainName, String nodeName) { 183 Transformer tr = new Transformer() { 184 185 public Object transform(Object input) { 186 return ((DataMap) input).getName(); 187 } 188 }; 189 190 Collection maps = findNode(domainName, nodeName).getDataMaps(); 191 return new TransformIterator(maps.iterator(), tr); 192 } 193 } 194 | Popular Tags |