use of org.apache.ofbiz.entity.datasource.GenericHelper in project ofbiz-framework by apache.
the class GenericDelegator method findCountByCondition.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#findCountByCondition(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.util.EntityFindOptions)
*/
@Override
public long findCountByCondition(String entityName, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, EntityFindOptions findOptions) throws GenericEntityException {
boolean beganTransaction = false;
try {
if (alwaysUseTransaction) {
beganTransaction = TransactionUtil.begin();
}
ModelEntity modelEntity = getModelReader().getModelEntity(entityName);
GenericValue dummyValue = GenericValue.create(modelEntity);
EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(modelEntity.getEntityName());
ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, dummyValue, false);
if (whereEntityCondition != null) {
whereEntityCondition.checkCondition(modelEntity);
}
if (havingEntityCondition != null) {
havingEntityCondition.checkCondition(modelEntity);
}
ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, dummyValue, false);
GenericHelper helper = getEntityHelper(modelEntity.getEntityName());
long count = helper.findCountByCondition(this, modelEntity, whereEntityCondition, havingEntityCondition, findOptions);
ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, dummyValue, false);
TransactionUtil.commit(beganTransaction);
return count;
} catch (GenericEntityException e) {
String errMsg = "Failure in findListIteratorByCondition operation for entity [DynamicView]: " + 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.datasource.GenericHelper in project ofbiz-framework by apache.
the class GenericDelegator method findByPrimaryKeyPartial.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#findByPrimaryKeyPartial(org.apache.ofbiz.entity.GenericPK, java.util.Set)
*/
@Override
public GenericValue findByPrimaryKeyPartial(GenericPK primaryKey, Set<String> keys) throws GenericEntityException {
boolean beganTransaction = false;
try {
if (alwaysUseTransaction) {
beganTransaction = TransactionUtil.begin();
}
EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(primaryKey.getEntityName());
ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, primaryKey, false);
GenericHelper helper = getEntityHelper(primaryKey.getEntityName());
GenericValue value = null;
if (!primaryKey.isPrimaryKey()) {
throw new GenericModelException("[GenericDelegator.findByPrimaryKey] Passed primary key is not a valid primary key: " + primaryKey);
}
ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, primaryKey, false);
try {
value = helper.findByPrimaryKeyPartial(primaryKey, keys);
} catch (GenericEntityNotFoundException e) {
}
if (value != null) {
value.setDelegator(this);
}
ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, primaryKey, false);
TransactionUtil.commit(beganTransaction);
return value;
} catch (GenericEntityException e) {
String errMsg = "Failure in findByPrimaryKeyPartial operation for entity [" + primaryKey.getEntityName() + "]: " + 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.datasource.GenericHelper in project ofbiz-framework by apache.
the class GenericDelegator method findOne.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#findOne(java.lang.String, java.util.Map, boolean)
*/
@Override
public GenericValue findOne(String entityName, Map<String, ? extends Object> fields, boolean useCache) throws GenericEntityException {
GenericPK primaryKey = this.makePK(entityName, fields);
if (!primaryKey.isPrimaryKey()) {
throw new GenericModelException("[GenericDelegator.findOne] Passed primary key is not a valid primary key: " + primaryKey);
}
EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(entityName);
if (useCache) {
ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_CHECK, EntityEcaHandler.OP_FIND, primaryKey, false);
GenericValue value = cache.get(primaryKey);
if (value == GenericValue.NULL_VALUE) {
return null;
}
if (value != null) {
return value;
}
}
boolean beganTransaction = false;
try {
if (alwaysUseTransaction) {
beganTransaction = TransactionUtil.begin();
}
ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, primaryKey, false);
GenericHelper helper = getEntityHelper(entityName);
GenericValue value = null;
ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, primaryKey, false);
try {
value = helper.findByPrimaryKey(primaryKey);
} catch (GenericEntityNotFoundException e) {
}
if (value != null) {
value.setDelegator(this);
}
if (useCache) {
if (value != null) {
ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_PUT, EntityEcaHandler.OP_FIND, value, false);
this.putInPrimaryKeyCache(primaryKey, value);
} else {
this.putInPrimaryKeyCache(primaryKey, GenericValue.NULL_VALUE);
}
}
ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, (value == null ? primaryKey : value), false);
TransactionUtil.commit(beganTransaction);
return value;
} catch (GenericEntityException e) {
String errMsg = "Failure in findOne 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.datasource.GenericHelper in project ofbiz-framework by apache.
the class GenericDelegator method store.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#store(org.apache.ofbiz.entity.GenericValue)
*/
@Override
public int store(GenericValue value) throws GenericEntityException {
boolean beganTransaction = false;
try {
if (alwaysUseTransaction) {
beganTransaction = TransactionUtil.begin();
}
EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(value.getEntityName());
ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_STORE, value, false);
GenericHelper helper = getEntityHelper(value.getEntityName());
ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_STORE, value, false);
// if audit log on for any fields, save old value before the update so we still have both
if (value.getModelEntity().getHasFieldWithAuditLog()) {
createEntityAuditLogAll(value, true, false);
}
GenericValue updatedEntity = null;
if (testMode) {
updatedEntity = this.findOne(value.getEntityName(), value.getPrimaryKey(), false);
}
int retVal = helper.store(value);
// doCacheClear
ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_STORE, value, false);
this.clearCacheLine(value);
if (testMode) {
storeForTestRollback(new TestOperation(OperationType.UPDATE, updatedEntity));
}
// refresh the valueObject to get the new version
if (value.lockEnabled()) {
refresh(value);
}
ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_STORE, value, false);
TransactionUtil.commit(beganTransaction);
return retVal;
} catch (IllegalStateException | GenericEntityException e) {
String errMsg = "Failure in store operation for entity [" + value.getEntityName() + "]: " + 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.datasource.GenericHelper in project ofbiz-framework by apache.
the class GenericDelegator method storeByCondition.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#storeByCondition(java.lang.String, java.util.Map, org.apache.ofbiz.entity.condition.EntityCondition)
*/
@Override
public int storeByCondition(String entityName, Map<String, ? extends Object> fieldsToSet, EntityCondition condition) throws GenericEntityException {
boolean beganTransaction = false;
try {
if (alwaysUseTransaction) {
beganTransaction = TransactionUtil.begin();
}
ModelEntity modelEntity = getModelReader().getModelEntity(entityName);
GenericHelper helper = getEntityHelper(entityName);
List<GenericValue> updatedEntities = null;
if (testMode) {
updatedEntities = this.findList(entityName, condition, null, null, null, false);
}
int rowsAffected = helper.storeByCondition(this, modelEntity, fieldsToSet, condition);
if (rowsAffected > 0) {
this.clearCacheLine(entityName);
}
if (testMode) {
for (GenericValue entity : updatedEntities) {
storeForTestRollback(new TestOperation(OperationType.UPDATE, entity));
}
}
TransactionUtil.commit(beganTransaction);
return rowsAffected;
} catch (IllegalStateException | GenericEntityException e) {
String errMsg = "Failure in storeByCondition operation for entity [" + entityName + "]: " + e.toString() + ". Rolling back transaction.";
Debug.logError(e, errMsg, module);
TransactionUtil.rollback(beganTransaction, errMsg, e);
throw new GenericEntityException(e);
}
}
Aggregations