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

knowledgepixels / nanodash / 24382503285

14 Apr 2026 05:24AM UTC coverage: 15.886% (-0.2%) from 16.094%
24382503285

push

github

web-flow
Merge pull request #438 from knowledgepixels/feature/item-list-view-435

feat: implement ItemListView and fix async view rendering

789 of 6130 branches covered (12.87%)

Branch coverage included in aggregate %.

1978 of 11288 relevant lines covered (17.52%)

2.4 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/QueryResultTable.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.*;
4
import com.knowledgepixels.nanodash.domain.MaintainedResource;
5
import com.knowledgepixels.nanodash.page.NanodashPage;
6
import com.knowledgepixels.nanodash.page.PublishPage;
7
import com.knowledgepixels.nanodash.repository.MaintainedResourceRepository;
8
import com.knowledgepixels.nanodash.template.Template;
9
import org.apache.wicket.Component;
10
import org.apache.wicket.ajax.AjaxRequestTarget;
11
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
12
import org.apache.wicket.behavior.AttributeAppender;
13
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackHeadersToolbar;
14
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxNavigationToolbar;
15
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
16
import org.apache.wicket.extensions.markup.html.repeater.data.table.*;
17
import org.apache.wicket.markup.html.basic.Label;
18
import org.apache.wicket.markup.html.form.TextField;
19
import org.apache.wicket.markup.html.link.AbstractLink;
20
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
21
import org.apache.wicket.util.string.Strings;
22
import org.apache.wicket.markup.repeater.Item;
23
import org.apache.wicket.model.IModel;
24
import org.apache.wicket.model.Model;
25
import org.apache.wicket.request.mapper.parameter.PageParameters;
26
import org.eclipse.rdf4j.model.IRI;
27
import org.nanopub.extra.services.ApiResponse;
28
import org.nanopub.extra.services.ApiResponseEntry;
29
import org.nanopub.extra.services.QueryRef;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

33
import java.util.ArrayList;
34
import java.util.List;
35

36
/**
37
 * Component for displaying query results in a table format.
38
 */
