1 19 20 package org.apache.cayenne.tools; 21 22 import java.io.File ; 23 import java.util.Collection ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 27 import org.apache.cayenne.access.DataDomain; 28 import org.apache.cayenne.access.DataNode; 29 import org.apache.cayenne.access.DataPort; 30 import org.apache.cayenne.conf.Configuration; 31 import org.apache.cayenne.conf.FileConfiguration; 32 import org.apache.cayenne.map.DataMap; 33 import org.apache.cayenne.util.Util; 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.Task; 37 38 45 public class DataPortTask extends Task { 46 47 protected File projectFile; 48 protected String maps; 49 protected String srcNode; 50 protected String destNode; 51 protected String includeTables; 52 protected String excludeTables; 53 protected boolean cleanDest = true; 54 55 public DataPortTask() { 56 this.cleanDest = true; 58 } 59 60 public void execute() throws BuildException { 61 validateParameters(); 62 63 FileConfiguration configuration = new FileConfiguration(projectFile); 64 65 try { 66 configuration.initialize(); 67 } 68 catch (Exception ex) { 69 throw new BuildException("Error loading Cayenne configuration from " 70 + projectFile, ex); 71 } 72 73 DataNode source = findNode(configuration, srcNode); 75 if (source == null) { 76 throw new BuildException("srcNode not found in the project: " + srcNode); 77 } 78 79 DataNode destination = findNode(configuration, destNode); 80 if (destination == null) { 81 throw new BuildException("destNode not found in the project: " + destNode); 82 } 83 84 log("Porting from '" + srcNode + "' to '" + destNode + "'."); 85 86 AntDataPortDelegate portDelegate = new AntDataPortDelegate( 87 this, 88 maps, 89 includeTables, 90 excludeTables); 91 DataPort dataPort = new DataPort(portDelegate); 92 dataPort.setEntities(getAllEntities(source, destination)); 93 dataPort.setCleaningDestination(cleanDest); 94 dataPort.setSourceNode(source); 95 dataPort.setDestinationNode(destination); 96 97 try { 98 dataPort.execute(); 99 } 100 catch (Exception e) { 101 Throwable topOfStack = Util.unwindException(e); 102 throw new BuildException( 103 "Error porting data: " + topOfStack.getMessage(), 104 topOfStack); 105 } 106 } 107 108 protected DataNode findNode(Configuration configuration, String name) { 109 Iterator domains = configuration.getDomains().iterator(); 110 while (domains.hasNext()) { 111 DataDomain domain = (DataDomain) domains.next(); 112 DataNode node = domain.getNode(name); 113 if (node != null) { 114 return node; 115 } 116 } 117 118 return null; 119 } 120 121 protected Collection getAllEntities(DataNode source, DataNode target) { 122 Collection allEntities = new HashSet (); 125 126 Iterator maps = source.getDataMaps().iterator(); 127 while (maps.hasNext()) { 128 DataMap map = (DataMap) maps.next(); 129 allEntities.addAll(map.getDbEntities()); 130 } 131 132 maps = target.getDataMaps().iterator(); 133 while (maps.hasNext()) { 134 DataMap map = (DataMap) maps.next(); 135 allEntities.addAll(map.getDbEntities()); 136 } 137 138 log("Number of entities: " + allEntities.size(), Project.MSG_VERBOSE); 139 140 if (allEntities.size() == 0) { 141 log("No entities found for either source or target."); 142 } 143 return allEntities; 144 } 145 146 protected void validateParameters() throws BuildException { 147 if (projectFile == null) { 148 throw new BuildException("Required 'projectFile' parameter is missing."); 149 } 150 151 if (!projectFile.exists()) { 152 throw new BuildException("'projectFile' does not exist: " + projectFile); 153 } 154 155 if (srcNode == null) { 156 throw new BuildException("Required 'srcNode' parameter is missing."); 157 } 158 159 if (destNode == null) { 160 throw new BuildException("Required 'destNode' parameter is missing."); 161 } 162 } 163 164 public void setDestNode(String destNode) { 165 this.destNode = destNode; 166 } 167 168 public void setExcludeTables(String excludeTables) { 169 this.excludeTables = excludeTables; 170 } 171 172 public void setIncludeTables(String includeTables) { 173 this.includeTables = includeTables; 174 } 175 176 public void setMaps(String maps) { 177 this.maps = maps; 178 } 179 180 public void setProjectFile(File projectFile) { 181 this.projectFile = projectFile; 182 } 183 184 public void setSrcNode(String srcNode) { 185 this.srcNode = srcNode; 186 } 187 188 public void setCleanDest(boolean flag) { 189 this.cleanDest = flag; 190 } 191 } 192 | Popular Tags |