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

knowledgepixels / nanodash / 17302137193

28 Aug 2025 04:35PM UTC coverage: 11.965% (-0.4%) from 12.355%
17302137193

Pull #244

github

web-flow
Merge 4e969b0ee into 3323a35f1
Pull Request #244: Use vocabularies with latest version of `nanopub-java`

331 of 3840 branches covered (8.62%)

Branch coverage included in aggregate %.

943 of 6808 relevant lines covered (13.85%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

39.87
src/main/java/com/knowledgepixels/nanodash/component/NanodashLink.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.User;
4
import com.knowledgepixels.nanodash.Utils;
5
import com.knowledgepixels.nanodash.connector.ios.DsConfig;
6
import com.knowledgepixels.nanodash.connector.ios.DsNanopubPage;
7
import com.knowledgepixels.nanodash.connector.pensoft.BdjConfig;
8
import com.knowledgepixels.nanodash.connector.pensoft.BdjNanopubPage;
9
import com.knowledgepixels.nanodash.connector.pensoft.RioConfig;
10
import com.knowledgepixels.nanodash.connector.pensoft.RioNanopubPage;
11
import com.knowledgepixels.nanodash.page.ExplorePage;
12
import com.knowledgepixels.nanodash.template.Template;
13
import com.knowledgepixels.nanodash.template.TemplateData;
14
import net.trustyuri.TrustyUriUtils;
15
import org.apache.commons.codec.Charsets;
16
import org.apache.wicket.Component;
17
import org.apache.wicket.behavior.AttributeAppender;
18
import org.apache.wicket.markup.html.basic.Label;
19
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
20
import org.apache.wicket.markup.html.link.ExternalLink;
21
import org.apache.wicket.markup.html.panel.Panel;
22
import org.apache.wicket.model.Model;
23
import org.apache.wicket.request.mapper.parameter.PageParameters;
24
import org.eclipse.rdf4j.model.IRI;
25
import org.eclipse.rdf4j.model.Statement;
26
import org.eclipse.rdf4j.model.ValueFactory;
27
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
28
import org.eclipse.rdf4j.model.vocabulary.RDFS;
29
import org.nanopub.Nanopub;
30
import org.nanopub.vocabulary.NP;
31
import org.nanopub.vocabulary.NTEMPLATE;
32

33
import java.net.URLEncoder;
34
import java.util.*;
35

36
/**
37
 * A Wicket component that creates a link to a nanopublication or an IRI.
38
 */
39
public class NanodashLink extends Panel {
40

41
    private static final long serialVersionUID = 1L;
42

43
    /**
44
     * Creates a link to a nanopublication or an IRI.
45
     *
46
     * @param id             the Wicket component ID
47
     * @param uri            the URI of the nanopublication or IRI
48
     * @param np             the nanopublication, or null if the link is not to a nanopublication
49
     * @param templateClass  the template class of the nanopublication, or null if the link is not to a nanopublication
50
     * @param objectPosition if true, the link is to an object position in a template, otherwise it is to a subject position
51
     * @param label          the label to display for the link, or null to derive it from the nanopublication or IRI
52
     */
53
    public NanodashLink(String id, String uri, Nanopub np, IRI templateClass, boolean objectPosition, String label) {
54
        super(id);
3✔
55

56
        final List<Template> templates = new ArrayList<>();
4✔
57
        final Map<IRI, String> labels = new HashMap<>();
4✔
58
        if (np != null) {
2!
59
            for (Statement st : np.getPubinfo()) {
11✔
60
                if (st.getPredicate().equals(NTEMPLATE.HAS_LABEL_FROM_API) || st.getPredicate().equals(RDFS.LABEL)) {
10!
61
                    labels.put((IRI) st.getSubject(), st.getObject().stringValue());
×
62
                }
63
            }
1✔
64
        }
65

66
        final TemplateData td = TemplateData.get();
2✔
67

68
        if (NTEMPLATE.ASSERTION_TEMPLATE.equals(templateClass)) {
4!
69
            IRI templateId = td.getTemplateId(np);
×
70
            if (templateId != null) {
×
71
                templates.add(td.getTemplate(templateId.stringValue()));
×
72
            }
73
        } else if (NTEMPLATE.PROVENANCE_TEMPLATE.equals(templateClass)) {
4!
74
            IRI templateId = td.getProvenanceTemplateId(np);
×
75
            if (templateId != null) {
×
76
                templates.add(td.getTemplate(templateId.stringValue()));
×
77
            }
78
        } else if (NTEMPLATE.PUBINFO_TEMPLATE.equals(templateClass)) {
4!
79
            Set<IRI> templateIds = td.getPubinfoTemplateIds(np);
×
80
            for (IRI templateId : templateIds) {
×
81
                templates.add(td.getTemplate(templateId.stringValue()));
×
82
            }
×
83
        }
84

85
        final IRI iriObj = vf.createIRI(uri);
4✔
86
        if (np != null && uri.equals(np.getUri().stringValue())) {
8!
87
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "this");
×
88
            add(link);
×
89
            add(new Label("description", "this specific nanopublication"));
×
90
        } else if (np != null && uri.equals(np.getAssertionUri().stringValue())) {
8!
91
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "this assertion");
×
92
            link.add(new AttributeAppender("class", " nanopub-assertion "));
×
93
            add(link);
×
94
            add(new Label("description", "the assertion of this specific nanopublication"));
×
95
        } else if (uri.equals(NP.HAS_ASSERTION.stringValue())) {
5!
96
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "assertion");
×
97
            link.add(new AttributeAppender("class", " nanopub-assertion "));
×
98
            add(link);
×
99
            add(new Label("description", "links a nanopublication to its assertion"));
×
100
        } else if (uri.equals(NP.HAS_PROVENANCE.stringValue())) {
5!
101
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "provenance");
×
102
            link.add(new AttributeAppender("class", " nanopub-provenance "));
