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

knowledgepixels / nanodash / 19666099531

25 Nov 2025 10:19AM UTC coverage: 13.938% (-0.1%) from 14.072%
19666099531

push

github

tkuhn
refactor(ResourceView): Consistent naming

523 of 4830 branches covered (10.83%)

Branch coverage included in aggregate %.

1390 of 8895 relevant lines covered (15.63%)

0.69 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 java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6

7
import org.apache.wicket.Component;
8
import org.apache.wicket.behavior.AttributeAppender;
9
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxNavigationToolbar;
10
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
11
import org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortState;
12
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
13
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
14
import org.apache.wicket.extensions.markup.html.repeater.data.table.HeadersToolbar;
15
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
16
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
17
import org.apache.wicket.extensions.markup.html.repeater.data.table.NoRecordsToolbar;
18
import org.apache.wicket.extensions.markup.html.repeater.util.SingleSortState;
19
import org.apache.wicket.markup.html.basic.Label;
20
import org.apache.wicket.markup.html.link.AbstractLink;
21
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
22
import org.apache.wicket.markup.html.panel.Panel;
23
import org.apache.wicket.markup.repeater.Item;
24
import org.apache.wicket.model.IModel;
25
import org.apache.wicket.model.Model;
26
import org.apache.wicket.request.mapper.parameter.PageParameters;
27
import org.eclipse.rdf4j.model.IRI;
28
import org.nanopub.extra.services.ApiResponse;
29
import org.nanopub.extra.services.ApiResponseEntry;
30
import org.nanopub.extra.services.QueryRef;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

34
import com.knowledgepixels.nanodash.GrlcQuery;
35
import com.knowledgepixels.nanodash.MaintainedResource;
36
import com.knowledgepixels.nanodash.ResourceView;
37
import com.knowledgepixels.nanodash.Space;
38
import com.knowledgepixels.nanodash.Utils;
39
import com.knowledgepixels.nanodash.ViewDisplay;
40
import com.knowledgepixels.nanodash.page.ExplorePage;
41
import com.knowledgepixels.nanodash.page.NanodashPage;
42
import com.knowledgepixels.nanodash.page.PublishPage;
43
import com.knowledgepixels.nanodash.template.Template;
44

45
/**
46
 * A table component that displays the results of a query.
47
 */
