use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDelegator method getRelatedDummyPK.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#getRelatedDummyPK(java.lang.String, java.util.Map, org.apache.ofbiz.entity.GenericValue)
*/
@Override
public GenericPK getRelatedDummyPK(String relationName, Map<String, ? extends Object> byAndFields, GenericValue value) 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);
}
ModelEntity relatedEntity = getModelReader().getModelEntity(relation.getRelEntityName());
// 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 GenericPK.create(this, relatedEntity, fields);
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDelegator method find.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#find(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.condition.EntityCondition, java.util.Set, java.util.List, org.apache.ofbiz.entity.util.EntityFindOptions)
*/
@Override
public EntityListIterator find(String entityName, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, Set<String> fieldsToSelect, List<String> orderBy, EntityFindOptions findOptions) throws GenericEntityException {
// if there is no transaction throw an exception, we don't want to create a transaction here since closing it would mess up the ELI
if (!TransactionUtil.isTransactionInPlace()) {
// throw new GenericEntityException("ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.");
// throwing an exception is a little harsh for now, just display a really big error message since we want to get all of these fixed...
Exception newE = new Exception("Stack Trace");
Debug.logError(newE, "ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
}
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());
EntityListIterator eli = helper.findListIteratorByCondition(this, modelEntity, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderBy, findOptions);
eli.setDelegator(this);
ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, dummyValue, false);
return eli;
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class ScriptHelperImpl method findOne.
public Map<String, Object> findOne(String entityName, Map<String, ? extends Object> fields, Map<String, ? extends Object> args) throws ScriptException {
Assert.notNull("entityName", entityName);
if (args == null) {
args = EMPTY_ARGS;
}
boolean useCache = "true".equals(args.get("useCache"));
boolean autoFieldMap = !"false".equals(args.get("autoFieldMap"));
List<String> selectFieldList = UtilGenerics.checkList(args.get("selectFieldList"));
ModelEntity modelEntity = ctxHelper.getDelegator().getModelEntity(entityName);
if (modelEntity == null) {
throw new ScriptException("Error running script " + ctxHelper.getScriptName() + " - no entity definition found for entity name [" + entityName + "]");
}
return runFindByPrimaryKey(modelEntity, ctxHelper, useCache, autoFieldMap, fields, selectFieldList);
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDAO method delete.
public int delete(GenericEntity entity, SQLProcessor sqlP) throws GenericEntityException {
ModelEntity modelEntity = entity.getModelEntity();
if (modelEntity instanceof ModelViewEntity) {
throw new org.apache.ofbiz.entity.GenericNotImplementedException("Operation delete not supported yet for view entities");
}
StringBuilder sql = new StringBuilder().append("DELETE FROM ").append(modelEntity.getTableName(datasource)).append(" WHERE ");
SqlJdbcUtil.makeWhereStringFromFields(sql, modelEntity.getPkFieldsUnmodifiable(), entity, "AND");
int retVal;
sqlP.prepareStatement(sql.toString());
SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
retVal = sqlP.executeUpdate();
entity.removedFromDatasource();
return retVal;
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class GenericDAO method select.
public void select(GenericEntity entity, SQLProcessor sqlP) throws GenericEntityException {
ModelEntity modelEntity = entity.getModelEntity();
if (modelEntity.getPksSize() <= 0) {
throw new GenericEntityException("Entity has no primary keys, cannot select by primary key");
}
StringBuilder sqlBuffer = new StringBuilder("SELECT ");
if (modelEntity.getNopksSize() > 0) {
modelEntity.colNameString(modelEntity.getNopksCopy(), sqlBuffer, "", ", ", "", datasource.getAliasViewColumns());
} else {
sqlBuffer.append("*");
}
sqlBuffer.append(SqlJdbcUtil.makeFromClause(modelEntity, modelFieldTypeReader, datasource));
sqlBuffer.append(SqlJdbcUtil.makeWhereClause(modelEntity, modelEntity.getPkFieldsUnmodifiable(), entity, "AND", datasource.getJoinStyle()));
sqlP.prepareStatement(sqlBuffer.toString(), true, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
sqlP.executeQuery();
if (sqlP.next()) {
int idx = 1;
Iterator<ModelField> nopkIter = modelEntity.getNopksIterator();
while (nopkIter.hasNext()) {
ModelField curField = nopkIter.next();
SqlJdbcUtil.getValue(sqlP.getResultSet(), idx, curField, entity, modelFieldTypeReader);
idx++;
}
entity.synchronizedWithDatasource();
} else {
// Debug.logWarning("[GenericDAO.select]: select failed, result set was empty for entity: " + entity.toString(), module);
throw new GenericEntityNotFoundException("Result set was empty for entity: " + entity.toString());
}
}
Aggregations