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

knowledgepixels / nanodash / 20171903096

12 Dec 2025 03:42PM UTC coverage: 15.509% (-0.1%) from 15.633%
20171903096

push

github

tkuhn
feat(NanodashLink): Don't pass "^" label so it makes good nanopub links

604 of 4976 branches covered (12.14%)

Branch coverage included in aggregate %.

1578 of 9093 relevant lines covered (17.35%)

2.29 hits per line

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

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

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

38
import java.net.URLEncoder;
39
import java.util.*;
40

41
/**
42
 * A Wicket component that creates a link to a nanopublication or an IRI.
43
 */
44
public class NanodashLink extends Panel {
45

46
    public NanodashLink(String id, String uri, Nanopub np, IRI templateClass, String label) {
47
        this(id, uri, np, templateClass, label, null);
24✔
48
    }
3✔
49

50
    /**
51
     * Creates a link to a nanopublication or an IRI.
52
     *
53
     * @param id            the Wicket component ID
54
     * @param uri           the URI of the nanopublication or IRI
55
     * @param np            the nanopublication, or null if the link is not to a nanopublication
56
     * @param templateClass the template class of the nanopublication, or null if the link is not to a nanopublication
57
     * @param label         the label to display for the link, or null to derive it from the nanopublication or IRI
58
     */
59
    public NanodashLink(String id, String uri, Nanopub np, IRI templateClass, String label, String contextId) {
60
        super(id);
9✔
61

62
        final List<Template> templates = new ArrayList<>();
12✔
63
        final Map<IRI, String> labels = new HashMap<>();
12✔
64
        if (np != null) {
6!
65
            for (Statement st : np.getPubinfo()) {
33✔
66
                if (st.getPredicate().equals(NTEMPLATE.HAS_LABEL_FROM_API) || st.getPredicate().equals(RDFS.LABEL)) {
30!
67
                    labels.put((IRI) st.getSubject(), st.getObject().stringValue());
×
68
                }
69
            }
3✔
70
        }
71

72
        final TemplateData td = TemplateData.get();
6✔
73

74
        if (NTEMPLATE.ASSERTION_TEMPLATE.equals(templateClass)) {
12!
75
            IRI templateId = td.getTemplateId(np);
×
76
            if (templateId != null) {
×
77
                templates.add(td.getTemplate(templateId.stringValue()));
×
78
            }
79
        } else if (NTEMPLATE.PROVENANCE_TEMPLATE.equals(templateClass)) {
12!
80
            IRI templateId = td.getProvenanceTemplateId(np);
×
81
            if (templateId != null) {
×
82
                templates.add(td.getTemplate(templateId.stringValue()));
×
83
            }
84
        } else if (NTEMPLATE.PUBINFO_TEMPLATE.equals(templateClass)) {
12!
85
            Set<IRI> templateIds = td.getPubinfoTemplateIds(np);
×
86
            for (IRI templateId : templateIds) {
×
87
                templates.add(td.getTemplate(templateId.stringValue()));
×
88
            }
×
89
        }
90

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

152
    /**
153
     * <p>createLink.</p>
154
     *
155
     * @param markupId a {@link java.lang.String} object
156
     * @param uri      a {@link java.lang.String} object
157
     * @param label    a {@link java.lang.String} object
158
     * @return a {@link org.apache.wicket.Component} object
159
     */
160
    public static Component createLink(String markupId, String uri, String label, String contextId) {
161
        boolean isNp = TrustyUriUtils.isPotentialTrustyUri(uri);
9✔
162
        PageParameters params = new PageParameters().set("id", uri);
21✔
163
        if (contextId != null) params.set("context", contextId);
6!
164
        // TODO Improve this
165
        if (isNp && uri.startsWith(DsConfig.get().getTargetNamespace())) {
6!
166
            return new BookmarkablePageLink<Void>(markupId, DsNanopubPage.class, params.set("mode", "final")).setBody(Model.of(label));
×
167
        } else if (isNp && uri.startsWith(BdjConfig.get().getTargetNamespace())) {
6!
168
            return new BookmarkablePageLink<Void>(markupId, BdjNanopubPage.class, params.set("mode", "final")).setBody(Model.of(label));
×
169
        } else if (isNp && uri.startsWith(RioConfig.get().getTargetNamespace())) {
6!
170
            return new BookmarkablePageLink<Void>(markupId, RioNanopubPage.class, params.set("mode", "final")).setBody(Model.of(label));
×
171
        } else if (Space.get(uri) != null) {
9!
172
            label = Space.get(uri).getLabel();
×
173
            return new BookmarkablePageLink<Void>(markupId, SpacePage.class, params).setBody(Model.of(label));
×
174
        } else if (MaintainedResource.get(uri) != null) {
9!
175
            label = MaintainedResource.get(uri).getLabel();
×
176
            return new BookmarkablePageLink<Void>(markupId, MaintainedResourcePage.class, params).setBody(Model.of(label));
×
177
        } else {
178
            if (!"^".equals(label)) params.set("label", label);
27!
179
            if (isPartOfResource(uri, contextId)) {
12!
180
                return new BookmarkablePageLink<Void>(markupId, ResourcePartPage.class, params).setBody(Model.of(label));
×
181
            } else {
182
                return new BookmarkablePageLink<Void>(markupId, ExplorePage.class, params.set("forward-to-part", "true")).setBody(Model.of(label));
39✔
183
            }
184
        }
185
    }
186

187
    private static boolean isPartOfResource(String uri, String contextId) {
188
        if (contextId == null) return false;
12!
189
        String uriNamespace = MaintainedResource.getNamespace(uri);
×
190
        MaintainedResource resource = MaintainedResource.getByNamespace(uriNamespace);
×
191
        if (resource == null) return false;
×
192
        return resource.getId().equals(contextId);
×
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
     * @param np            the nanopublication, or null if the link is not to a nanopublication
201
     * @param templateClass the template class of the nanopublication, or null if the link is not to a nanopublication
202
     */
203
    public NanodashLink(String id, String uri, Nanopub np, IRI templateClass) {
204
        this(id, uri, np, templateClass, null);
21✔
205
    }
3✔
206

207
    /**
208
     * Creates a link to a nanopublication or an IRI.
209
     *
210
     * @param id  the Wicket component ID
211
     * @param uri the URI of the nanopublication or IRI
212
     * @param np  the nanopublication, or null if the link is not to a nanopublication
213
     */
214
    public NanodashLink(String id, String uri, Nanopub np) {
215
        this(id, uri, np, null);
×
216
    }
×
217

218
    /**
219
     * Creates a link to a nanopublication or an IRI.
220
     *
221
     * @param id  the Wicket component ID
222
     * @param uri the URI of the nanopublication or IRI
223
     */
224
    public NanodashLink(String id, String uri) {
225
        this(id, uri, null, null);
×
226
    }
×
227

228
    private static ValueFactory vf = SimpleValueFactory.getInstance();
9✔
229

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