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

knowledgepixels / nanodash / 18308478708

07 Oct 2025 09:37AM UTC coverage: 13.522% (-0.2%) from 13.709%
18308478708

push

github

tkuhn
feat(Spaces): Improve template button links on Space pages

446 of 4164 branches covered (10.71%)

Branch coverage included in aggregate %.

1153 of 7661 relevant lines covered (15.05%)

0.67 hits per line

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

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

3
import com.github.jsonldjava.shaded.com.google.common.collect.Ordering;
4
import com.google.common.collect.ArrayListMultimap;
5
import com.google.common.collect.Multimap;
6
import com.knowledgepixels.nanodash.template.Template;
7
import com.knowledgepixels.nanodash.template.TemplateData;
8
import jakarta.xml.bind.DatatypeConverter;
9
import org.eclipse.rdf4j.model.IRI;
10
import org.eclipse.rdf4j.model.Literal;
11
import org.eclipse.rdf4j.model.Statement;
12
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
13
import org.eclipse.rdf4j.model.vocabulary.OWL;
14
import org.nanopub.Nanopub;
15
import org.nanopub.extra.services.ApiResponse;
16
import org.nanopub.extra.services.ApiResponseEntry;
17
import org.nanopub.extra.services.QueryRef;
18
import org.nanopub.vocabulary.NTEMPLATE;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

22
import java.io.Serializable;
23
import java.time.format.DateTimeParseException;
24
import java.util.*;
25

26
import static com.knowledgepixels.nanodash.Utils.vf;
27

28
/**
29
 * Class representing a "Space", which can be any kind of collaborative unit, like a project, group, or event.
30
 */
