| 1 | |
package org.apache.ojb.broker.ant; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
import java.io.File; |
| 19 | |
import java.io.FileReader; |
| 20 | |
import java.io.FileWriter; |
| 21 | |
import java.io.PrintWriter; |
| 22 | |
import java.io.Writer; |
| 23 | |
import java.util.ArrayList; |
| 24 | |
import java.util.Iterator; |
| 25 | |
|
| 26 | |
import org.apache.ddlutils.model.Database; |
| 27 | |
import org.apache.ojb.broker.metadata.DescriptorRepository; |
| 28 | |
import org.apache.tools.ant.BuildException; |
| 29 | |
import org.apache.tools.ant.DirectoryScanner; |
| 30 | |
import org.apache.tools.ant.Project; |
| 31 | |
import org.apache.tools.ant.Task; |
| 32 | |
import org.apache.tools.ant.types.FileSet; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public class WriteDataSqlToFileCommand extends Command |
| 41 | |
{ |
| 42 | |
|
| 43 | |
private File _singleDataFile = null; |
| 44 | |
|
| 45 | |
private ArrayList _fileSets = new ArrayList(); |
| 46 | |
|
| 47 | |
private File _outputFile; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public void addConfiguredFileset(FileSet fileset) |
| 55 | |
{ |
| 56 | |
_fileSets.add(fileset); |
| 57 | |
} |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public void setDataFile(File dataFile) |
| 65 | |
{ |
| 66 | |
_singleDataFile = dataFile; |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public void setOutputFile(File outputFile) |
| 75 | |
{ |
| 76 | |
_outputFile = outputFile; |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public void execute(Task task, Database dbModel, DescriptorRepository objModel) throws BuildException |
| 83 | |
{ |
| 84 | |
try |
| 85 | |
{ |
| 86 | |
DdlUtilsDataHandling handling = new DdlUtilsDataHandling(); |
| 87 | |
PrintWriter writer = new PrintWriter(new FileWriter(_outputFile), true); |
| 88 | |
|
| 89 | |
handling.setModel(dbModel, objModel); |
| 90 | |
handling.setPlatform(getPlatform()); |
| 91 | |
|
| 92 | |
if (_singleDataFile != null) |
| 93 | |
{ |
| 94 | |
readSingleDataFile(task, handling, _singleDataFile, writer); |
| 95 | |
} |
| 96 | |
else |
| 97 | |
{ |
| 98 | |
for (Iterator it = _fileSets.iterator(); it.hasNext();) |
| 99 | |
{ |
| 100 | |
FileSet fileSet = (FileSet)it.next(); |
| 101 | |
File fileSetDir = fileSet.getDir(task.getProject()); |
| 102 | |
DirectoryScanner scanner = fileSet.getDirectoryScanner(task.getProject()); |
| 103 | |
String[] files = scanner.getIncludedFiles(); |
| 104 | |
|
| 105 | |
for (int idx = 0; (files != null) && (idx < files.length); idx++) |
| 106 | |
{ |
| 107 | |
readSingleDataFile(task, handling, new File(fileSetDir, files[idx]), writer); |
| 108 | |
} |
| 109 | |
} |
| 110 | |
} |
| 111 | |
} |
| 112 | |
catch (Exception ex) |
| 113 | |
{ |
| 114 | |
if (ex instanceof BuildException) |
| 115 | |
{ |
| 116 | |
throw (BuildException)ex; |
| 117 | |
} |
| 118 | |
else |
| 119 | |
{ |
| 120 | |
throw new BuildException(ex); |
| 121 | |
} |
| 122 | |
} |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
private void readSingleDataFile(Task task, DdlUtilsDataHandling handling, File dataFile, Writer output) |
| 133 | |
{ |
| 134 | |
if (!dataFile.exists()) |
| 135 | |
{ |
| 136 | |
task.log("Could not find data file "+dataFile.getAbsolutePath(), Project.MSG_ERR); |
| 137 | |
} |
| 138 | |
else if (!dataFile.isFile()) |
| 139 | |
{ |
| 140 | |
task.log("Path "+dataFile.getAbsolutePath()+" does not denote a data file", Project.MSG_ERR); |
| 141 | |
} |
| 142 | |
else if (!dataFile.canRead()) |
| 143 | |
{ |
| 144 | |
task.log("Could not read data file "+dataFile.getAbsolutePath(), Project.MSG_ERR); |
| 145 | |
} |
| 146 | |
else |
| 147 | |
{ |
| 148 | |
try |
| 149 | |
{ |
| 150 | |
FileReader reader = new FileReader(dataFile.getAbsolutePath()); |
| 151 | |
|
| 152 | |
handling.getInsertDataSql(reader, output); |
| 153 | |
output.flush(); |
| 154 | |
output.close(); |
| 155 | |
task.log("Read data file "+dataFile.getAbsolutePath(), Project.MSG_INFO); |
| 156 | |
} |
| 157 | |
catch (Exception ex) |
| 158 | |
{ |
| 159 | |
if (isFailOnError()) |
| 160 | |
{ |
| 161 | |
throw new BuildException("Could not read data file "+dataFile.getAbsolutePath(), ex); |
| 162 | |
} |
| 163 | |
} |
| 164 | |
} |
| 165 | |
} |
| 166 | |
} |