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

knowledgepixels / nanodash / 26638862479

29 May 2026 01:03PM UTC coverage: 20.826% (-0.01%) from 20.836%
26638862479

Pull #477

github

web-flow
Merge 3337ee0f8 into 7ef27216c
Pull Request #477: feat: render /explore "Described in" as a view display + list empty-state note

1003 of 6132 branches covered (16.36%)

Branch coverage included in aggregate %.

2584 of 11092 relevant lines covered (23.3%)

3.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
src/main/java/com/knowledgepixels/nanodash/page/ExplorePage.java
1
package com.knowledgepixels.nanodash.page;
2

3
import com.google.common.collect.ArrayListMultimap;
4
import com.google.common.collect.Multimap;
5
import com.knowledgepixels.nanodash.*;
6
import com.knowledgepixels.nanodash.component.*;
7
import com.knowledgepixels.nanodash.domain.AbstractResourceWithProfile;
8
import com.knowledgepixels.nanodash.domain.IndividualAgent;
9
import com.knowledgepixels.nanodash.domain.User;
10
import com.knowledgepixels.nanodash.repository.MaintainedResourceRepository;
11
import com.knowledgepixels.nanodash.repository.SpaceRepository;
12
import jakarta.servlet.http.HttpServletRequest;
13
import net.trustyuri.TrustyUriUtils;
14
import org.apache.wicket.RestartResponseException;
15
import org.apache.wicket.markup.html.WebMarkupContainer;
16
import org.apache.wicket.markup.html.basic.Label;
17
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
18
import org.apache.wicket.markup.html.link.ExternalLink;
19
import org.apache.wicket.model.LoadableDetachableModel;
20
import org.apache.wicket.model.Model;
21
import org.apache.wicket.request.flow.RedirectToUrlException;
22
import org.apache.wicket.request.mapper.parameter.INamedParameters.NamedPair;
23
import org.apache.wicket.request.mapper.parameter.PageParameters;
24
import org.commonjava.mimeparse.MIMEParse;
25
import org.eclipse.rdf4j.model.IRI;
26
import org.eclipse.rdf4j.model.Statement;
27
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
28
import org.eclipse.rdf4j.model.vocabulary.RDF;
29
import org.eclipse.rdf4j.model.vocabulary.SKOS;
30
import org.nanopub.Nanopub;
31
import org.nanopub.NanopubUtils;
32
import org.nanopub.extra.security.SignatureUtils;
33
import org.nanopub.extra.services.ApiResponse;
34
import org.nanopub.extra.services.QueryRef;
35
import org.nanopub.vocabulary.KPXL_GRLC;
36
import org.nanopub.vocabulary.NPX;
37
import org.nanopub.vocabulary.NTEMPLATE;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

41
import java.util.HashMap;
42
import java.util.HashSet;
43
import java.util.Map;
44
import java.util.Set;
45

46
/**
47
 * ExplorePage is a page that allows users to explore a specific Nanopublication or Thing.
48
 */
