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

knowledgepixels / nanodash / 17820456396

18 Sep 2025 06:47AM UTC coverage: 13.877% (-0.1%) from 13.974%
17820456396

Pull #258

github

web-flow
Merge cd5b653dd into 374f1d611
Pull Request #258: Update Nanopublication raw links

443 of 4020 branches covered (11.02%)

Branch coverage included in aggregate %.

1133 of 7337 relevant lines covered (15.44%)

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 static com.knowledgepixels.nanodash.Utils.vf;
4

5
import java.io.Serializable;
6
import java.util.ArrayList;
7
import java.util.Collections;
8
import java.util.HashMap;
9
import java.util.HashSet;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.Set;
13

14
import org.eclipse.rdf4j.model.IRI;
15
import org.eclipse.rdf4j.model.Literal;
16
import org.eclipse.rdf4j.model.Statement;
17
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
18
import org.nanopub.Nanopub;
19
import org.nanopub.extra.services.ApiResponse;
20
import org.nanopub.extra.services.ApiResponseEntry;
21
import org.nanopub.vocabulary.NTEMPLATE;
22

23
import com.github.jsonldjava.shaded.com.google.common.collect.Ordering;
24
import com.knowledgepixels.nanodash.template.Template;
25
import com.knowledgepixels.nanodash.template.TemplateData;
26

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

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

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

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

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

55
    public static synchronized void refresh(ApiResponse resp) {
56
        spaceList = new ArrayList<>();
×
57
        spaceListByType = new HashMap<>();
×
58
        Map<String,Space> prevSpacesByCoreInfoPrev = spacesByCoreInfo;
×
59
        spacesByCoreInfo = new HashMap<>();
×
60
        spacesById = new HashMap<>();
×
61
        subspaceMap = new HashMap<>();
×
62
        superspaceMap = new HashMap<>();
×
63
        for (ApiResponseEntry entry : resp.getData()) {
×
64
            Space space = new Space(entry);
×
65
            Space prevSpace = prevSpacesByCoreInfoPrev.get(space.getCoreInfoString());
×
66
            if (prevSpace != null) space = prevSpace;
×
67
            spaceList.add(space);
×
68
            spaceListByType.computeIfAbsent(space.getType(), k -> new ArrayList<>()).add(space);
×
69
            spacesByCoreInfo.put(space.getCoreInfoString(), space);
×
70
            spacesById.put(space.getId(), space);
×
71
        }
×
72
        for (Space space : spaceList) {
×
73
            Space superSpace = space.getIdSuperspace();
×
74
            if (superSpace == null) continue;
×
75
            subspaceMap.computeIfAbsent(superSpace, k -> new HashSet<>()).add(space);
×
76
            superspaceMap.computeIfAbsent(space, k -> new HashSet<>()).add(superSpace);
×
77
        }
×
78
        loaded = true;
×
79
    }
×
80

81
    public static boolean isLoaded() {
82
        return loaded;
×
83
    }
84

85
    public static void ensureLoaded() {
86
        if (spaceList == null) {
×
87
            refresh(QueryApiAccess.forcedGet("get-spaces"));
×
88
        }
89
    }
×
90

91
    public static List<Space> getSpaceList() {
92
        ensureLoaded();
×
93
        return spaceList;
×
94
    }
95

96
    public static List<Space> getSpaceList(String type) {
97
        ensureLoaded();
×
98
        return spaceListByType.computeIfAbsent(type, k -> new ArrayList<>());
×
99
    }
100

101
    public static Space get(String id) {
102
        ensureLoaded();
×
103
        return spacesById.get(id);
×
104
    }
105

106
    public static void refresh() {
107
        ensureLoaded();
×
108
        for (Space space : spaceList) {
×
109
            space.dataNeedsUpdate = true;
×
110
        }
×
111
    }
×
112

113
    private String id, label, rootNanopubId, type;
114
    private Nanopub rootNanopub = null;
×
115
    private SpaceData data = new SpaceData();
×
116

117
    private static class SpaceData implements Serializable {
×
118
        String description = null;
×
119
        IRI defaultProvenance = null;
×
120
        List<IRI> admins = new ArrayList<>();
×
121
        List<IRI> members = new ArrayList<>();
×
122
        Map<String,IRI> adminPubkeyMap = new HashMap<>();
×
123
        List<Serializable> pinnedResources = new ArrayList<>();
×
124
        Set<String> pinGroupTags = new HashSet<>();
×
125
        Map<String, List<Serializable>> pinnedResourceMap = new HashMap<>();
×
126

127
        void addAdmin(IRI admin) {
128
            // TODO This isn't efficient for long owner lists:
129
            if (admins.contains(admin)) return;
×
130
            admins.add(admin);
×
131
            UserData ud = User.getUserData();
×
132
            for (String pubkeyhash : ud.getPubkeyhashes(admin, true)) {
×
133
                adminPubkeyMap.put(pubkeyhash, admin);
×
134
            }
×
135
        }
×
136
    }