39
public class QueryResultTable extends QueryResult {
40

41
    private static final Logger logger = LoggerFactory.getLogger(QueryResultTable.class);
×
42

43
    private Model<String> errorMessages = Model.of("");
×
44
    private DataTable<ApiResponseEntry, String> table;
45
    private Label errorLabel;
46
    private FilteredQueryResultDataProvider filteredDataProvider;
47
    private Model<String> filterModel = Model.of("");
×
48

49
    QueryResultTable(String id, QueryRef queryRef, ApiResponse response, ViewDisplay viewDisplay, boolean plain) {
50
        super(id, queryRef, response, viewDisplay);
×
51

52
        if (plain) {
×
53
            add(new Label("label").setVisible(false));
×
54
            add(new Label("np").setVisible(false));
×
55
            showViewDisplayMenu = false;
×
56
        } else {
57
            String label = grlcQuery.getLabel();
×
58
            if (viewDisplay.getTitle() != null) {
×
59
                label = viewDisplay.getTitle();
×
60
            }
61
            add(new Label("label", label));
×
62
        }
63

64
        errorLabel = new Label("error-messages", errorMessages);
×
65
        errorLabel.setVisible(false);
×
66
        add(errorLabel);
×
67

68
        TextField<String> filterField = new TextField<>("filter", filterModel);
×
69
        filterField.setOutputMarkupId(true);
×
70
        filterField.add(new AjaxFormComponentUpdatingBehavior("change") {
×
71
            @Override
72
            protected void onUpdate(AjaxRequestTarget target) {
73
                if (filteredDataProvider != null && table != null) {
×
74
                    filteredDataProvider.setFilterText(filterModel.getObject());
×
75
                    target.add(table);
×
76
                }
77
            }
×
78
        });
79
        add(filterField);
×
80

81
        populateComponent();
×
82
    }
×
83

84
    private void addErrorMessage(String errorMessage) {
85
        String s = errorMessages.getObject();
×
86
        if (s.isEmpty()) {
×
87
            s = "Error: " + errorMessage;
×
88
        } else {
89
            s += ", " + errorMessage;
×
90
        }
91
        errorMessages.setObject(s);
×
92
        errorLabel.setVisible(true);
×
93
        if (table != null) table.setVisible(false);
×
94
    }
×
95

96
    @Override
97
    protected void populateComponent() {
98
        List<IColumn<ApiResponseEntry, String>> columns = new ArrayList<>();
×
99
        QueryResultDataProvider dataProvider;
100
        try {
101
            for (String h : response.getHeader()) {
×
102
                if (h.endsWith("_label") || h.endsWith("_label_multi")) {
×
103
                    continue;
×
104
                }
105
                String displayLabel = h;
×
106
                if (displayLabel.endsWith("_multi_iri")) {
×
107
                    displayLabel = displayLabel.substring(0, displayLabel.length() - "_multi_iri".length());
×
108
                } else if (displayLabel.endsWith("_multi_val")) {
×
109
                    displayLabel = displayLabel.substring(0, displayLabel.length() - "_multi_val".length());
×
110
                } else if (displayLabel.endsWith("_multi")) {
×
111
                    displayLabel = displayLabel.substring(0, displayLabel.length() - "_multi".length());
×
112
                }
113
                columns.add(new Column(displayLabel.replaceAll("_", " "), h));
×
114
            }
115
            if (viewDisplay.getView() != null && !viewDisplay.getView().getViewEntryActionList().isEmpty()) {
×
116
                columns.add(new Column("", Column.ACTIONS));
×
117
            }
118
            dataProvider = new QueryResultDataProvider(response.getData());
×
119
            filteredDataProvider = new FilteredQueryResultDataProvider(dataProvider, response);
×
120
            table = new DataTable<>("table", columns, filteredDataProvider, viewDisplay.getPageSize() < 1 ? Integer.MAX_VALUE : viewDisplay.getPageSize());
×
121
            table.setOutputMarkupId(true);
×
122
            table.addBottomToolbar(new AjaxNavigationToolbar(table));
×
123
            table.addBottomToolbar(new NoRecordsToolbar(table));
×
124
            table.addTopToolbar(new AjaxFallbackHeadersToolbar<String>(table, dataProvider));
×
125
            add(table);
×
126
        } catch (Exception ex) {
×
127
            logger.error("Error creating table for query {}", grlcQuery.getQueryId(), ex);
×
128
            add(new Label("table", "").setVisible(false));
×
129
            addErrorMessage(ex.getMessage());
×
130
        }
×
131
    }
×
132

133
    private class Column extends AbstractColumn<ApiResponseEntry, String> {
134

135
        private String key;
136
        public static final String ACTIONS = "*actions*";
137

138
        public Column(String title, String key) {
×
139
            super(new Model<String>(title), key);
×
140
            this.key = key;
×
141
        }
×
142

143
        @Override
144
        public void populateItem(Item<ICellPopulator<ApiResponseEntry>> cellItem, String componentId, IModel<ApiResponseEntry> rowModel) {
145
            try {
146
                View view = viewDisplay.getView();
×
147
                if (key.equals(ACTIONS) && view != null) {
×
148
                    List<AbstractLink> links = new ArrayList<>();
×
149
                    for (IRI actionIri : view.getViewEntryActionList()) {
×
150
                        // TODO Copied code and adjusted from QueryResultTableBuilder:
151
                        Template t = view.getTemplateForAction(actionIri);
×
152
                        if (t == null) continue;
×
153
                        String targetField = view.getTemplateTargetFieldForAction(actionIri);
×
154
                        if (targetField == null) targetField = "resource";
×
155
                        String label = view.getLabelForAction(actionIri);
×
156
                        if (label == null) label = "action...";
×
157
                        if (!label.endsWith("...")) label += "...";
×
158
                        PageParameters params = new PageParameters().set("template", t.getId())
×
159
                                .set("param_" + targetField, contextId)
×
160
                                .set("context", contextId)
×
161
                                .set("template-version", "latest");
×
162
                        if (partId != null && contextId != null && !partId.equals(contextId)) {
×
163
                            params.set("part", partId);
×
164
                        }
165
                        String partField = view.getTemplatePartFieldForAction(actionIri);
×
166
                        if (partField != null) {
×
167
                            // TODO Find a better way to pass the MaintainedResource object to this method:
168
                            MaintainedResource r = MaintainedResourceRepository.get().findById(contextId);
×
169
                            if (r != null && r.getNamespace() != null) {
×
170
                                params.set("param_" + partField, r.getNamespace() + "<SET-SUFFIX>");
×
171
                            }
172
                        }
173
                        String queryMapping = view.getTemplateQueryMapping(actionIri);
×
174
                        if (queryMapping != null && queryMapping.contains(":")) {
×
175
                            // This part is different from the code in QueryResultTableBuilder:
176
                            String queryParam = queryMapping.split(":")[0];
×
177
                            String templateParam = queryMapping.split(":")[1];
×
178
                            params.set("param_" + templateParam, rowModel.getObject().get(queryParam));
×
179
                        }
180
                        params.set("refresh-upon-publish", queryRef.getAsUrlString());
×
181
                        AbstractLink button = new BookmarkablePageLink<NanodashPage>("button", PublishPage.class, params);
×
182
                        button.setBody(Model.of(label));
×
183
                        links.add(button);
×
184
                    }
×
185
                    cellItem.add(new ButtonList(componentId, resourceWithProfile, links, null, null));
×
186
                } else {
×
187
                    String value = rowModel.getObject().get(key);
×
188
                    if (key.endsWith("_multi_iri")) {
×
189
                        String[] uris = value.split("\\s+");
×
190
                        String labelKey = key.substring(0, key.length() - "_multi_iri".length()) + "_label_multi";
×
191
                        String labelValue = rowModel.getObject().get(labelKey);
×
192
                        String[] labels = labelValue != null ? labelValue.split("\n", -1) : null;
×
193
                        List<Component> links = new ArrayList<>();
×
194
                        for (int i = 0; i < uris.length; i++) {
×
195
                            String label = truncateLabel((labels != null && i < labels.length && !labels[i].isBlank()) ? Utils.unescapeMultiValue(labels[i]) : null);
×
196
                            links.add(new NanodashLink("component", uris[i], null, null, label, contextId));
×
197
                        }
198
                        cellItem.add(new ComponentSequence(componentId, ", ", links));
×
199
                    } else if (key.endsWith("_multi_val")) {
×
200
                        String labelKey = key.substring(0, key.length() - "_multi_val".length()) + "_label_multi";
×
201
                        String labelValue = rowModel.getObject().get(labelKey);
×
202
                        String[] parts = value.split("\n", -1);
×
203
                        String[] labels = labelValue != null ? labelValue.split("\n", -1) : null;
×
204
                        List<Component> components = new ArrayList<>();
×
205
                        for (int i = 0; i < parts.length; i++) {
×
206
                            String part = parts[i];
×
207
                            String label = truncateLabel((labels != null && i < labels.length && !labels[i].isBlank()) ? Utils.unescapeMultiValue(labels[i]) : null);
×
208
                            if (part.matches("https?://.+")) {
×
209
                                components.add(new NanodashLink("component", part, null, null, label, contextId));
×
210
                            } else {
211
                                String display = label != null ? label : Utils.unescapeMultiValue(part);
×
212
                                if (Utils.looksLikeHtml(display)) {
×
213
                                    components.add(new Label("component", Utils.sanitizeHtml(display))
×
214
                                            .setEscapeModelStrings(false)
×
215
                                            .add(new AttributeAppender("class", "cell-data-html")));
×
216
                                } else {
217
                                    components.add(new Label("component", display));
×
218
                                }
219
                            }
220
                        }
221
                        cellItem.add(new ComponentSequence(componentId, ", ", components));
×
222
                    } else if (key.endsWith("_multi")) {
×
223
                        String[] parts = value.split("\n", -1);
×
224
                        String labelKey = key.substring(0, key.length() - "_multi".length()) + "_label_multi";
×
225
                        String labelValue = rowModel.getObject().get(labelKey);
×
226
                        String[] labels = labelValue != null ? labelValue.split("\n", -1) : null;
×
227
                        List<Component> components = new ArrayList<>();
×
228
                        for (int i = 0; i < parts.length; i++) {
×
229
                            String display;
230
                            if (labels != null && i < labels.length && !labels[i].isBlank()) {
×
231
                                display = Utils.unescapeMultiValue(labels[i]);
×
232
                            } else {
233
                                display = Utils.unescapeMultiValue(parts[i]);
×
234
                            }
235
                            if (Utils.looksLikeHtml(display)) {
×
236
                                components.add(new Label("component", Utils.sanitizeHtml(display))
×
237
                                        .setEscapeModelStrings(false)
×
238
                                        .add(new AttributeAppender("class", "cell-data-html")));
×
239
                            } else {
240
                                components.add(new Label("component", display));
×
241
                            }
242
                        }
243
                        cellItem.add(new ComponentSequence(componentId, ", ", components));
×
244
                    } else if (key.endsWith("template_iri")) {
×
245
                        String label = truncateLabel(rowModel.getObject().get(key + "_label"));
×
246
                        if (label == null || label.isBlank()) label = truncateLabel(value);
×
247
                        String templateUrl = PublishPage.MOUNT_PATH + "?template=" + Utils.urlEncode(value) + "&template-version=latest";
×
248
                        String html = "<a href=\"" + Strings.escapeMarkup(templateUrl) + "\">" + Strings.escapeMarkup(label) + "</a>";
×
249
                        cellItem.add(new Label(componentId, html).setEscapeModelStrings(false));
×
250
                    } else if (value.matches("https?://.+")) {
×
251
                        String label = truncateLabel(rowModel.getObject().get(key + "_label"));
×
252
                        cellItem.add(new NanodashLink(componentId, value, null, null, label, contextId));
×
253
                    } else {
×
254
                        if (key.startsWith("pubkey")) {
×
255
                            cellItem.add(new Label(componentId, value).add(new AttributeAppender("style", "overflow-wrap: anywhere;")));
×
256
                        } else {
257
                            Label cellLabel;
258
                            if (Utils.looksLikeHtml(value)) {
×
259
                                cellLabel = (Label) new Label(componentId, Utils.sanitizeHtml(value))
×
260
                                        .setEscapeModelStrings(false)
×
261
                                        .add(new AttributeAppender("class", "cell-data-html"));
×
262
                            } else {
263
                                cellLabel = new Label(componentId, value);
×
264
                            }
265
                            cellItem.add(cellLabel);
×
266
                        }
267
                    }
268
                }
269
            } catch (Exception ex) {
×
270
                logger.error("Failed to populate table column: ", ex);
×
271
                cellItem.add(new Label(componentId).setVisible(false));
×
272
                addErrorMessage(ex.getMessage());
×
273
            }
×
274
        }
×
275

276
    }
277

278
    private static String truncateLabel(String label) {
279
        return Utils.truncateLabel(label);
×
280
    }
281

282
}
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