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

knowledgepixels / nanodash / 22679314407

04 Mar 2026 04:42PM UTC coverage: 15.81% (-0.1%) from 15.951%
22679314407

Pull #372

github

web-flow
Merge 62604b6f9 into bf50833e3
Pull Request #372: Add component for showing full URIs as plain links + ExploreDisplayMenu

701 of 5379 branches covered (13.03%)

Branch coverage included in aggregate %.

1727 of 9978 relevant lines covered (17.31%)

2.37 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.IndividualAgent;
8
import com.knowledgepixels.nanodash.domain.User;
9
import com.knowledgepixels.nanodash.repository.MaintainedResourceRepository;
10
import com.knowledgepixels.nanodash.repository.SpaceRepository;
11
import jakarta.servlet.http.HttpServletRequest;
12
import net.trustyuri.TrustyUriUtils;
13
import org.apache.wicket.RestartResponseException;
14
import org.apache.wicket.markup.html.WebMarkupContainer;
15
import org.apache.wicket.markup.html.basic.Label;
16
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
17
import org.apache.wicket.markup.html.link.ExternalLink;
18
import org.apache.wicket.model.Model;
19
import org.apache.wicket.request.flow.RedirectToUrlException;
20
import org.apache.wicket.request.mapper.parameter.INamedParameters.NamedPair;
21
import org.apache.wicket.request.mapper.parameter.PageParameters;
22
import org.commonjava.mimeparse.MIMEParse;
23
import org.eclipse.rdf4j.model.IRI;
24
import org.eclipse.rdf4j.model.Statement;
25
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
26
import org.eclipse.rdf4j.model.vocabulary.RDF;
27
import org.eclipse.rdf4j.model.vocabulary.SKOS;
28
import org.nanopub.Nanopub;
29
import org.nanopub.NanopubUtils;
30
import org.nanopub.extra.security.SignatureUtils;
31
import org.nanopub.extra.services.ApiResponse;
32
import org.nanopub.extra.services.QueryRef;
33
import org.nanopub.vocabulary.NPX;
34
import org.nanopub.vocabulary.NTEMPLATE;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

38
import java.util.HashMap;
39
import java.util.HashSet;
40
import java.util.Map;
41
import java.util.Set;
42

43
/**
44
 * ExplorePage is a page that allows users to explore a specific Nanopublication or Thing.
45
 */