31
public class Space implements Serializable {
32

33
    private static final Logger logger = LoggerFactory.getLogger(Space.class);
×
34

35
    /**
36
     * The predicate to assign the admins of the space.
37
     */
38
    public static final IRI HAS_ADMIN = vf.createIRI("https://w3id.org/kpxl/gen/terms/hasAdmin");
×
39

40
    /**
41
     * The predicate for pinned templates in the space.
42
     */
43
    public static final IRI HAS_PINNED_TEMPLATE = vf.createIRI("https://w3id.org/kpxl/gen/terms/hasPinnedTemplate");
×
44

45
    /**
46
     * The predicate for pinned queries in the space.
47
     */
48
    public static final IRI HAS_PINNED_QUERY = vf.createIRI("https://w3id.org/kpxl/gen/terms/hasPinnedQuery");
×
49

50
    private static List<Space> spaceList;
51
    private static Map<String, List<Space>> spaceListByType;
52
    private static Map<String, Space> spacesByCoreInfo = new HashMap<>();
×
53
    private static Map<String, Space> spacesById;
54
    private static Map<Space, Set<Space>> subspaceMap;
55
    private static Map<Space, Set<Space>> superspaceMap;
56
    private static boolean loaded = false;
×
57

58
    /**
59
     * Refresh the list of spaces from the API response.
60
     *
61
     * @param resp The API response containing space data.
62
     */
63
    public static synchronized void refresh(ApiResponse resp) {
64
        spaceList = new ArrayList<>();
×
65
        spaceListByType = new HashMap<>();
×
66
        Map<String, Space> prevSpacesByCoreInfoPrev = spacesByCoreInfo;
×
67
        spacesByCoreInfo = new HashMap<>();
×
68
        spacesById = new HashMap<>();
×
69
        subspaceMap = new HashMap<>();
×
70
        superspaceMap = new HashMap<>();
×
71
        for (ApiResponseEntry entry : resp.getData()) {
×
72
            Space space = new Space(entry);
×
73
            Space prevSpace = prevSpacesByCoreInfoPrev.get(space.getCoreInfoString());
×
74
            if (prevSpace != null) space = prevSpace;
×
75
            spaceList.add(space);
×
76
            spaceListByType.computeIfAbsent(space.getType(), k -> new ArrayList<>()).add(space);
×
77
            spacesByCoreInfo.put(space.getCoreInfoString(), space);
×
78
            spacesById.put(space.getId(), space);
×
79
        }
×
80
        for (Space space : spaceList) {
×
81
            Space superSpace = space.getIdSuperspace();
×
82
            if (superSpace == null) continue;
×
83
            subspaceMap.computeIfAbsent(superSpace, k -> new HashSet<>()).add(space);
×
84
            superspaceMap.computeIfAbsent(space, k -> new HashSet<>()).add(superSpace);
×
85
        }
×
86
        loaded = true;
×
87
    }
×
88

89
    /**
90
     * Check if the spaces have been loaded.
91
     *
92
     * @return true if loaded, false otherwise.
93
     */
94
    public static boolean isLoaded() {
95
        return loaded;
×
96
    }
97

98
    /**
99
     * Ensure that the spaces are loaded, fetching them from the API if necessary.
100
     */
101
    public static void ensureLoaded() {
102
        if (spaceList == null) {
×
103
            refresh(QueryApiAccess.forcedGet(new QueryRef("get-spaces")));
×
104
        }
105
    }
×
106

107
    /**
108
     * Get the list of all spaces.
109
     *
110
     * @return List of spaces.
111
     */
112
    public static List<Space> getSpaceList() {
113
        ensureLoaded();
×
114
        return spaceList;
×
115
    }
116

117
    /**
118
     * Get the list of spaces of a specific type.
119
     *
120
     * @param type The type of spaces to retrieve.
121
     * @return List of spaces of the specified type.
122
     */
123
    public static List<Space> getSpaceList(String type) {
124
        ensureLoaded();
×
125
        return spaceListByType.computeIfAbsent(type, k -> new ArrayList<>());
×
126
    }
127

128
    /**
129
     * Get a space by its id.
130
     *
131
     * @param id The id of the space.
132
     * @return The corresponding Space object, or null if not found.
133
     */
134
    public static Space get(String id) {
135
        ensureLoaded();
×
136
        return spacesById.get(id);
×
137
    }
138

139
    /**
140
     * Mark all spaces as needing a data update.
141
     */
142
    public static void refresh() {
143
        ensureLoaded();
×
144
        for (Space space : spaceList) {
×
145
            space.dataNeedsUpdate = true;
×
146
        }
×
147
    }
×
148

149
    private String id, label, rootNanopubId, type;
150
    private Nanopub rootNanopub = null;
×
151
    private SpaceData data = new SpaceData();
×
152

153
    private static class SpaceData implements Serializable {
×
154

155
        List<String> altIds = new ArrayList<>();
×
156

157
        String description = null;
×
158
        Calendar startDate, endDate;
159
        IRI defaultProvenance = null;
×
160

161
        List<IRI> admins = new ArrayList<>();
×
162
        Map<IRI, Set<SpaceMemberRole>> users = new HashMap<>();
×
163
        List<SpaceMemberRole> roles = new ArrayList<>();
×
164
        Map<IRI, SpaceMemberRole> roleMap = new HashMap<>();
×
165

166
        Map<String, IRI> adminPubkeyMap = new HashMap<>();
×
167
        List<Serializable> pinnedResources = new ArrayList<>();
×
168
        List<SpaceQueryView> views = new ArrayList<>();
×
169
        Set<String> pinGroupTags = new HashSet<>();
×
170
        Map<String, List<Serializable>> pinnedResourceMap = new HashMap<>();
×
171

172
        void addAdmin(IRI admin) {
173
            // TODO This isn't efficient for long owner lists:
174
            if (admins.contains(admin)) return;
×
175
            admins.add(admin);
×
176
            UserData ud = User.getUserData();
×
177
            for (String pubkeyhash : ud.getPubkeyhashes(admin, true)) {
×
178
                adminPubkeyMap.put(pubkeyhash, admin);
×
179
            }
×
180
        }
×
181

182
    }
183

184
    private boolean dataInitialized = false;
×
185
    private boolean dataNeedsUpdate = true;
×
186

187
    private Space(ApiResponseEntry resp) {
×
188
        this.id = resp.get("space");
×
189
        this.label = resp.get("label");
×
190
        this.type = resp.get("type");
×
191
        this.rootNanopubId = resp.get("np");
×
192
        this.rootNanopub = Utils.getAsNanopub(rootNanopubId);
×
193
        setCoreData(data);
×
194
    }
×
195

196
    /**
197
     * Get the ID of the space.
198
     *
199
     * @return The space ID.
200
     */
201
    public String getId() {
202
        return id;
×
203
    }
204

205
    /**
206
     * Get the root nanopublication ID of the space.
207
     *
208
     * @return The root nanopub ID.
209
     */
210
    public String getRootNanopubId() {
211
        return rootNanopubId;
×
212
    }
213

214
    /**
215
     * Get a string combining the space ID and root nanopub ID for core identification.
216
     *
217
     * @return The core info string.
218
     */
219
    public String getCoreInfoString() {
220
        return id + " " + rootNanopubId;
×
221
    }
222

223
    /**
224
     * Get the root nanopublication of the space.
225
     *
226
     * @return The root Nanopub object.
227
     */
228
    public Nanopub getRootNanopub() {
229
        return rootNanopub;
×
230
    }
231

232
    /**
233
     * Get the label of the space.
234
     *
235
     * @return The space label.
236
     */
237
    public String getLabel() {
238
        return label;
×
239
    }
240

241
    /**
242
     * Get the type of the space.
243
     *
244
     * @return The space type.
245
     */
246
    public String getType() {
247
        return type;
×
248
    }
249

250
    /**
251
     * Get the start date of the space.
252
     *
253
     * @return The start date as a Calendar object, or null if not set.
254
     */
255
    public Calendar getStartDate() {
256
        return data.startDate;
×
257
    }
258

259
    /**
260
     * Get the end date of the space.
261
     *
262
     * @return The end date as a Calendar object, or null if not set.
263
     */
264
    public Calendar getEndDate() {
265
        return data.endDate;
×
266
    }
267

268
    /**
269
     * Get a simplified label for the type of space by removing any namespace prefix.
270
     *
271
     * @return The simplified type label.
272
     */
273
    public String getTypeLabel() {
274
        return type.replaceFirst("^.*/", "");
×
275
    }
276

277
    /**
278
     * Get the description of the space.
279
     *
280
     * @return The description string.
281
     */
282
    public String getDescription() {
283
        return data.description;
×
284
    }
285

286
    /**
287
     * Check if the space data has been initialized.
288
     *
289
     * @return true if initialized, false otherwise.
290
     */
291
    public boolean isDataInitialized() {
292
        triggerDataUpdate();
×
293
        return dataInitialized;
×
294
    }
295

296
    /**
297
     * Get the list of admins in this space.
298
     *
299
     * @return List of admin IRIs.
300
     */
301
    public List<IRI> getAdmins() {
302
        triggerDataUpdate();
×
303
        return data.admins;
×
304
    }
305

306
    /**
307
     * Get the list of members in this space.
308
     *
309
     * @return List of member IRIs.
310
     */
311
    public List<IRI> getUsers() {
312
        triggerDataUpdate();
×
313
        List<IRI> users = new ArrayList<IRI>(data.users.keySet());
×
314
        users.sort(User.getUserData().userComparator);
×
315
        return users;
×
316
    }
317

318
    /**
319
     * Get the roles of a specific member in this space.
320
     *
321
     * @param userId The IRI of the member.
322
     * @return Set of roles assigned to the member, or null if the member is not part of this space.
323
     */
324
    public Set<SpaceMemberRole> getMemberRoles(IRI userId) {
325
        triggerDataUpdate();
×
326
        return data.users.get(userId);
×
327
    }
328

329
    /**
330
     * Check if a user is a member of this space.
331
     *
332
     * @param userId The IRI of the user to check.
333
     * @return true if the user is a member, false otherwise.
334
     */
335
    public boolean isMember(IRI userId) {
336
        triggerDataUpdate();
×
337
        return data.users.containsKey(userId);
×
338
    }
339

340
    /**
341
     * Get the list of pinned resources in this space.
342
     *
343
     * @return List of pinned resources.
344
     */
345
    public List<Serializable> getPinnedResources() {
346
        triggerDataUpdate();
×
347
        return data.pinnedResources;
×
348
    }
349

350
    /**
351
     * Get the set of tags used for grouping pinned resources.
352
     *
353
     * @return Set of tags.
354
     */
355
    public Set<String> getPinGroupTags() {
356
        triggerDataUpdate();
×
357
        return data.pinGroupTags;
×
358
    }
359

360
    /**
361
     * Get a map of pinned resources grouped by their tags.
362
     *
363
     * @return Map where keys are tags and values are lists of pinned resources (Templates or GrlcQueries).
364
     */
365
    public Map<String, List<Serializable>> getPinnedResourceMap() {
366
        triggerDataUpdate();
×
367
        return data.pinnedResourceMap;
×
368
    }
369

370
    /**
371
     * Get the list of views (GrlcQueries) associated with this space.
372
     *
373
     * @return List of GrlcQuery views.
374
     */
375
    public List<SpaceQueryView> getViews() {
376
        return data.views;
×
377
    }
378

379
    /**
380
     * Get the default provenance IRI for this space.
381
     *
382
     * @return The default provenance IRI, or null if not set.
383
     */
384
    public IRI getDefaultProvenance() {
385
        return data.defaultProvenance;
×
386
    }
387

388
    /**
389
     * Get the roles defined in this space.
390
     *
391
     * @return List of roles.
392
     */
393
    public List<SpaceMemberRole> getRoles() {
394
        return data.roles;
×
395
    }
396

397
    /**
398
     * Get the super ID of the space.
399
     *
400
     * @return Always returns null. Use getIdSuperspace() instead.
401
     */
402
    public String getSuperId() {
403
        return null;
×
404
    }
405

406
    /**
407
     * Get the superspace ID.
408
     *
409
     * @return The superspace, or null if not applicable.
410
     */
411
    public Space getIdSuperspace() {
412
        if (!id.matches("https?://[^/]+/.*/[^/]*/?")) return null;
×
413
        String superId = id.replaceFirst("(https?://[^/]+/.*)/[^/]*/?", "$1");
×
414
        if (spacesById.containsKey(superId)) {
×
415
            return spacesById.get(superId);
×
416
        }
417
        return null;
×
418
    }
419

420
    /**
421
     * Get superspaces of this space.
422
     *
423
     * @return List of superspaces.
424
     */
425
    public List<Space> getSuperspaces() {
426
        if (superspaceMap.containsKey(this)) {
×
427
            List<Space> superspaces = new ArrayList<>(superspaceMap.get(this));
×
428
            Collections.sort(superspaces, Ordering.usingToString());
×
429
            return superspaces;
×
430
        }
431
        return new ArrayList<>();
×
432
    }
433

434
    /**
435
     * Get subspaces of this space.
436
     *
437
     * @return List of subspaces.
438
     */
439
    public List<Space> getSubspaces() {
440
        if (subspaceMap.containsKey(this)) {
×
441
            List<Space> subspaces = new ArrayList<>(subspaceMap.get(this));
×
442
            Collections.sort(subspaces, Ordering.usingToString());
×
443
            return subspaces;
×
444
        }
445
        return new ArrayList<>();
×
446
    }
447

448
    /**
449
     * Get subspaces of a specific type.
450
     *
451
     * @param type The type of subspaces to retrieve.
452
     * @return List of subspaces of the specified type.
453
     */
454
    public List<Space> getSubspaces(String type) {
455
        List<Space> l = new ArrayList<>();
×
456
        for (Space s : getSubspaces()) {
×
457
            if (s.getType().equals(type)) l.add(s);
×
458
        }
×
459
        return l;
×
460
    }
461

462
    /**
463
     * Get alternative IDs for the space.
464
     *
465
     * @return List of alternative IDs.
466
     */
467
    public List<String> getAltIDs() {
468
        return data.altIds;
×
469
    }
470

471
    private synchronized void triggerDataUpdate() {
472
        if (dataNeedsUpdate) {
×
473
            new Thread(() -> {
×
474
                try {
475
                    SpaceData newData = new SpaceData();
×
476
                    setCoreData(newData);
×
477

478
                    newData.roles.add(SpaceMemberRole.ADMIN_ROLE);
×
479
                    newData.roleMap.put(SpaceMemberRole.HAS_ADMIN_PREDICATE, SpaceMemberRole.ADMIN_ROLE);
×
480

481
                    Multimap<String, String> spaceIds = ArrayListMultimap.create();
×
482
                    spaceIds.put("space", id);
×
483
                    for (String id : newData.altIds) {
×
484
                        spaceIds.put("space", id);
×
485
                    }
×
486

487
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-admins", spaceIds)).getData()) {
×
488
                        String pubkeyhash = r.get("pubkey");
×
489
                        if (newData.adminPubkeyMap.containsKey(pubkeyhash)) {
×
490
                            IRI adminId = Utils.vf.createIRI(r.get("admin"));
×
491
                            newData.addAdmin(adminId);
×
492
                            newData.users.computeIfAbsent(adminId, (k) -> new HashSet<>()).add(SpaceMemberRole.ADMIN_ROLE);
×
493
                        }
494
                    }
×
495
                    newData.admins.sort(User.getUserData().userComparator);
×
496

497
                    Multimap<String, String> getSpaceMemberParams = ArrayListMultimap.create(spaceIds);
×
498

499
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-space-member-roles", spaceIds)).getData()) {
×
500
                        if (!newData.adminPubkeyMap.containsKey(r.get("pubkey"))) continue;
×
501
                        SpaceMemberRole role = new SpaceMemberRole(r);
×
502
                        newData.roles.add(role);
×
503

504
                        // TODO Handle cases of overlapping properties:
505
                        for (IRI p : role.getRegularProperties()) newData.roleMap.put(p, role);
×
506
                        for (IRI p : role.getInverseProperties()) newData.roleMap.put(p, role);
×
507

508
                        role.addRoleParams(getSpaceMemberParams);
×
509
                    }
×
510

511
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-space-members", getSpaceMemberParams)).getData()) {
×
512
                        IRI memberId = Utils.vf.createIRI(r.get("member"));
×
513
                        SpaceMemberRole role = newData.roleMap.get(Utils.vf.createIRI(r.get("role")));
×
514
                        newData.users.computeIfAbsent(memberId, (k) -> new HashSet<>()).add(role);
×
515
                    }
×
516

517
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-pinned-templates", spaceIds)).getData()) {
×
518
                        if (!newData.adminPubkeyMap.containsKey(r.get("pubkey"))) continue;
×
519
                        Template t = TemplateData.get().getTemplate(r.get("template"));
×
520
                        if (t == null) continue;
×
521
                        newData.pinnedResources.add(t);
×
522
                        String tag = r.get("tag");
×
523
                        if (tag != null && !tag.isEmpty()) {
×
524
                            newData.pinGroupTags.add(r.get("tag"));
×
525
                            newData.pinnedResourceMap.computeIfAbsent(tag, k -> new ArrayList<>()).add(TemplateData.get().getTemplate(r.get("template")));
×
526
                        }
527
                    }
×
528
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-pinned-queries", spaceIds)).getData()) {
×
529
                        if (!newData.adminPubkeyMap.containsKey(r.get("pubkey"))) continue;
×
530
                        GrlcQuery query = GrlcQuery.get(r.get("query"));
×
531
                        if (query == null) continue;
×
532
                        newData.pinnedResources.add(query);
×
533
                        String tag = r.get("tag");
×
534
                        if (tag != null && !tag.isEmpty()) {
×
535
                            newData.pinGroupTags.add(r.get("tag"));
×
536
                            newData.pinnedResourceMap.computeIfAbsent(tag, k -> new ArrayList<>()).add(query);
×
537
                        }
538
                    }
×
539
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-views-for-space", spaceIds)).getData()) {
×
540
                        if (!newData.adminPubkeyMap.containsKey(r.get("pubkey"))) continue;
×
541
                        GrlcQuery query = GrlcQuery.get(r.get("query"));
×
542
                        if (query == null) continue;
×
543
                        SpaceQueryView view = new SpaceQueryView(this, query, r.get("title"));
×
544
                        newData.views.add(view);
×
545
                    }
