Search in sources :

Example 1 with ModelViewEntity

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;
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) SAXException(org.xml.sax.SAXException) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with ModelViewEntity

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;
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) GenericNotImplementedException(org.apache.ofbiz.entity.GenericNotImplementedException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 3 with ModelViewEntity

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();
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) GenericNotImplementedException(org.apache.ofbiz.entity.GenericNotImplementedException) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap) LinkedList(java.util.LinkedList) EntityConditionParam(org.apache.ofbiz.entity.condition.EntityConditionParam)

Example 4 with ModelViewEntity

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());
        }
    }
}
Also used : GenericModelException(org.apache.ofbiz.entity.GenericModelException) GenericNotImplementedException(org.apache.ofbiz.entity.GenericNotImplementedException) LinkedList(java.util.LinkedList) SQLProcessor(org.apache.ofbiz.entity.jdbc.SQLProcessor) ModelField(org.apache.ofbiz.entity.model.ModelField) TreeSet(java.util.TreeSet) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) GenericEntityNotFoundException(org.apache.ofbiz.entity.GenericEntityNotFoundException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 5 with ModelViewEntity

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;
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition)

Aggregations

ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)37 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)16 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)14 LinkedList (java.util.LinkedList)13 ModelField (org.apache.ofbiz.entity.model.ModelField)13 SQLException (java.sql.SQLException)11 Connection (java.sql.Connection)7 Statement (java.sql.Statement)7 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)7 GenericModelException (org.apache.ofbiz.entity.GenericModelException)6 ModelFieldType (org.apache.ofbiz.entity.model.ModelFieldType)6 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)6 ModelRelation (org.apache.ofbiz.entity.model.ModelRelation)6 GenericNotImplementedException (org.apache.ofbiz.entity.GenericNotImplementedException)5 HashMap (java.util.HashMap)4 TreeSet (java.util.TreeSet)4 GenericValue (org.apache.ofbiz.entity.GenericValue)4 IOException (java.io.IOException)3 Map (java.util.Map)3 GenericDataSourceException (org.apache.ofbiz.entity.GenericDataSourceException)3