use of org.apache.ofbiz.entity.model.ModelEntity 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);
}
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDelegator method findList.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#findList(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition, java.util.Set, java.util.List, org.apache.ofbiz.entity.util.EntityFindOptions, boolean)
*/
@Override
public List<GenericValue> findList(String entityName, EntityCondition entityCondition, Set<String> fieldsToSelect, List<String> orderBy, EntityFindOptions findOptions, boolean useCache) throws GenericEntityException {
EntityEcaRuleRunner<?> ecaRunner = null;
GenericValue dummyValue = null;
if (useCache) {
ecaRunner = this.getEcaRuleRunner(entityName);
ModelEntity modelEntity = getModelReader().getModelEntity(entityName);
dummyValue = GenericValue.create(modelEntity);
ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_CHECK, EntityEcaHandler.OP_FIND, dummyValue, false);
List<GenericValue> cacheList = this.cache.get(entityName, entityCondition, orderBy);
if (cacheList != null) {
return cacheList;
}
}
boolean beganTransaction = false;
try {
if (alwaysUseTransaction) {
beganTransaction = TransactionUtil.begin();
}
List<GenericValue> list = null;
try (EntityListIterator eli = this.find(entityName, entityCondition, null, fieldsToSelect, orderBy, findOptions)) {
list = eli.getCompleteList();
}
if (useCache) {
ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_PUT, EntityEcaHandler.OP_FIND, dummyValue, false);
this.cache.put(entityName, entityCondition, orderBy, list);
}
TransactionUtil.commit(beganTransaction);
return list;
} catch (GenericEntityException e) {
String errMsg = "Failure in findByCondition 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 GenericDelegator method getRelated.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#getRelated(java.lang.String, java.util.Map, java.util.List, org.apache.ofbiz.entity.GenericValue, boolean)
*/
@Override
public List<GenericValue> getRelated(String relationName, Map<String, ? extends Object> byAndFields, List<String> orderBy, GenericValue value, boolean useCache) throws GenericEntityException {
ModelEntity modelEntity = value.getModelEntity();
ModelRelation relation = modelEntity.getRelation(relationName);
if (relation == null) {
throw new GenericModelException("Could not find relation for relationName: " + relationName + " for value " + value);
}
// put the byAndFields (if not null) into the hash map first,
// they will be overridden by value's fields if over-specified this is important for security and cleanliness
Map<String, Object> fields = new HashMap<>();
if (byAndFields != null) {
fields.putAll(byAndFields);
}
for (ModelKeyMap keyMap : relation.getKeyMaps()) {
fields.put(keyMap.getRelFieldName(), value.get(keyMap.getFieldName()));
}
return this.findByAnd(relation.getRelEntityName(), fields, orderBy, useCache);
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDelegator method clearCacheLine.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#clearCacheLine(java.lang.String, java.util.Map)
*/
@Override
public void clearCacheLine(String entityName, Map<String, ? extends Object> fields) {
// if no fields passed, do the all cache quickly and return
if (fields == null) {
cache.remove(entityName);
return;
}
ModelEntity entity = this.getModelEntity(entityName);
if (entity == null) {
throw new IllegalArgumentException("[GenericDelegator.clearCacheLine] could not find entity for entityName: " + entityName);
}
// if never cached, then don't bother clearing
if (entity.getNeverCache()) {
return;
}
GenericValue dummyValue = GenericValue.create(this, entity, fields);
this.clearCacheLineFlexible(dummyValue);
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDelegator method getNextSeqIdLong.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#getNextSeqIdLong(java.lang.String, long)
*/
public Long getNextSeqIdLong(String seqName, long staggerMax) {
try {
SequenceUtil sequencer = this.AtomicRefSequencer.get();
if (sequencer == null) {
ModelEntity seqEntity = this.getModelEntity("SequenceValueItem");
sequencer = new SequenceUtil(this.getEntityHelperInfo("SequenceValueItem"), seqEntity, "seqName", "seqId");
if (!AtomicRefSequencer.compareAndSet(null, sequencer)) {
sequencer = this.AtomicRefSequencer.get();
}
}
ModelEntity seqModelEntity = null;
try {
seqModelEntity = getModelReader().getModelEntity(seqName);
} catch (GenericEntityException e) {
Debug.logInfo("Entity definition not found for sequence name " + seqName, module);
}
Long newSeqId = sequencer == null ? null : sequencer.getNextSeqId(seqName, staggerMax, seqModelEntity);
return newSeqId;
} catch (Exception e) {
String errMsg = "Failure in getNextSeqIdLong operation for seqName [" + seqName + "]: " + e.toString() + ". Rolling back transaction.";
Debug.logError(e, errMsg, module);
throw new GeneralRuntimeException(errMsg, e);
}
}
Aggregations