private Object toScriptEngineValueMap(Entity entity, int depth) {
    if (entity != null) {
      Object idValue = toScriptEngineValue(entity, entity.getEntityType().getIdAttribute(), 0);
      if (depth == 0) {
        return idValue;
      } else {
        Map<String, Object> map = Maps.newHashMap();
        entity
            .getEntityType()
            .getAtomicAttributes()
            .forEach(attr -> map.put(attr.getName(), toScriptEngineValue(entity, attr, depth)));
        map.put(KEY_ID_VALUE, idValue);
        return map;
      }
    } else {
      return null;
    }
  }
private Object toScriptEngineValueMap ( Entity entity , int depth ) { if ( entity != null ) { Object idValue = toScriptEngineValue ( entity , entity . getEntityType ( ) . getIdAttribute ( ) , 0 ) ; if ( depth == 0 ) { return idValue ; } else { Map < String , Object > map = Maps . newHashMap ( ) ; entity . getEntityType ( ) . getAtomicAttributes ( ) . forEach ( attr -> map . put ( attr . getName ( ) , toScriptEngineValue ( entity , attr , depth ) ) ) ; map . put ( KEY_ID_VALUE , idValue ) ; return map ; } } else { return null ; } }
Convert entity to a JavaScript object. Adds "_idValue" as a special key to every level for
quick access to the id value of an entity.

@param entity The entity to be flattened, should start with non null entity
@param depth Represents the number of reference levels being added to the JavaScript object
@return A JavaScript object in Tree form, containing entities and there references