private Map<?, ?> convertFlagsToKeys(Class<? extends Entity> javaType, Map<?, ?> config) {
    if (config == null || config.isEmpty() || javaType == null) return config;

    Map<String, ConfigKey<?>> configKeys = EntityTypes.getDefinedConfigKeys(javaType);
    Map<Object, Object> result = new LinkedHashMap<Object, Object>();
    for (Map.Entry<?, ?> entry : config.entrySet()) {
      log.debug(
          "Setting key {} to {} for REST creation of {}",
          new Object[] {entry.getKey(), entry.getValue(), javaType});
      Object key = configKeys.get(entry.getKey());
      if (key == null) {
        log.warn(
            "Unrecognised config key {} passed to {}; will be treated as flag (and likely ignored)",
            entry.getKey(),
            javaType);
        key = entry.getKey();
      }
      result.put(key, entry.getValue());
    }
    return result;
  }
  public static BasicEntityMemento.Builder newEntityMementoBuilder(Entity entity) {
    EntityDynamicType definedType = EntityTypes.getDefinedEntityType(entity.getClass());
    BasicEntityMemento.Builder builder = BasicEntityMemento.builder();

    builder.id = entity.getId();
    builder.displayName = entity.getDisplayName();
    builder.type = entity.getClass().getName();
    builder.typeClass = entity.getClass();

    // TODO the dynamic attributeKeys and configKeys are computed in the BasicEntityMemento
    // whereas effectors are computed here -- should be consistent!
    // (probably best to compute attrKeys and configKeys here)
    builder.effectors.addAll(entity.getEntityType().getEffectors());
    builder.effectors.removeAll(definedType.getEffectors().values());

    builder.isTopLevelApp = (entity instanceof Application && entity.getParent() == null);

    Map<ConfigKey<?>, Object> localConfig =
        ((EntityInternal) entity).getConfigMap().getLocalConfig();
    for (Map.Entry<ConfigKey<?>, Object> entry : localConfig.entrySet()) {
      ConfigKey<?> key = checkNotNull(entry.getKey(), localConfig);
      Object value = configValueToPersistable(entry.getValue());
      builder.config.put(key, value);
    }

    Map<String, Object> localConfigUnmatched =
        MutableMap.copyOf(
            ((EntityInternal) entity).getConfigMap().getLocalConfigBag().getAllConfig());
    for (ConfigKey<?> key : localConfig.keySet()) {
      localConfigUnmatched.remove(key.getName());
    }
    for (Map.Entry<String, Object> entry : localConfigUnmatched.entrySet()) {
      String key = checkNotNull(entry.getKey(), localConfig);
      Object value = entry.getValue();
      // TODO Not transforming; that code is deleted in another pending PR anyway!
      builder.configUnmatched.put(key, value);
    }

    @SuppressWarnings("rawtypes")
    Map<AttributeSensor, Object> allAttributes = ((EntityInternal) entity).getAllAttributes();
    for (@SuppressWarnings("rawtypes")
    Map.Entry<AttributeSensor, Object> entry : allAttributes.entrySet()) {
      AttributeSensor<?> key = checkNotNull(entry.getKey(), allAttributes);
      Object value = entry.getValue();
      builder.attributes.put((AttributeSensor<?>) key, value);
    }

    for (Location location : entity.getLocations()) {
      builder.locations.add(location.getId());
    }

    for (Entity child : entity.getChildren()) {
      builder.children.add(child.getId());
    }

    for (Policy policy : entity.getPolicies()) {
      builder.policies.add(policy.getId());
    }

    for (Enricher enricher : entity.getEnrichers()) {
      builder.enrichers.add(enricher.getId());
    }

    Entity parentEntity = entity.getParent();
    builder.parent = (parentEntity != null) ? parentEntity.getId() : null;

    if (entity instanceof Group) {
      for (Entity member : ((Group) entity).getMembers()) {
        builder.members.add(member.getId());
      }
    }

    return builder;
  }