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

knowledgepixels / nanodash / 18933056119

30 Oct 2025 07:23AM UTC coverage: 13.749% (-0.6%) from 14.376%
18933056119

push

github

tkuhn
Merge branch 'master' of github.com:knowledgepixels/nanodash

492 of 4504 branches covered (10.92%)

Branch coverage included in aggregate %.

1282 of 8399 relevant lines covered (15.26%)

0.68 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
import java.util.Set;
6

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

31
import com.google.common.collect.ArrayListMultimap;
32
import com.google.common.collect.Multimap;
33
import com.knowledgepixels.nanodash.GrlcQuery;
34
import com.knowledgepixels.nanodash.MaintainedResource;
35
import com.knowledgepixels.nanodash.NanodashSession;
36
import com.knowledgepixels.nanodash.NanopubElement;
37
import com.knowledgepixels.nanodash.QueryApiAccess;
38
import com.knowledgepixels.nanodash.Space;
39
import com.knowledgepixels.nanodash.User;
40
import com.knowledgepixels.nanodash.Utils;
41
import com.knowledgepixels.nanodash.component.ExploreDataTable;
42
import com.knowledgepixels.nanodash.component.IriItem;
43
import com.knowledgepixels.nanodash.component.NanopubItem;
44
import com.knowledgepixels.nanodash.component.StatusLine;
45
import com.knowledgepixels.nanodash.component.ThingListPanel;
46
import com.knowledgepixels.nanodash.component.TitleBar;
47

48
import jakarta.servlet.http.HttpServletRequest;
49
import net.trustyuri.TrustyUriUtils;
50

51
/**
52
 * ExplorePage is a page that allows users to explore a specific Nanopublication or Thing.
53
 */
54
public class ExplorePage extends NanodashPage {
55

56
    private static final Logger logger = LoggerFactory.getLogger(ExplorePage.class);
×
57

58
    /**
59
     * The mount path for this page.
60
     */
61
    public static final String MOUNT_PATH = "/explore";
62

63
    /**
64
     * {@inheritDoc}
65
     */
66
    @Override
67
    public String getMountPath() {
68
        return MOUNT_PATH;
×
69
    }
70

71
    private Nanopub publishedNanopub = null;
×
72

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

84
    public ExplorePage(final Nanopub publishedNanopub, final PageParameters parameters) {
85
        super(parameters.add("id", publishedNanopub.getUri()));
×
86
        this.publishedNanopub = publishedNanopub;
×
87

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

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

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

121
        initPage();
×
122
    }
×
123

124
    private void initPage() {
125
        PageParameters parameters = getPageParameters();
×
126

127
        add(new TitleBar("titlebar", this, null));
×
128

129
        String tempRef = parameters.get("id").toString();
×
130

131
        String contextId = parameters.get("context").toString("");
×
132
        if (Space.get(contextId) != null) {
×
133
            add(new BookmarkablePageLink<Void>("back-to-context-link", SpacePage.class, new PageParameters().add("id", contextId)).setBody(Model.of("back to " + Space.get(contextId).getLabel())));
×
134
        } else if (MaintainedResource.get(contextId) != null) {
×
135
            add(new BookmarkablePageLink<Void>("back-to-context-link", MaintainedResourcePage.class, new PageParameters().add("id", contextId)).setBody(Model.of("back to " + MaintainedResource.get(contextId).getLabel())));
×
136
        } else {
137
            add(new Label("back-to-context-link").setVisible(false));
×
138
        }
139

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

148
        WebMarkupContainer raw = new WebMarkupContainer("raw");
×
149
        add(raw);
×
150

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

182
            if (parameters.get("forward-to-part").toString("").equals("true")) {
×
183
                parameters.remove("forward-to-part");
×
184
                Set<String> introducedIds = Utils.getIntroducedIriIds(np);
×
185
                if (introducedIds.size() == 1) {
×
186
                    String subj = introducedIds.iterator().next();
×
187
                    for (Statement st : np.getAssertion()) {
×
188
                        if (!st.getSubject().stringValue().equals(subj)) continue;
×
189
                        if (!st.getPredicate().equals(DCTERMS.IS_PART_OF) && !st.getPredicate().equals(SKOS.IN_SCHEME)) continue;
×
190
                        String resourceId = st.getObject().stringValue();
×
191
                        if (MaintainedResource.get(resourceId) == null) continue;
×
192
                        throw new RestartResponseException(ResourcePartPage.class, parameters);
×
193
                    }
194
                }
195
            }
196

197
            // Check whether we should redirect to Nanopub Registry for machine-friendly formats:
198
            String mimeType = Utils.TYPE_HTML;
×
199
            try {
200
                HttpServletRequest httpRequest = (HttpServletRequest) getRequest().getContainerRequest();
×
201
                mimeType = MIMEParse.bestMatch(Utils.SUPPORTED_TYPES_LIST, httpRequest.getHeader("Accept"));
×
202
            } catch (Exception ex) {
×
203
                logger.error("Error determining MIME type from Accept header.", ex);
×
204
            }
×
205
            if (!mimeType.equals(Utils.TYPE_HTML)) {
×
206
                logger.info("Non-HTML content type: {}", mimeType);
×
207
                // TODO Make this registry URL configurable/dynamic:
208
                String redirectUrl = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
209
                logger.info("Redirecting to: {}", redirectUrl);
×
210
                throw new RedirectToUrlException(redirectUrl, 302);
×
211
            }
212

213
            String nanopubHeaderLabel = "<h4>%s</h4>";
×
214
            if (isNanopubId) {
×
215
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "Nanopublication");
×
216
            } else {
217
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "Minted in Nanopublication");
×
218
            }