49
public class ExplorePage extends NanodashPage {
50

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

53
    /**
54
     * The mount path for this page.
55
     */
56
    public static final String MOUNT_PATH = "/explore";
57

58
    private static final String DESCRIBED_IN_VIEW = "https://w3id.org/np/RAuOf1T2Dto4qh604lq9meHf7y-7jCKR_q7yXUuX-6-5c/described-in-view";
59

60
    /**
61
     * {@inheritDoc}
62
     */
63
    @Override
64
    public String getMountPath() {
65
        return MOUNT_PATH;
×
66
    }
67

68
    private Nanopub publishedNanopub = null;
×
69

70
    /**
71
     * Constructor for ExplorePage.
72
     *
73
     * @param parameters Page parameters containing the ID of the Nanopublication or Thing to explore.
74
     */
75
    public ExplorePage(final PageParameters parameters) {
76
        super(parameters);
×
77
        add(new Label("publish-confirm-panel").setVisible(false));
×
78
        initPage();
×
79
    }
×
80

81
    public ExplorePage(final Nanopub publishedNanopub, final PageParameters parameters) {
82
        super(parameters.set("id", publishedNanopub.getUri()));
×
83
        this.publishedNanopub = publishedNanopub;
×
84

85
        WebMarkupContainer publishConfirmPanel = new WebMarkupContainer("publish-confirm-panel");
×
86
        final NanodashSession session = NanodashSession.get();
×
87
        boolean hasKnownOwnLocalIntro = session.getLocalIntroCount() > 0;
×
88
        boolean someIntroJustNowPublished = Utils.usesPredicateInAssertion(publishedNanopub, NPX.DECLARED_BY);
×
89
        if (someIntroJustNowPublished) NanodashSession.get().setIntroPublishedNow();
×
90
        boolean lastIntroPublishedMoreThanFiveMinsAgo = session.getTimeSinceLastIntroPublished() > 5 * 60 * 1000;
×
91
        if (!hasKnownOwnLocalIntro && session.hasIntroPublished()) User.refreshUsers();
×
92
        publishConfirmPanel.add(new WebMarkupContainer("missing-intro-warning").setVisible(!hasKnownOwnLocalIntro && lastIntroPublishedMoreThanFiveMinsAgo));
×
93

94
        PageParameters plainLinkParams = new PageParameters();
×
95
        plainLinkParams.set("template", parameters.get("template"));
×
96
        if (!parameters.get("template-version").isEmpty()) {
×
97
            plainLinkParams.set("template-version", parameters.get("template-version"));
×
98
        }
99
        publishConfirmPanel.add(new BookmarkablePageLink<Void>("publish-another-link", PublishPage.class, plainLinkParams));
×
100

101
        PageParameters linkParams = new PageParameters(parameters);
×
102
        linkParams.remove("supersede");
×
103
        linkParams.remove("supersede-a");
×
104
        boolean publishAnotherFilledLinkVisible = false;
×
105
        for (NamedPair n : linkParams.getAllNamed()) {
×
106
            if (n.getKey().matches("(param|prparam|piparam[1-9][0-9]*)_.*")) {
×
107
                publishAnotherFilledLinkVisible = true;
×
108
                break;
×
109
            }
110
        }
×
111
        if (publishAnotherFilledLinkVisible) {
×
112
            publishConfirmPanel.add(new BookmarkablePageLink<Void>("publish-another-filled-link", PublishPage.class, linkParams));
×
113
        } else {
114
            publishConfirmPanel.add(new Label("publish-another-filled-link", "").setVisible(false));
×
115
        }
116
        add(publishConfirmPanel);
×
117

118
        initPage();
×
119
    }
×
120

121
    private void initPage() {
122
        PageParameters parameters = getPageParameters();
×
123

124
        String tempRef = parameters.get("id").toString();
×
125
        // Sometimes these Wicket session IDs end up here and they can mess up the query cache:
126
        tempRef = tempRef.replaceFirst(";jsessionid.*$", "");
×
127

128
        String contextId = parameters.get("context").toString("");
×
129

130
        add(new TitleBar("titlebar", this, null));
×
131

132
        if (SpaceRepository.get().findById(contextId) != null) {
×
133
            add(new BookmarkablePageLink<Void>("back-to-context-link", SpacePage.class, new PageParameters().set("id", contextId)).setBody(LoadableDetachableModel.of(() -> {
×
134
                var space = SpaceRepository.get().findById(contextId);
×
135
                return "back to " + (space == null ? contextId : space.getLabel());
×
136
            })));
137
        } else if (MaintainedResourceRepository.get().findById(contextId) != null) {
×
138
            add(new BookmarkablePageLink<Void>("back-to-context-link", MaintainedResourcePage.class, new PageParameters().set("id", contextId)).setBody(LoadableDetachableModel.of(() -> {
×
139
                var resource = MaintainedResourceRepository.get().findById(contextId);
×
140
                return "back to " + (resource == null ? contextId : resource.getLabel());
×
141
            })));
142
        } else if (IndividualAgent.isUser(contextId)) {
×
143
            add(new BookmarkablePageLink<Void>("back-to-context-link", UserPage.class, new PageParameters().set("id", contextId)).setBody(LoadableDetachableModel.of(() -> "back to " + User.getShortDisplayName(Utils.vf.createIRI(contextId)))));
×
144
        } else {
145
            add(new Label("back-to-context-link").setVisible(false));
×
146
        }
147

148
        if (User.getUserData().isUser(tempRef)) {
×
149
            add(new BookmarkablePageLink<Void>("to-specific-page-link", UserPage.class, parameters).setBody(Model.of("go to user page")));
×
150
        } else if (SpaceRepository.get().findById(tempRef) != null) {
×
151
            add(new BookmarkablePageLink<Void>("to-specific-page-link", SpacePage.class, parameters).setBody(Model.of("go to Space page")));
×
152
        } else {
153
            add(new Label("to-specific-page-link").setVisible(false));
×
154
        }
155

156
        WebMarkupContainer raw = new WebMarkupContainer("raw");
×
157
        add(raw);
×
158

159
        Map<String, String> nanopubParams = new HashMap<>();
×
160
        nanopubParams.put("ref", tempRef);
×
161
        Nanopub np = Utils.getAsNanopub(tempRef);
×
162
        boolean isNanopubId = (np != null);
×
163
        if (isNanopubId) {
×
164
            tempRef = np.getUri().stringValue();
×
165
        }
166
        if (!isNanopubId && tempRef.matches("^(.*[^A-Za-z0-9-_])?RA[A-Za-z0-9-_]{43}[^A-Za-z0-9-_].*$")) {
×
167
            np = Utils.getAsNanopub(tempRef.replaceFirst("^(.*[^A-Za-z0-9-_])?(RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$2"));
×
168
            if (np != null) {
×
169
                String npId = np.getUri().stringValue();
×
170
                tempRef = npId + tempRef.replaceFirst("^(.*[^A-Za-z0-9-_])?(RA[A-Za-z0-9-_]{43})([^A-Za-z0-9-_].*)$", "$3");
×
171
                Multimap<String, String> params = ArrayListMultimap.create();
×
172
                params.put("thing", tempRef);
×
173
                params.put("np", npId);
×
174
                ApiResponse resp = ApiCache.retrieveResponseSync(new QueryRef(QueryApiAccess.GET_LATEST_THING_NANOPUB, params), false);
×
175
                if (!resp.getData().isEmpty()) {
×
176
                    // TODO We take the most recent in case more than one latest version exists. Make other latest versions visible too.
177
                    npId = resp.getData().getFirst().get("latestVersion");
×
178
                }
179
                np = Utils.getAsNanopub(npId);
×
180
            }
181
        }
182

183
        if (parameters.get("forward-to-part").toString("").equals("true") && !contextId.isEmpty() && publishedNanopub == null) {
×
184
            parameters.remove("forward-to-part");
×
185
            Set<IRI> classes = new HashSet<>();
×
186
            if (np != null) {
×
187
                Set<String> introducedIds = NanopubUtils.getIntroducedIriIds(np);
×
188
                if (introducedIds.size() == 1 && introducedIds.iterator().next().equals(tempRef)) {
×
189
                    for (Statement st : np.getAssertion()) {
×
190
                        if (!st.getSubject().stringValue().equals(tempRef)) continue;
×
191
                        if (st.getPredicate().equals(DCTERMS.IS_PART_OF) || st.getPredicate().equals(SKOS.IN_SCHEME)) {
×
192
                            String resourceId = st.getObject().stringValue();
×
193
                            if (MaintainedResourceRepository.get().findById(resourceId) == null) continue;
×
194
                            throw new RestartResponseException(ResourcePartPage.class, parameters);
×
195
                        } else if (st.getPredicate().equals(RDF.TYPE) && st.getObject() instanceof IRI objIri) {
×
196
                            classes.add(objIri);
×
197
                        }
198
                    }
×
199
                }
200
            }
201
            AbstractResourceWithProfile contextResource = AbstractResourceWithProfile.get(contextId);
×
202
            if (contextResource instanceof IndividualAgent && !IndividualAgent.isUser(contextId)) {
×
203
                contextResource = null;
×
204
            }
205
            if (contextResource != null && contextResource.appliesTo(tempRef, classes)) {
×
206
                throw new RestartResponseException(ResourcePartPage.class, parameters);
×
207
            }
208
        }
209

210
        WebMarkupContainer nanopubSection = new WebMarkupContainer("nanopub-section");
×
211

212
        if (np == null) {
×
213
            nanopubSection.setVisible(false);
×
214
            nanopubSection.add(new Label("nanopub-header", ""));
×
215
            nanopubSection.add(new Label("nanopub", ""));
×
216
            raw.setVisible(false);
×
217
            nanopubSection.add(raw);
×
218
            nanopubSection.add(new WebMarkupContainer("use-template").setVisible(false));
×
219
            nanopubSection.add(new WebMarkupContainer("run-query").setVisible(false));
×
220
        } else {
221

222
            // Check whether we should redirect to Nanopub Registry for machine-friendly formats:
223
            String mimeType = Utils.TYPE_HTML;
×
224
            try {
225
                HttpServletRequest httpRequest = (HttpServletRequest) getRequest().getContainerRequest();
×
226
                mimeType = MIMEParse.bestMatch(Utils.SUPPORTED_TYPES_LIST, httpRequest.getHeader("Accept"));
×
227
            } catch (Exception ex) {
×
228
                logger.error("Error determining MIME type from Accept header.", ex);
×
229
            }
×
230
            if (!mimeType.equals(Utils.TYPE_HTML)) {
×
231
                logger.info("Non-HTML content type: {}", mimeType);
×
232
                // TODO Make this registry URL configurable/dynamic:
233
                String redirectUrl = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
234
                logger.info("Redirecting to: {}", redirectUrl);
×
235
                throw new RedirectToUrlException(redirectUrl, 302);
×
236
            }
237

238
            String nanopubHeaderLabel = "<h4>%s</h4>";
×
239
            if (isNanopubId) {
×
240
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "📌 Nanopublication");
×
241
            } else {
242
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "🚩 Minted in Nanopublication");
×
243
            }
244
            nanopubSection.add(new Label("nanopub-header", nanopubHeaderLabel).setEscapeModelStrings(false));
×
245
            nanopubSection.add(new NanopubItem("nanopub", NanopubElement.get(np)));
×
246
            String url = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
247
            raw.add(new ExternalLink("trig-txt", url + ".trig.txt"));
×
248
            raw.add(new ExternalLink("jsonld-txt", url + ".jsonld.txt"));
×
249
            raw.add(new ExternalLink("nq-txt", url + ".nq.txt"));
×
250
            raw.add(new ExternalLink("xml-txt", url + ".xml.txt"));
×
251
            raw.add(new ExternalLink("trig", url + ".trig"));
×
252
            raw.add(new ExternalLink("jsonld", url + ".jsonld"));
×
253
            raw.add(new ExternalLink("nq", url + ".nq"));
×
254
            raw.add(new ExternalLink("xml", url + ".xml"));
×
255
            String npUri = np.getUri().stringValue();
×
256
            raw.add(createAssertionLink("a-turtle", npUri, "turtle", false));
×
257
            raw.add(createAssertionLink("a-turtle-txt", npUri, "turtle", true));
×
258
            raw.add(createAssertionLink("a-jsonld", npUri, "jsonld", false));
×
259
            raw.add(createAssertionLink("a-jsonld-txt", npUri, "jsonld", true));
×
260
            raw.add(createAssertionLink("a-nt", npUri, "nt", false));
×
261
            raw.add(createAssertionLink("a-nt-txt", npUri, "nt", true));
×
262
            raw.add(createAssertionLink("a-rdfxml", npUri, "rdfxml", false));
×
263
            raw.add(createAssertionLink("a-rdfxml-txt", npUri, "rdfxml", true));
×
264
            nanopubSection.add(raw);
×
265
            if (Utils.isNanopubOfClass(np, NTEMPLATE.ASSERTION_TEMPLATE)) {
×
266
                nanopubSection.add(new WebMarkupContainer("use-template").add(new BookmarkablePageLink<Void>("template-link", PublishPage.class, new PageParameters().set("template", np.getUri()))));
×
267
            } else {
268
                nanopubSection.add(new WebMarkupContainer("use-template").setVisible(false));
×
269
            }
270
            if (Utils.isNanopubOfClass(np, KPXL_GRLC.GRLC_QUERY)) {
×
271
                nanopubSection.add(new WebMarkupContainer("run-query").add(new BookmarkablePageLink<Void>("query-link", QueryPage.class, new PageParameters().set("id", np.getUri()))));
×
272
            } else {
273
                nanopubSection.add(new WebMarkupContainer("run-query").setVisible(false));
×
274
            }
275
        }
