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;
}
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();
}
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);
}
}
}
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));
}
}
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));
}
}
Aggregations