use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.
the class GenericDelegator method findListIteratorByCondition.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#findListIteratorByCondition(org.apache.ofbiz.entity.model.DynamicViewEntity, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.condition.EntityCondition, java.util.Collection, java.util.List, org.apache.ofbiz.entity.util.EntityFindOptions)
*/
@Override
public EntityListIterator findListIteratorByCondition(DynamicViewEntity dynamicViewEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, Collection<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);
}
ModelViewEntity modelViewEntity = dynamicViewEntity.makeModelViewEntity(this);
if (whereEntityCondition != null) {
whereEntityCondition.checkCondition(modelViewEntity);
}
if (havingEntityCondition != null) {
havingEntityCondition.checkCondition(modelViewEntity);
}
GenericHelper helper = getEntityHelper(dynamicViewEntity.getOneRealEntityName());
EntityListIterator eli = helper.findListIteratorByCondition(this, modelViewEntity, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderBy, findOptions);
eli.setDelegator(this);
// TODO: add decrypt fields
return eli;
}
use of org.apache.ofbiz.entity.model.ModelViewEntity 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.ModelViewEntity in project ofbiz-framework by apache.
the class GenericDAO method updateByCondition.
public int updateByCondition(ModelEntity modelEntity, Map<String, ? extends Object> fieldsToSet, EntityCondition condition, SQLProcessor sqlP) throws GenericEntityException {
if (modelEntity == null || fieldsToSet == null || condition == null)
return 0;
if (modelEntity instanceof ModelViewEntity) {
throw new org.apache.ofbiz.entity.GenericNotImplementedException("Operation updateByCondition not supported yet for view entities");
}
StringBuilder sql = new StringBuilder("UPDATE ").append(modelEntity.getTableName(datasource));
sql.append(" SET ");
List<EntityConditionParam> params = new LinkedList<EntityConditionParam>();
for (Map.Entry<String, ? extends Object> entry : fieldsToSet.entrySet()) {
String name = entry.getKey();
ModelField field = modelEntity.getField(name);
if (field != null) {
if (!params.isEmpty()) {
sql.append(", ");
}
sql.append(field.getColName()).append(" = ?");
params.add(new EntityConditionParam(field, entry.getValue()));
}
}
sql.append(" WHERE ").append(condition.makeWhereString(modelEntity, params, this.datasource));
sqlP.prepareStatement(sql.toString());
for (EntityConditionParam param : params) {
SqlJdbcUtil.setValue(sqlP, param.getModelField(), modelEntity.getEntityName(), param.getFieldValue(), modelFieldTypeReader);
}
return sqlP.executeUpdate();
}
use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.
the class GenericDAO method partialSelect.
public void partialSelect(GenericEntity entity, Set<String> keys) throws GenericEntityException {
ModelEntity modelEntity = entity.getModelEntity();
if (modelEntity instanceof ModelViewEntity) {
throw new org.apache.ofbiz.entity.GenericNotImplementedException("Operation partialSelect not supported yet for view entities");
}
// we don't want to select ALL fields, just the nonpk fields that are in the passed GenericEntity
List<ModelField> partialFields = new LinkedList<ModelField>();
Set<String> tempKeys = new TreeSet<String>(keys);
Iterator<ModelField> entityFieldIter = modelEntity.getFieldsIterator();
while (entityFieldIter.hasNext()) {
ModelField curField = entityFieldIter.next();
if (tempKeys.contains(curField.getName())) {
partialFields.add(curField);
tempKeys.remove(curField.getName());
}
}
if (tempKeys.size() > 0) {
throw new GenericModelException("In partialSelect invalid field names specified: " + tempKeys.toString());
}
StringBuilder sqlBuffer = new StringBuilder("SELECT ");
if (partialFields.size() > 0) {
modelEntity.colNameString(partialFields, sqlBuffer, "", ", ", "", datasource.getAliasViewColumns());
} else {
sqlBuffer.append("*");
}
sqlBuffer.append(SqlJdbcUtil.makeFromClause(modelEntity, modelFieldTypeReader, datasource));
sqlBuffer.append(SqlJdbcUtil.makeWhereClause(modelEntity, modelEntity.getPkFieldsUnmodifiable(), entity, "AND", datasource.getJoinStyle()));
try (SQLProcessor sqlP = new SQLProcessor(entity.getDelegator(), helperInfo)) {
sqlP.prepareStatement(sqlBuffer.toString(), true, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
sqlP.executeQuery();
if (sqlP.next()) {
for (int j = 0; j < partialFields.size(); j++) {
ModelField curField = partialFields.get(j);
SqlJdbcUtil.getValue(sqlP.getResultSet(), j + 1, curField, entity, modelFieldTypeReader);
}
entity.synchronizedWithDatasource();
} else {
throw new GenericEntityNotFoundException("Result set was empty for entity: " + entity.toString());
}
}
}
use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.
the class GenericDAO method makeConditionHavingString.
protected StringBuilder makeConditionHavingString(StringBuilder havingString, String prefix, ModelEntity modelEntity, EntityCondition havingEntityCondition, List<EntityCondition> viewHavingConditions, List<EntityConditionParam> havingEntityConditionParams) throws GenericEntityException {
ModelViewEntity modelViewEntity = null;
if (modelEntity instanceof ModelViewEntity) {
modelViewEntity = (ModelViewEntity) modelEntity;
}
String entityCondHavingString = "";
if (havingEntityCondition != null) {
entityCondHavingString = havingEntityCondition.makeWhereString(modelEntity, havingEntityConditionParams, this.datasource);
}
String viewEntityCondHavingString = null;
if (modelViewEntity != null) {
EntityCondition viewHavingEntityCondition = EntityCondition.makeCondition(viewHavingConditions);
viewEntityCondHavingString = viewHavingEntityCondition.makeWhereString(modelEntity, havingEntityConditionParams, this.datasource);
}
if (UtilValidate.isNotEmpty(entityCondHavingString) || UtilValidate.isNotEmpty(viewEntityCondHavingString)) {
havingString.append(prefix);
}
if (UtilValidate.isNotEmpty(entityCondHavingString)) {
boolean addParens = entityCondHavingString.charAt(0) != '(';
if (addParens)
havingString.append("(");
havingString.append(entityCondHavingString);
if (addParens)
havingString.append(")");
}
if (UtilValidate.isNotEmpty(viewEntityCondHavingString)) {
if (UtilValidate.isNotEmpty(entityCondHavingString))
havingString.append(" AND ");
boolean addParens = viewEntityCondHavingString.charAt(0) != '(';
if (addParens)
havingString.append("(");
havingString.append(viewEntityCondHavingString);
if (addParens)
havingString.append(")");
}
return havingString;
}
Aggregations