Search in sources :

Example 36 with ModelViewEntity

use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.

the class EntityConditionBase method getColName.

protected String getColName(Map<String, String> tableAliases, ModelEntity modelEntity, ModelField modelField, String fieldName, boolean includeTableNamePrefix, Datasource datasourceInfo) {
    if (modelEntity == null || modelField == null) {
        return fieldName;
    }
    // if this is a view entity and we are configured to alias the views, use the alias here instead of the composite (ie table.column) field name
    if (datasourceInfo != null && datasourceInfo.getAliasViewColumns() && modelEntity instanceof ModelViewEntity) {
        ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity;
        ModelAlias modelAlias = modelViewEntity.getAlias(fieldName);
        if (modelAlias != null) {
            return modelAlias.getColAlias();
        }
    }
    String colName = getColName(modelField, fieldName);
    if (includeTableNamePrefix && datasourceInfo != null) {
        String tableName = modelEntity.getTableName(datasourceInfo);
        if (tableAliases.containsKey(tableName)) {
            tableName = tableAliases.get(tableName);
        }
        colName = tableName + "." + colName;
    }
    return colName;
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ModelAlias(org.apache.ofbiz.entity.model.ModelViewEntity.ModelAlias)

Example 37 with ModelViewEntity

use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.

the class EntityConditionSubSelect method addSqlValue.

@Override
public void addSqlValue(StringBuilder sql, Map<String, String> tableAliases, ModelEntity parentModelEntity, List<EntityConditionParam> entityConditionParams, boolean includeTableNamePrefix, Datasource datasourceInfo) {
    if (localModelEntity instanceof ModelViewEntity && datasourceInfo == null) {
        throw new IllegalArgumentException("Call to EntityConditionSubSelect.addSqlValue with datasourceInfo=null which is not allowed because the local entity [" + this.localModelEntity.getEntityName() + "] is a view entity");
    }
    try {
        // add select and where and such, based on local entity not on the main entity
        ModelField localModelField = localModelEntity.getField(this.keyFieldName);
        if (this.requireAll) {
            sql.append(" ALL(");
        } else {
            sql.append(" ANY(");
        }
        sql.append("SELECT ");
        sql.append(localModelField.getColName());
        // FROM clause and when necessary the JOIN or LEFT JOIN clause(s) as well
        sql.append(SqlJdbcUtil.makeFromClause(localModelEntity, null, datasourceInfo));
        // WHERE clause
        StringBuilder whereString = new StringBuilder();
        String entityCondWhereString = "";
        if (this.whereCond != null) {
            entityCondWhereString = this.whereCond.makeWhereString(localModelEntity, entityConditionParams, datasourceInfo);
        }
        String viewClause = SqlJdbcUtil.makeViewWhereClause(localModelEntity, (datasourceInfo != null ? datasourceInfo.getJoinStyle() : null));
        if (viewClause.length() > 0) {
            if (entityCondWhereString.length() > 0) {
                whereString.append("(");
                whereString.append(entityCondWhereString);
                whereString.append(") AND ");
            }
            whereString.append(viewClause);
        } else {
            whereString.append(entityCondWhereString);
        }
        if (whereString.length() > 0) {
            sql.append(" WHERE ");
            sql.append(whereString.toString());
        }
        sql.append(")");
    } catch (GenericEntityException e) {
        String errMsg = "Could not generate sub-select SQL: " + e.toString();
        Debug.logError(e, errMsg, module);
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity)

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