jerryshao commented on code in PR #9467:
URL: https://github.com/apache/gravitino/pull/9467#discussion_r2613376621


##########
core/src/main/java/org/apache/gravitino/utils/NameIdentifierUtil.java:
##########
@@ -731,4 +733,149 @@ public static Entity.EntityType 
parentEntityType(Entity.EntityType type) {
         throw new IllegalArgumentException("Metalake has no parent entity 
type");
     }
   }
+
+  /**
+   * Build a NameIdentifier for the given entity type and name, using the 
provided entity context.
+   * This method constructs the identifier hierarchically using the parent 
entity type
+   * relationships.
+   *
+   * <p>Examples:
+   *
+   * <pre>
+   * 1. Build a TABLE identifier:
+   *    Input:
+   *      type = Entity.EntityType.TABLE
+   *      name = "users"
+   *      entities = {
+   *        METALAKE -> "my_metalake",
+   *        CATALOG -> "my_catalog",
+   *        SCHEMA -> "my_schema"
+   *      }
+   *    Result: NameIdentifier.of("my_metalake", "my_catalog", "my_schema", 
"users")
+   *
+   * 2. Build a SCHEMA identifier:
+   *    Input:
+   *      type = Entity.EntityType.SCHEMA
+   *      name = "my_schema"
+   *      entities = {
+   *        METALAKE -> "my_metalake",
+   *        CATALOG -> "my_catalog"
+   *      }
+   *    Result: NameIdentifier.of("my_metalake", "my_catalog", "my_schema")
+   *
+   * 3. Build a USER identifier (virtual namespace type):
+   *    Input:
+   *      type = Entity.EntityType.USER
+   *      name = "john_doe"
+   *      entities = {
+   *        METALAKE -> "my_metalake"
+   *      }
+   *    Result: NameIdentifier.of(NamespaceUtil.ofUser("my_metalake"), 
"john_doe")
+   *            which creates a NameIdentifier with namespace ["my_metalake", 
"system", "user"]
+   *            and name "john_doe"
+   *
+   * 4. Build a COLUMN identifier:
+   *    Input:
+   *      type = Entity.EntityType.COLUMN
+   *      name = "id"
+   *      entities = {
+   *        METALAKE -> "my_metalake",
+   *        CATALOG -> "my_catalog",
+   *        SCHEMA -> "my_schema",
+   *        TABLE -> "users"
+   *      }
+   *    Result: NameIdentifier.of("my_metalake", "my_catalog", "my_schema", 
"users", "id")
+   * </pre>
+   *
+   * @param type The entity type
+   * @param name The entity name
+   * @param entities Map containing the metalake, catalog, schema names and 
other parent entities
+   * @return The created NameIdentifier
+   * @throws IllegalArgumentException if the entity type is not supported or 
required parent
+   *     entities are missing
+   */
+  public static NameIdentifier buildNameIdentifier(
+      Entity.EntityType type, String name, Map<Entity.EntityType, String> 
entities) {
+    String metalake = entities.get(Entity.EntityType.METALAKE);
+
+    // Handle metalake as the root
+    if (type == Entity.EntityType.METALAKE) {
+      return ofMetalake(metalake);
+    }
+
+    // Build the full name path by traversing the hierarchy
+    List<String> namePath = Lists.newArrayList();
+    namePath.add(name);
+
+    Entity.EntityType currentType = type;
+    while (currentType != Entity.EntityType.METALAKE) {
+      Entity.EntityType parentType = parentEntityType(currentType);
+      String parentName = entities.get(parentType);
+
+      if (parentType == Entity.EntityType.METALAKE) {
+        namePath.add(0, metalake);
+        break;
+      } else if (parentName == null) {
+        throw new IllegalArgumentException(
+            "Cannot build NameIdentifier for type "
+                + type
+                + ": missing parent entity of type "
+                + parentType);
+      }
+
+      namePath.add(0, parentName);
+      currentType = parentType;
+    }
+
+    if (SUPPORT_VIRTUAL_NAMESPACE_TYPES.contains(type)) {
+      Namespace namespace = createVirtualNamespace(type, metalake);
+      return NameIdentifier.of(namespace, name);
+    }
+
+    // Create NameIdentifier from the full path
+    return NameIdentifier.of(namePath.toArray(new String[0]));
+  }
+
+  private static Namespace createVirtualNamespace(Entity.EntityType type, 
String metalake) {
+    switch (type) {
+      case USER:
+        return NamespaceUtil.ofUser(metalake);
+      case GROUP:
+        return NamespaceUtil.ofGroup(metalake);
+      case ROLE:
+        return NamespaceUtil.ofRole(metalake);
+      case TAG:
+        return NamespaceUtil.ofTag(metalake);
+      case POLICY:
+        return NamespaceUtil.ofPolicy(metalake);
+      case JOB:
+        return NamespaceUtil.ofJob(metalake);
+      case JOB_TEMPLATE:
+        return NamespaceUtil.ofJobTemplate(metalake);
+      default:
+        throw new IllegalArgumentException("Unsupported virtual namespace 
type: " + type);
+    }
+  }
+
+  /**
+   * Build a map of NameIdentifiers from entity data.
+   *
+   * @param entities Map containing entity types and their names (e.g., 
METALAKE, CATALOG, SCHEMA,
+   *     etc.)
+   * @return Map of entity types to their NameIdentifiers
+   */
+  public static Map<Entity.EntityType, NameIdentifier> buildNameIdentifierMap(

Review Comment:
   Seems like this method (and the above one) will only be used in 
`Authorization` module, my feeling is that it would be better to move these 
methods to the `authorization` module, what do you think?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to