48
public class QueryResultTable extends Panel {
49

50
    private static final Logger logger = LoggerFactory.getLogger(QueryResultTable.class);
×
51

52
    private Model<String> errorMessages = Model.of("");
×
53
    private DataTable<ApiResponseEntry, String> table;
54
    private Label errorLabel;
55
    private boolean finalized = false;
×
56
    private List<AbstractLink> buttons = new ArrayList<>();
×
57
    private String contextId;
58
    private Space space;
59
    private final QueryRef queryRef;
60
    private final ViewDisplay viewDisplay;
61

62
    QueryResultTable(String id, QueryRef queryRef, ApiResponse response, boolean plain, ViewDisplay viewDisplay, String contextId) {
63
        super(id);
×
64
        this.contextId = contextId;
×
65
        this.queryRef = queryRef;
×
66
        this.viewDisplay = viewDisplay;
×
67

68
        final GrlcQuery grlcQuery = GrlcQuery.get(queryRef);
×
69
        add(new AttributeAppender("class", " col-" + viewDisplay.getDisplayWidth()));
×
70

71
        if (plain) {
×
72
            add(new Label("label").setVisible(false));
×
73
            add(new Label("morelink").setVisible(false));
×
74
        } else {
75
            String label = grlcQuery.getLabel();
×
76
            if (viewDisplay.getTitle() != null) label = viewDisplay.getTitle();
×
77
            add(new Label("label", label));
×
78
            if (viewDisplay.getNanopubId() != null) {
×
79
                add(new BookmarkablePageLink<Void>("morelink", ExplorePage.class, new PageParameters().set("id", viewDisplay.getNanopubId())));
×
80
            } else {
81
                add(new Label("morelink").setVisible(false));
×
82
            }
83
        }
84

85
        errorLabel = new Label("error-messages", errorMessages);
×
86
        errorLabel.setVisible(false);
×
87
        add(errorLabel);
×
88

89
        List<IColumn<ApiResponseEntry, String>> columns = new ArrayList<>();
×
90
        DataProvider dp;
91
        try {
92
            for (String h : response.getHeader()) {
×
93
                if (h.endsWith("_label")) continue;
×
94
                columns.add(new Column(h.replaceAll("_", " "), h));
×
95
            }
96
            if (!viewDisplay.getView().getViewEntryActionList().isEmpty()) {
×
97
                columns.add(new Column("", Column.ACTIONS));
×
98
            }
99
            dp = new DataProvider(response.getData());
×
100
            table = new DataTable<>("table", columns, dp, viewDisplay.getPageSize() < 1 ? Integer.MAX_VALUE : viewDisplay.getPageSize());
×
101
            table.setOutputMarkupId(true);
×
102
            table.addBottomToolbar(new AjaxNavigationToolbar(table));
×
103
            table.addBottomToolbar(new NoRecordsToolbar(table));
×
104
            table.addTopToolbar(new HeadersToolbar<String>(table, dp));
×
105
            add(table);
×
106
        } catch (Exception ex) {
×
107
            logger.error("Error creating table for query {}", grlcQuery.getQueryId(), ex);
×
108
            add(new Label("table", "").setVisible(false));
×
109
            addErrorMessage(ex.getMessage());
×
110
        }
×
111
    }
×
112

113
    // TODO button adding method copied and adjusted from ItemListPanel
114
    // TODO Improve this (member/admin) button handling:
115
    public QueryResultTable addButton(String label, Class<? extends NanodashPage> pageClass, PageParameters parameters) {
116
        if (parameters == null) parameters = new PageParameters();
×
117
        if (contextId != null) parameters.set("context", contextId);
×
118
        AbstractLink button = new BookmarkablePageLink<NanodashPage>("button", pageClass, parameters);
×
119
        button.setBody(Model.of(label));
×
120
        buttons.add(button);
×
121
        return this;
×
122
    }
123

124
    @Override
125
    protected void onBeforeRender() {
126
        if (!finalized) {
×
127
            if (!buttons.isEmpty()) {
×
128
                add(new ButtonList("buttons", space, buttons, null, null));
×
129
            } else {
130
                add(new Label("buttons").setVisible(false));
×
131
            }
132
            finalized = true;
×
133
        }
134
        super.onBeforeRender();
×
135
    }
×
136

137
    /**
138
     * Set the space for this component.
139
     *
140
     * @param space The space to set.
141
     */
142
    public void setSpace(Space space) {
143
        this.space = space;
×
144
    }
×
145

146
    private void addErrorMessage(String errorMessage) {
147
        String s = errorMessages.getObject();
×
148
        if (s.isEmpty()) {
×
149
            s = "Error: " + errorMessage;
×
150
        } else {
151
            s += ", " + errorMessage;
×
152
        }
153
        errorMessages.setObject(s);
×
154
        errorLabel.setVisible(true);
×
155
        if (table != null) table.setVisible(false);
×
156
    }
×
157

158

159
    private class Column extends AbstractColumn<ApiResponseEntry, String> {
160

161
        private String key;
162
        public static final String ACTIONS = "*actions*";
163

164
        public Column(String title, String key) {
×
165
            super(new Model<String>(title), key);
×
166
            this.key = key;
×
167
        }
×
168

169
        @Override
170
        public void populateItem(Item<ICellPopulator<ApiResponseEntry>> cellItem, String componentId, IModel<ApiResponseEntry> rowModel) {
171
            try {
172
                if (key.equals(ACTIONS)) {
×
173
                    List<AbstractLink> links = new ArrayList<>();
×
174
                    ResourceView view = viewDisplay.getView();
×
175
                    for (IRI actionIri : view.getViewEntryActionList()) {
×
176
                        // TODO Copied code and adjusted from QueryResultTableBuilder:
177
                        Template t = view.getTemplateForAction(actionIri);
×
178
                        if (t == null) continue;
×
179
                        String targetField = view.getTemplateTargetFieldForAction(actionIri);
×
180
                        if (targetField == null) targetField = "resource";
×
181
                        String label = view.getLabelForAction(actionIri);
×
182
                        if (label == null) label = "action...";
×
183
                        PageParameters params = new PageParameters().set("template", t.getId()).set("param_" + targetField, contextId).set("context", contextId);
×
184
                        String partField = view.getTemplatePartFieldForAction(actionIri);
×
185
                        if (partField != null) {
×
186
                            // TODO Find a better way to pass the MaintainedResource object to this method:
187
                            MaintainedResource r = MaintainedResource.get(contextId);
×
188
                            if (r != null && r.getNamespace() != null) {
×
189
                                params.set("param_" + partField, r.getNamespace() + "<SET-SUFFIX>");
×
190
                            }
191
                        }
192
                        String queryMapping = view.getTemplateQueryMapping(actionIri);
×
193
                        if (queryMapping != null && queryMapping.contains(":")) {
×
194
                            // This part is different from the code in QueryResultTableBuilder:
195
                            String queryParam = queryMapping.split(":")[0];
×
196
                            String templateParam = queryMapping.split(":")[1];
×
197
                            params.set("param_" + templateParam, rowModel.getObject().get(queryParam));
×
198
                        }
199
                        params.set("refresh-upon-publish", queryRef.getAsUrlString());
×
200
                        AbstractLink button = new BookmarkablePageLink<NanodashPage>("button", PublishPage.class, params);
×
201
                        button.setBody(Model.of(label));
×
202
                        links.add(button);
×
203
                    }
×
204
                    cellItem.add(new ButtonList(componentId, space, links, null, null));
×
205
                } else {
×
206
                    String value = rowModel.getObject().get(key);
×
207
                    if (value.matches("https?://.+ .+")) {
×
208
                        List<Component> links = new ArrayList<>();
×
209
                        for (String v : value.split(" ")) {
×
210
                            links.add(new NanodashLink("component", v));
×
211
                        }
212
                        cellItem.add(new ComponentSequence(componentId, ", ", links));
×
213
                    } else if (value.matches("https?://.+")) {
×
214
                        String label = rowModel.getObject().get(key + "_label");
×
215
                        cellItem.add(new NanodashLink(componentId, value, null, null, label, contextId));
×
216
                    } else {
×
217
                        if (key.startsWith("pubkey")) {
×
218
                            cellItem.add(new Label(componentId, value).add(new AttributeAppender("style", "overflow-wrap: anywhere;")));
×
219
                        } else {
220
                            Label cellLabel;
221
                            if (Utils.looksLikeHtml(value)) {
×
222
                                cellLabel = (Label) new Label(componentId, Utils.sanitizeHtml(value))
×
223
                                        .setEscapeModelStrings(false)
×
224
                                        .add(new AttributeAppender("class", "cell-data-html"));
×
225
                            } else {
226
                                cellLabel = new Label(componentId, value);
×
227
                            }
228
                            cellItem.add(cellLabel);
×
229
                        }
230
                    }
231
                }
232
            } catch (Exception ex) {
×
233
                logger.error("Failed to populate table column: ", ex);
×
234
                cellItem.add(new Label(componentId).setVisible(false));
×
235
                addErrorMessage(ex.getMessage());
×
236
            }
×
237
        }
×
238

239
    }
240

241

242
    private class DataProvider implements ISortableDataProvider<ApiResponseEntry, String> {
243

244
        private List<ApiResponseEntry> data = new ArrayList<>();
×
245
        private SingleSortState<String> sortState = new SingleSortState<>();
×
246

247
        public DataProvider() {
×
248
//                        sortState.setSort(new SortParam<String>("date", false));
249
        }
×
250

251
        public DataProvider(List<ApiResponseEntry> data) {
252
            this();
×
253
            this.data = data;
×
254
        }
×
255

256
        @Override
257
        public Iterator<? extends ApiResponseEntry> iterator(long first, long count) {
258
//                        List<ApiResponseEntry> copy = new ArrayList<>(data);
259
//                        ApiResponseComparator comparator = new ApiResponseComparator(sortState.getSort());
260
//                        Collections.sort(copy, comparator);
261
//                        return Utils.subList(copy, first, first + count).iterator();
262
            return Utils.subList(data, first, first + count).iterator();
×
263
        }
264

265
        @Override
266
        public IModel<ApiResponseEntry> model(ApiResponseEntry object) {
267
            return new Model<ApiResponseEntry>(object);
×
268
        }
269

270
        @Override
271
        public long size() {
272
            return data.size();
×
273
        }
274

275
        @Override
276
        public ISortState<String> getSortState() {
277
            return sortState;
×
278
        }
279

280
        @Override
281
        public void detach() {
282
        }
×
283

284
    }
285

286
//    private class ApiResponseComparator implements Comparator<ApiResponseEntry>, Serializable {
287
//
288
//        private SortParam<String> sortParam;
289
//
290
//        public ApiResponseComparator(SortParam<String> sortParam) {
291
//            this.sortParam = sortParam;
292
//        }
293
//
294
//        @Override
295
//        public int compare(ApiResponseEntry o1, ApiResponseEntry o2) {
296
//            String p = sortParam.getProperty();
297
//            int result;
298
//            if (o1.get(p) == null && o2.get(p) == null) {
299
//                result = 0;
300
//            } else if (o1.get(p) == null) {
301
//                result = 1;
302
//            } else if (o2.get(p) == null) {
303
//                result = -1;
304
//            } else {
305
//                result = o1.get(p).compareTo(o2.get(p));
306
//            }
307
//            if (!sortParam.isAscending()) result = -result;
308
//            return result;
309
//        }
310
//
311
//    }
312

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