Search in sources :

Example 91 with ModelEntity

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

the class GenericEntity method get.

/**
 * Same as the getResource method that does not take resource name, but instead allows manually
 *    specifying the resource name. In general you should use the other method for more consistent
 *    naming and use of the corresponding properties files.
 * @param name The name of the field on the entity
 * @param resource The name of the resource to get the value from; if null defaults to the
 *    default-resource-name on the entity definition, if specified there
 * @param locale The locale to use when finding the ResourceBundle, if null uses the default
 *    locale for the current instance of Java
 * @return If the specified resource is found and contains a key as described above, then that
 *    property value is returned; otherwise returns the field value
 */
public Object get(String name, String resource, Locale locale) {
    Object fieldValue = get(name);
    // In case of view entity first try to retrieve with View field names
    ModelEntity modelEntityToUse = this.getModelEntity();
    Object resourceValue = get(this.getModelEntity(), modelEntityToUse, name, resource, locale);
    if (resourceValue == null) {
        if (modelEntityToUse instanceof ModelViewEntity) {
            // now try to retrieve with the field heading from the real entity linked to the view
            ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntityToUse;
            Iterator<ModelAlias> it = modelViewEntity.getAliasesIterator();
            while (it.hasNext()) {
                ModelAlias modelAlias = it.next();
                if (modelAlias.getName().equalsIgnoreCase(name)) {
                    modelEntityToUse = modelViewEntity.getMemberModelEntity(modelAlias.getEntityAlias());
                    name = modelAlias.getField();
                    break;
                }
            }
            resourceValue = get(this.getModelEntity(), modelEntityToUse, name, resource, locale);
            if (resourceValue == null) {
                return fieldValue;
            }
            return resourceValue;
        }
        return fieldValue;
    }
    return resourceValue;
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ModelAlias(org.apache.ofbiz.entity.model.ModelViewEntity.ModelAlias) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 92 with ModelEntity

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

the class CommonServices method entitySortTest.

/**
 * Test entity sorting
 */
public static Map<String, Object> entitySortTest(DispatchContext dctx, Map<String, ?> context) {
    Delegator delegator = dctx.getDelegator();
    Set<ModelEntity> set = new TreeSet<>();
    set.add(delegator.getModelEntity("Person"));
    set.add(delegator.getModelEntity("PartyRole"));
    set.add(delegator.getModelEntity("Party"));
    set.add(delegator.getModelEntity("ContactMech"));
    set.add(delegator.getModelEntity("PartyContactMech"));
    set.add(delegator.getModelEntity("OrderHeader"));
    set.add(delegator.getModelEntity("OrderItem"));
    set.add(delegator.getModelEntity("OrderContactMech"));
    set.add(delegator.getModelEntity("OrderRole"));
    set.add(delegator.getModelEntity("Product"));
    set.add(delegator.getModelEntity("RoleType"));
    for (ModelEntity modelEntity : set) {
        Debug.logInfo(modelEntity.getEntityName(), module);
    }
    return ServiceUtil.returnSuccess();
}
Also used : Delegator(org.apache.ofbiz.entity.Delegator) TreeSet(java.util.TreeSet) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 93 with ModelEntity

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

the class EntityExpr method checkRhsType.

public void checkRhsType(ModelEntity modelEntity, Delegator delegator) {
    if (this.rhs == null || this.rhs == GenericEntity.NULL_FIELD || modelEntity == null) {
        return;
    }
    Object value = this.rhs;
    if (this.rhs instanceof EntityFunction<?>) {
        value = UtilGenerics.<EntityFunction<?>>cast(this.rhs).getOriginalValue();
    }
    if (value instanceof Collection<?>) {
        Collection<?> valueCol = UtilGenerics.cast(value);
        if (valueCol.size() > 0) {
            value = valueCol.iterator().next();
        } else {
            value = null;
        }
    }
    if (delegator == null) {
        // this will be the common case for now as the delegator isn't available where we want to do this
        // we'll cheat a little here and assume the default delegator
        delegator = DelegatorFactory.getDelegator("default");
    }
    String fieldName = null;
    ModelField curField;
    if (this.lhs instanceof EntityFieldValue) {
        EntityFieldValue efv = (EntityFieldValue) this.lhs;
        fieldName = efv.getFieldName();
        curField = efv.getModelField(modelEntity);
    } else {
        // nothing to check
        return;
    }
    if (curField == null) {
        throw new IllegalArgumentException("FieldName " + fieldName + " not found for entity: " + modelEntity.getEntityName());
    }
    ModelFieldType type = null;
    try {
        type = delegator.getEntityFieldType(modelEntity, curField.getType());
    } catch (GenericEntityException e) {
        Debug.logWarning(e, module);
    }
    if (type == null) {
        throw new IllegalArgumentException("Type " + curField.getType() + " not found for entity [" + modelEntity.getEntityName() + "]; probably because there is no datasource (helper) setup for the entity group that this entity is in: [" + delegator.getEntityGroupName(modelEntity.getEntityName()) + "]");
    }
    if (value instanceof EntityConditionSubSelect) {
        ModelFieldType valueType = null;
        try {
            ModelEntity valueModelEntity = ((EntityConditionSubSelect) value).getModelEntity();
            valueType = delegator.getEntityFieldType(valueModelEntity, valueModelEntity.getField(((EntityConditionSubSelect) value).getKeyFieldName()).getType());
        } catch (GenericEntityException e) {
            Debug.logWarning(e, module);
        }
        if (valueType == null) {
            throw new IllegalArgumentException("Type " + curField.getType() + " not found for entity [" + modelEntity.getEntityName() + "]; probably because there is no datasource (helper) setup for the entity group that this entity is in: [" + delegator.getEntityGroupName(modelEntity.getEntityName()) + "]");
        }
        // make sure the type of keyFieldName of EntityConditionSubSelect  matches the field Java type
        try {
            if (!ObjectType.instanceOf(ObjectType.loadClass(valueType.getJavaType()), type.getJavaType())) {
                String errMsg = "Warning using [" + value.getClass().getName() + "] and entity field [" + modelEntity.getEntityName() + "." + curField.getName() + "]. The Java type of keyFieldName : [" + valueType.getJavaType() + "] is not compatible with the Java type of the field [" + type.getJavaType() + "]";
                // eventually we should do this, but for now we'll do a "soft" failure: throw new IllegalArgumentException(errMsg);
                Debug.logWarning(new Exception("Location of database type warning"), "=-=-=-=-=-=-=-=-= Database type warning in EntityExpr =-=-=-=-=-=-=-=-= " + errMsg, module);
            }
        } catch (ClassNotFoundException e) {
            String errMsg = "Warning using [" + value.getClass().getName() + "] and entity field [" + modelEntity.getEntityName() + "." + curField.getName() + "]. The Java type of keyFieldName : [" + valueType.getJavaType() + "] could not be found]";
            // eventually we should do this, but for now we'll do a "soft" failure: throw new IllegalArgumentException(errMsg);
            Debug.logWarning(e, "=-=-=-=-=-=-=-=-= Database type warning in EntityExpr =-=-=-=-=-=-=-=-= " + errMsg, module);
        }
    } else if (value instanceof EntityFieldValue) {
        EntityFieldValue efv = (EntityFieldValue) this.lhs;
        String rhsFieldName = efv.getFieldName();
        ModelField rhsField = efv.getModelField(modelEntity);
        if (rhsField == null) {
            throw new IllegalArgumentException("FieldName " + rhsFieldName + " not found for entity: " + modelEntity.getEntityName());
        }
        ModelFieldType rhsType = null;
        try {
            rhsType = delegator.getEntityFieldType(modelEntity, rhsField.getType());
        } catch (GenericEntityException e) {
            Debug.logWarning(e, module);
        }
        try {
            if (!ObjectType.instanceOf(ObjectType.loadClass(rhsType.getJavaType()), type.getJavaType())) {
                String errMsg = "Warning using [" + value.getClass().getName() + "] and entity field [" + modelEntity.getEntityName() + "." + curField.getName() + "]. The Java type [" + rhsType.getJavaType() + "] of rhsFieldName : [" + rhsFieldName + "] is not compatible with the Java type of the field [" + type.getJavaType() + "]";
                // eventually we should do this, but for now we'll do a "soft" failure: throw new IllegalArgumentException(errMsg);
                Debug.logWarning(new Exception("Location of database type warning"), "=-=-=-=-=-=-=-=-= Database type warning in EntityExpr =-=-=-=-=-=-=-=- " + errMsg, module);
            }
        } catch (ClassNotFoundException e) {
            String errMsg = "Warning using [" + value.getClass().getName() + "] and entity field [" + modelEntity.getEntityName() + "." + curField.getName() + "]. The Java type [" + rhsType.getJavaType() + "] of rhsFieldName : [" + rhsFieldName + "] could not be found]";
            // eventually we should do this, but for now we'll do a "soft" failure: throw new IllegalArgumentException(errMsg);
            Debug.logWarning(e, "=-=-=-=-=-=-=-=-= Database type warning in EntityExpr =-=-=-=-=-=-=-=-= " + errMsg, module);
        }
    } else {
        // make sure the type matches the field Java type
        if (!ObjectType.instanceOf(value, type.getJavaType())) {
            String errMsg = "In entity field [" + modelEntity.getEntityName() + "." + curField.getName() + "] set the value passed in [" + value.getClass().getName() + "] is not compatible with the Java type of the field [" + type.getJavaType() + "]";
            // eventually we should do this, but for now we'll do a "soft" failure: throw new IllegalArgumentException(errMsg);
            Debug.logWarning(new Exception("Location of database type warning"), "=-=-=-=-=-=-=-=-= Database type warning in EntityExpr =-=-=-=-=-=-=-=-= " + errMsg, module);
        }
    }
}
Also used : GenericModelException(org.apache.ofbiz.entity.GenericModelException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelFieldType(org.apache.ofbiz.entity.model.ModelFieldType) Collection(java.util.Collection) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 94 with ModelEntity

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

the class EntityFieldValue method addSqlValue.

@Override
public void addSqlValue(StringBuilder sql, Map<String, String> tableAliases, ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, boolean includeTableNamePrefix, Datasource datasourceInfo) {
    if (this.modelViewEntity != null) {
        if (UtilValidate.isNotEmpty(entityAlias)) {
            ModelEntity memberModelEntity = modelViewEntity.getMemberModelEntity(entityAlias);
            ModelField modelField = memberModelEntity.getField(fieldName);
            // using entityAliasStack (ordered top to bottom) build a big long alias; not that dots will be replaced after it is combined with the column name in the SQL gen
            if (UtilValidate.isNotEmpty(this.entityAliasStack)) {
                boolean dotUsed = false;
                for (String curEntityAlias : entityAliasStack) {
                    sql.append(curEntityAlias);
                    if (dotUsed) {
                        sql.append("_");
                    } else {
                        sql.append(".");
                        dotUsed = true;
                    }
                }
                sql.append(entityAlias);
                sql.append("_");
                sql.append(modelField.getColName());
            } else {
                sql.append(entityAlias);
                sql.append(".");
                sql.append(modelField.getColName());
            }
        } else {
            sql.append(getColName(tableAliases, modelViewEntity, fieldName, includeTableNamePrefix, datasourceInfo));
        }
    } else {
        sql.append(getColName(tableAliases, modelEntity, fieldName, includeTableNamePrefix, datasourceInfo));
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 95 with ModelEntity

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

the class AbstractEntityConditionCache method remove.

/**
 * Removes all condition caches that include the specified entity.
 */
public void remove(GenericEntity entity) {
    UtilCache.clearCache(getCacheName(entity.getEntityName()));
    ModelEntity model = entity.getModelEntity();
    Iterator<String> it = model.getViewConvertorsIterator();
    while (it.hasNext()) {
        String targetEntityName = it.next();
        UtilCache.clearCache(getCacheName(targetEntityName));
    }
}
Also used : ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Aggregations

ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)102 GenericValue (org.apache.ofbiz.entity.GenericValue)37 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)29 ModelField (org.apache.ofbiz.entity.model.ModelField)28 HashMap (java.util.HashMap)22 Delegator (org.apache.ofbiz.entity.Delegator)17 ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)16 LinkedList (java.util.LinkedList)14 Locale (java.util.Locale)12 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)11 ArrayList (java.util.ArrayList)10 ModelRelation (org.apache.ofbiz.entity.model.ModelRelation)10 IOException (java.io.IOException)8 TreeSet (java.util.TreeSet)8 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)8 Map (java.util.Map)7 GeneralRuntimeException (org.apache.ofbiz.base.util.GeneralRuntimeException)7 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)7 ModelFieldType (org.apache.ofbiz.entity.model.ModelFieldType)7 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)7