| 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.IOException; |
| 19 | |
import java.io.Reader; |
| 20 | |
import java.io.StringReader; |
| 21 | |
import java.io.Writer; |
| 22 | |
import java.util.Iterator; |
| 23 | |
import java.util.List; |
| 24 | |
|
| 25 | |
import org.apache.commons.beanutils.DynaBean; |
| 26 | |
import org.apache.commons.digester.Digester; |
| 27 | |
import org.apache.commons.digester.ExtendedBaseRules; |
| 28 | |
import org.apache.commons.digester.Rule; |
| 29 | |
import org.apache.commons.digester.RuleSetBase; |
| 30 | |
import org.apache.ddlutils.Platform; |
| 31 | |
import org.apache.ddlutils.model.Column; |
| 32 | |
import org.apache.ddlutils.model.Database; |
| 33 | |
import org.apache.ojb.broker.metadata.ClassDescriptor; |
| 34 | |
import org.apache.ojb.broker.metadata.DescriptorRepository; |
| 35 | |
import org.xml.sax.Attributes; |
| 36 | |
import org.xml.sax.EntityResolver; |
| 37 | |
import org.xml.sax.InputSource; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public class DdlUtilsDataHandling |
| 45 | |
{ |
| 46 | |
private class DynaFactoryCreateRule extends Rule |
| 47 | |
{ |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public void begin(String namespace, String name, Attributes attributes) throws Exception |
| 52 | |
{ |
| 53 | |
DynaBean bean = _preparedModel.createBeanFor(name); |
| 54 | |
|
| 55 | |
if (bean == null) |
| 56 | |
{ |
| 57 | |
throw new DataTaskException("Unknown element "+name); |
| 58 | |
} |
| 59 | |
|
| 60 | |
for (int idx = 0; idx < attributes.getLength(); idx++) |
| 61 | |
{ |
| 62 | |
String attrName = attributes.getLocalName(idx); |
| 63 | |
String attrValue = attributes.getValue(idx); |
| 64 | |
Column column = _preparedModel.getColumnFor(name, attrName); |
| 65 | |
|
| 66 | |
if (column == null) |
| 67 | |
{ |
| 68 | |
throw new DataTaskException("Unknown attribute "+attrName+" of element "+name); |
| 69 | |
} |
| 70 | |
bean.set(column.getName(), attrValue); |
| 71 | |
} |
| 72 | |
DdlUtilsDataHandling.this._digester.push(bean); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public void end(String namespace, String name) throws Exception |
| 79 | |
{ |
| 80 | |
DynaBean bean = (DynaBean)DdlUtilsDataHandling.this._digester.pop(); |
| 81 | |
|
| 82 | |
((DataSet)DdlUtilsDataHandling.this._digester.peek()).add(bean); |
| 83 | |
} |
| 84 | |
} |
| 85 | |
|
| 86 | |
public class DataRuleSet extends RuleSetBase |
| 87 | |
{ |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
public void addRuleInstances(Digester digester) |
| 92 | |
{ |
| 93 | |
digester.addObjectCreate("dataset", DataSet.class); |
| 94 | |
digester.addRule("*/dataset/*", new DynaFactoryCreateRule()); |
| 95 | |
} |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
private Database _dbModel; |
| 100 | |
|
| 101 | |
private Platform _platform; |
| 102 | |
|
| 103 | |
private PreparedModel _preparedModel; |
| 104 | |
|
| 105 | |
private Digester _digester; |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public DdlUtilsDataHandling() |
| 111 | |
{ |
| 112 | |
_digester = new Digester(); |
| 113 | |
_digester.setEntityResolver(new EntityResolver() { |
| 114 | |
public InputSource resolveEntity(String publicId, String systemId) |
| 115 | |
{ |
| 116 | |
|
| 117 | |
return new InputSource(new StringReader("")); |
| 118 | |
} |
| 119 | |
|
| 120 | |
}); |
| 121 | |
_digester.setNamespaceAware(true); |
| 122 | |
_digester.setValidating(false); |
| 123 | |
_digester.setUseContextClassLoader(true); |
| 124 | |
_digester.setRules(new ExtendedBaseRules()); |
| 125 | |
_digester.addRuleSet(new DataRuleSet()); |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public void setModel(Database databaseModel, DescriptorRepository objModel) |
| 135 | |
{ |
| 136 | |
_dbModel = databaseModel; |
| 137 | |
_preparedModel = new PreparedModel(objModel, databaseModel); |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public void setPlatform(Platform platform) |
| 146 | |
{ |
| 147 | |
_platform = platform; |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public void getDataDTD(Writer output) throws DataTaskException |
| 156 | |
{ |
| 157 | |
try |
| 158 | |
{ |
| 159 | |
output.write("<!ELEMENT dataset (\n"); |
| 160 | |
for (Iterator it = _preparedModel.getElementNames(); it.hasNext();) |
| 161 | |
{ |
| 162 | |
String elementName = (String)it.next(); |
| 163 | |
|
| 164 | |
output.write(" "); |
| 165 | |
output.write(elementName); |
| 166 | |
output.write("*"); |
| 167 | |
output.write(it.hasNext() ? " |\n" : "\n"); |
| 168 | |
} |
| 169 | |
output.write(")>\n<!ATTLIST dataset\n name CDATA #REQUIRED\n>\n"); |
| 170 | |
for (Iterator it = _preparedModel.getElementNames(); it.hasNext();) |
| 171 | |
{ |
| 172 | |
String elementName = (String)it.next(); |
| 173 | |
List classDescs = _preparedModel.getClassDescriptorsMappingTo(elementName); |
| 174 | |
|
| 175 | |
if (classDescs == null) |
| 176 | |
{ |
| 177 | |
output.write("\n<!-- Indirection table"); |
| 178 | |
} |
| 179 | |
else |
| 180 | |
{ |
| 181 | |
output.write("\n<!-- Mapped to : "); |
| 182 | |
for (Iterator classDescIt = classDescs.iterator(); classDescIt.hasNext();) |
| 183 | |
{ |
| 184 | |
ClassDescriptor classDesc = (ClassDescriptor)classDescIt.next(); |
| 185 | |
|
| 186 | |
output.write(classDesc.getClassNameOfObject()); |
| 187 | |
if (classDescIt.hasNext()) |
| 188 | |
{ |
| 189 | |
output.write("\n "); |
| 190 | |
} |
| 191 | |
} |
| 192 | |
} |
| 193 | |
output.write(" -->\n<!ELEMENT "); |
| 194 | |
output.write(elementName); |
| 195 | |
output.write(" EMPTY>\n<!ATTLIST "); |
| 196 | |
output.write(elementName); |
| 197 | |
output.write("\n"); |
| 198 | |
|
| 199 | |
for (Iterator attrIt = _preparedModel.getAttributeNames(elementName); attrIt.hasNext();) |
| 200 | |
{ |
| 201 | |
String attrName = (String)attrIt.next(); |
| 202 | |
|
| 203 | |
output.write(" "); |
| 204 | |
output.write(attrName); |
| 205 | |
output.write(" CDATA #"); |
| 206 | |
output.write(_preparedModel.isRequired(elementName, attrName) ? "REQUIRED" : "IMPLIED"); |
| 207 | |
output.write("\n"); |
| 208 | |
} |
| 209 | |
output.write(">\n"); |
| 210 | |
} |
| 211 | |
} |
| 212 | |
catch (IOException ex) |
| 213 | |
{ |
| 214 | |
throw new DataTaskException(ex); |
| 215 | |
} |
| 216 | |
} |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
public void getInsertDataSql(Reader input, Writer output) throws DataTaskException |
| 228 | |
{ |
| 229 | |
try |
| 230 | |
{ |
| 231 | |
DataSet set = (DataSet)_digester.parse(input); |
| 232 | |
|
| 233 | |
set.createInsertionSql(_dbModel, _platform, output); |
| 234 | |
} |
| 235 | |
catch (Exception ex) |
| 236 | |
{ |
| 237 | |
if (ex instanceof DataTaskException) |
| 238 | |
{ |
| 239 | |
|
| 240 | |
throw (DataTaskException)ex; |
| 241 | |
} |
| 242 | |
else |
| 243 | |
{ |
| 244 | |
throw new DataTaskException(ex); |
| 245 | |
} |
| 246 | |
} |
| 247 | |
} |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public void insertData(Reader input, int batchSize) throws DataTaskException |
| 259 | |
{ |
| 260 | |
try |
| 261 | |
{ |
| 262 | |
DataSet set = (DataSet)_digester.parse(input); |
| 263 | |
|
| 264 | |
set.insert(_platform, _dbModel, batchSize); |
| 265 | |
} |
| 266 | |
catch (Exception ex) |
| 267 | |
{ |
| 268 | |
if (ex instanceof DataTaskException) |
| 269 | |
{ |
| 270 | |
|
| 271 | |
throw (DataTaskException)ex; |
| 272 | |
} |
| 273 | |
else |
| 274 | |
{ |
| 275 | |
throw new DataTaskException(ex); |
| 276 | |
} |
| 277 | |
} |
| 278 | |
} |
| 279 | |
} |