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

knowledgepixels / nanodash / 19772182304

28 Nov 2025 07:14PM UTC coverage: 13.901% (-0.2%) from 14.051%
19772182304

push

github

tkuhn
feat(UserPage): Add support for view displays

525 of 4894 branches covered (10.73%)

Branch coverage included in aggregate %.

1398 of 8940 relevant lines covered (15.64%)

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.ProfiledResource;
37
import com.knowledgepixels.nanodash.ResourceView;
38
import com.knowledgepixels.nanodash.Space;
39
import com.knowledgepixels.nanodash.Utils;
40
import com.knowledgepixels.nanodash.ViewDisplay;
41
import com.knowledgepixels.nanodash.page.ExplorePage;
42
import com.knowledgepixels.nanodash.page.NanodashPage;
43
import com.knowledgepixels.nanodash.page.PublishPage;
44
import com.knowledgepixels.nanodash.template.Template;
45

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

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

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

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

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

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

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

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

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

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

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

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

159

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

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

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

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

240
    }
241

242

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

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

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

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

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

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

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

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

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

285
    }
286

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

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