• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 17548792663

08 Sep 2025 11:10AM UTC coverage: 11.88% (-0.03%) from 11.91%
17548792663

push

github

tkuhn
Paging for template lists

334 of 3888 branches covered (8.59%)

Branch coverage included in aggregate %.

960 of 7004 relevant lines covered (13.71%)

0.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
src/main/java/com/knowledgepixels/nanodash/component/TemplateList.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.ApiCache;
4
import com.knowledgepixels.nanodash.template.TemplateData;
5
import jakarta.xml.bind.DatatypeConverter;
6
import org.apache.wicket.Component;
7
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
8
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
9
import org.apache.wicket.extensions.markup.html.repeater.data.table.NavigatorLabel;
10
import org.apache.wicket.markup.html.WebMarkupContainer;
11
import org.apache.wicket.markup.html.basic.Label;
12
import org.apache.wicket.markup.html.panel.Panel;
13
import org.apache.wicket.markup.repeater.Item;
14
import org.apache.wicket.markup.repeater.data.DataView;
15
import org.apache.wicket.markup.repeater.data.ListDataProvider;
16
import org.nanopub.extra.services.ApiResponse;
17
import org.nanopub.extra.services.ApiResponseEntry;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

21
import java.io.Serializable;
22
import java.util.ArrayList;
23
import java.util.Calendar;
24
import java.util.HashMap;
25
import java.util.Map;
26

27
/**
28
 * A list of templates, grouped by topic.
29
 */
