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

knowledgepixels / nanodash / 26521965200

27 May 2026 03:45PM UTC coverage: 20.643% (+0.06%) from 20.586%
26521965200

push

github

web-flow
Merge pull request #472 from knowledgepixels/feat/publish-all-templates-table

Show all templates on Publish page as a single sortable table

1007 of 6186 branches covered (16.28%)

Branch coverage included in aggregate %.

2604 of 11307 relevant lines covered (23.03%)

3.29 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
                } else if (displayLabel.endsWith("_iri")) {
×
113
                    displayLabel = displayLabel.substring(0, displayLabel.length() - "_iri".length());
×
114
                }
115
                columns.add(new Column(displayLabel.replaceAll("_", " "), h));
×
116
            }
117
            if (viewDisplay.getView() != null && !viewDisplay.getView().getViewEntryActionList().isEmpty()) {
×
118
                columns.add(new Column("", Column.ACTIONS));
×
119
            }
120
            dataProvider = new QueryResultDataProvider(response.getData());
×
121
            filteredDataProvider = new FilteredQueryResultDataProvider(dataProvider, response);
×
122
            table = new DataTable<>("table", columns, filteredDataProvider, viewDisplay.getPageSize() < 1 ? Integer.MAX_VALUE : viewDisplay.getPageSize());
×
123
            table.setOutputMarkupId(true);
×
124
            table.addBottomToolbar(new AjaxNavigationToolbar(table));
×
125
            table.addBottomToolbar(new NoRecordsToolbar(table));
×
126
            table.addTopToolbar(new AjaxFallbackHeadersToolbar<String>(table, dataProvider));
×
127
            add(table);
×
128
        } catch (Exception ex) {
×
129
            logger.error("Error creating table for query {}", grlcQuery.getQueryId(), ex);
×
130
            add(new Label("table", "").setVisible(false));
×
131
            addErrorMessage(ex.getMessage());
×
132
        }
×
133
    }
×
134

135
    private class Column extends AbstractColumn<ApiResponseEntry, String> {
136

137
        private String key;
138
        public static final String ACTIONS = "*actions*";
139

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

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

287
    }
288

289
    private static String truncateLabel(String label) {
290
        return Utils.truncateLabel(label);
×
291
    }
292

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