46
public class ExplorePage extends NanodashPage {
47

48
    private static final Logger logger = LoggerFactory.getLogger(ExplorePage.class);
×
49

50
    /**
51
     * The mount path for this page.
52
     */
53
    public static final String MOUNT_PATH = "/explore";
54

55
    /**
56
     * {@inheritDoc}
57
     */
58
    @Override
59
    public String getMountPath() {
60
        return MOUNT_PATH;
×
61
    }
62

63
    private Nanopub publishedNanopub = null;
×
64

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

76
    public ExplorePage(final Nanopub publishedNanopub, final PageParameters parameters) {
77
        super(parameters.set("id", publishedNanopub.getUri()));
×
78
        this.publishedNanopub = publishedNanopub;
×
79

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

89
        PageParameters plainLinkParams = new PageParameters();
×
90
        plainLinkParams.set("template", parameters.get("template"));
×
91
        if (!parameters.get("template-version").isEmpty()) {
×
92
            plainLinkParams.set("template-version", parameters.get("template-version"));
×
93
        }
94
        publishConfirmPanel.add(new BookmarkablePageLink<Void>("publish-another-link", PublishPage.class, plainLinkParams));
×
95

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

113
        initPage();
×
114
    }
×
115

116
    private void initPage() {
117
        PageParameters parameters = getPageParameters();
×
118

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

123
        String contextId = parameters.get("context").toString("");
×
124

125
        add(new TitleBar("titlebar", this, null));
×
126

127
        if (SpaceRepository.get().findById(contextId) != null) {
×
128
            add(new BookmarkablePageLink<Void>("back-to-context-link", SpacePage.class, new PageParameters().set("id", contextId)).setBody(Model.of("back to " + SpaceRepository.get().findById(contextId).getLabel())));
×
129
        } else if (MaintainedResourceRepository.get().findById(contextId) != null) {
×
130
            add(new BookmarkablePageLink<Void>("back-to-context-link", MaintainedResourcePage.class, new PageParameters().set("id", contextId)).setBody(Model.of("back to " + MaintainedResourceRepository.get().findById(contextId).getLabel())));
×
131
        } else if (IndividualAgent.isUser(contextId)) {
×
132
            add(new BookmarkablePageLink<Void>("back-to-context-link", UserPage.class, new PageParameters().set("id", contextId)).setBody(Model.of("back to " + User.getShortDisplayName(Utils.vf.createIRI(contextId)))));
×
133
        } else {
134
            add(new Label("back-to-context-link").setVisible(false));
×
135
        }
136

137
        if (User.getUserData().isUser(tempRef)) {
×
138
            add(new BookmarkablePageLink<Void>("to-specific-page-link", UserPage.class, parameters).setBody(Model.of("go to user page")));
×
139
        } else if (SpaceRepository.get().findById(tempRef) != null) {
×
140
            add(new BookmarkablePageLink<Void>("to-specific-page-link", SpacePage.class, parameters).setBody(Model.of("go to Space page")));
×
141
        } else {
142
            add(new Label("to-specific-page-link").setVisible(false));
×
143
        }
144

145
        WebMarkupContainer raw = new WebMarkupContainer("raw");
×
146
        add(raw);
×
147

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

172
        if (parameters.get("forward-to-part").toString("").equals("true") && !contextId.isEmpty() && publishedNanopub == null) {
×
173
            parameters.remove("forward-to-part");
×
174
            Set<IRI> classes = new HashSet<>();
×
175
            if (np != null) {
×
176
                Set<String> introducedIds = NanopubUtils.getIntroducedIriIds(np);
×
177
                if (introducedIds.size() == 1 && introducedIds.iterator().next().equals(tempRef)) {
×
178
                    for (Statement st : np.getAssertion()) {
×
179
                        if (!st.getSubject().stringValue().equals(tempRef)) continue;
×
180
                        if (st.getPredicate().equals(DCTERMS.IS_PART_OF) || st.getPredicate().equals(SKOS.IN_SCHEME)) {
×
181
                            String resourceId = st.getObject().stringValue();
×
182
                            if (MaintainedResourceRepository.get().findById(resourceId) == null) continue;
×
183
                            throw new RestartResponseException(ResourcePartPage.class, parameters);
×
184
                        } else if (st.getPredicate().equals(RDF.TYPE) && st.getObject() instanceof IRI objIri) {
×
185
                            classes.add(objIri);
×
186
                        }
187
                    }
×
188
                }
189
            }
190
            // TODO Improve this so we have just one check:
191
            if (SpaceRepository.get().findById(contextId) != null && SpaceRepository.get().findById(contextId).appliesTo(tempRef, classes)) {
×
192
                throw new RestartResponseException(ResourcePartPage.class, parameters);
×
193
            } else if (MaintainedResourceRepository.get().findById(contextId) != null && MaintainedResourceRepository.get().findById(contextId).appliesTo(tempRef, classes)) {
×
194
                throw new RestartResponseException(ResourcePartPage.class, parameters);
×
195
            } else if (IndividualAgent.isUser(contextId) && IndividualAgent.get(contextId).appliesTo(tempRef, classes)) {
×
196
                throw new RestartResponseException(ResourcePartPage.class, parameters);
×
197
            }
198
        }
199

200
        if (np == null) {
×
201
            raw.setVisible(false);
×
202
            add(new Label("nanopub-header", ""));
×
203
            add(new Label("nanopub", ""));
×
204
            add(new WebMarkupContainer("use-template").setVisible(false));
×
205
            add(new WebMarkupContainer("run-query").setVisible(false));
×
206
        } else {
207

208
            // Check whether we should redirect to Nanopub Registry for machine-friendly formats:
209
            String mimeType = Utils.TYPE_HTML;
×
210
            try {
211
                HttpServletRequest httpRequest = (HttpServletRequest) getRequest().getContainerRequest();
×
212
                mimeType = MIMEParse.bestMatch(Utils.SUPPORTED_TYPES_LIST, httpRequest.getHeader("Accept"));
×
213
            } catch (Exception ex) {
×
214
                logger.error("Error determining MIME type from Accept header.", ex);
×
215
            }
×
216
            if (!mimeType.equals(Utils.TYPE_HTML)) {
×
217
                logger.info("Non-HTML content type: {}", mimeType);
×
218
                // TODO Make this registry URL configurable/dynamic:
219
                String redirectUrl = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
220
                logger.info("Redirecting to: {}", redirectUrl);
×
221
                throw new RedirectToUrlException(redirectUrl, 302);
×
222
            }
223

224
            String nanopubHeaderLabel = "<h4>%s</h4>";
×
225
            if (isNanopubId) {
×
226
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "Nanopublication");
×
227
            } else {
228
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "Minted in Nanopublication");
×
229
            }
230
            add(new Label("nanopub-header", nanopubHeaderLabel).setEscapeModelStrings(false));
×
231
            add(new NanopubItem("nanopub", NanopubElement.get(np)));
×
232
            String url = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
233
            raw.add(new ExternalLink("trig-txt", url + ".trig.txt"));
×
234
            raw.add(new ExternalLink("jsonld-txt", url + ".jsonld.txt"));
×
235
            raw.add(new ExternalLink("nq-txt", url + ".nq.txt"));
×
236
            raw.add(new ExternalLink("xml-txt", url + ".xml.txt"));
×
237
            raw.add(new ExternalLink("trig", url + ".trig"));
×
238
            raw.add(new ExternalLink("jsonld", url + ".jsonld"));
×
239
            raw.add(new ExternalLink("nq", url + ".nq"));
×
240
            raw.add(new ExternalLink("xml", url + ".xml"));
×
241
            if (Utils.isNanopubOfClass(np, NTEMPLATE.ASSERTION_TEMPLATE)) {
×
242
                add(new WebMarkupContainer("use-template").add(new BookmarkablePageLink<Void>("template-link", PublishPage.class, new PageParameters().set("template", np.getUri()))));
×
243
            } else {
244
                add(new WebMarkupContainer("use-template").setVisible(false));
×
245
            }
246
            if (Utils.isNanopubOfClass(np, GrlcQuery.GRLC_QUERY_CLASS)) {
×
247
                add(new WebMarkupContainer("run-query").add(new BookmarkablePageLink<Void>("query-link", QueryPage.class, new PageParameters().set("id", np.getUri()))));
×
248
            } else {
249
                add(new WebMarkupContainer("run-query").setVisible(false));
×
250
            }
251
        }
