private void sortSectionsAndCreateIndexFiles()

in src/main/java/org/apache/tomee/website/GroupedIndex.java [73:195]


    private void sortSectionsAndCreateIndexFiles(List<Doc> docs, String language) {

        final Map<String, List<Doc>> sections = new HashMap<>();

        //filtering only documents with the same language for: target/jbake/<tomeeBranch>/docs
        if (this.type.equalsIgnoreCase("docsindex")) {

            for (Doc doc1 : docs) {
                if (doc1.language.equalsIgnoreCase(language)) {
                    sections.computeIfAbsent(doc1.getGroup(), k -> new ArrayList<>()).add(doc1);
                }
            }
        } else {

            if (this.type.equalsIgnoreCase("examplesindex")) {
                for (Doc doc1 : docs) { //filtering only documents with the same language for: target/jbake/<tomeeBranch> and type = examplesindex
                    if (doc1.language.equalsIgnoreCase(language) && doc1.source.getParentFile().getName().equalsIgnoreCase("examples")) {
                        sections.computeIfAbsent(doc1.getGroup(), k -> new ArrayList<>()).add(doc1);
                    }
                }
            } else {//any type (used in GroupedIndexTest.java
                for (Doc doc1 : docs) {
                    if (doc1.language.equalsIgnoreCase(language)) {
                        sections.computeIfAbsent(doc1.getGroup(), k -> new ArrayList<>()).add(doc1);
                    }
                }
            }
        }

        final List<String> sectionsFormatted = new ArrayList<>();

        /**
         * We want to sort the sections with the most entries towards the top.
         * Unless it is crazy big, then we put it in a special section at the bottom.
         */
        sections.entrySet().stream()
                .filter(entry -> entry.getValue().size() < 10)
                .sorted((o1, o2) -> new Integer(o2.getValue().size()).compareTo(o1.getValue().size()))
                .forEach(entry -> {

                    final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    final PrintStream out = new PrintStream(bytes);

                    out.printf("            <div class=\"group-title\">%s</div>\n", entry.getKey());
                    out.printf("            <ul class=\"group\">\n");
                    entry.getValue().stream().sorted().forEach(doc -> {

                        out.printf(
                                "              <li class=\"group-item\"><span class=\"group-item-i\" ><i class=\"fa fa-angle-right\"></i></span><a href=\"%s\">%s</a></li>\n",
                                doc.getHref(), doc.getTitle());


                    });
                    out.printf("            </ul>\n");

                    sectionsFormatted.add(new String(bytes.toByteArray()));

                });

        try (final PrintStream out = print(directory.getAbsolutePath(), "index.html", language, this.type)) {

            out.printf("type=%s\n", type);
            out.printf("status=published\n");
            out.printf("~~~~~~\n");

            {
                final ListIterator<String> iterator = sectionsFormatted.listIterator();
                while (iterator.hasNext()) {
                    out.printf("        <div class=\"row\">\n");
                    out.printf("          <div class=\"col-md-4\">\n");
                    out.printf(iterator.hasNext() ? iterator.next() : "");
                    out.printf("          </div>\n");
                    out.printf("          <div class=\"col-md-4\">\n");
                    out.printf(iterator.hasNext() ? iterator.next() : "");
                    out.printf("          </div>\n");
                    out.printf("          <div class=\"col-md-4\">\n");
                    out.printf(iterator.hasNext() ? iterator.next() : "");
                    out.printf("          </div>\n");
                    out.printf("        </div>\n");
                }
            }

            sections.entrySet().stream()
                    .filter(entry -> entry.getValue().size() >= 10)
                    .sorted((o1, o2) -> new Integer(o1.getValue().size()).compareTo(o2.getValue().size()))
                    .forEach(entry -> {

                        out.printf("        <div class=\"row\">\n");
                        out.printf("          <div class=\"col-md-12\">\n");
                        out.printf("            <div class=\"group-title large\">%s</div>\n", entry.getKey());
                        out.printf("          </div>\n");
                        out.printf("        </div>\n");

                        final ListIterator<Doc> iterator = entry.getValue().stream()
                                .sorted()
                                .collect(Collectors.toList())
                                .listIterator();

                        final int i = (int) Math.ceil(entry.getValue().size() / 3f);

                        out.printf("        <div class=\"row\">\n");
                        while (iterator.hasNext()) {
                            int count = 0;

                            out.printf("          <div class=\"col-md-4\">\n");
                            out.printf("            <ul class=\"group\">\n");
                            while (iterator.hasNext() && count++ < i) {
                                final Doc doc = iterator.next();
                                out.printf(
                                        "              <li class=\"group-item\"><span class=\"group-item-i\" ><i class=\"fa fa-angle-right\"></i></span><a href=\"%s\">%s</a></li>\n",
                                        doc.getHref(), doc.getTitle());

                            }
                            out.printf("            </ul>\n");
                            out.printf("          </div>\n");
                        }
                        out.printf("        </div>\n");
                    });

        } catch (Exception e) {
            throw e; //ToDo: implementing proper logger and catch.
        }
    }