276
        add(nanopubSection);
×
277

278
        final String ref = tempRef;
×
279
        final String shortName;
280
        if (publishedNanopub != null) {
×
281
            shortName = NanopubUtils.getLabel(np);
×
282
        } else if (parameters.get("label").isEmpty()) {
×
283
            shortName = Utils.getShortNameFromURI(ref);
×
284
        } else {
285
            shortName = parameters.get("label").toString();
×
286
        }
287
        add(new Label("pagetitle", shortName + " (explore) | nanodash"));
×
288
        add(new Label("termname", shortName));
×
289

290
        //add(new ExternalLink("urilink", ref, ref));
291
        add(new ExternalLinkWithActionsPanel("urilink", Model.of(ref)));
×
292

293
        add(new BookmarkablePageLink<Void>("references-link", ReferencesPage.class, new PageParameters().set("id", ref)));
×
294
        if (publishedNanopub != null) {
×
295
            add(new Label("statusLine").setVisible(false));
×
296
        } else if (np != null && SignatureUtils.seemsToHaveSignature(np)) {
×
297
            add(StatusLine.createComponent("statusLine", np.getUri().stringValue()));
×
298
        } else {
299
            add(new Label("statusLine").setVisible(false));
×
300
        }
301
        WebMarkupContainer infoSection = new WebMarkupContainer("info-section");
