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.util.ArrayList; |
21 | |
import java.util.Iterator; |
22 | |
|
23 | |
import org.apache.ddlutils.model.Database; |
24 | |
import org.apache.ojb.broker.metadata.DescriptorRepository; |
25 | |
import org.apache.tools.ant.BuildException; |
26 | |
import org.apache.tools.ant.DirectoryScanner; |
27 | |
import org.apache.tools.ant.Project; |
28 | |
import org.apache.tools.ant.Task; |
29 | |
import org.apache.tools.ant.types.FileSet; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class WriteDataToDatabaseCommand extends Command |
38 | |
{ |
39 | |
|
40 | |
private File _singleDataFile = null; |
41 | |
|
42 | |
private ArrayList _fileSets = new ArrayList(); |
43 | |
|
44 | |
private Boolean _useBatchMode; |
45 | |
|
46 | |
private Integer _batchSize; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public void addConfiguredFileset(FileSet fileset) |
54 | |
{ |
55 | |
_fileSets.add(fileset); |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public void setDataFile(File dataFile) |
64 | |
{ |
65 | |
_singleDataFile = dataFile; |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public void setBatchSize(int batchSize) |
74 | |
{ |
75 | |
_batchSize = new Integer(batchSize); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void setUseBatchMode(boolean useBatchMode) |
84 | |
{ |
85 | |
_useBatchMode = Boolean.valueOf(useBatchMode); |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void execute(Task task, Database dbModel, DescriptorRepository objModel) throws BuildException |
92 | |
{ |
93 | |
try |
94 | |
{ |
95 | |
DdlUtilsDataHandling handling = new DdlUtilsDataHandling(); |
96 | |
|
97 | |
handling.setModel(dbModel, objModel); |
98 | |
handling.setPlatform(getPlatform()); |
99 | |
|
100 | |
if (_singleDataFile != null) |
101 | |
{ |
102 | |
readSingleDataFile(task, handling, _singleDataFile); |
103 | |
} |
104 | |
else |
105 | |
{ |
106 | |
for (Iterator it = _fileSets.iterator(); it.hasNext();) |
107 | |
{ |
108 | |
FileSet fileSet = (FileSet)it.next(); |
109 | |
File fileSetDir = fileSet.getDir(task.getProject()); |
110 | |
DirectoryScanner scanner = fileSet.getDirectoryScanner(task.getProject()); |
111 | |
String[] files = scanner.getIncludedFiles(); |
112 | |
|
113 | |
for (int idx = 0; (files != null) && (idx < files.length); idx++) |
114 | |
{ |
115 | |
readSingleDataFile(task, handling, new File(fileSetDir, files[idx])); |
116 | |
} |
117 | |
} |
118 | |
} |
119 | |
} |
120 | |
catch (Exception ex) |
121 | |
{ |
122 | |
if (ex instanceof BuildException) |
123 | |
{ |
124 | |
throw (BuildException)ex; |
125 | |
} |
126 | |
else |
127 | |
{ |
128 | |
throw new BuildException(ex); |
129 | |
} |
130 | |
} |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
private void readSingleDataFile(Task task, DdlUtilsDataHandling handling, File dataFile) |
141 | |
{ |
142 | |
if (!dataFile.exists()) |
143 | |
{ |
144 | |
task.log("Could not find data file "+dataFile.getAbsolutePath(), Project.MSG_ERR); |
145 | |
} |
146 | |
else if (!dataFile.isFile()) |
147 | |
{ |
148 | |
task.log("Path "+dataFile.getAbsolutePath()+" does not denote a data file", Project.MSG_ERR); |
149 | |
} |
150 | |
else if (!dataFile.canRead()) |
151 | |
{ |
152 | |
task.log("Could not read data file "+dataFile.getAbsolutePath(), Project.MSG_ERR); |
153 | |
} |
154 | |
else |
155 | |
{ |
156 | |
int batchSize = 1; |
157 | |
|
158 | |
if ((_useBatchMode != null) && _useBatchMode.booleanValue()) |
159 | |
{ |
160 | |
if (_batchSize != null) |
161 | |
{ |
162 | |
batchSize = _batchSize.intValue(); |
163 | |
} |
164 | |
} |
165 | |
try |
166 | |
{ |
167 | |
handling.insertData(new FileReader(dataFile), batchSize); |
168 | |
task.log("Read data file "+dataFile.getAbsolutePath(), Project.MSG_INFO); |
169 | |
} |
170 | |
catch (Exception ex) |
171 | |
{ |
172 | |
if (isFailOnError()) |
173 | |
{ |
174 | |
throw new BuildException("Could not read data file "+dataFile.getAbsolutePath(), ex); |
175 | |
} |
176 | |
else |
177 | |
{ |
178 | |
task.log("Could not read data file "+dataFile.getAbsolutePath(), Project.MSG_ERR); |
179 | |
} |
180 | |
} |
181 | |
} |
182 | |
} |
183 | |
} |