219
            add(new Label("nanopub-header", nanopubHeaderLabel).setEscapeModelStrings(false));
×
220
            add(new NanopubItem("nanopub", NanopubElement.get(np)));
×
221
            String url = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
222
            raw.add(new ExternalLink("trig-txt", url + ".trig.txt"));
×
223
            raw.add(new ExternalLink("jsonld-txt", url + ".jsonld.txt"));
×
224
            raw.add(new ExternalLink("nq-txt", url + ".nq.txt"));
×
225
            raw.add(new ExternalLink("xml-txt", url + ".xml.txt"));
×
226
            raw.add(new ExternalLink("trig", url + ".trig"));
×
227
            raw.add(new ExternalLink("jsonld", url + ".jsonld"));
×
228
            raw.add(new ExternalLink("nq", url + ".nq"));
×
229
            raw.add(new ExternalLink("xml", url + ".xml"));
×
230
            if (Utils.isNanopubOfClass(np, NTEMPLATE.ASSERTION_TEMPLATE)) {
×
231
                add(new WebMarkupContainer("use-template").add(new BookmarkablePageLink<Void>("template-link", PublishPage.class, new PageParameters().add("template", np.getUri()))));
×
232
            } else {
233
                add(new WebMarkupContainer("use-template").setVisible(false));
×
234
            }
235
            if (Utils.isNanopubOfClass(np, GrlcQuery.GRLC_QUERY_CLASS)) {
×
236
                add(new WebMarkupContainer("run-query").add(new BookmarkablePageLink<Void>("query-link", QueryPage.class, new PageParameters().add("id", np.getUri()))));
×
237
            } else {
238
                add(new WebMarkupContainer("run-query").setVisible(false));
×
239
            }
240
        }
241

242
        final String ref = tempRef;
×
243
        final String shortName;
244
        if (publishedNanopub != null) {
×
245
            shortName = NanopubUtils.getLabel(np);
×
246
        } else if (parameters.get("label").isEmpty()) {
×
247
            shortName = IriItem.getShortNameFromURI(ref);
×
248
        } else {
249
            shortName = parameters.get("label").toString();
×
250
        }
251
        add(new Label("pagetitle", shortName + " (explore) | nanodash"));
×
252
        add(new Label("termname", shortName));
×
253
        add(new ExternalLink("urilink", ref, ref));
×
254
        if (publishedNanopub != null) {
×
255
            add(new Label("statusline", "<h4>Status</h4><p>Successfully published.</p>").setEscapeModelStrings(false));
×
256
        } else if (isNanopubId && SignatureUtils.seemsToHaveSignature(np)) {
×
257
            add(StatusLine.createComponent("statusline", ref));
×
258
        } else {
259
            add(new Label("statusline").setVisible(false));
×
260
        }
261
        if (publishedNanopub != null) {
×
262
            add(new Label("classes-panel").setVisible(false));
×
263
        } else {
264
            add(ThingListPanel.createComponent("classes-panel", ThingListPanel.Mode.CLASSES, ref, "<em>Searching for classes...</em>"));
×
265
        }
266
        if (isNanopubId) {
×
267
            add(new Label("definitions-panel").setVisible(false));
×
268
            add(new Label("instances-panel").setVisible(false));
×
269
            add(new Label("parts-panel").setVisible(false));
×
270
            add(new Label("templates-panel").setVisible(false));
×
271
        } else {
272
            add(ThingListPanel.createComponent("definitions-panel", ThingListPanel.Mode.DESCRIPTIONS, ref, "<em>Searching for term descriptions...</em>"));
×
273
            add(ThingListPanel.createComponent("instances-panel", ThingListPanel.Mode.INSTANCES, ref, "<em>Searching for instances...</em>"));
×
274
            add(ThingListPanel.createComponent("parts-panel", ThingListPanel.Mode.PARTS, ref, "<em>Searching for parts...</em>"));
×
275
            add(ThingListPanel.createComponent("templates-panel", ThingListPanel.Mode.TEMPLATES, ref, "<em>Searching for templates...</em>"));
×
276
        }
277
        add(ExploreDataTable.createComponent("reftable", ref));
×
278
    }
×
279

280
    @Override
281
    protected void onBeforeRender() {
282
        if (publishedNanopub != null && !getPageParameters().get("postpub-redirect-url").isNull()) {
×
283
            String forwardUrl = getPageParameters().get("postpub-redirect-url").toString();
×
284
            if (forwardUrl.contains("?")) {
×
285
                // TODO: Add here URI of created nanopublication too?
286
                throw new RedirectToUrlException(forwardUrl);
×
287
            } else {
288
                String paramString = Utils.getPageParametersAsString(new PageParameters().add("id", publishedNanopub.getUri()));
×
289
                throw new RedirectToUrlException(forwardUrl + "?" + paramString);
×
290
            }
291
        }
292
        super.onBeforeRender();
×
293
    }
×
294

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