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

knowledgepixels / nanodash / 19640315218

24 Nov 2025 03:48PM UTC coverage: 14.394% (-0.2%) from 14.6%
19640315218

push

github

tkuhn
feat(ResourceView): Allow for entry-level actions

542 of 4828 branches covered (11.23%)

Branch coverage included in aggregate %.

1433 of 8893 relevant lines covered (16.11%)

0.72 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
            columns.add(new Column("", Column.ACTIONS));
×
97
            dp = new DataProvider(response.getData());
×
98
            table = new DataTable<>("table", columns, dp, viewDisplay.getPageSize() < 1 ? Integer.MAX_VALUE : viewDisplay.getPageSize());
×
99
            table.setOutputMarkupId(true);
×
100
            table.addBottomToolbar(new AjaxNavigationToolbar(table));
×
101
            table.addBottomToolbar(new NoRecordsToolbar(table));
×
102
            table.addTopToolbar(new HeadersToolbar<String>(table, dp));
×
103
            add(table);
×
104
        } catch (Exception ex) {
×
105
            logger.error("Error creating table for query {}", grlcQuery.getQueryId(), ex);
×
106
            add(new Label("table", "").setVisible(false));
×
107
            addErrorMessage(ex.getMessage());
×
108
        }
×
109
    }
×
110

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

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

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

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

156

157
    private class Column extends AbstractColumn<ApiResponseEntry, String> {
158

159
        private String key;
160
        public static final String ACTIONS = "*actions*";
161

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

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

237
    }
238

239

240
    private class DataProvider implements ISortableDataProvider<ApiResponseEntry, String> {
241

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

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

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

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

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

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

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

278
        @Override
279
        public void detach() {
280
        }
×
281

282
    }
283

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

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