use of org.apache.ofbiz.entity.model.ModelEntity 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);
}
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDelegator method makeValue.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#makeValue(org.w3c.dom.Element)
*/
@Override
public GenericValue makeValue(Element element) {
if (element == null) {
return null;
}
String entityName = element.getTagName();
// 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);
}
GenericValue value = this.makeValue(entityName);
ModelEntity modelEntity = value.getModelEntity();
Iterator<ModelField> modelFields = modelEntity.getFieldsIterator();
while (modelFields.hasNext()) {
ModelField modelField = modelFields.next();
String name = modelField.getName();
String attr = element.getAttribute(name);
if (UtilValidate.isNotEmpty(attr)) {
// that and treat it as null
if (GenericEntity.NULL_FIELD.toString().equals(attr)) {
value.set(name, null);
} else {
value.setString(name, attr);
}
} else {
// if no attribute try a subelement
Element subElement = UtilXml.firstChildElement(element, name);
if (subElement != null) {
value.setString(name, UtilXml.elementValue(subElement));
}
}
}
return value;
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDelegator method removeByCondition.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#removeByCondition(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition)
*/
@Override
public int removeByCondition(String entityName, EntityCondition condition) throws GenericEntityException {
boolean beganTransaction = false;
try {
if (alwaysUseTransaction) {
beganTransaction = TransactionUtil.begin();
}
ModelEntity modelEntity = getModelReader().getModelEntity(entityName);
GenericHelper helper = getEntityHelper(entityName);
List<GenericValue> removedEntities = null;
if (testMode) {
removedEntities = this.findList(entityName, condition, null, null, null, false);
}
int rowsAffected = helper.removeByCondition(this, modelEntity, condition);
if (rowsAffected > 0) {
this.clearCacheLine(entityName);
}
if (testMode) {
for (GenericValue entity : removedEntities) {
storeForTestRollback(new TestOperation(OperationType.DELETE, entity));
}
}
TransactionUtil.commit(beganTransaction);
return rowsAffected;
} catch (IllegalStateException | GenericEntityException e) {
String errMsg = "Failure in removeByCondition operation for entity [" + entityName + "]: " + e.toString() + ". Rolling back transaction.";
Debug.logError(e, errMsg, module);
TransactionUtil.rollback(beganTransaction, errMsg, e);
throw new GenericEntityException(e);
}
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDAO method update.
public int update(GenericEntity entity) throws GenericEntityException {
ModelEntity modelEntity = entity.getModelEntity();
// we don't want to update ALL fields, just the nonpk fields that are in the passed GenericEntity
List<ModelField> partialFields = new LinkedList<ModelField>();
Collection<String> keys = entity.getAllKeys();
Iterator<ModelField> nopkIter = modelEntity.getNopksIterator();
while (nopkIter.hasNext()) {
ModelField curField = nopkIter.next();
if (keys.contains(curField.getName())) {
partialFields.add(curField);
}
}
return customUpdate(entity, modelEntity, partialFields);
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericEntity method checkFks.
/**
* Checks to see if all foreign key records exist in the database. Will create a dummy value for
* those missing when specified.
*
* @param insertDummy Create a dummy record using the provided fields
* @return true if all FKs exist (or when all missing are created)
* @throws GenericEntityException
*/
public boolean checkFks(boolean insertDummy) throws GenericEntityException {
ModelEntity model = this.getModelEntity();
Iterator<ModelRelation> relItr = model.getRelationsIterator();
while (relItr.hasNext()) {
ModelRelation relation = relItr.next();
if ("one".equalsIgnoreCase(relation.getType())) {
// see if the related value exists
Map<String, Object> fields = new HashMap<>();
for (ModelKeyMap keyMap : relation.getKeyMaps()) {
fields.put(keyMap.getRelFieldName(), this.get(keyMap.getFieldName()));
}
EntityFieldMap ecl = EntityCondition.makeCondition(fields);
long count = this.getDelegator().findCountByCondition(relation.getRelEntityName(), ecl, null, null);
if (count == 0) {
if (insertDummy) {
// create the new related value (dummy)
GenericValue newValue = this.getDelegator().makeValue(relation.getRelEntityName());
boolean allFieldsSet = true;
for (ModelKeyMap mkm : relation.getKeyMaps()) {
if (this.get(mkm.getFieldName()) != null) {
newValue.set(mkm.getRelFieldName(), this.get(mkm.getFieldName()));
if (Debug.infoOn()) {
Debug.logInfo("Set [" + mkm.getRelFieldName() + "] to - " + this.get(mkm.getFieldName()), module);
}
} else {
allFieldsSet = false;
}
}
if (allFieldsSet) {
if (Debug.infoOn()) {
Debug.logInfo("Creating place holder value : " + newValue, module);
}
// inherit create and update times from this value in order to make this not seem like new/fresh data
newValue.put(ModelEntity.CREATE_STAMP_FIELD, this.get(ModelEntity.CREATE_STAMP_FIELD));
newValue.put(ModelEntity.CREATE_STAMP_TX_FIELD, this.get(ModelEntity.CREATE_STAMP_TX_FIELD));
newValue.put(ModelEntity.STAMP_FIELD, this.get(ModelEntity.STAMP_FIELD));
newValue.put(ModelEntity.STAMP_TX_FIELD, this.get(ModelEntity.STAMP_TX_FIELD));
// set isFromEntitySync so that create/update stamp fields set above will be preserved
newValue.setIsFromEntitySync(true);
// check the FKs for the newly created entity
newValue.checkFks(true);
newValue.create();
}
} else {
return false;
}
}
}
}
return true;
}
Aggregations