* @param code
* @return Returns null if no entity was found.
* @throws RulesException
*/
public IREntity findEntity(AutoDataMap autoDataMap, String entity, Label label, Object key) {
IREntity e = null;
IRObject iKey = iconvert(key);
try {
if(label.isSingular()){ // If singular, look for an instance
e = autoDataMap.getEntities().get(entity); // on the entity stack.
if(e==null){ // None found? create one.
e = autoDataMap.getSession().getState().findEntity(entity+"."+entity);
if(e==null){
e = autoDataMap.getSession().createEntity(null,entity);
autoDataMap.getEntities().put(entity,e); // Remember the one we created, so we
}
} // don't create another one.
}else {
String skey = entity+"$"+iKey.stringValue();
if(key!=null &&
!(key instanceof String
&& ((String)key).length()==0)) { // NOTE: We are NOT allowing "" as a key here!
// If so, construct a key for that entity
e = (IREntity)autoDataMap.getEntities().get(skey); // and look for it.
}
if(e==null) { // Haven't created an entity with that key?
e = autoDataMap.getSession().createEntity(iKey,entity);// do so.
if(key!=null){
autoDataMap.getEntities().put(skey,e); // If we have a key, remember this entity!
}
}
}
if(e==null)throw new RuntimeException("Failed to create the entity "+entity);
if(key != null){
e.addAttribute(
IREntity.mappingKey,
key.toString(),
iKey,
false,
true,
IRObject.iString,
"", "", "","");
e.put(null, IREntity.mappingKey, iKey);
}
return e;
} catch (RulesException e1) {
return null;