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

knowledgepixels / nanodash / 20302275428

17 Dec 2025 12:08PM UTC coverage: 14.405% (+0.004%) from 14.401%
20302275428

push

github

tkuhn
fix(ResourcePartPage): Synchronous retrieval of definition

546 of 5002 branches covered (10.92%)

Branch coverage included in aggregate %.

1496 of 9174 relevant lines covered (16.31%)

2.13 hits per line

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

0.0
src/main/java/com/knowledgepixels/nanodash/page/ResourcePartPage.java
1
package com.knowledgepixels.nanodash.page;
2

3
import java.util.ArrayList;
4
import java.util.HashSet;
5
import java.util.List;
6
import java.util.Set;
7

8
import org.apache.wicket.Component;
9
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
10
import org.apache.wicket.markup.html.basic.Label;
11
import org.apache.wicket.markup.html.link.AbstractLink;
12
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
13
import org.apache.wicket.model.Model;
14
import org.apache.wicket.request.mapper.parameter.PageParameters;
15
import org.eclipse.rdf4j.model.IRI;
16
import org.eclipse.rdf4j.model.Statement;
17
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
18
import org.eclipse.rdf4j.model.vocabulary.RDF;
19
import org.eclipse.rdf4j.model.vocabulary.RDFS;
20
import org.eclipse.rdf4j.model.vocabulary.SKOS;
21
import org.nanopub.Nanopub;
22
import org.nanopub.extra.services.ApiResponse;
23
import org.nanopub.extra.services.QueryRef;
24

25
import com.knowledgepixels.nanodash.ApiCache;
26
import com.knowledgepixels.nanodash.MaintainedResource;
27
import com.knowledgepixels.nanodash.NanodashPageRef;
28
import com.knowledgepixels.nanodash.ProfiledResource;
29
import com.knowledgepixels.nanodash.Space;
30
import com.knowledgepixels.nanodash.User;
31
import com.knowledgepixels.nanodash.Utils;
32
import com.knowledgepixels.nanodash.component.ButtonList;
33
import com.knowledgepixels.nanodash.component.TitleBar;
34
import com.knowledgepixels.nanodash.component.ViewList;
35

36
/**
37
 * This class represents a page for a maintained resource.
38
 */
