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

knowledgepixels / nanodash / 19820983664

01 Dec 2025 11:24AM UTC coverage: 15.298% (+1.9%) from 13.36%
19820983664

push

github

web-flow
Merge pull request #313 from knowledgepixels/296-refactor-usage-of-pair-as-classes

Replace Pair with new class

591 of 4966 branches covered (11.9%)

Branch coverage included in aggregate %.

1559 of 9088 relevant lines covered (17.15%)

0.76 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.sort.ISortState;
13
import org.apache.wicket.extensions.markup.html.repeater.data.table.*;
14
import org.apache.wicket.extensions.markup.html.repeater.util.SingleSortState;
15
import org.apache.wicket.markup.html.basic.Label;
16
import org.apache.wicket.markup.html.link.AbstractLink;
17
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
18
import org.apache.wicket.markup.html.panel.Panel;
19
import org.apache.wicket.markup.repeater.Item;
20
import org.apache.wicket.model.IModel;
21
import org.apache.wicket.model.Model;
22
import org.apache.wicket.request.mapper.parameter.PageParameters;
23
import org.eclipse.rdf4j.model.IRI;
24
import org.nanopub.extra.services.ApiResponse;
25
import org.nanopub.extra.services.ApiResponseEntry;
26
import org.nanopub.extra.services.QueryRef;
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29

30
import java.util.ArrayList;
31
import java.util.Iterator;
32
import java.util.List;
33

34
/**
35
 * A table component that displays the results of a query.
36
 */
37
public class QueryResultTable extends Panel {
38

39
    private static final Logger logger = LoggerFactory.getLogger(QueryResultTable.class);
×
40

41
    private Model<String> errorMessages = Model.of("");
×
42
    private DataTable<ApiResponseEntry, String> table;
43
    private Label errorLabel;
44
    private boolean finalized = false;
×
45
    private List<AbstractLink> buttons = new ArrayList<>();
×
46
    private String contextId;
47
    private ProfiledResource profiledResource;
48
    private Space space;
49
    private final QueryRef queryRef;
50
    private final ViewDisplay viewDisplay;
51

52
    QueryResultTable(String id, QueryRef queryRef, ApiResponse response, boolean plain, ViewDisplay viewDisplay) {
53
        super(id);
×
54
        this.queryRef = queryRef;
×
55
        this.viewDisplay = viewDisplay;
×
56

57
        final GrlcQuery grlcQuery = GrlcQuery.get(queryRef);
×
58
        add(new AttributeAppender("class", " col-" + viewDisplay.getDisplayWidth()));
×
59

60
        if (plain) {
×
61
            add(new Label("label").setVisible(false));
×
62
            add(new Label("morelink").setVisible(false));
×
63
        } else {
64
            String label = grlcQuery.getLabel();
×
65
            if (viewDisplay.getTitle() != null) label = viewDisplay.getTitle();
×
66
            add(new Label("label", label));
×
67
            if (viewDisplay.getNanopubId() != null) {
×
68
                add(new BookmarkablePageLink<Void>("morelink", ExplorePage.class, new PageParameters().set("id", viewDisplay.getNanopubId())));
×
69
            } else {
70
                add(new Label("morelink").setVisible(false));
×
71
            }
72
        }
73

74
        errorLabel = new Label("error-messages", errorMessages);
×
75
        errorLabel.setVisible(false);
×
76
        add(errorLabel);
×
77

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

102
    // TODO button adding method copied and adjusted from ItemListPanel
103
    // TODO Improve this (member/admin) button handling:
104
    public void addButton(String label, Class<? extends NanodashPage> pageClass, PageParameters parameters) {
105
        if (parameters == null) parameters = new PageParameters();
×
106
        if (contextId != null) parameters.set("context", contextId);
×
107
        AbstractLink button = new BookmarkablePageLink<NanodashPage>("button", pageClass, parameters);
×
108
        button.setBody(Model.of(label));
×
109
        buttons.add(button);
×
110
    }
×
111

112
    @Override
113
    protected void onBeforeRender() {
114
        if (!finalized) {
×
115
            if (!buttons.isEmpty()) {
×
116
                add(new ButtonList("buttons", profiledResource, buttons, null, null));
×
117
            } else {
118
                add(new Label("buttons").setVisible(false));
×
119
            }
120
            finalized = true;
×
121
        }
122
        super.onBeforeRender();
×
123
    }
×
124

125
    /**
126
     * Set the profiled resource for this component.
127
     *
128
     * @param profiledResource The profiled resource to set.
129
     */
130
    public void setProfiledResource(ProfiledResource profiledResource) {
131
        this.profiledResource = profiledResource;
×
132
    }
×
133

134
    /**
135
     * Set the context ID for this component.
136
     *
137
     * @param contextId The context ID to set.
138
     */
139
    public void setContextId(String contextId) {
140
        this.contextId = contextId;
×
141
    }
×
142

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

155
    private class Column extends AbstractColumn<ApiResponseEntry, String> {
156

157
        private String key;
158
        public static final String ACTIONS = "*actions*";
159

160
        public Column(String title, String key) {
×
161
            super(new Model<String>(title), key);
×
162
            this.key = key;
×
163
        }
×
164

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

235
    }
236

237

238
    private class DataProvider implements ISortableDataProvider<ApiResponseEntry, String> {
239

240
        private List<ApiResponseEntry> data = new ArrayList<>();
×
241
        private SingleSortState<String> sortState = new SingleSortState<>();
×
242

243
        public DataProvider() {
×
244
//                        sortState.setSort(new SortParam<String>("date", false));
245
        }
×
246

247
        public DataProvider(List<ApiResponseEntry> data) {
248
            this();
×
249
            this.data = data;
×
250
        }
×
251

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

261
        @Override
262
        public IModel<ApiResponseEntry> model(ApiResponseEntry object) {
263
            return new Model<ApiResponseEntry>(object);
×
264
        }
265

266
        @Override
267
        public long size() {
268
            return data.size();
×
269
        }
270

271
        @Override
272
        public ISortState<String> getSortState() {
273
            return sortState;
×
274
        }
275

276
        @Override
277
        public void detach() {
278
        }
×
279

280
    }
281

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

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