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

knowledgepixels / nanodash / 26594957788

28 May 2026 06:43PM UTC coverage: 20.76% (+0.1%) from 20.649%
26594957788

push

github

web-flow
Merge pull request #474 from knowledgepixels/feat/queries-page-as-proper-view

feat: render /queries panel as a proper ResourceView with dropdown menu

1008 of 6154 branches covered (16.38%)

Branch coverage included in aggregate %.

2594 of 11197 relevant lines covered (23.17%)

3.32 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/QueryResultList.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.*;
4
import com.knowledgepixels.nanodash.domain.IndividualAgent;
5
import com.knowledgepixels.nanodash.domain.MaintainedResource;
6
import com.knowledgepixels.nanodash.domain.User;
7
import com.knowledgepixels.nanodash.page.NanodashPage;
8
import com.knowledgepixels.nanodash.page.PublishPage;
9
import com.knowledgepixels.nanodash.page.QueryPage;
10
import com.knowledgepixels.nanodash.page.UserPage;
11
import com.knowledgepixels.nanodash.repository.MaintainedResourceRepository;
12
import com.knowledgepixels.nanodash.template.Template;
13
import org.apache.wicket.Component;
14
import org.apache.wicket.ajax.AjaxRequestTarget;
15
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
16
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
17
import org.apache.wicket.markup.html.form.TextField;
18
import org.apache.wicket.extensions.markup.html.repeater.data.table.NavigatorLabel;
19
import org.apache.wicket.markup.html.WebMarkupContainer;
20
import org.apache.wicket.markup.html.basic.Label;
21
import org.apache.wicket.markup.html.link.AbstractLink;
22
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
23
import org.apache.wicket.markup.repeater.Item;
24
import org.apache.wicket.markup.repeater.RepeatingView;
25
import org.apache.wicket.markup.repeater.data.DataView;
26
import org.apache.wicket.model.Model;
27
import org.apache.wicket.request.cycle.RequestCycle;
28
import org.apache.wicket.request.mapper.parameter.PageParameters;
29
import org.apache.wicket.request.resource.ContextRelativeResourceReference;
30
import org.apache.wicket.util.string.Strings;
31
import org.eclipse.rdf4j.model.IRI;
32
import org.nanopub.extra.services.ApiResponse;
33
import org.nanopub.extra.services.ApiResponseEntry;
34
import org.nanopub.extra.services.QueryRef;
35

36
import java.util.ArrayList;
37
import java.util.List;
38

39
/**
40
 * Component for displaying query results in a list format.
41
 */
