• 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

41.53
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);
8✔
48
    }
1✔
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);
3✔
61

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

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

74
        if (NTEMPLATE.ASSERTION_TEMPLATE.equals(templateClass)) {
4!
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)) {
4!
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)) {
4!
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);
4✔
92
        if (np != null && uri.equals(np.getUri().stringValue())) {
8!
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())) {
8!
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())) {
5!
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())) {
5!
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())) {
5!
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()) {
2!
118
                label = IriItem.getShortNameFromURI(uri);
3✔
119
                if (iriObj.equals(User.getSignatureOwnerIri(np))) {
5!
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) {
3!
123
                    label = User.getShortDisplayName(iriObj);
×
124
                } else {
125
                    for (Template template : templates) {
6!
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]*$", "");
5✔
141
            add(createLink("link", uri, shortLabel, contextId));
13✔
142
            String description = "";
2✔
143
            if (np != null && uri.startsWith(np.getUri().stringValue())) {
8!
144
                description = "This is a local identifier that was minted when the nanopublication was created.";
×
145
            }
146
            if (label.contains(" - ")) description = label.replaceFirst("^.* - ", "");
4!
147
            add(new Label("description", description));
13✔
148
        }
149
        add(Utils.getUriLink("uri", uri));
11✔
150
    }
1✔
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);
3✔
162
        PageParameters params = new PageParameters().set("id", uri);
7✔
163
        if (contextId != null) params.set("context", contextId);
2!
164
        // TODO Improve this
165
        if (isNp && uri.startsWith(DsConfig.get().getTargetNamespace())) {
2!
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())) {
2!
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())) {
2!
170
            return new BookmarkablePageLink<Void>(markupId, RioNanopubPage.class, params.set("mode", "final")).setBody(Model.of(label));
×
171
        } else if (Space.get(uri) != null) {
3!
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) {
3!
175
            label = MaintainedResource.get(uri).getLabel();
×
176
            return new BookmarkablePageLink<Void>(markupId, MaintainedResourcePage.class, params).setBody(Model.of(label));
×
177
        } else if (isPartOfResource(uri, contextId)) {
4!
178
            return new BookmarkablePageLink<Void>(markupId, ResourcePartPage.class, params.set("label", label)).setBody(Model.of(label));
×
179
        } else {
180
            return new BookmarkablePageLink<Void>(markupId, ExplorePage.class, params.set("label", label).set("forward-to-part", "true")).setBody(Model.of(label));
16✔
181
        }
182
    }
183

184
    private static boolean isPartOfResource(String uri, String contextId) {
185
        if (contextId == null) return false;
4!
186
        String uriNamespace = MaintainedResource.getNamespace(uri);
×
187
        MaintainedResource resource = MaintainedResource.getByNamespace(uriNamespace);
×
188
        if (resource == null) return false;
×
189
        return resource.getId().equals(contextId);
×
190
    }
191

192
    /**
193
     * Creates a link to a nanopublication or an IRI.
194
     *
195
     * @param id             the Wicket component ID
196
     * @param uri            the URI of the nanopublication or IRI
197
     * @param np             the nanopublication, or null if the link is not to a nanopublication
198
     * @param templateClass  the template class of the nanopublication, or null if the link is not to a nanopublication
199
     */
200
    public NanodashLink(String id, String uri, Nanopub np, IRI templateClass) {
201
        this(id, uri, np, templateClass, null);
7✔
202
    }
1✔
203

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

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

225
    private static ValueFactory vf = SimpleValueFactory.getInstance();
3✔
226

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