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

knowledgepixels / nanodash / 18156079066

01 Oct 2025 08:04AM UTC coverage: 13.719% (-0.09%) from 13.806%
18156079066

push

github

ashleycaselli
ci(deps): update docker/build-push-action action to v6.18.0

446 of 4084 branches covered (10.92%)

Branch coverage included in aggregate %.

1146 of 7520 relevant lines covered (15.24%)

0.68 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>> members = 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<GrlcQuery> 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> getMembers() {
312
        triggerDataUpdate();
×
313
        List<IRI> members = new ArrayList<IRI>(data.members.keySet());
×
314
        members.sort(User.getUserData().userComparator);
×
315
        return members;
×
316
    }
317

318
    /**
319
     * Get the roles of a specific member in this space.
320
     *
321
     * @param memberId 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 memberId) {
325
        return data.members.get(memberId);
×
326
    }
327

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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