42
public class QueryResultList extends QueryResult {
43

44
    private static final String SEPARATOR = " ยท ";
45

46
    private FilteredQueryResultDataProvider filteredDataProvider;
47
    private Model<String> filterModel = Model.of("");
×
48
    private WebMarkupContainer itemsContainer;
49

50
    /**
51
     * Constructor for QueryResultList.
52
     *
53
     * @param markupId    the markup ID
54
     * @param queryRef    the query reference
55
     * @param response    the API response
56
     * @param viewDisplay the view display
57
     */
58
    QueryResultList(String markupId, QueryRef queryRef, ApiResponse response, ViewDisplay viewDisplay) {
59
        super(markupId, queryRef, response, viewDisplay);
×
60

61
        String label = grlcQuery.getLabel();
×
62
        if (viewDisplay.getTitle() != null) {
×
63
            label = viewDisplay.getTitle();
×
64
        }
65
        add(new Label("label", label));
×
66
        setOutputMarkupId(true);
×
67

68
        TextField<String> filterField = new TextField<>("filter", filterModel);
×
69
        filterField.setOutputMarkupId(true);
×
70
        filterField.add(new AjaxFormComponentUpdatingBehavior("change") {
×
71
            @Override
72
            protected void onUpdate(AjaxRequestTarget target) {
73
                if (filteredDataProvider != null && itemsContainer != null) {
×
74
                    filteredDataProvider.setFilterText(filterModel.getObject());
×
75
                    target.add(itemsContainer);
×
76
                }
77
            }
×
78
        });
79
        add(filterField);
×
80

81
        populateComponent();
×
82
    }
×
83

84
    @Override
85
    protected void populateComponent() {
86
        QueryResultDataProvider dataProvider = new QueryResultDataProvider(response.getData());
×
87
        filteredDataProvider = new FilteredQueryResultDataProvider(dataProvider, response);
×
88
        DataView<ApiResponseEntry> dataView = new DataView<>("items", filteredDataProvider) {
×
89

90
            @Override
91
            protected void populateItem(Item<ApiResponseEntry> item) {
92
                ApiResponseEntry entry = item.getModelObject();
×
93
                RepeatingView listItem = new RepeatingView("listItem");
×
94

95
                List<Component> components = new ArrayList<>();
×
96
                for (String key : response.getHeader()) {
×
97
                    if (key.endsWith("_label") || key.endsWith("_label_multi")) {
×
98
                        continue;
×
99
                    }
100
                    String entryValue = entry.get(key);
×
101
                    if (entryValue != null && !entryValue.isBlank()) {
×
102
                        if (key.endsWith("user_iri")) {
×
103
                            IRI userIri = Utils.vf.createIRI(entryValue);
×
104
                            IRI profilePicIri = User.getProfilePicture(userIri);
×
105
                            String imgSrc;
106
                            if (profilePicIri != null) {
×
107
                                imgSrc = Strings.escapeMarkup(profilePicIri.stringValue()).toString();
×
108
                            } else if (IndividualAgent.isSoftware(userIri)) {
×
109
                                imgSrc = RequestCycle.get().urlFor(new ContextRelativeResourceReference("images/bot-icon.svg", false), null).toString();
×
110
                            } else {
111
                                imgSrc = RequestCycle.get().urlFor(new ContextRelativeResourceReference("images/user-icon.svg", false), null).toString();
×
112
                            }
113
                            String userLabel = entry.get(key + "_label");
×
114
                            String displayLabel = userLabel != null && !userLabel.isBlank() ? userLabel : User.getShortDisplayName(userIri);
×
115
                            String userUrl = UserPage.MOUNT_PATH + "?id=" + Utils.urlEncode(entryValue);
×
116
                            String linkHtml = "<a href=\"" + Strings.escapeMarkup(userUrl) + "\">" + Strings.escapeMarkup(displayLabel) + "</a>";
×
117
                            components.add(new ComponentSequence("component", " ", List.of(
×
118
                                    new Label("component", "<img class=\"user-icon\" src=\"" + imgSrc + "\" />").setEscapeModelStrings(false),
×
119
                                    new Label("component", linkHtml).setEscapeModelStrings(false))));
×
120
                        } else if (key.endsWith("template_iri")) {
×
121
                            String templateLabel = entry.get(key + "_label");
×
122
                            String displayLabel = templateLabel != null && !templateLabel.isBlank() ? templateLabel : entryValue;
×
123
                            String templateUrl = PublishPage.MOUNT_PATH + "?template=" + Utils.urlEncode(entryValue) + "&template-version=latest";
×
124
                            String linkHtml = "<a href=\"" + Strings.escapeMarkup(templateUrl) + "\">" + Strings.escapeMarkup(displayLabel) + "</a>";
×
125
                            components.add(new ComponentSequence("component", " ", List.of(
×
126
                                    new Label("component", "<span class=\"form-icon\"></span>").setEscapeModelStrings(false),
×
127
                                    new Label("component", linkHtml).setEscapeModelStrings(false))));
×
128
                        } else if (key.endsWith("query_iri")) {
×
129
                            String queryLabel = entry.get(key + "_label");
×
130
                            String displayLabel = queryLabel != null && !queryLabel.isBlank() ? queryLabel : entryValue;
×
131
                            String queryUrl = QueryPage.MOUNT_PATH + "?id=" + Utils.urlEncode(entryValue);
×
132
                            String linkHtml = "<a href=\"" + Strings.escapeMarkup(queryUrl) + "\">" + Strings.escapeMarkup(displayLabel) + "</a>";
×
133
                            components.add(new Label("component", linkHtml).setEscapeModelStrings(false));
×
134
                        } else if (key.endsWith("_multi_iri")) {
×
135
                            String[] uris = entryValue.split("\\s+");
×
136
                            String labelKey = key.substring(0, key.length() - "_multi_iri".length()) + "_label_multi";
×
137
                            String labelValue = entry.get(labelKey);
×
138
                            String[] labels = labelValue != null ? labelValue.split("\n", -1) : null;
×
139
                            List<Component> links = new ArrayList<>();
×
140
                            for (int i = 0; i < uris.length; i++) {
×
141
                                String uri = uris[i];
×
142
                                if (uri.isBlank()) continue;
×
143
                                String label = (labels != null && i < labels.length && !labels[i].isBlank()) ? Utils.unescapeMultiValue(labels[i]) : null;
×
144
                                // SPARQL coalesce often falls back to the URI string itself; treat that as no label
145
                                // so NanodashLink can derive a short name from the URI.
146
                                if (label != null && label.equals(uri)) label = null;
×
147
                                links.add(new NanodashLink("component", uri, null, null, label, contextId));
×
148
                            }
149
                            components.add(new ComponentSequence("component", ", ", links));
×
150
                        } else if (key.endsWith("_multi_val")) {
×
151
                            String labelKey = key.substring(0, key.length() - "_multi_val".length()) + "_label_multi";
×
152
                            String labelValue = entry.get(labelKey);
×
153
                            String[] parts = entryValue.split("\n", -1);
×
154
                            String[] labels = labelValue != null ? labelValue.split("\n", -1) : null;
×
155
                            List<Component> multiComponents = new ArrayList<>();
×
156
                            for (int i = 0; i < parts.length; i++) {
×
157
                                String part = parts[i];
×
158
                                String label = (labels != null && i < labels.length && !labels[i].isBlank()) ? Utils.unescapeMultiValue(labels[i]) : null;
×
159
                                if (part.matches("https?://.+")) {
×
160
                                    if (label != null && label.equals(part)) label = null;
×
161
                                    multiComponents.add(new NanodashLink("component", part, null, null, label, contextId));
×
162
                                } else {
163
                                    String display = label != null ? label : Utils.unescapeMultiValue(part);
×
164
                                    boolean isHtml = Utils.looksLikeHtml(display);
×
165
                                    if (isHtml) {
×
166
                                        display = Utils.sanitizeHtml(display);
×
167
                                    }
168
                                    multiComponents.add(new Label("component", display).setEscapeModelStrings(!isHtml));
×
169
                                }
170
                            }
171
                            components.add(new ComponentSequence("component", ", ", multiComponents));
×
172
                        } else if (key.endsWith("_multi")) {
×
173
                            String[] parts = entryValue.split("\n", -1);
×
174
                            String labelKey = key.substring(0, key.length() - "_multi".length()) + "_label_multi";
×
175
                            String labelValue = entry.get(labelKey);
×
176
                            String[] labels = labelValue != null ? labelValue.split("\n", -1) : null;
×
177
                            List<Component> multiComponents = new ArrayList<>();
×
178
                            for (int i = 0; i < parts.length; i++) {
×
179
                                String display;
180
                                if (labels != null && i < labels.length && !labels[i].isBlank()) {
×
181
                                    display = Utils.unescapeMultiValue(labels[i]);
×
182
                                } else {
183
                                    display = Utils.unescapeMultiValue(parts[i]);
×
184
                                }
185
                                multiComponents.add(new Label("component", display));
×
186
                            }
187
                            components.add(new ComponentSequence("component", ", ", multiComponents));
×
188
                        } else if (entryValue.matches("https?://.+")) {
×
189
                            String entryLabel = entry.get(key + "_label");
×
190
                            components.add(new NanodashLink("component", entryValue, null, null, entryLabel, contextId));
×
191
                        } else {
×
192
                            if (Utils.looksLikeHtml(entryValue)) {
×
193
                                entryValue = Utils.sanitizeHtml(entryValue);
×
194
                            }
195
                            components.add(new Label("component", entryValue).setEscapeModelStrings(false));
×
196
                        }
197
                    }
198
                }
199
                View view = viewDisplay.getView();
×
200
                if (view != null && !view.getViewEntryActionList().isEmpty()) {
×
201
                    List<AbstractLink> links = new ArrayList<>();
×
202
                    for (IRI actionIri : view.getViewEntryActionList()) {
×
203
                        // TODO Copied code and adjusted from QueryResultTableBuilder:
204
                        Template t = view.getTemplateForAction(actionIri);
×
205
                        if (t == null) continue;
×
206
                        String targetField = view.getTemplateTargetFieldForAction(actionIri);
×
207
                        if (targetField == null) targetField = "resource";
×
208
                        String labelForAction = view.getLabelForAction(actionIri);
×
209
                        if (labelForAction == null) labelForAction = "action...";
×
210
                        if (!labelForAction.endsWith("...")) labelForAction += "...";
×
211
                        PageParameters params = new PageParameters().set("template", t.getId())
×
212
                                .set("param_" + targetField, contextId)
×
213
                                .set("context", contextId)
×
214
                                .set("template-version", "latest");
×
215
                        String partField = view.getTemplatePartFieldForAction(actionIri);
×
216
                        if (partField != null) {
×
217
                            // TODO Find a better way to pass the MaintainedResource object to this method:
218
                            MaintainedResource r = MaintainedResourceRepository.get().findById(contextId);
×
219
                            if (r != null && r.getNamespace() != null) {
×
220
                                params.set("param_" + partField, r.getNamespace() + "<SET-SUFFIX>");
×
221
                            }
222
                        }
223
                        String queryMapping = view.getTemplateQueryMapping(actionIri);
×
224
                        if (queryMapping != null && queryMapping.contains(":")) {
×
225
                            // This part is different from the code in QueryResultTableBuilder:
226
                            String queryParam = queryMapping.split(":")[0];
×
227
                            String templateParam = queryMapping.split(":")[1];
×
228
                            params.set("param_" + templateParam, entry.get(queryParam));
×
229
                        }
230
                        params.set("refresh-upon-publish", queryRef.getAsUrlString());
×
231
                        AbstractLink button = new BookmarkablePageLink<NanodashPage>("button", PublishPage.class, params);
×
232
                        button.setBody(Model.of(labelForAction));
×
233
                        links.add(button);
×
234
                    }
×
235
                    components.add(new ButtonList("component", resourceWithProfile, links, null, null));
×
236
                }
237
                ComponentSequence componentSequence = new ComponentSequence(listItem.newChildId(), SEPARATOR, components);
×
238
                listItem.add(componentSequence);
×
239
                item.add(listItem);
×
240
            }
×
241
        };
242
        dataView.setItemsPerPage(viewDisplay.getPageSize());
×
243

244
        WebMarkupContainer navigation = new WebMarkupContainer("navigation");
×
245
        navigation.add(new NavigatorLabel("navigatorLabel", dataView));
×
246
        AjaxPagingNavigator pagingNavigator = new AjaxPagingNavigator("navigator", dataView);
×
247
        navigation.setVisible(dataView.getPageCount() > 1);
×
248
        navigation.add(pagingNavigator);
×
249

250
        itemsContainer = new WebMarkupContainer("items-container");
×
251
        itemsContainer.setOutputMarkupId(true);
×
252
        itemsContainer.add(dataView);
×
253
        itemsContainer.add(navigation);
×
254
        add(itemsContainer);
×
255
    }
×
256

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