137

138
    private boolean dataInitialized = false;
×
139
    private boolean dataNeedsUpdate = true;
×
140

141
    private Space(ApiResponseEntry resp) {
×
142
        this.id = resp.get("space");
×
143
        this.label = resp.get("label");
×
144
        this.type = resp.get("type");
×
145
        this.rootNanopubId = resp.get("np");
×
146
        this.rootNanopub = Utils.getAsNanopub(rootNanopubId);
×
147
        setCoreData(data);
×
148
    }
×
149

150
    public String getId() {
151
        return id;
×
152
    }
153

154
    public String getRootNanopubId() {
155
        return rootNanopubId;
×
156
    }
157

158
    public String getCoreInfoString() {
159
        return id + " " + rootNanopubId;
×
160
    }
161

162
    public Nanopub getRootNanopub() {
163
        return rootNanopub;
×
164
    }
165

166
    public String getLabel() {
167
        return label;
×
168
    }
169

170
    public String getType() {
171
        return type;
×
172
    }
173

174
    public String getTypeLabel() {
175
        return type.replaceFirst("^.*/", "");
×
176
    }
177

178
    public String getDescription() {
179
        return data.description;
×
180
    }
181

182
    public boolean isDataInitialized() {
183
        triggerDataUpdate();
×
184
        return dataInitialized;
×
185
    }
186

187
    public List<IRI> getAdmins() {
188
        triggerDataUpdate();
×
189
        return data.admins;
×
190
    }
191

192
    public List<IRI> getMembers() {
193
        triggerDataUpdate();
×
194
        return data.members;
×
195
    }
196

197
    public boolean isMember(IRI userId) {
198
        triggerDataUpdate();
×
199
        // TODO This is inefficient for large member lists:
200
        return data.admins.contains(userId) || data.members.contains(userId);
×
201
    }
202

203
    public List<Serializable> getPinnedResources() {
204
        triggerDataUpdate();
×
205
        return data.pinnedResources;
×
206
    }
207

208
    public Set<String> getPinGroupTags() {
209
        triggerDataUpdate();
×
210
        return data.pinGroupTags;
×
211
    }
212

213
    public Map<String, List<Serializable>> getPinnedResourceMap() {
214
        triggerDataUpdate();
×
215
        return data.pinnedResourceMap;
×
216
    }
217

218
    public IRI getDefaultProvenance() {
219
        return data.defaultProvenance;
×
220
    }
221

222
    public String getSuperId() {
223
        return null;
×
224
    }
225

226
    public Space getIdSuperspace() {
227
        if (!id.matches("https?://[^/]+/.*/[^/]*/?")) return null;
×
228
        String superId = id.replaceFirst("(https?://[^/]+/.*)/[^/]*/?", "$1");
×
229
        if (spacesById.containsKey(superId)) {
×
230
            return spacesById.get(superId);
×
231
        }
232
        return null;
×
233
    }
234

235
    public List<Space> getSuperspaces() {
236
        if (superspaceMap.containsKey(this)) {
×
237
            List<Space> superspaces = new ArrayList<>(superspaceMap.get(this));
×
238
            Collections.sort(superspaces, Ordering.usingToString());
×
239
            return superspaces;
×
240
        }
241
        return new ArrayList<>();
×
242
    }
243

244
    public List<Space> getSubspaces() {
245
        if (subspaceMap.containsKey(this)) {
×
246
            List<Space> subspaces = new ArrayList<>(subspaceMap.get(this));
×
247
            Collections.sort(subspaces, Ordering.usingToString());
×
248
            return subspaces;
×
249
        }
250
        return new ArrayList<>();
×
251
    }
252

253
    public List<Space> getSubspaces(String type) {
254
        List<Space> l = new ArrayList<>();
×
255
        for (Space s : getSubspaces()) {
×
256
            if (s.getType().equals(type)) l.add(s);
×
257
        }
×
258
        return l;
×
259
    }
260