39
public class ResourcePartPage extends NanodashPage {
40

41
    /**
42
     * The mount path for this page.
43
     */
44
    public static final String MOUNT_PATH = "/part";
45

46
    /**
47
     * {@inheritDoc}
48
     */
49
    @Override
50
    public String getMountPath() {
51
        return MOUNT_PATH;
×
52
    }
53

54
    /**
55
     * Profiled resource (Space or MaintainedResource) object with the data shown on this page.
56
     */
57
    private ProfiledResource profiledResource;
58

59
    public ResourcePartPage(final PageParameters parameters) {
60
        super(parameters);
×
61

62
        final String id = parameters.get("id").toString();
×
63
        final String contextId = parameters.get("context").toString();
×
64
        final String nanopubId;
65
        String label = id.replaceFirst("^.*[#/]([^#/]+)$", "$1");
×
66
        String description = null;
×
67
        Set<IRI> classes = new HashSet<>();
×
68

69
        profiledResource = MaintainedResource.get(contextId);
×
70
        if (profiledResource == null) {
×
71
            if (Space.get(contextId) == null) {
×
72
                throw new IllegalArgumentException("Not a resource or space: " + contextId);
×
73
            }
74
            profiledResource = Space.get(contextId);
×
75
        }
76

77
        QueryRef getDefQuery = new QueryRef("get-term-definitions", "term", id);
×
78
        for (IRI userIri : profiledResource.getSpace().getUsers()) {
×
79
            for (String pubkey : User.getUserData().getPubkeyhashes(userIri, true)) {
×
80
                getDefQuery.getParams().put("pubkey", pubkey);
×
81
            }
×
82
        }
×
83

84
        ApiResponse getDefResp = ApiCache.retrieveResponseSync(getDefQuery, false);
×
85
        if (getDefResp != null && !getDefResp.getData().isEmpty()) {
×
86
            nanopubId = getDefResp.getData().iterator().next().get("np");
×
87

88
            Nanopub nanopub = Utils.getAsNanopub(nanopubId);
×
89
            for (Statement st : nanopub.getAssertion()) {
×
90
                if (!st.getSubject().stringValue().equals(id)) {
×
91
                    continue;
×
92
                }
93
                if (st.getPredicate().equals(RDFS.LABEL)) {
×
94
                    label = st.getObject().stringValue();
×
95
                }
96
                if (st.getPredicate().equals(SKOS.DEFINITION) || st.getPredicate().equals(DCTERMS.DESCRIPTION) || st.getPredicate().equals(RDFS.COMMENT)) {
×
97
                    description = st.getObject().stringValue();
×
98
                }
99
                if (st.getPredicate().equals(RDF.TYPE) && st.getObject() instanceof IRI objIri) {
×
100
                    classes.add(objIri);
×
101
                }
102
            }
×
103
        } else {
×
104
            nanopubId = null;
×
105
        }
106
//        if (getDefResp == null || getDefResp.getData().isEmpty()) {
107
//            throw new RestartResponseException(ExplorePage.class, parameters);
108
//        }
109

110
        if (description != null) {
×
111
            add(new Label("description", description));
×
112
        } else {
113
            add(new Label("description").setVisible(false));
×
114
        }
115

116
        List<ProfiledResource> superSpaces = profiledResource.getSpace().getAllSuperSpacesUntilRoot();
×
117
        if (profiledResource instanceof MaintainedResource) {
×
118
            superSpaces.add(profiledResource.getSpace());
×
119
        }
120
        superSpaces.add(profiledResource);
×
121
        List<NanodashPageRef> breadCrumb = new ArrayList<>(superSpaces.stream().map(ss -> new NanodashPageRef(SpacePage.class, new PageParameters().add("id", ss.getId()), ss.getLabel())).toList());
×
122
        breadCrumb.add(new NanodashPageRef(ResourcePartPage.class, new PageParameters().add("id", id).add("context", contextId).add("label", label), label));
×
123
        NanodashPageRef[] breadCrumbArray = breadCrumb.toArray(new NanodashPageRef[0]);
×
124
        add(new TitleBar("titlebar", this, null,
×
125
                breadCrumbArray
126
        ));
127

128
        add(new Label("pagetitle", label + " (resource part) | nanodash"));
×
129
        add(new Label("name", label));
×
130
        add(new BookmarkablePageLink<Void>("id", ExplorePage.class, parameters.set("label", label)).setBody(Model.of(id)));
×
131
        add(new BookmarkablePageLink<Void>("np", ExplorePage.class, new PageParameters().set("id", nanopubId == null ? id : nanopubId)));
×
132

133
        // TODO Improve this code, e.g. make Space a subclass of MaintainedResource or otherwise refactor:
134
        // we now use the ProfileResource abstraction, but the code still has to be imprved
135
        if (profiledResource != null) {
×
136
            final List<AbstractLink> viewButtons = new ArrayList<>();
×
137
            AbstractLink addViewButton = new BookmarkablePageLink<NanodashPage>("button", PublishPage.class, new PageParameters()
×
138
                    .set("template", "https://w3id.org/np/RAPxKWDTDP4neVtRckQcTqKHqCC_GHWWPrs7DESb2BJjo")
×
139
                    .set("template-version", "latest")
×
140
                    .set("param_resource", profiledResource.getId())
×
141
                    .set("context", profiledResource.getId())
×
142
            );
143
            addViewButton.setBody(Model.of("+ view"));
×
144
            viewButtons.add(addViewButton);
×
145

146
            final String nanopubRef = nanopubId == null ? "x:" : nanopubId;
×
147
            if (profiledResource.isDataInitialized()) {
×
148
                add(new ViewList("views", profiledResource, id, nanopubRef, classes));
×
149
                add(new ButtonList("view-buttons", profiledResource.getSpace(), null, null, viewButtons));
×
150
            } else {
151
                add(new AjaxLazyLoadPanel<Component>("views") {
×
152

153
                    @Override
154
                    public Component getLazyLoadComponent(String markupId) {
155
                        return new ViewList(markupId, profiledResource, id, nanopubRef, classes);
×
156
                    }
157

158
                    @Override
159
                    protected boolean isContentReady() {
160
                        return profiledResource.isDataInitialized();
×
161
                    }
162

163
                });
164
                add(new AjaxLazyLoadPanel<Component>("view-buttons") {
×
165

166
                    @Override
167
                    public Component getLazyLoadComponent(String markupId) {
168
                        return new ButtonList(markupId, profiledResource.getSpace(), null, null, viewButtons);
×
169
                    }
170

171
                    @Override
172
                    protected boolean isContentReady() {
173
                        return profiledResource.isDataInitialized();
×
174
                    }
175

176
                    public Component getLoadingComponent(String id) {
177
                        return new Label(id).setVisible(false);
×
178
                    }
179

180
                });
181
            }
182
        } else {
×
183
            // TODO Ugly code duplication (see above):
184

185
            final List<AbstractLink> viewButtons = new ArrayList<>();
×
186
            AbstractLink addViewButton = new BookmarkablePageLink<NanodashPage>("button", PublishPage.class, new PageParameters()
×
187
                    .set("template", "https://w3id.org/np/RAPxKWDTDP4neVtRckQcTqKHqCC_GHWWPrs7DESb2BJjo")
×
188
                    .set("template-version", "latest")
×
189
                    .set("param_resource", profiledResource.getSpace().getId())
×
190
                    .set("context", profiledResource.getSpace().getId())
×
191
            );
192
            addViewButton.setBody(Model.of("+ view"));
×
193
            viewButtons.add(addViewButton);
×
194

195
            if (profiledResource.getSpace().isDataInitialized()) {
×
196
                add(new ViewList("views", profiledResource.getSpace(), id, nanopubId, classes));
×
197
                add(new ButtonList("view-buttons", profiledResource.getSpace(), null, null, viewButtons));
×
198
            } else {
199
                add(new AjaxLazyLoadPanel<Component>("views") {
×
200

201
                    @Override
202
                    public Component getLazyLoadComponent(String markupId) {
203
                        return new ViewList(markupId, profiledResource.getSpace(), id, nanopubId, classes);
×
204
                    }
205

206
                    @Override
207
                    protected boolean isContentReady() {
208
                        return profiledResource.getSpace().isDataInitialized();
×
209
                    }
210

211
                });
212
                add(new AjaxLazyLoadPanel<Component>("view-buttons") {
×
213

214
                    @Override
215
                    public Component getLazyLoadComponent(String markupId) {
216
                        return new ButtonList(markupId, profiledResource.getSpace(), null, null, viewButtons);
×
217
                    }
218

219
                    @Override
220
                    protected boolean isContentReady() {
221
                        return profiledResource.getSpace().isDataInitialized();
×
222
                    }
223

224
                    public Component getLoadingComponent(String id) {
225
                        return new Label(id).setVisible(false);
×
226
                    }
227

228
                });
229
            }
230
        }
231
    }
×
232

233
    /**
234
     * Checks if auto-refresh is enabled for this page.
235
     *
236
     * @return true if auto-refresh is enabled, false otherwise
237
     */
238
    protected boolean hasAutoRefreshEnabled() {
239
        return true;
×
240
    }
241

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