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

knowledgepixels / nanodash / 20264440287

16 Dec 2025 10:18AM UTC coverage: 15.358% (-0.1%) from 15.457%
20264440287

push

github

tkuhn
feat: Make use of template-version=latest in more places

593 of 4976 branches covered (11.92%)

Branch coverage included in aggregate %.

1571 of 9114 relevant lines covered (17.24%)

2.22 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.page.ExplorePage;
5
import com.knowledgepixels.nanodash.page.NanodashPage;
6
import com.knowledgepixels.nanodash.page.PublishPage;
7
import com.knowledgepixels.nanodash.template.Template;
8
import org.apache.wicket.Component;
9
import org.apache.wicket.behavior.AttributeAppender;
10
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxNavigationToolbar;
11
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
12
import org.apache.wicket.extensions.markup.html.repeater.data.table.*;
13
import org.apache.wicket.markup.html.basic.Label;
14
import org.apache.wicket.markup.html.link.AbstractLink;
15
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
16
import org.apache.wicket.markup.repeater.Item;
17
import org.apache.wicket.model.IModel;
18
import org.apache.wicket.model.Model;
19
import org.apache.wicket.request.mapper.parameter.PageParameters;
20
import org.eclipse.rdf4j.model.IRI;
21
import org.nanopub.extra.services.ApiResponse;
22
import org.nanopub.extra.services.ApiResponseEntry;
23
import org.nanopub.extra.services.QueryRef;
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

27
import java.util.ArrayList;
28
import java.util.List;
29

30
/**
31
 * Component for displaying query results in a table format.
32
 */