30
public class TemplateList extends Panel {
31

32
    private static final long serialVersionUID = 1L;
33
    private static final Logger logger = LoggerFactory.getLogger(TemplateList.class);
×
34

35
    /**
36
     * A list of templates, grouped by topic.
37
     *
38
     * @param id the wicket id of this component
39
     */
40
    public TemplateList(String id) {
41
        super(id);
×
42
        setOutputMarkupId(true);
×
43

44
        final Map<String, String> ptParams = new HashMap<>();
×
45
        final String ptQueryName = "get-most-used-templates-last30d";
×
46
        ApiResponse ptResponse = ApiCache.retrieveResponse(ptQueryName, ptParams);
×
47
        if (ptResponse != null) {
×
48
            add(TemplateResults.fromApiResponse("populartemplates", ptResponse));
×
49
        } else {
50
            add(new AjaxLazyLoadPanel<Component>("populartemplates") {
×
51

52
                private static final long serialVersionUID = 1L;
53

54
                @Override
55
                public Component getLazyLoadComponent(String markupId) {
56
                    ApiResponse r = null;
×
57
                    while (true) {
58
                        try {
59
                            Thread.sleep(500);
×
60
                        } catch (InterruptedException ex) {
×
61
                            logger.error("Thread interrupted", ex);
×
62
                        }
×
63
                        if (!ApiCache.isRunning(ptQueryName, ptParams)) {
×
64
                            r = ApiCache.retrieveResponse(ptQueryName, ptParams);
×
65
                            if (r != null) break;
×
66
                        }
67
                    }
68
                    return TemplateResults.fromApiResponse(markupId, r);
×
69
                }
70

71
            });
72
        }
73

74
        final Map<String, String> stParams = new HashMap<>();
×
75
        final String stQueryName = "get-suggested-templates-to-get-started";
×
76
        ApiResponse stResponse = ApiCache.retrieveResponse(stQueryName, stParams);
×
77
        if (stResponse != null) {
×
78
            add(TemplateResults.fromApiResponse("getstartedtemplates", stResponse));
×
79
        } else {
80
            add(new AjaxLazyLoadPanel<Component>("getstartedtemplates") {
×
81

82
                private static final long serialVersionUID = 1L;
83

84
                @Override
85
                public Component getLazyLoadComponent(String markupId) {
86
                    ApiResponse r = null;
×
87
                    while (true) {
88
                        try {
89
                            Thread.sleep(500);
×
90
                        } catch (InterruptedException ex) {
×
91
                            logger.error("Thread interrupted", ex);
×
92
                        }
×
93
                        if (!ApiCache.isRunning(stQueryName, stParams)) {
×
94
                            r = ApiCache.retrieveResponse(stQueryName, stParams);
×
95
                            if (r != null) break;
×
96
                        }
97
                    }
98
                    return TemplateResults.fromApiResponse(markupId, r);
×
99
                }
100

101
            });
102
        }
103

104
        ArrayList<ApiResponseEntry> templateList = new ArrayList<>(TemplateData.get().getAssertionTemplates());
×
105
        templateList.sort((t1, t2) -> {
×
106
            Calendar c1 = getTime(t1);
×
107
            Calendar c2 = getTime(t2);
×
108
            if (c1 == null && c2 == null) return 0;
×
109
            if (c1 == null) return 1;
×
110
            if (c2 == null) return -1;
×
111
            return c2.compareTo(c1);
×
112
        });
113

114
        Map<String, Topic> topics = new HashMap<>();
×
115
        for (ApiResponseEntry entry : templateList) {
×
116
            String tag = entry.get("tag");
×
117
            if ("".equals(tag)) tag = null;
×
118
            if (!topics.containsKey(tag)) {
×
119
                topics.put(tag, new Topic(tag));
×
120
            }
121
            topics.get(tag).templates.add(entry);
×
122

123
        }
×
124
        ArrayList<Topic> topicList = new ArrayList<Topic>(topics.values());
×
125
        topicList.sort((t0, t1) -> {
×
126
            if (t0.tag == null) return 1;
×
127
            if (t1.tag == null) return -1;
×
128
            return t1.templates.size() - t0.templates.size();
×
129
        });
130
        DataView<Topic> topicDataView = new DataView<Topic>("topics", new ListDataProvider<Topic>(topicList)) {
×
131

132
            private static final long serialVersionUID = 1L;
133

134
            @Override
135
            protected void populateItem(Item<Topic> item) {
136
                String tag = item.getModelObject().tag;
×
137
                if (tag == null) tag = "Other";
×
138
                item.add(new Label("title", tag));
×
139
                DataView<ApiResponseEntry> entryDataView = new DataView<>("template-list", new ListDataProvider<ApiResponseEntry>(item.getModelObject().templates)) {
×
140

141
                    private static final long serialVersionUID = 1L;
142

143
                    @Override
144
                    protected void populateItem(Item<ApiResponseEntry> item) {
145
                        item.add(new TemplateItem("template", item.getModelObject()));
×
146
                    }
×
147

148
                };
149
                entryDataView.setItemsPerPage(10);
×
150
                entryDataView.setOutputMarkupId(true);
×
151
                item.add(entryDataView);
×
152

153
                WebMarkupContainer navigation = new WebMarkupContainer("navigation");
×
154
                navigation.add(new NavigatorLabel("navigatorLabel", entryDataView));
×
155
                AjaxPagingNavigator pagingNavigator = new AjaxPagingNavigator("navigator", entryDataView);
×
156
                navigation.setVisible(entryDataView.getPageCount() > 1);
×
157
                navigation.add(pagingNavigator);
×
158
                item.add(navigation);
×
159
                item.setOutputMarkupId(true);
×
160
            }
×
161
        };
162
        topicDataView.setOutputMarkupId(true);
×
163
        add(topicDataView);
×
164
    }
×
165

166
    private static Calendar getTime(ApiResponseEntry entry) {
167
        return DatatypeConverter.parseDateTime(entry.get("date"));
×
168
    }
169

170

171
    private static class Topic implements Serializable {
172

173
        private static final long serialVersionUID = 5919614141679468774L;
174

175
        String tag;
176
        ArrayList<ApiResponseEntry> templates = new ArrayList<>();
×
177

178
        Topic(String tag) {
×
179
            this.tag = tag;
×
180
        }
×
181

182
    }
183

184
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc