use of org.apache.ofbiz.entity.util.EntityStoreOptions in project ofbiz-framework by apache.
the class GenericDelegator method storeAll.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#storeAll(java.util.List, org.apache.ofbiz.entity.util.EntityStoreOptions)
*/
@Override
public int storeAll(List<GenericValue> values, EntityStoreOptions storeOptions) throws GenericEntityException {
if (values == null) {
return 0;
}
// if no store options passed, use default
if (storeOptions == null) {
storeOptions = new EntityStoreOptions();
}
int numberChanged = 0;
boolean beganTransaction = false;
try {
beganTransaction = TransactionUtil.begin();
for (GenericValue value : values) {
String entityName = value.getEntityName();
GenericPK primaryKey = value.getPrimaryKey();
GenericHelper helper = getEntityHelper(entityName);
// NOTE: don't use findByPrimaryKey because we don't want to the ECA events to fire and such
if (!primaryKey.isPrimaryKey()) {
throw new GenericModelException("[GenericDelegator.storeAll] One of the passed primary keys is not a valid primary key: " + primaryKey);
}
GenericValue existing = null;
try {
existing = helper.findByPrimaryKey(primaryKey);
} catch (GenericEntityNotFoundException e) {
existing = null;
}
if (existing == null) {
if (storeOptions.isCreateDummyFks()) {
value.checkFks(true);
}
this.create(value);
numberChanged++;
} else {
// don't send fields that are the same, and if no fields have changed, update nothing
ModelEntity modelEntity = value.getModelEntity();
GenericValue toStore = GenericValue.create(this, modelEntity, value.getPrimaryKey());
boolean atLeastOneField = false;
Iterator<ModelField> nonPksIter = modelEntity.getNopksIterator();
while (nonPksIter.hasNext()) {
ModelField modelField = nonPksIter.next();
String fieldName = modelField.getName();
if (value.containsKey(fieldName)) {
Object fieldValue = value.get(fieldName);
Object oldValue = existing.get(fieldName);
if (!UtilObject.equalsHelper(oldValue, fieldValue)) {
toStore.put(fieldName, fieldValue);
atLeastOneField = true;
}
}
}
if (atLeastOneField) {
if (storeOptions.isCreateDummyFks()) {
value.checkFks(true);
}
numberChanged += this.store(toStore);
}
}
}
TransactionUtil.commit(beganTransaction);
return numberChanged;
} catch (GenericEntityException e) {
String errMsg = "Failure in storeAll operation: " + e.toString() + ". Rolling back transaction.";
Debug.logError(e, errMsg, module);
TransactionUtil.rollback(beganTransaction, errMsg, e);
throw new GenericEntityException(e);
}
}
Aggregations