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

knowledgepixels / nanodash / 18594773645

17 Oct 2025 01:50PM UTC coverage: 14.51% (+0.9%) from 13.57%
18594773645

push

github

tkuhn
feat(ExplorePage): "context" param for "back to space" button

482 of 4204 branches covered (11.47%)

Branch coverage included in aggregate %.

1250 of 7733 relevant lines covered (16.16%)

0.73 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 java.util.HashMap;
4
import java.util.Map;
5

6
import org.apache.wicket.RestartResponseException;
7
import org.apache.wicket.markup.html.WebMarkupContainer;
8
import org.apache.wicket.markup.html.basic.Label;
9
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
10
import org.apache.wicket.markup.html.link.ExternalLink;
11
import org.apache.wicket.model.Model;
12
import org.apache.wicket.request.flow.RedirectToUrlException;
13
import org.apache.wicket.request.mapper.parameter.PageParameters;
14
import org.apache.wicket.request.mapper.parameter.INamedParameters.NamedPair;
15
import org.commonjava.mimeparse.MIMEParse;
16
import org.nanopub.Nanopub;
17
import org.nanopub.NanopubUtils;
18
import org.nanopub.extra.security.SignatureUtils;
19
import org.nanopub.extra.services.ApiResponse;
20
import org.nanopub.extra.services.QueryRef;
21
import org.nanopub.vocabulary.NPX;
22
import org.nanopub.vocabulary.NTEMPLATE;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

26
import com.google.common.collect.ArrayListMultimap;
27
import com.google.common.collect.Multimap;
28
import com.knowledgepixels.nanodash.GrlcQuery;
29
import com.knowledgepixels.nanodash.NanodashSession;
30
import com.knowledgepixels.nanodash.NanopubElement;
31
import com.knowledgepixels.nanodash.QueryApiAccess;
32
import com.knowledgepixels.nanodash.Space;
33
import com.knowledgepixels.nanodash.User;
34
import com.knowledgepixels.nanodash.Utils;
35
import com.knowledgepixels.nanodash.component.ExploreDataTable;
36
import com.knowledgepixels.nanodash.component.IriItem;
37
import com.knowledgepixels.nanodash.component.NanopubItem;
38
import com.knowledgepixels.nanodash.component.StatusLine;
39
import com.knowledgepixels.nanodash.component.ThingListPanel;
40
import com.knowledgepixels.nanodash.component.TitleBar;
41

42
import jakarta.servlet.http.HttpServletRequest;
43
import net.trustyuri.TrustyUriUtils;
44

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

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

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

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

65
    private Nanopub publishedNanopub = null;
×
66

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

78
    public ExplorePage(final Nanopub publishedNanopub, final PageParameters parameters) {
79
        super(parameters.add("id", publishedNanopub.getUri()));
×
80
        this.publishedNanopub = publishedNanopub;
×
81

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

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

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

115
        initPage();
×
116
    }
×
117

118
    private void initPage() {
119
        PageParameters parameters = getPageParameters();
×
120

121
        add(new TitleBar("titlebar", this, null));
×
122

123
        String tempRef = parameters.get("id").toString();
×
124

125
        Space contextSpace = Space.get(parameters.get("context").toString(""));
×
126
        if (contextSpace != null) {
×
127
            add(new WebMarkupContainer("back-to-context").add(new BookmarkablePageLink<Void>("back-to-context-link", SpacePage.class, new PageParameters().add("id", contextSpace.getId())).setBody(Model.of("back to " + contextSpace.getLabel()))));
×
128
        } else {
129
            add(new WebMarkupContainer("back-to-context").setVisible(false));
×
130
        }
131

132
        WebMarkupContainer raw = new WebMarkupContainer("raw");
×
133
        add(raw);
×
134

135
        if (User.getUserData().isUser(tempRef)) {
×
136
            throw new RestartResponseException(UserPage.class, parameters);
×
137
        } else if (Space.get(tempRef) != null) {
×
138
            throw new RestartResponseException(SpacePage.class, parameters);
×
139
        }
140

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

172
            // Check whether we should redirect to Nanopub Registry for machine-friendly formats:
173
            String mimeType = Utils.TYPE_HTML;
×
174
            try {
175
                HttpServletRequest httpRequest = (HttpServletRequest) getRequest().getContainerRequest();
×
176
                mimeType = MIMEParse.bestMatch(Utils.SUPPORTED_TYPES_LIST, httpRequest.getHeader("Accept"));
×
177
            } catch (Exception ex) {
×
178
                logger.error("Error determining MIME type from Accept header.", ex);
×
179
            }
×
180
            if (!mimeType.equals(Utils.TYPE_HTML)) {
×
181
                logger.info("Non-HTML content type: {}", mimeType);
×
182
                // TODO Make this registry URL configurable/dynamic:
183
                String redirectUrl = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
184
                logger.info("Redirecting to: {}", redirectUrl);
×
185
                throw new RedirectToUrlException(redirectUrl, 302);
×
186
            }
187

188
            String nanopubHeaderLabel = "<h4>%s</h4>";
×
189
            if (isNanopubId) {
×
190
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "Nanopublication");
×
191
            } else {
192
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "Minted in Nanopublication");
×
193
            }
