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

knowledgepixels / nanodash / 22618039211

03 Mar 2026 10:05AM UTC coverage: 16.058% (+0.2%) from 15.884%
22618039211

Pull #365

github

web-flow
Merge 1e7e700f0 into a8c4b4a77
Pull Request #365: Refactor of `ResourceWithProfile` and related classes

699 of 5287 branches covered (13.22%)

Branch coverage included in aggregate %.

1721 of 9783 relevant lines covered (17.59%)

2.41 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.domain.MaintainedResource;
4
import com.knowledgepixels.nanodash.domain.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.MaintainedResourcePage;
14
import com.knowledgepixels.nanodash.page.ResourcePartPage;
15
import com.knowledgepixels.nanodash.page.SpacePage;
16
import com.knowledgepixels.nanodash.repository.MaintainedResourceRepository;
17
import com.knowledgepixels.nanodash.repository.SpaceRepository;
18
import com.knowledgepixels.nanodash.template.Template;
19
import com.knowledgepixels.nanodash.template.TemplateData;
20
import net.trustyuri.TrustyUriUtils;
21
import org.apache.commons.codec.Charsets;
22
import org.apache.wicket.Component;
23
import org.apache.wicket.behavior.AttributeAppender;
24
import org.apache.wicket.markup.html.basic.Label;
25
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
26
import org.apache.wicket.markup.html.link.ExternalLink;
27
import org.apache.wicket.markup.html.panel.Panel;
28
import org.apache.wicket.model.Model;
29
import org.apache.wicket.request.mapper.parameter.PageParameters;
30
import org.eclipse.rdf4j.model.IRI;
31
import org.eclipse.rdf4j.model.Statement;
32
import org.eclipse.rdf4j.model.ValueFactory;
33
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
34
import org.eclipse.rdf4j.model.vocabulary.RDFS;
35
import org.nanopub.Nanopub;
36
import org.nanopub.vocabulary.NP;
37
import org.nanopub.vocabulary.NTEMPLATE;
38

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

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

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

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

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

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

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

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

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

188
    private static boolean isPartOfResource(String uri, String contextId) {
189
        if (contextId == null) return false;
12!
190
        String uriNamespace = MaintainedResource.getNamespace(uri);
×
191
        MaintainedResource resource = MaintainedResourceRepository.get().findByNamespace(uriNamespace);
×
192
        if (resource == null) return false;
×
193
        return resource.getId().equals(contextId);
×
194
    }
195

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

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

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

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

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