252

253
        final String ref = tempRef;
×
254
        final String shortName;
255
        if (publishedNanopub != null) {
×
256
            shortName = NanopubUtils.getLabel(np);
×
257
        } else if (parameters.get("label").isEmpty()) {
×
258
            shortName = Utils.getShortNameFromURI(ref);
×
259
        } else {
260
            shortName = parameters.get("label").toString();
×
261
        }
262
        add(new Label("pagetitle", shortName + " (explore) | nanodash"));
×
263
        add(new Label("termname", shortName));
×
264

265
        //add(new ExternalLink("urilink", ref, ref));
266
        add(new ExternalLinkWithActionsPanel("urilink", Model.of(ref)));
×
267

268
        if (publishedNanopub != null) {
×
269
            add(new Label("statusline", "<h4>Status</h4><p>Successfully published.</p>").setEscapeModelStrings(false));
×
270
        } else if (isNanopubId && SignatureUtils.seemsToHaveSignature(np)) {
×
271
            add(StatusLine.createComponent("statusline", ref));
×
272
        } else {
273
            add(new Label("statusline").setVisible(false));
×
274
        }
275
        if (publishedNanopub != null) {
×
276
            add(new Label("classes-panel").setVisible(false));
×
277
        } else {
278
            add(ThingListPanel.createComponent("classes-panel", ThingListPanel.Mode.CLASSES, ref, "<em>Searching for classes...</em>"));
×
279
        }
280
        if (isNanopubId) {
×
281
            add(new Label("definitions-panel").setVisible(false));
×
282
            add(new Label("instances-panel").setVisible(false));
×
283
            add(new Label("parts-panel").setVisible(false));
×
284
            add(new Label("templates-panel").setVisible(false));
×
285
        } else {
286
            add(ThingListPanel.createComponent("definitions-panel", ThingListPanel.Mode.DESCRIPTIONS, ref, "<em>Searching for term descriptions...</em>"));
×
287
            add(ThingListPanel.createComponent("instances-panel", ThingListPanel.Mode.INSTANCES, ref, "<em>Searching for instances...</em>"));
×
288
            add(ThingListPanel.createComponent("parts-panel", ThingListPanel.Mode.PARTS, ref, "<em>Searching for parts...</em>"));
×
289
            add(ThingListPanel.createComponent("templates-panel", ThingListPanel.Mode.TEMPLATES, ref, "<em>Searching for templates...</em>"));
×
290
        }
291
        add(ExploreDataTable.createComponent("reftable", ref));
×
292
    }
×
293

294
    @Override
295
    protected void onBeforeRender() {
296
        if (publishedNanopub != null && !getPageParameters().get("postpub-redirect-url").isNull()) {
×
297
            String forwardUrl = getPageParameters().get("postpub-redirect-url").toString();
×
298
            if (forwardUrl.contains("?")) {
×
299
                // TODO: Add here URI of created nanopublication too?
300
                throw new RedirectToUrlException(forwardUrl);
×
301
            } else {
302
                String paramString = Utils.getPageParametersAsString(new PageParameters().set("id", publishedNanopub.getUri()));
×
303
                throw new RedirectToUrlException(forwardUrl + "?" + paramString);
×
304
            }
305
        }
306
        super.onBeforeRender();
×
307
    }
×
308

309
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc