1 56 package org.objectstyle.cayenne.tools; 57 58 import java.io.File ; 59 import java.util.Collection ; 60 import java.util.HashSet ; 61 import java.util.Iterator ; 62 63 import org.apache.tools.ant.BuildException; 64 import org.apache.tools.ant.Project; 65 import org.objectstyle.cayenne.access.DataDomain; 66 import org.objectstyle.cayenne.access.DataNode; 67 import org.objectstyle.cayenne.access.DataPort; 68 import org.objectstyle.cayenne.conf.Configuration; 69 import org.objectstyle.cayenne.conf.FileConfiguration; 70 import org.objectstyle.cayenne.map.DataMap; 71 import org.objectstyle.cayenne.util.Util; 72 73 80 public class DataPortTask extends CayenneTask { 81 82 protected File projectFile; 83 protected String maps; 84 protected String srcNode; 85 protected String destNode; 86 protected String includeTables; 87 protected String excludeTables; 88 protected boolean cleanDest = true; 89 90 public DataPortTask() { 91 this.cleanDest = true; 93 } 94 95 public void execute() throws BuildException { 96 configureLogging(); 97 validateParameters(); 98 99 FileConfiguration configuration = new FileConfiguration(projectFile); 100 101 configuration.setClassLoader(getClass().getClassLoader()); 103 104 try { 105 configuration.initialize(); 106 } 107 catch (Exception ex) { 108 throw new BuildException("Error loading Cayenne configuration from " 109 + projectFile); 110 } 111 112 DataNode source = findNode(configuration, srcNode); 114 if (source == null) { 115 throw new BuildException("srcNode not found in the project: " + srcNode); 116 } 117 118 DataNode destination = findNode(configuration, destNode); 119 if (destination == null) { 120 throw new BuildException("destNode not found in the project: " + destNode); 121 } 122 123 log("Porting from '" + srcNode + "' to '" + destNode + "'."); 124 125 AntDataPortDelegate portDelegate = new AntDataPortDelegate( 126 this, 127 maps, 128 includeTables, 129 excludeTables); 130 DataPort dataPort = new DataPort(portDelegate); 131 dataPort.setEntities(getAllEntities(source, destination)); 132 dataPort.setCleaningDestination(cleanDest); 133 dataPort.setSourceNode(source); 134 dataPort.setDestinationNode(destination); 135 136 try { 137 dataPort.execute(); 138 } 139 catch (Exception e) { 140 Throwable topOfStack = Util.unwindException(e); 141 throw new BuildException( 142 "Error porting data: " + topOfStack.getMessage(), 143 topOfStack); 144 } 145 } 146 147 protected DataNode findNode(Configuration configuration, String name) { 148 Iterator domains = configuration.getDomains().iterator(); 149 while (domains.hasNext()) { 150 DataDomain domain = (DataDomain) domains.next(); 151 DataNode node = domain.getNode(name); 152 if (node != null) { 153 return node; 154 } 155 } 156 157 return null; 158 } 159 160 protected Collection getAllEntities(DataNode source, DataNode target) { 161 Collection allEntities = new HashSet (); 164 165 Iterator maps = source.getDataMaps().iterator(); 166 while (maps.hasNext()) { 167 DataMap map = (DataMap) maps.next(); 168 allEntities.addAll(map.getDbEntities()); 169 } 170 171 maps = target.getDataMaps().iterator(); 172 while (maps.hasNext()) { 173 DataMap map = (DataMap) maps.next(); 174 allEntities.addAll(map.getDbEntities()); 175 } 176 177 log("Number of entities: " + allEntities.size(), Project.MSG_VERBOSE); 178 179 if (allEntities.size() == 0) { 180 log("No entities found for either source or target."); 181 } 182 return allEntities; 183 } 184 185 protected void validateParameters() throws BuildException { 186 if (projectFile == null) { 187 throw new BuildException("Required 'projectFile' parameter is missing."); 188 } 189 190 if (!projectFile.exists()) { 191 throw new BuildException("'projectFile' does not exist: " + projectFile); 192 } 193 194 if (srcNode == null) { 195 throw new BuildException("Required 'srcNode' parameter is missing."); 196 } 197 198 if (destNode == null) { 199 throw new BuildException("Required 'destNode' parameter is missing."); 200 } 201 } 202 203 public void setDestNode(String destNode) { 204 this.destNode = destNode; 205 } 206 207 public void setExcludeTables(String excludeTables) { 208 this.excludeTables = excludeTables; 209 } 210 211 public void setIncludeTables(String includeTables) { 212 this.includeTables = includeTables; 213 } 214 215 public void setMaps(String maps) { 216 this.maps = maps; 217 } 218 219 public void setProjectFile(File projectFile) { 220 this.projectFile = projectFile; 221 } 222 223 public void setSrcNode(String srcNode) { 224 this.srcNode = srcNode; 225 } 226 227 public void setCleanDest(boolean flag) { 228 this.cleanDest = flag; 229 } 230 } | Popular Tags |