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

knowledgepixels / nanodash / 18679837432

21 Oct 2025 09:46AM UTC coverage: 13.113% (-0.04%) from 13.155%
18679837432

push

github

tkuhn
feat(ExplorePage): Replace user/space redirection with buttons

451 of 4316 branches covered (10.45%)

Branch coverage included in aggregate %.

1164 of 8000 relevant lines covered (14.55%)

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

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

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

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

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

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

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

64
    private Nanopub publishedNanopub = null;
×
65

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

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

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

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

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

114
        initPage();
×
115
    }
×
116

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

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

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

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

131
        if (User.getUserData().isUser(tempRef)) {
×
132
            add(new BookmarkablePageLink<Void>("to-specific-page-link", UserPage.class, parameters).setBody(Model.of("go to user page")));
×
133
        } else if (Space.get(tempRef) != null) {
×
134
            add(new BookmarkablePageLink<Void>("to-specific-page-link", SpacePage.class, parameters).setBody(Model.of("go to Space page")));
×
135
        } else {
136
            add(new Label("to-specific-page-link").setVisible(false));
×
137
        }
138

139
        WebMarkupContainer raw = new WebMarkupContainer("raw");
×
140
        add(raw);
×
141

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

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

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

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

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

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