×
302
        if (publishedNanopub != null) {
×
303
            infoSection.add(new Label("classes-panel").setVisible(false));
×
304
        } else {
305
            infoSection.add(ThingListPanel.createComponent("classes-panel", ThingListPanel.Mode.CLASSES, ref, "<em>Searching for classes...</em>"));
×
306
        }
307
        if (isNanopubId) {
×
308
            infoSection.add(new Label("definitions-panel").setVisible(false));
×
309
            infoSection.add(new Label("instances-panel").setVisible(false));
×
310
            infoSection.add(new Label("parts-panel").setVisible(false));
×
311
            infoSection.add(new Label("templates-panel").setVisible(false));
×
312
        } else {
313
            View describedInView = View.get(DESCRIBED_IN_VIEW);
×
314
            QueryRef describedInQueryRef = new QueryRef(describedInView.getQuery().getQueryId(), "term", ref);
×
315
            infoSection.add(QueryResultListBuilder.create("definitions-panel", describedInQueryRef, new ViewDisplay(describedInView)).build());
×
316
            infoSection.add(ThingListPanel.createComponent("instances-panel", ThingListPanel.Mode.INSTANCES, ref, "<em>Searching for instances...</em>"));
×
317
            infoSection.add(ThingListPanel.createComponent("parts-panel", ThingListPanel.Mode.PARTS, ref, "<em>Searching for parts...</em>"));
×
318
            infoSection.add(ThingListPanel.createComponent("templates-panel", ThingListPanel.Mode.TEMPLATES, ref, "<em>Searching for templates...</em>"));
×
319
        }
