protected void listChildren()

in indexer-core/src/main/java/org/apache/maven/index/treeview/DefaultIndexTreeView.java [116:226]


    protected void listChildren(TreeNode root, TreeViewRequest request, Set<String> allGroups) throws IOException {
        String path = root.getPath();

        Map<String, TreeNode> folders = new HashMap<>();

        String rootPartialGroupId = StringUtils.strip(root.getPath().replaceAll("/", "."), ".");

        folders.put(Type.G + ":" + rootPartialGroupId, root);

        try (IteratorSearchResponse artifacts = getArtifacts(root, request)) {
            for (ArtifactInfo ai : artifacts) {
                String versionKey = Type.V + ":" + ai.getArtifactId() + ":" + ai.getVersion();

                TreeNode versionResource = folders.get(versionKey);

                if (versionResource == null) {
                    String artifactKey = Type.A + ":" + ai.getArtifactId();

                    TreeNode artifactResource = folders.get(artifactKey);

                    if (artifactResource == null) {
                        TreeNode groupParentResource = root;

                        TreeNode groupResource;

                        // here comes the twist: we have to search for parent G node
                        String partialGroupId = null;

                        String[] groupIdElems = ai.getGroupId().split("\\.");

                        for (String groupIdElem : groupIdElems) {
                            if (partialGroupId == null) {
                                partialGroupId = groupIdElem;
                            } else {
                                partialGroupId = partialGroupId + "." + groupIdElem;
                            }

                            String groupKey = Type.G + ":" + partialGroupId;

                            groupResource = folders.get(groupKey);

                            // it needs to be created only if not found (is null) and is _below_ groupParentResource
                            if (groupResource == null
                                    && groupParentResource.getPath().length()
                                            < getPathForAi(ai, MAVEN.GROUP_ID).length()) {
                                String gNodeName = partialGroupId.lastIndexOf('.') > -1
                                        ? partialGroupId.substring(partialGroupId.lastIndexOf('.') + 1)
                                        : partialGroupId;

                                groupResource = request.getFactory()
                                        .createGNode(
                                                this,
                                                request,
                                                "/" + partialGroupId.replaceAll("\\.", "/") + "/",
                                                gNodeName);

                                groupParentResource.getChildren().add(groupResource);

                                folders.put(groupKey, groupResource);

                                groupParentResource = groupResource;
                            } else if (groupResource != null) {
                                // we found it as already existing, break if this is the node we want
                                if (groupResource.getPath().equals(getPathForAi(ai, MAVEN.GROUP_ID))) {
                                    break;
                                }

                                groupParentResource = groupResource;
                            }
                        }

                        artifactResource = request.getFactory()
                                .createANode(this, request, ai, getPathForAi(ai, MAVEN.ARTIFACT_ID));

                        groupParentResource.getChildren().add(artifactResource);

                        folders.put(artifactKey, artifactResource);
                    }

                    versionResource =
                            request.getFactory().createVNode(this, request, ai, getPathForAi(ai, MAVEN.VERSION));

                    artifactResource.getChildren().add(versionResource);

                    folders.put(versionKey, versionResource);
                }

                String nodePath = getPathForAi(ai, null);

                versionResource.getChildren().add(request.getFactory().createArtifactNode(this, request, ai, nodePath));
            }
        }

        if (!request.hasFieldHints()) {
            Set<String> groups = getGroups(path, allGroups);

            for (String group : groups) {
                TreeNode groupResource = root.findChildByPath(path + group + "/", Type.G);

                if (groupResource == null) {
                    groupResource = request.getFactory().createGNode(this, request, path + group + "/", group);

                    root.getChildren().add(groupResource);
                } else {
                    // if the folder has been created as an artifact name,
                    // we need to check for possible nested groups as well
                    listChildren(groupResource, request, allGroups);
                }
            }
        }
    }