* @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.