33
public class QueryResultTable extends QueryResult {
34

35
    private static final Logger logger = LoggerFactory.getLogger(QueryResultTable.class);
×
36

37
    private Model<String> errorMessages = Model.of("");
×
38
    private DataTable<ApiResponseEntry, String> table;
39
    private Label errorLabel;
40

41
    QueryResultTable(String id, QueryRef queryRef, ApiResponse response, ViewDisplay viewDisplay, boolean plain) {
42
        super(id, queryRef, response, viewDisplay);
×
43

44
        if (plain) {
×
45
            add(new Label("label").setVisible(false));
×
46
            add(new Label("np").setVisible(false));
×
47
        } else {
48
            String label = grlcQuery.getLabel();
×
49
            if (viewDisplay.getTitle() != null) {
×
50
                label = viewDisplay.getTitle();
×
51
            }
52
            add(new Label("label", label));
×
53
            if (viewDisplay.getNanopubId() != null) {
×
54
                add(new BookmarkablePageLink<Void>("np", ExplorePage.class, new PageParameters().set("id", viewDisplay.getNanopubId())));
×
55
            } else {
56
                add(new Label("np").setVisible(false));
×
57
            }
58
        }
59

60
        errorLabel = new Label("error-messages", errorMessages);
×
61
        errorLabel.setVisible(false);
×
62
        add(errorLabel);
×
63

64
        populateComponent();
×
65
    }
×
66

67
    private void addErrorMessage(String errorMessage) {
68
        String s = errorMessages.getObject();
×
69
        if (s.isEmpty()) {
×
70
            s = "Error: " + errorMessage;
×
71
        } else {
72
            s += ", " + errorMessage;
×
73
        }
74
        errorMessages.setObject(s);
×
75
        errorLabel.setVisible(true);
×
76
        if (table != null) table.setVisible(false);
×
77
    }
×
78

79
    @Override
80
    protected void populateComponent() {
81
        List<IColumn<ApiResponseEntry, String>> columns = new ArrayList<>();
×
82
        QueryResultDataProvider dataProvider;
83
        try {
84
            for (String h : response.getHeader()) {
×
85
                if (h.endsWith("_label")) {
×
86
                    continue;
×
87
                }
88
                columns.add(new Column(h.replaceAll("_", " "), h));
×
89
            }
90
            if (viewDisplay.getView() != null && !viewDisplay.getView().getViewEntryActionList().isEmpty()) {
×
91
                columns.add(new Column("", Column.ACTIONS));
×
92
            }
93
            dataProvider = new QueryResultDataProvider(response.getData());
×
94
            table = new DataTable<>("table", columns, dataProvider, viewDisplay.getPageSize() < 1 ? Integer.MAX_VALUE : viewDisplay.getPageSize());
×
95
            table.setOutputMarkupId(true);
×
96
            table.addBottomToolbar(new AjaxNavigationToolbar(table));
×
97
            table.addBottomToolbar(new NoRecordsToolbar(table));
×
98
            table.addTopToolbar(new HeadersToolbar<String>(table, dataProvider));
×
99
            add(table);
×
100
        } catch (Exception ex) {
×
101
            logger.error("Error creating table for query {}", grlcQuery.getQueryId(), ex);
×
102
            add(new Label("table", "").setVisible(false));
×
103
            addErrorMessage(ex.getMessage());
×
104
        }
×
105
    }
×
106

107
    private class Column extends AbstractColumn<ApiResponseEntry, String> {
108

109
        private String key;
110
        public static final String ACTIONS = "*actions*";
111

112
        public Column(String title, String key) {
×
113
            super(new Model<String>(title), key);
×
114
            this.key = key;
×
115
        }
×
116

117
        @Override
118
        public void populateItem(Item<ICellPopulator<ApiResponseEntry>> cellItem, String componentId, IModel<ApiResponseEntry> rowModel) {
119
            try {
120
                ResourceView view = viewDisplay.getView();
×
121
                if (key.equals(ACTIONS) && view != null) {
×
122
                    List<AbstractLink> links = new ArrayList<>();
×
123
                    for (IRI actionIri : view.getViewEntryActionList()) {
×
124
                        // TODO Copied code and adjusted from QueryResultTableBuilder:
125
                        Template t = view.getTemplateForAction(actionIri);
×
126
                        if (t == null) continue;
×
127
                        String targetField = view.getTemplateTargetFieldForAction(actionIri);
×
128
                        if (targetField == null) targetField = "resource";
×
129
                        String label = view.getLabelForAction(actionIri);
×
130
                        if (label == null) label = "action...";
×
131
                        PageParameters params = new PageParameters().set("template", t.getId())
×
132
                                .set("param_" + targetField, contextId)
×
133
                                .set("context", contextId)
×
134
                                .set("template-version", "latest");
×
135
                        String partField = view.getTemplatePartFieldForAction(actionIri);
×
136
                        if (partField != null) {
×
137
                            // TODO Find a better way to pass the MaintainedResource object to this method:
138
                            MaintainedResource r = MaintainedResource.get(contextId);
×
139
                            if (r != null && r.getNamespace() != null) {
×
140
                                params.set("param_" + partField, r.getNamespace() + "<SET-SUFFIX>");
×
141
                            }
142
                        }
143
                        String queryMapping = view.getTemplateQueryMapping(actionIri);
×
144
                        if (queryMapping != null && queryMapping.contains(":")) {
×
145
                            // This part is different from the code in QueryResultTableBuilder:
146
                            String queryParam = queryMapping.split(":")[0];
×
147
                            String templateParam = queryMapping.split(":")[1];
×
148
                            params.set("param_" + templateParam, rowModel.getObject().get(queryParam));
×
149
                        }
150
                        params.set("refresh-upon-publish", queryRef.getAsUrlString());
×
151
                        AbstractLink button = new BookmarkablePageLink<NanodashPage>("button", PublishPage.class, params);
×
152
                        button.setBody(Model.of(label));
×
153
                        links.add(button);
×
154
                    }
×
155
                    cellItem.add(new ButtonList(componentId, profiledResource, links, null, null));
×
156
                } else {
×
157
                    String value = rowModel.getObject().get(key);
×
158
                    if (value.matches("https?://.+ .+")) {
×
159
                        List<Component> links = new ArrayList<>();
×
160
                        for (String v : value.split(" ")) {
×
161
                            links.add(new NanodashLink("component", v));
×
162
                        }
163
                        cellItem.add(new ComponentSequence(componentId, ", ", links));
×
164
                    } else if (value.matches("https?://.+")) {
×
165
                        String label = rowModel.getObject().get(key + "_label");
×
166
                        cellItem.add(new NanodashLink(componentId, value, null, null, label, contextId));
×
167
                    } else {
×
168
                        if (key.startsWith("pubkey")) {
×
169
                            cellItem.add(new Label(componentId, value).add(new AttributeAppender("style", "overflow-wrap: anywhere;")));
×
170
                        } else {
171
                            Label cellLabel;
172
                            if (Utils.looksLikeHtml(value)) {
×
173
                                cellLabel = (Label) new Label(componentId, Utils.sanitizeHtml(value))
×
174
                                        .setEscapeModelStrings(false)
×
175
                                        .add(new AttributeAppender("class", "cell-data-html"));
×
176
                            } else {
177
                                cellLabel = new Label(componentId, value);
×
178
                            }
179
                            cellItem.add(cellLabel);
×
180
                        }
181
                    }
182
                }
183
            } catch (Exception ex) {
×
184
                logger.error("Failed to populate table column: ", ex);
×
185
                cellItem.add(new Label(componentId).setVisible(false));
×
186
                addErrorMessage(ex.getMessage());
×
187
            }
×
188
        }
×
189

190
    }
191

192
//    private class ApiResponseComparator implements Comparator<ApiResponseEntry>, Serializable {
193
//
194
//        private SortParam<String> sortParam;
195
//
196
//        public ApiResponseComparator(SortParam<String> sortParam) {
197
//            this.sortParam = sortParam;
198
//        }
199
//
200
//        @Override
201
//        public int compare(ApiResponseEntry o1, ApiResponseEntry o2) {
202
//            String p = sortParam.getProperty();
203
//            int result;
204
//            if (o1.get(p) == null && o2.get(p) == null) {
205
//                result = 0;
206
//            } else if (o1.get(p) == null) {
207
//                result = 1;
208
//            } else if (o2.get(p) == null) {
209
//                result = -1;
210
//            } else {
211
//                result = o1.get(p).compareTo(o2.get(p));
212
//            }
213
//            if (!sortParam.isAscending()) result = -result;
214
//            return result;
215
//        }
216
//
217
//    }
218

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