261
    private synchronized void triggerDataUpdate() {
262
        if (dataNeedsUpdate) {
×
263
            new Thread(() -> {
×
264
                SpaceData newData = new SpaceData();
×
265
                setCoreData(newData);
×
266

267
                for (ApiResponseEntry r : QueryApiAccess.forcedGet("get-admins", "unit", id).getData()) {
×
268
                    String pubkeyhash = r.get("pubkeyhash");
×
269
                    if (newData.adminPubkeyMap.containsKey(pubkeyhash)) {
×
270
                        newData.addAdmin(Utils.vf.createIRI(r.get("admin")));
×
271
                    }
272
                }
×
273
                newData.members = new ArrayList<>();
×
274
                for (ApiResponseEntry r : QueryApiAccess.forcedGet("get-members", "unit", id).getData()) {
×
275
                    IRI memberId = Utils.vf.createIRI(r.get("member"));
×
276
                    // TODO These checks are inefficient for long member lists:
277
                    if (newData.admins.contains(memberId)) continue;
×
278
                    if (newData.members.contains(memberId)) continue;
×
279
                    newData.members.add(memberId);
×
280
                }
×
281
                newData.admins.sort(User.getUserData().userComparator);
×
282
                newData.members.sort(User.getUserData().userComparator);
×
283

284
                for (ApiResponseEntry r : QueryApiAccess.forcedGet("get-pinned-templates", "space", id).getData()) {
×
285
                    if (!newData.adminPubkeyMap.containsKey(r.get("pubkey"))) continue;
×
286
                    Template t = TemplateData.get().getTemplate(r.get("template"));
×
287
                    if (t == null) continue;
×
288
                    newData.pinnedResources.add(t);
×
289
                    String tag = r.get("tag");
×
290
                    if (tag != null && !tag.isEmpty()) {
×
291
                        newData.pinGroupTags.add(r.get("tag"));
×
292
                        newData.pinnedResourceMap.computeIfAbsent(tag, k -> new ArrayList<>()).add(TemplateData.get().getTemplate(r.get("template")));
×
293
                    }
294
                }
×
295
                for (ApiResponseEntry r : QueryApiAccess.forcedGet("get-pinned-queries", "space", id).getData()) {
×
296
                    if (!newData.adminPubkeyMap.containsKey(r.get("pubkey"))) continue;
×
297
                    GrlcQuery query = GrlcQuery.get(r.get("query"));
×
298
                    if (query == null) continue;
×
299
                    newData.pinnedResources.add(query);
×
300
                    String tag = r.get("tag");
×
301
                    if (tag != null && !tag.isEmpty()) {
×
302
                        newData.pinGroupTags.add(r.get("tag"));
×
303
                        newData.pinnedResourceMap.computeIfAbsent(tag, k -> new ArrayList<>()).add(query);
×
304
                    }
305
                }
×
306
                data = newData;
×
307
                dataInitialized = true;
×
308
            }).start();
×
309
            dataNeedsUpdate = false;
×
310
        }
311
    }
×
312

313
    private void setCoreData(SpaceData data) {
314
        for (Statement st : rootNanopub.getAssertion()) {
×
315
            if (st.getSubject().stringValue().equals(getId())) {
×
316
                if (st.getPredicate().equals(DCTERMS.DESCRIPTION)) {
×
317
                    data.description = st.getObject().stringValue();
×
318
                } else if (st.getPredicate().equals(HAS_ADMIN) && st.getObject() instanceof IRI obj) {
×
319
                    data.addAdmin(obj);
×
320
                } else if (st.getPredicate().equals(HAS_PINNED_TEMPLATE) && st.getObject() instanceof IRI obj) {
×
321
                    data.pinnedResources.add(TemplateData.get().getTemplate(obj.stringValue()));
×
322
                } else if (st.getPredicate().equals(HAS_PINNED_QUERY) && st.getObject() instanceof IRI obj) {
×
323
                    data.pinnedResources.add(GrlcQuery.get(obj.stringValue()));
×
324
                } else if (st.getPredicate().equals(NTEMPLATE.HAS_DEFAULT_PROVENANCE) && st.getObject() instanceof IRI obj) {
×
325
                    data.defaultProvenance = obj;
×
326
                }
327
            } else if (st.getPredicate().equals(NTEMPLATE.HAS_TAG) && st.getObject() instanceof Literal l) {
×
328
                data.pinGroupTags.add(l.stringValue());
×
329
                List<Serializable> list = data.pinnedResourceMap.get(l.stringValue());
×
330
                if (list == null) {
×
331
                    list = new ArrayList<>();
×
332
                    data.pinnedResourceMap.put(l.stringValue(), list);
×
333
                }
334
                list.add(TemplateData.get().getTemplate(st.getSubject().stringValue()));
×
335
            }
336
        }
×
337
    }
×
338

339
    @Override
340
    public String toString() {
341
        return id;
×
342
    }
343

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