×
103
            add(link);
×
104
            add(new Label("description", "links a nanopublication to its provenance"));
×
105
        } else if (uri.equals(NP.HAS_PUBINFO.stringValue())) {
5!
106
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "pubinfo");
×
107
            link.add(new AttributeAppender("class", " nanopub-pubinfo "));
×
108
            add(link);
×
109
            add(new Label("description", "links a nanopublication to its pubinfo"));
×
110
        } else {
×
111
            if (label == null || label.isBlank()) {
2!
112
                label = IriItem.getShortNameFromURI(uri);
3✔
113
                if (iriObj.equals(User.getSignatureOwnerIri(np))) {
5!
114
                    if (objectPosition) {
×
115
                        label = "me (" + User.getShortDisplayName(iriObj) + ")";
×
116
                    } else {
117
                        label = "I (" + User.getShortDisplayName(iriObj) + ")";
×
118
                    }
119
                } else if (User.getName(iriObj) != null) {
3!
120
                    label = User.getShortDisplayName(iriObj);
×
121
                } else {
122
                    for (Template template : templates) {
6!
123
                        // TODO For pubinfo templates, we don't consider which triple came from which template (which is non-trivial):
124
                        String l = template.getLabel(iriObj);
×
125
                        if (l != null) {
×
126
                            label = l;
×
127
                            break;
×
128
                        }
129
                        l = labels.get(iriObj);
×
130
                        if (l != null) {
×
131
                            label = l;
×
132
                            break;
×
133
                        }
134
                    }
×
135
                }
136
            }
137
            String shortLabel = label.replaceFirst(" - [\\s\\S]*$", "");
5✔
138
            add(createLink("link", uri, shortLabel));
12✔
139
            String description = "";
2✔
140
            if (np != null && uri.startsWith(np.getUri().stringValue())) {
8!
141
                description = "This is a local identifier that was minted when the nanopublication was created.";
×
142
            }
143
            if (label.contains(" - ")) description = label.replaceFirst("^.* - ", "");
4!
144
            add(new Label("description", description));
13✔
145
        }
146
        add(Utils.getUriLink("uri", uri));
11✔
147
    }
1✔
148

149
    /**
150
     * <p>createLink.</p>
151
     *
152
     * @param markupId a {@link java.lang.String} object
153
     * @param uri      a {@link java.lang.String} object
154
     * @param label    a {@link java.lang.String} object
155
     * @return a {@link org.apache.wicket.Component} object
156
     */
157
    public static Component createLink(String markupId, String uri, String label) {
158
        boolean isNp = TrustyUriUtils.isPotentialTrustyUri(uri);
3✔
159
        // TODO Improve this
160
        if (isNp && uri.startsWith(DsConfig.get().getTargetNamespace())) {
2!
161
            return new BookmarkablePageLink<Void>(markupId, DsNanopubPage.class, new PageParameters().add("id", uri).add("mode", "final")).setBody(Model.of(label));
×
162
        } else if (isNp && uri.startsWith(BdjConfig.get().getTargetNamespace())) {
2!
163
            return new BookmarkablePageLink<Void>(markupId, BdjNanopubPage.class, new PageParameters().add("id", uri).add("mode", "final")).setBody(Model.of(label));
×
164
        } else if (isNp && uri.startsWith(RioConfig.get().getTargetNamespace())) {
2!
165
            return new BookmarkablePageLink<Void>(markupId, RioNanopubPage.class, new PageParameters().add("id", uri).add("mode", "final")).setBody(Model.of(label));
×
166
        } else {
167
            return new BookmarkablePageLink<Void>(markupId, ExplorePage.class, new PageParameters().add("id", uri).add("label", label)).setBody(Model.of(label));
18✔
168
        }
169
    }
170

171
    /**
172
     * Creates a link to a nanopublication or an IRI.
173
     *
174
     * @param id             the Wicket component ID
175
     * @param uri            the URI of the nanopublication or IRI
176
     * @param np             the nanopublication, or null if the link is not to a nanopublication
177
     * @param templateClass  the template class of the nanopublication, or null if the link is not to a nanopublication
178
     * @param objectPosition if true, the link is to an object position in a template, otherwise it is to a subject position
179
     */
180
    public NanodashLink(String id, String uri, Nanopub np, IRI templateClass, boolean objectPosition) {
181
        this(id, uri, np, templateClass, objectPosition, null);
8✔
182
    }
1✔
183

184
    /**
185
     * Creates a link to a nanopublication or an IRI.
186
     *
187
     * @param id  the Wicket component ID
188
     * @param uri the URI of the nanopublication or IRI
189
     * @param np  the nanopublication, or null if the link is not to a nanopublication
190
     */
191
    public NanodashLink(String id, String uri, Nanopub np) {
192
        this(id, uri, np, null, false);
×
193
    }
×
194

195
    /**
196
     * Creates a link to a nanopublication or an IRI.
197
     *
198
     * @param id  the Wicket component ID
199
     * @param uri the URI of the nanopublication or IRI
200
     */
201
    public NanodashLink(String id, String uri) {
202
        this(id, uri, null, null, false);
×
203
    }
×
204

205
    private static ValueFactory vf = SimpleValueFactory.getInstance();
3✔
206

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