private Optional saveParentChildRelationship()

in metacat-main/src/main/java/com/netflix/metacat/main/services/impl/TableServiceImpl.java [184:249]


    private Optional<Runnable> saveParentChildRelationship(final QualifiedName child, final TableDto tableDto) {
        if (tableDto.getDefinitionMetadata() != null) {
            final ObjectNode definitionMetadata = tableDto.getDefinitionMetadata();
            if (definitionMetadata.has(ParentChildRelMetadataConstants.PARENT_CHILD_RELINFO)) {

                if (!config.isParentChildCreateEnabled()) {
                    throw new RuntimeException("parent child creation is currently disabled");
                }

                final JsonNode parentChildRelInfo =
                    definitionMetadata.get(ParentChildRelMetadataConstants.PARENT_CHILD_RELINFO);

                String parentName;
                if (!parentChildRelInfo.has(ParentChildRelMetadataConstants.PARENT_NAME)) {
                    throw new IllegalArgumentException("parent name is not specified");
                }
                parentName = parentChildRelInfo.path(ParentChildRelMetadataConstants.PARENT_NAME)
                    .asText();
                final QualifiedName parent = QualifiedName.fromString(parentName);
                validate(parent);
                if (!exists(parent)) {
                    throw new IllegalArgumentException("Parent Table:" + parent + " does not exist");
                }

                // fetch parent and child uuid
                String parentUUID;
                String childUUID;
                if (!parentChildRelInfo.has(ParentChildRelMetadataConstants.PARENT_UUID)) {
                    throw new IllegalArgumentException("parent_table_uuid is not specified for parent table="
                            + parentName);
                }

                if (!parentChildRelInfo.has(ParentChildRelMetadataConstants.CHILD_UUID)) {
                    throw new IllegalArgumentException("child_table_uuid is not specified for child table=" + child);
                }
                parentUUID = parentChildRelInfo.path(ParentChildRelMetadataConstants.PARENT_UUID).asText();
                childUUID = parentChildRelInfo.path(ParentChildRelMetadataConstants.CHILD_UUID).asText();

                // fetch relationshipType
                String relationType;
                if (parentChildRelInfo.has(ParentChildRelMetadataConstants.RELATION_TYPE)) {
                    relationType = parentChildRelInfo.path(ParentChildRelMetadataConstants.RELATION_TYPE).asText();
                } else {
                    relationType = "CLONE";
                }

                // Create parent child relationship
                parentChildRelMetadataService.createParentChildRelation(parent, parentUUID,
                    child, childUUID, relationType, config.getParentChildRelationshipProperties());

                // Return a Runnable for deleting the relationship
                return Optional.of(() -> {
                    try {
                        parentChildRelMetadataService.deleteParentChildRelation(parent,
                            parentUUID, child, childUUID, relationType);
                    } catch (Exception e) {
                        log.error("parentChildRelMetadataService: Failed to delete parent child relationship "
                                + "after failing to create the table={} with the following parameters: "
                                + "parent={}, parentUUID={}, child={}, childUUID={}, relationType={}",
                            child, parent, parentUUID, child, childUUID, relationType, e);
                    }
                });
            }
        }
        return Optional.empty();
    }