diff --git a/framework/entity/src/org/ofbiz/entity/model/ModelEntity.java b/framework/entity/src/org/ofbiz/entity/model/ModelEntity.java index 099bc52580..dba9d33fcc 100644 --- a/framework/entity/src/org/ofbiz/entity/model/ModelEntity.java +++ b/framework/entity/src/org/ofbiz/entity/model/ModelEntity.java @@ -694,6 +694,20 @@ public class ModelEntity extends ModelInfo implements Comparable, S return nameList; } + /** + * @return field names list, managed by entity-engine + */ + public List getAutomaticFieldNames() { + List nameList = FastList.newInstance(); + if (! this.noAutoStamp) { + nameList.add(STAMP_FIELD); + nameList.add(STAMP_TX_FIELD); + nameList.add(CREATE_STAMP_FIELD); + nameList.add(CREATE_STAMP_TX_FIELD); + } + return nameList; + } + public int getRelationsSize() { return this.relations.size(); } diff --git a/framework/entity/src/org/ofbiz/entity/util/EntitySaxReader.java b/framework/entity/src/org/ofbiz/entity/util/EntitySaxReader.java index 1f86db9f91..0e2175e582 100644 --- a/framework/entity/src/org/ofbiz/entity/util/EntitySaxReader.java +++ b/framework/entity/src/org/ofbiz/entity/util/EntitySaxReader.java @@ -40,12 +40,15 @@ import javolution.xml.sax.XMLReaderImpl; import org.ofbiz.base.location.FlexibleLocation; import org.ofbiz.base.util.Base64; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.UtilXml; import org.ofbiz.base.util.template.FreeMarkerWorker; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.GenericEntityNotFoundException; import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.datasource.GenericHelper; import org.ofbiz.entity.eca.EntityEcaHandler; import org.ofbiz.entity.model.ModelEntity; import org.ofbiz.entity.model.ModelField; @@ -78,6 +81,11 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error protected CharSequence currentFieldName = null; protected CharSequence currentFieldValue = null; protected long numberRead = 0; + protected long numberCreated = 0; + protected long numberUpdated = 0; + protected long numberReplaced = 0; + protected long numberDeleted = 0; + protected long numberSkipped = 0; protected int valuesPerWrite = 100; protected int valuesPerMessage = 1000; @@ -88,9 +96,13 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error protected boolean checkDataOnly = false; protected boolean doCacheClear = true; protected boolean disableEeca = false; + protected enum Action {CREATE, CREATE_UPDATE, CREATE_REPLACE, DELETE}; + protected List actionTags = UtilMisc.toList("create", "create-update", "create-replace", "delete"); + protected Action currentAction = Action.CREATE_UPDATE; protected List messageList = null; protected List valuesToWrite = new ArrayList(valuesPerWrite); + protected List valuesToDelete = new ArrayList(valuesPerWrite); protected boolean isParseForTemplate = false; protected CharSequence templatePath = null; @@ -202,6 +214,14 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error } } + public void setAction(Action action) { + this.currentAction = action; + } + + public Action getAction() { + return this.currentAction; + } + public long parse(String content) throws SAXException, java.io.IOException { if (content == null) { Debug.logWarning("content was null, doing nothing", module); @@ -272,10 +292,14 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error try { parser.parse(is); // make sure all of the values to write got written... - if (valuesToWrite.size() > 0) { + if (! valuesToWrite.isEmpty()) { writeValues(valuesToWrite); valuesToWrite.clear(); } + if (! valuesToDelete.isEmpty()) { + delegator.removeAll(valuesToDelete); + valuesToDelete.clear(); + } TransactionUtil.commit(beganTransaction); } catch (Exception e) { String errMsg = "An error occurred saving the data, rolling back transaction (" + beganTransaction + ")"; @@ -287,6 +311,11 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error 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; } @@ -384,6 +413,12 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error 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)) { @@ -419,22 +454,58 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error } try { - if (this.useTryInsertMethod && !this.checkDataOnly) { - // this technique is faster for data sets where most, if not all, values do not already exist in the database - try { - currentValue.create(); - } catch (GenericEntityException e1) { - // create failed, try a store, if that fails too we have a real error and the catch outside of this should handle it - currentValue.store(); + boolean exist = true; + boolean skip = false; + //if verbose on, check if entity exist on database for count each action + //It's necessay 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;} } - } else { - valuesToWrite.add(currentValue); - if (valuesToWrite.size() >= valuesPerWrite) { - writeValues(valuesToWrite); - valuesToWrite.clear(); + if (Action.CREATE == currentAction && exist) { skip = true; } + else if (Action.DELETE == currentAction && ! exist) { skip = true; } + } + if (! skip) { + if (this.useTryInsertMethod && !this.checkDataOnly) { + if (Action.CREATE == currentAction) { currentValue.create(); } + else if (Action.DELETE == currentAction) { + try { + currentValue.remove(); + } catch (GenericEntityException e1) { + String errMsg = "Error deleting value"; + Debug.logError(e1, errMsg, module); + throw new SAXException(errMsg, e1); + } + } else { + // this technique is faster for data sets where most, if not all, values do not already exist in the database + try { + currentValue.create(); + } catch (GenericEntityException e1) { + // create failed, try a store, if that fails too we have a real error and the catch outside of this should handle it + currentValue.store(); + } + } + } else { + if (Action.DELETE == currentAction) { + valuesToDelete.add(currentValue); + if (valuesToDelete.size() >= valuesPerWrite) { + delegator.removeAll(valuesToDelete, doCacheClear); + 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); } @@ -448,6 +519,15 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error } } + //Use for detail the loading entities + protected 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++; + } + public void endPrefixMapping(CharArray prefix) throws org.xml.sax.SAXException {} public void ignorableWhitespace(char[] values, int offset, int count) throws org.xml.sax.SAXException { @@ -526,6 +606,15 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error 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 = fullName; @@ -554,6 +643,13 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error if (currentValue != null) { int length = attributes.getLength(); + List 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); @@ -567,6 +663,7 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error if (UtilValidate.isNotEmpty(value)) { if (currentValue.getModelEntity().isField(name.toString())) { currentValue.setString(name.toString(), value.toString()); + 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); } @@ -575,6 +672,11 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error 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); + } + } } } } diff --git a/specialpurpose/example/data/ExampleDemoData.xml b/specialpurpose/example/data/ExampleDemoData.xml index 57b861e538..619c893940 100644 --- a/specialpurpose/example/data/ExampleDemoData.xml +++ b/specialpurpose/example/data/ExampleDemoData.xml @@ -61,6 +61,20 @@ under the License. - - + + + + + + + + + + + + + + + +