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

knowledgepixels / nanodash / 18532870446

15 Oct 2025 02:46PM UTC coverage: 13.621% (+0.006%) from 13.615%
18532870446

push

github

tkuhn
fix: Show "use this template" button only once upon template publication

452 of 4190 branches covered (10.79%)

Branch coverage included in aggregate %.

1171 of 7725 relevant lines covered (15.16%)

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

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.request.flow.RedirectToUrlException;
12
import org.apache.wicket.request.mapper.parameter.PageParameters;
13
import org.apache.wicket.request.mapper.parameter.INamedParameters.NamedPair;
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().equals("template")) continue;
×
103
            if (n.getKey().equals("template-version")) continue;
×
104
            publishAnotherFilledLinkVisible = true;
×
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
        add(new TitleBar("titlebar", this, null));
×
120

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

123
        WebMarkupContainer raw = new WebMarkupContainer("raw");
×
124
        add(raw);
×
125

126
        if (User.getUserData().isUser(tempRef)) {
×
127
            throw new RestartResponseException(UserPage.class, parameters);
×
128
        } else if (Space.get(tempRef) != null) {
×
129
            throw new RestartResponseException(SpacePage.class, parameters);
×
130
        }
131

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

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

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

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

244
    @Override
245
    protected void onBeforeRender() {
246
        if (publishedNanopub != null && !getPageParameters().get("postpub-redirect-url").isNull()) {
×
247
            String forwardUrl = getPageParameters().get("postpub-redirect-url").toString();
×
248
            if (forwardUrl.contains("?")) {
×
249
                // TODO: Add here URI of created nanopublication too?
250
                throw new RedirectToUrlException(forwardUrl);
×
251
            } else {
252
                String paramString = Utils.getPageParametersAsString(new PageParameters().add("id", publishedNanopub.getUri()));
×
253
                throw new RedirectToUrlException(forwardUrl + "?" + paramString);
×
254
            }
255
        }
256
        super.onBeforeRender();
×
257
    }
×
258

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