320
        add(infoSection);
×
321
    }
×
322

323
    @Override
324
    protected void onBeforeRender() {
325
        if (publishedNanopub != null && !getPageParameters().get("postpub-redirect-url").isNull()) {
×
326
            String forwardUrl = getPageParameters().get("postpub-redirect-url").toString();
×
327
            if (forwardUrl.contains("?")) {
×
328
                // TODO: Add here URI of created nanopublication too?
329
                throw new RedirectToUrlException(forwardUrl);
×
330
            } else {
331
                Set<String> introducedIds = NanopubUtils.getIntroducedIriIds(publishedNanopub);
×
332
                String redirectId = introducedIds.size() == 1 ? introducedIds.iterator().next() : publishedNanopub.getUri().stringValue();
×
333
                String paramString = Utils.getPageParametersAsString(new PageParameters().set("id", redirectId));
×
334
                throw new RedirectToUrlException(forwardUrl + "?" + paramString);
×
335
            }
336
        }
337
        super.onBeforeRender();
×
338
    }
×
339

340
    private BookmarkablePageLink<Void> createAssertionLink(String wicketId, String npUri, String format, boolean txt) {
341
        PageParameters params = new PageParameters()
×
342
                .set("type", "np")
×
343
                .set("id", npUri)
×
344
                .set("format", format)
×
345
                .set("assertions", "");
×
346
        if (txt) {
×
347
            params.set("txt", "");
×
348
        }
349
        return new BookmarkablePageLink<>(wicketId, DownloadRdfPage.class, params);
×
350
    }
351

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