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

knowledgepixels / nanodash / 18715370422

22 Oct 2025 11:58AM UTC coverage: 13.159% (-0.7%) from 13.858%
18715370422

push

github

tkuhn
feat(ResourceView): Implement configurable actions

454 of 4396 branches covered (10.33%)

Branch coverage included in aggregate %.

1206 of 8219 relevant lines covered (14.67%)

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.MaintainedResource;
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
        String contextId = parameters.get("context").toString("");
×
126
        if (Space.get(contextId) != null) {
×
127
            add(new BookmarkablePageLink<Void>("back-to-context-link", SpacePage.class, new PageParameters().add("id", contextId)).setBody(Model.of("back to " + Space.get(contextId).getLabel())));
×
128
        } else if (MaintainedResource.get(contextId) != null) {
×
129
            add(new BookmarkablePageLink<Void>("back-to-context-link", MaintainedResourcePage.class, new PageParameters().add("id", contextId)).setBody(Model.of("back to " + MaintainedResource.get(contextId).getLabel())));
×
130
        } else {
131
            add(new Label("back-to-context-link").setVisible(false));
×
132
        }
133

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

142
        WebMarkupContainer raw = new WebMarkupContainer("raw");
×
143
        add(raw);
×
144

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

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

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

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

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

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