/
EntitySaxReader.java
606 lines (542 loc) · 27 KB
/
EntitySaxReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.apache.ofbiz.entity.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.ofbiz.base.location.FlexibleLocation;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.UtilXml;
import org.apache.ofbiz.base.util.string.FlexibleStringExpander;
import org.apache.ofbiz.base.util.template.FreeMarkerWorker;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericEntityNotFoundException;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.entity.datasource.GenericHelper;
import org.apache.ofbiz.entity.eca.EntityEcaHandler;
import org.apache.ofbiz.entity.model.ModelEntity;
import org.apache.ofbiz.entity.model.ModelField;
import org.apache.ofbiz.entity.transaction.GenericTransactionException;
import org.apache.ofbiz.entity.transaction.TransactionUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import freemarker.ext.dom.NodeModel;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;
/**
* SAX XML Parser Content Handler for Entity Engine XML files
*/
public class EntitySaxReader extends DefaultHandler {
public static final String module = EntitySaxReader.class.getName();
public static final int DEFAULT_TX_TIMEOUT = 7200;
protected org.xml.sax.Locator locator;
private Delegator delegator;
private EntityEcaHandler<?> ecaHandler = null;
private GenericValue currentValue = null;
private CharSequence currentFieldName = null;
private char[] currentFieldValue = null;
private long numberRead = 0;
private long numberCreated = 0;
private long numberUpdated = 0;
private long numberReplaced = 0;
private long numberDeleted = 0;
private long numberSkipped = 0;
private int valuesPerWrite = 100;
private int valuesPerMessage = 1000;
private int transactionTimeout = 7200;
private boolean useTryInsertMethod = false;
private boolean maintainTxStamps = false;
private boolean createDummyFks = false;
private boolean checkDataOnly = false;
private boolean continueOnFail = false;
private enum Action {CREATE, CREATE_UPDATE, CREATE_REPLACE, DELETE}
private List<String> actionTags = UtilMisc.toList("create", "create-update", "create-replace", "delete");
private Action currentAction = Action.CREATE_UPDATE;
private List<Object> messageList = null;
private List<GenericValue> valuesToWrite = new ArrayList<>(valuesPerWrite);
private List<GenericValue> valuesToDelete = new ArrayList<>(valuesPerWrite);
private boolean isParseForTemplate = false;
private CharSequence templatePath = null;
private Node rootNodeForTemplate = null;
private Node currentNodeForTemplate = null;
private Document documentForTemplate = null;
private Map<String, Object> placeholderValues = null; //contains map of values for corresponding placeholders (eg. ${key}) in the entity xml data file.
protected EntitySaxReader() {}
public EntitySaxReader(Delegator delegator, int transactionTimeout) {
// clone the delegator right off so there is no chance of making change to the initial object
this.delegator = delegator.cloneDelegator();
this.transactionTimeout = transactionTimeout;
}
public EntitySaxReader(Delegator delegator) {
this(delegator, DEFAULT_TX_TIMEOUT);
}
public int getTransactionTimeout() {
return this.transactionTimeout;
}
public void setUseTryInsertMethod(boolean value) {
this.useTryInsertMethod = value;
}
public void setTransactionTimeout(int transactionTimeout) throws GenericTransactionException {
if (this.transactionTimeout != transactionTimeout) {
TransactionUtil.setTransactionTimeout(transactionTimeout);
this.transactionTimeout = transactionTimeout;
}
}
public void setMaintainTxStamps(boolean maintainTxStamps) {
this.maintainTxStamps = maintainTxStamps;
}
public void setCreateDummyFks(boolean createDummyFks) {
this.createDummyFks = createDummyFks;
}
public void setCheckDataOnly(boolean checkDataOnly) {
this.checkDataOnly = checkDataOnly;
}
public void setContinueOnFail(boolean continueOnFail) {
this.continueOnFail = continueOnFail;
}
public void setPlaceholderValues(Map<String,Object> placeholderValues) {
this.placeholderValues = placeholderValues;
}
public List<Object> getMessageList() {
if (this.checkDataOnly && this.messageList == null) {
messageList = new LinkedList<>();
}
return this.messageList;
}
public void setDisableEeca(boolean disableEeca) {
if (disableEeca) {
if (this.ecaHandler == null) {
this.ecaHandler = delegator.getEntityEcaHandler();
}
this.delegator.setEntityEcaHandler(null);
} else {
if (ecaHandler != null) {
this.delegator.setEntityEcaHandler(ecaHandler);
}
}
}
private void setAction(Action action) {
this.currentAction = action;
}
public long parse(String content) throws SAXException, java.io.IOException {
if (content == null) {
Debug.logWarning("content was null, doing nothing", module);
return 0;
}
try (ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes("UTF-8"))) {
return this.parse(bis, "Internal Content");
}
}
public long parse(URL location) throws SAXException, java.io.IOException {
if (location == null) {
Debug.logWarning("location URL was null, doing nothing", module);
return 0;
}
Debug.logImportant("Beginning import from URL: " + location.toExternalForm(), module);
long numberRead = 0;
try (InputStream is = location.openStream()) {
numberRead = this.parse(is, location.toString());
}
return numberRead;
}
private long parse(InputStream is, String docDescription) throws SAXException, java.io.IOException {
SAXParser parser;
try {
parser = SAXParserFactory.newInstance().newSAXParser();
} catch(ParserConfigurationException pce) {
throw new SAXException("Unable to create the SAX parser", pce);
}
numberRead = 0;
try {
boolean beganTransaction = false;
if (transactionTimeout > -1) {
beganTransaction = TransactionUtil.begin(transactionTimeout);
Debug.logImportant("Transaction Timeout set to " + transactionTimeout / 3600 + " hours (" + transactionTimeout + " seconds)", module);
}
try {
parser.parse(is, this);
// make sure all of the values to write got written...
if (!valuesToWrite.isEmpty()) {
writeValues(valuesToWrite);
valuesToWrite.clear();
}
if (!valuesToDelete.isEmpty()) {
delegator.removeAll(valuesToDelete);
valuesToDelete.clear();
}
TransactionUtil.commit(beganTransaction);
} catch (GenericEntityException | IOException | IllegalArgumentException | SAXException e) {
String errMsg = "An error occurred saving the data, rolling back transaction (" + beganTransaction + ")";
Debug.logError(e, errMsg, module);
TransactionUtil.rollback(beganTransaction, errMsg, e);
throw new SAXException("A transaction error occurred reading data", e);
}
} catch (GenericTransactionException e) {
throw new SAXException("A transaction error occurred reading data", e);
}
Debug.logImportant("Finished " + numberRead + " values from " + docDescription, module);
if (Debug.verboseOn()) {
Debug.logVerbose(" Detail created : " + numberCreated + ", skipped : " + numberSkipped +
", updated : " + numberUpdated + ", replaced : " + numberReplaced +
", deleted : " + numberDeleted, module);
}
return numberRead;
}
private void writeValues(List<GenericValue> valuesToWrite) throws GenericEntityException {
if (this.checkDataOnly) {
EntityDataAssert.checkValueList(valuesToWrite, delegator, this.getMessageList());
} else {
delegator.storeAll(valuesToWrite, new EntityStoreOptions(createDummyFks));
}
}
private void countValue(boolean skip, boolean exist) {
if (skip) numberSkipped++;
else if (Action.DELETE == currentAction) numberDeleted++;
else if (Action.CREATE == currentAction || !exist) numberCreated++;
else if (Action.CREATE_REPLACE == currentAction) numberReplaced++;
else numberUpdated++;
}
// ======== ContentHandler interface implementation ========
@Override
public void characters(char[] values, int offset, int count) throws SAXException {
if (isParseForTemplate) {
// if null, don't worry about it
if (this.currentNodeForTemplate != null) {
Node newNode = this.documentForTemplate.createTextNode(new String(values, offset, count));
this.currentNodeForTemplate.appendChild(newNode);
}
return;
}
if (currentValue != null && currentFieldName != null) {
char[] newChunk = Arrays.copyOfRange(values, offset, offset + count);
if (currentFieldValue == null) {
// this is the first chunk
currentFieldValue = newChunk;
} else {
// append the new chunk to currentFieldValue
char[] combined = new char[currentFieldValue.length + newChunk.length];
System.arraycopy(currentFieldValue, 0, combined, 0, currentFieldValue.length);
System.arraycopy(newChunk, 0, combined, currentFieldValue.length, newChunk.length);
currentFieldValue = combined;
}
}
}
@Override
public void endElement(String namespaceURI, String localName, String fullNameString) throws SAXException {
if (Debug.verboseOn()) Debug.logVerbose("endElement: localName=" + localName + ", fullName=" + fullNameString + ", numberRead=" + numberRead, module);
if ("entity-engine-xml".equals(fullNameString)) {
return;
}
if ("entity-engine-transform-xml".equals(fullNameString)) {
// transform file & parse it, then return
URL templateUrl = null;
try {
templateUrl = FlexibleLocation.resolveLocation(templatePath.toString());
} catch (MalformedURLException e) {
throw new SAXException("Could not find transform template with resource path [" + templatePath + "]; error was: " + e.toString());
}
if (templateUrl == null) {
throw new SAXException("Could not find transform template with resource path: " + templatePath);
} else {
try {
BufferedReader templateReader = new BufferedReader(new InputStreamReader(templateUrl.openStream(),StandardCharsets.UTF_8));
StringWriter outWriter = new StringWriter();
Configuration config = FreeMarkerWorker.newConfiguration();
config.setObjectWrapper(FreeMarkerWorker.getDefaultOfbizWrapper());
config.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
Template template = new Template("FMImportFilter", templateReader, config);
NodeModel nodeModel = NodeModel.wrap(this.rootNodeForTemplate);
Map<String, Object> context = new HashMap<>();
TemplateHashModel staticModels = FreeMarkerWorker.getDefaultOfbizWrapper().getStaticModels();
context.put("Static", staticModels);
context.put("doc", nodeModel);
template.process(context, outWriter);
String s = outWriter.toString();
if (Debug.verboseOn()) Debug.logVerbose("transformed xml: " + s, module);
EntitySaxReader reader = new EntitySaxReader(delegator);
reader.setUseTryInsertMethod(this.useTryInsertMethod);
try {
reader.setTransactionTimeout(this.transactionTimeout);
} catch (GenericTransactionException e1) {
Debug.logWarning("couldn't set tx timeout, hopefully shouldn't be a big deal", module);
}
numberRead += reader.parse(s);
} catch (TemplateException | IOException e) {
throw new SAXException("Error storing value", e);
}
}
return;
}
if (isParseForTemplate) {
this.currentNodeForTemplate = this.currentNodeForTemplate.getParentNode();
return;
}
//Test if end action tag, set action to default
if (actionTags.contains(fullNameString)) {
setAction(Action.CREATE_UPDATE);
return;
}
if (currentValue != null) {
if (currentFieldName != null) {
if (UtilValidate.isNotEmpty(currentFieldValue)) {
if (currentValue.getModelEntity().isField(currentFieldName.toString())) {
ModelEntity modelEntity = currentValue.getModelEntity();
ModelField modelField = modelEntity.getField(currentFieldName.toString());
String type = modelField.getType();
if (type != null && "blob".equals(type)) {
byte[] binData = Base64.getMimeDecoder().decode((new String(currentFieldValue)).getBytes());
currentValue.setBytes(currentFieldName.toString(), binData);
} else {
currentValue.setString(currentFieldName.toString(), new String(currentFieldValue));
}
} else {
Debug.logWarning("Ignoring invalid field name [" + currentFieldName + "] found for the entity: "
+ currentValue.getEntityName() + " with value=" + currentFieldValue.toString(), module);
}
currentFieldValue = null;
}
currentFieldName = null;
} else {
// before we write currentValue check to see if PK is there, if not and it is one field, generate it from a sequence using the entity name
if (!currentValue.containsPrimaryKey()) {
if (currentValue.getModelEntity().getPksSize() == 1) {
ModelField modelField = currentValue.getModelEntity().getOnlyPk();
String newSeq = delegator.getNextSeqId(currentValue.getEntityName());
currentValue.setString(modelField.getName(), newSeq);
} else {
throw new SAXException("Cannot store value with incomplete primary key with more than 1 primary key field: " + currentValue);
}
}
try {
boolean exist = true;
boolean skip = false;
//if verbose on, check if entity exist on database for count each action
//It's necessary to check also for specific action CREATE and DELETE to ensure it's OK
if (Action.CREATE == currentAction || Action.DELETE == currentAction || Debug.verboseOn()) {
GenericHelper helper = delegator.getEntityHelper(currentValue.getEntityName());
if (currentValue.containsPrimaryKey()) {
try {
helper.findByPrimaryKey(currentValue.getPrimaryKey());
} catch (GenericEntityNotFoundException e) {exist = false;}
}
if (Action.CREATE == currentAction && exist) { skip = true; }
else if (Action.DELETE == currentAction && !exist) { skip = true; }
}
if (!skip) {
if (this.useTryInsertMethod && !this.checkDataOnly) {
if (Action.DELETE == currentAction) {
currentValue.remove();
} else {
currentValue.create();
}
} else {
if (Action.DELETE == currentAction) {
valuesToDelete.add(currentValue);
if (valuesToDelete.size() >= valuesPerWrite) {
delegator.removeAll(valuesToDelete);
valuesToDelete.clear();
}
} else {
valuesToWrite.add(currentValue);
if (valuesToWrite.size() >= valuesPerWrite) {
writeValues(valuesToWrite);
valuesToWrite.clear();
}
}
}
}
numberRead++;
if (Debug.verboseOn()) countValue(skip, exist);
if ((numberRead % valuesPerMessage) == 0) {
Debug.logImportant("Another " + valuesPerMessage + " values imported: now up to " + numberRead, module);
}
currentValue = null;
} catch (GenericEntityException e) {
String errMsg = "Error performing action " + currentAction;
Debug.logError(e, errMsg, module);
throw new SAXException(errMsg, e);
}
}
}
}
@Override
public void setDocumentLocator(org.xml.sax.Locator locator) {
this.locator = locator;
}
@Override
public void startElement(String namepsaceURI, String localName, String fullNameString, Attributes attributes) throws SAXException {
if (Debug.verboseOn()) Debug.logVerbose("startElement: localName=" + localName + ", fullName=" + fullNameString + ", attributes=" + attributes, module);
if ("entity-engine-xml".equals(fullNameString)) {
// check the maintain-timestamp flag
CharSequence maintainTx = attributes.getValue("maintain-timestamps");
if (maintainTx != null) {
this.setMaintainTxStamps("true".equalsIgnoreCase(maintainTx.toString()));
}
// check the disable-eeca flag
CharSequence ecaDisable = attributes.getValue("disable-eeca");
if (ecaDisable != null) {
this.setDisableEeca("true".equalsIgnoreCase(ecaDisable.toString()));
}
// check the use-dummy-fk flag
CharSequence dummyFk = attributes.getValue("create-dummy-fk");
if (dummyFk != null) {
this.setCreateDummyFks("true".equalsIgnoreCase(dummyFk.toString()));
}
return;
}
if ("entity-engine-transform-xml".equals(fullNameString)) {
templatePath = attributes.getValue("template");
isParseForTemplate = true;
documentForTemplate = UtilXml.makeEmptyXmlDocument();
return;
}
if (isParseForTemplate) {
Element newElement = this.documentForTemplate.createElement(fullNameString);
int length = attributes.getLength();
for (int i = 0; i < length; i++) {
CharSequence name = attributes.getLocalName(i);
CharSequence value = attributes.getValue(i);
if (UtilValidate.isEmpty(name)) {
name = attributes.getQName(i);
}
newElement.setAttribute(name.toString(), value.toString());
}
if (this.currentNodeForTemplate == null) {
this.currentNodeForTemplate = newElement;
this.rootNodeForTemplate = newElement;
} else {
this.currentNodeForTemplate.appendChild(newElement);
this.currentNodeForTemplate = newElement;
}
return;
}
//Test if action change
if (actionTags.contains(fullNameString)) {
if ("create".equals(fullNameString)) setAction(Action.CREATE);
if ("create-update".equals(fullNameString)) setAction(Action.CREATE_UPDATE);
if ("create-replace".equals(fullNameString)) setAction(Action.CREATE_REPLACE);
if ("delete".equals(fullNameString)) setAction(Action.DELETE);
return;
}
if (currentValue != null) {
// we have a nested value/CDATA element
currentFieldName = fullNameString;
} else {
String entityName = fullNameString;
// if a dash or colon is in the tag name, grab what is after it
if (entityName.indexOf('-') > 0) {
entityName = entityName.substring(entityName.indexOf('-') + 1);
}
if (entityName.indexOf(':') > 0) {
entityName = entityName.substring(entityName.indexOf(':') + 1);
}
try {
currentValue = delegator.makeValue(entityName);
if (this.maintainTxStamps) {
currentValue.setIsFromEntitySync(true);
}
} catch (Exception e) {
if (continueOnFail) {
Debug.logError(e, module);
} else {
throw new SAXException(e);
}
}
if (currentValue != null) {
int length = attributes.getLength();
List<String> absentFields = null;
if (Action.CREATE_REPLACE == currentAction) {
//get all non pk fields
ModelEntity currentEntity = currentValue.getModelEntity();
absentFields = currentEntity.getNoPkFieldNames();
absentFields.removeAll(currentEntity.getAutomaticFieldNames());
}
for (int i = 0; i < length; i++) {
CharSequence name = attributes.getLocalName(i);
CharSequence value = attributes.getValue(i);
if (UtilValidate.isNotEmpty(value)) {
String tmp = FlexibleStringExpander.expandString(value.toString(), placeholderValues);
value = tmp.subSequence(0, tmp.length());
}
if (UtilValidate.isEmpty(name)) {
name = attributes.getQName(i);
}
try {
// treat empty strings as nulls, but do NOT ignore them, instead set as null and update
if (value != null) {
if (currentValue.getModelEntity().isField(name.toString())) {
String valueString = (value.length() > 0 ? value.toString() : null);
currentValue.setString(name.toString(), valueString);
if (Action.CREATE_REPLACE == currentAction && absentFields != null) absentFields.remove(name);
} else {
Debug.logWarning("Ignoring invalid field name [" + name + "] found for the entity: " + currentValue.getEntityName() + " with value=" + value, module);
}
}
} catch (Exception e) {
Debug.logWarning(e, "Could not set field " + entityName + "." + name + " to the value " + value, module);
}
}
if (Action.CREATE_REPLACE == currentAction && absentFields != null) {
for (String fieldName : absentFields) {
currentValue.set(fieldName, null);
}
}
}
}
}
// ======== ErrorHandler interface implementation ========
@Override
public void error(org.xml.sax.SAXParseException exception) throws SAXException {
Debug.logWarning(exception, "Error reading XML on line " + exception.getLineNumber() + ", column " + exception.getColumnNumber(), module);
}
@Override
public void fatalError(org.xml.sax.SAXParseException exception) throws SAXException {
Debug.logError(exception, "Fatal Error reading XML on line " + exception.getLineNumber() + ", column " + exception.getColumnNumber(), module);
throw new SAXException("Fatal Error reading XML on line " + exception.getLineNumber() + ", column " + exception.getColumnNumber(), exception);
}
@Override
public void warning(org.xml.sax.SAXParseException exception) throws SAXException {
Debug.logWarning(exception, "Warning reading XML on line " + exception.getLineNumber() + ", column " + exception.getColumnNumber(), module);
}
}