194
            add(new Label("nanopub-header", nanopubHeaderLabel).setEscapeModelStrings(false));
×
195
            add(new NanopubItem("nanopub", NanopubElement.get(np)));
×
196
            String url = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
197
            raw.add(new ExternalLink("trig-txt", url + ".trig.txt"));
×
198
            raw.add(new ExternalLink("jsonld-txt", url + ".jsonld.txt"));
×
199
            raw.add(new ExternalLink("nq-txt", url + ".nq.txt"));
×
200
            raw.add(new ExternalLink("xml-txt", url + ".xml.txt"));
×
201
            raw.add(new ExternalLink("trig", url + ".trig"));
×
202
            raw.add(new ExternalLink("jsonld", url + ".jsonld"));
×
203
            raw.add(new ExternalLink("nq", url + ".nq"));
×
204
            raw.add(new ExternalLink("xml", url + ".xml"));
×
205
            if (Utils.isNanopubOfClass(np, NTEMPLATE.ASSERTION_TEMPLATE)) {
×
206
                add(new WebMarkupContainer("use-template").add(new BookmarkablePageLink<Void>("template-link", PublishPage.class, new PageParameters().add("template", np.getUri()))));
×
207
            } else {
208
                add(new WebMarkupContainer("use-template").setVisible(false));
×
209
            }
210
            if (Utils.isNanopubOfClass(np, GrlcQuery.GRLC_QUERY_CLASS)) {
×
211
                add(new WebMarkupContainer("run-query").add(new BookmarkablePageLink<Void>("query-link", QueryPage.class, new PageParameters().add("id", np.getUri()))));
×
212
            } else {
213
                add(new WebMarkupContainer("run-query").setVisible(false));
×
214
            }
215
        }
216

217
        final String ref = tempRef;
×
218
        final String shortName;
219
        if (publishedNanopub != null) {
×
220
            shortName = NanopubUtils.getLabel(np);
×
221
        } else if (parameters.get("label").isEmpty()) {
×
222
            shortName = IriItem.getShortNameFromURI(ref);
×
223
        } else {
224
            shortName = parameters.get("label").toString();
×
225
        }
226
        add(new Label("pagetitle", shortName + " (explore) | nanodash"));
×
227
        add(new Label("termname", shortName));
×
228
        add(new ExternalLink("urilink", ref, ref));
×
229
        if (publishedNanopub != null) {
×
230
            add(new Label("statusline", "<h4>Status</h4><p>Successfully published.</p>").setEscapeModelStrings(false));
×
231
        } else if (isNanopubId && SignatureUtils.seemsToHaveSignature(np)) {
×
232
            add(StatusLine.createComponent("statusline", ref));
×
233
        } else {
234
            add(new Label("statusline").setVisible(false));
×
235
        }
236
        if (publishedNanopub != null) {
×
237
            add(new Label("classes-panel").setVisible(false));
×
238
        } else {
239
            add(ThingListPanel.createComponent("classes-panel", ThingListPanel.Mode.CLASSES, ref, "<em>Searching for classes...</em>"));
×
240
        }
241
        if (isNanopubId) {
×
242
            add(new Label("instances-panel").setVisible(false));
×
243
            add(new Label("parts-panel").setVisible(false));
×
244
            add(new Label("templates-panel").setVisible(false));
×
245
        } else {
246
            add(ThingListPanel.createComponent("instances-panel", ThingListPanel.Mode.INSTANCES, ref, "<em>Searching for instances...</em>"));
×
247
            add(ThingListPanel.createComponent("parts-panel", ThingListPanel.Mode.PARTS, ref, "<em>Searching for parts...</em>"));
×
248
            add(ThingListPanel.createComponent("templates-panel", ThingListPanel.Mode.TEMPLATES, ref, "<em>Searching for templates...</em>"));
×
249
        }
250
        add(ExploreDataTable.createComponent("reftable", ref));
×
251
    }
×
252

253
    @Override
254
    protected void onBeforeRender() {
255
        if (publishedNanopub != null && !getPageParameters().get("postpub-redirect-url").isNull()) {
×
256
            String forwardUrl = getPageParameters().get("postpub-redirect-url").toString();
×
257
            if (forwardUrl.contains("?")) {
×
258
                // TODO: Add here URI of created nanopublication too?
259
                throw new RedirectToUrlException(forwardUrl);
×
260
            } else {
261
                String paramString = Utils.getPageParametersAsString(new PageParameters().add("id", publishedNanopub.getUri()));
×
262
                throw new RedirectToUrlException(forwardUrl + "?" + paramString);
×
263
            }
264
        }
265
        super.onBeforeRender();
×
266
    }
×
267

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