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

knowledgepixels / nanodash / 18834771957

27 Oct 2025 08:37AM UTC coverage: 13.674% (+0.01%) from 13.662%
18834771957

push

github

tkuhn
feat(MaintainedResources): Add resource part pages

481 of 4418 branches covered (10.89%)

Branch coverage included in aggregate %.

1255 of 8278 relevant lines covered (15.16%)

0.68 hits per line

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

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

35
import java.net.URLEncoder;
36
import java.util.*;
37

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

43
    public NanodashLink(String id, String uri, Nanopub np, IRI templateClass, String label) {
44
        this(id, uri, np, templateClass, label, null);
8✔
45
    }
1✔
46

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

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

69
        final TemplateData td = TemplateData.get();
2✔
70

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

88
        final IRI iriObj = vf.createIRI(uri);
4✔
89
        if (np != null && uri.equals(np.getUri().stringValue())) {
8!
90
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "this");
×
91
            add(link);
×
92
            add(new Label("description", "this specific nanopublication"));
×
93
        } else if (np != null && uri.equals(np.getAssertionUri().stringValue())) {
8!
94
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "this assertion");
×
95
            link.add(new AttributeAppender("class", " nanopub-assertion "));
×
96
            add(link);
×
97
            add(new Label("description", "the assertion of this specific nanopublication"));
×
98
        } else if (uri.equals(NP.HAS_ASSERTION.stringValue())) {
5!
99
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "assertion");
×
100
            link.add(new AttributeAppender("class", " nanopub-assertion "));
×
101
            add(link);
×
102
            add(new Label("description", "links a nanopublication to its assertion"));
×
103
        } else if (uri.equals(NP.HAS_PROVENANCE.stringValue())) {
5!
104
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "provenance");
×
105
            link.add(new AttributeAppender("class", " nanopub-provenance "));
×
106
            add(link);
×
107
            add(new Label("description", "links a nanopublication to its provenance"));
×
108
        } else if (uri.equals(NP.HAS_PUBINFO.stringValue())) {
5!
109
            ExternalLink link = new ExternalLink("link", ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(uri, Charsets.UTF_8), "pubinfo");
×
110
            link.add(new AttributeAppender("class", " nanopub-pubinfo "));
×
111
            add(link);
×
112
            add(new Label("description", "links a nanopublication to its pubinfo"));
×
113
        } else {
×
114
            if (label == null || label.isBlank()) {
2!
115
                label = IriItem.getShortNameFromURI(uri);
3✔
116
                if (iriObj.equals(User.getSignatureOwnerIri(np))) {
5!
117
                    // TODO We might want to introduce a "(you)" flag here at some point
118
                    label = User.getShortDisplayName(iriObj);
×
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, contextId));
13✔
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, String contextId) {
158
        boolean isNp = TrustyUriUtils.isPotentialTrustyUri(uri);
3✔
159
        PageParameters params = new PageParameters().add("id", uri);
7✔
160
        if (contextId != null) params.add("context", contextId);
2!
161
        // TODO Improve this
162
        if (isNp && uri.startsWith(DsConfig.get().getTargetNamespace())) {
2!
163
            return new BookmarkablePageLink<Void>(markupId, DsNanopubPage.class, params.add("mode", "final")).setBody(Model.of(label));
×
164
        } else if (isNp && uri.startsWith(BdjConfig.get().getTargetNamespace())) {
2!
165
            return new BookmarkablePageLink<Void>(markupId, BdjNanopubPage.class, params.add("mode", "final")).setBody(Model.of(label));
×
166
        } else if (isNp && uri.startsWith(RioConfig.get().getTargetNamespace())) {
2!
167
            return new BookmarkablePageLink<Void>(markupId, RioNanopubPage.class, params.add("mode", "final")).setBody(Model.of(label));
×
168
        } else if (isPartOfResource(uri, contextId)) {
4!
169
            return new BookmarkablePageLink<Void>(markupId, ResourcePartPage.class, params.add("label", label)).setBody(Model.of(label));
×
170
        } else {
171
            return new BookmarkablePageLink<Void>(markupId, ExplorePage.class, params.add("label", label)).setBody(Model.of(label));
13✔
172
        }
173
    }
174

175
    private static boolean isPartOfResource(String uri, String contextId) {
176
        if (contextId == null) return false;
4!
177
        String uriNamespace = MaintainedResource.getNamespace(uri);
×
178
        MaintainedResource resource = MaintainedResource.getByNamespace(uriNamespace);
×
179
        if (resource == null) return false;
×
180
        return resource.getId().equals(contextId);
×
181
    }
182

183
    /**
184
     * Creates a link to a nanopublication or an IRI.
185
     *
186
     * @param id             the Wicket component ID
187
     * @param uri            the URI of the nanopublication or IRI
188
     * @param np             the nanopublication, or null if the link is not to a nanopublication
189
     * @param templateClass  the template class of the nanopublication, or null if the link is not to a nanopublication
190
     */
191
    public NanodashLink(String id, String uri, Nanopub np, IRI templateClass) {
192
        this(id, uri, np, templateClass, null);
7✔
193
    }
1✔
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
     */
202
    public NanodashLink(String id, String uri, Nanopub np) {
203
        this(id, uri, np, null);
×
204
    }
×
205

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

216
    private static ValueFactory vf = SimpleValueFactory.getInstance();
3✔
217

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