×
546
                    data = newData;
×
547
                    dataInitialized = true;
×
548
                } catch (Exception ex) {
×
549
                    logger.error("Error while trying to update space data: {}", ex);
×
550
                }
×
551
            }).start();
×
552
            dataNeedsUpdate = false;
×
553
        }
554
    }
×
555

556
    private void setCoreData(SpaceData data) {
557
        for (Statement st : rootNanopub.getAssertion()) {
×
558
            if (st.getSubject().stringValue().equals(getId())) {
×
559
                if (st.getPredicate().equals(OWL.SAMEAS) && st.getObject() instanceof IRI objIri) {
×
560
                    data.altIds.add(objIri.stringValue());
×
561
                } else if (st.getPredicate().equals(DCTERMS.DESCRIPTION)) {
×
562
                    data.description = st.getObject().stringValue();
×
563
                } else if (st.getPredicate().stringValue().equals("http://schema.org/startDate")) {
×
564
                    try {
565
                        data.startDate = DatatypeConverter.parseDateTime(st.getObject().stringValue());
×
566
                    } catch (DateTimeParseException ex) {
×
567
                        logger.error("Failed to parse date {}", st.getObject().stringValue());
×
568
                    }
×
569
                } else if (st.getPredicate().stringValue().equals("http://schema.org/endDate")) {
×
570
                    try {
571
                        data.endDate = DatatypeConverter.parseDateTime(st.getObject().stringValue());
×
572
                    } catch (IllegalArgumentException ex) {
×
573
                        logger.error("Failed to parse date {}", st.getObject().stringValue());
×
574
                    }
×
575
                } else if (st.getPredicate().equals(HAS_ADMIN) && st.getObject() instanceof IRI obj) {
×
576
                    data.addAdmin(obj);
×
577
                } else if (st.getPredicate().equals(HAS_PINNED_TEMPLATE) && st.getObject() instanceof IRI obj) {
×
578
                    data.pinnedResources.add(TemplateData.get().getTemplate(obj.stringValue()));
×
579
                } else if (st.getPredicate().equals(HAS_PINNED_QUERY) && st.getObject() instanceof IRI obj) {
×
580
                    data.pinnedResources.add(GrlcQuery.get(obj.stringValue()));
×
581
                } else if (st.getPredicate().equals(NTEMPLATE.HAS_DEFAULT_PROVENANCE) && st.getObject() instanceof IRI obj) {
×
582
                    data.defaultProvenance = obj;
×
583
                }
584
            } else if (st.getPredicate().equals(NTEMPLATE.HAS_TAG) && st.getObject() instanceof Literal l) {
×
585
                data.pinGroupTags.add(l.stringValue());
×
586
                List<Serializable> list = data.pinnedResourceMap.get(l.stringValue());
×
587
                if (list == null) {
×
588
                    list = new ArrayList<>();
×
589
                    data.pinnedResourceMap.put(l.stringValue(), list);
×
590
                }
591
                list.add(TemplateData.get().getTemplate(st.getSubject().stringValue()));
×
592
            }
593
        }
×
594
    }
×
595

596
    @Override
597
    public String toString() {
598
        return id;
×
599
    }
600

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