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

knowledgepixels / nanodash / 18868860701

28 Oct 2025 08:37AM UTC coverage: 13.485% (+0.4%) from 13.1%
18868860701

push

github

tkuhn
feat(MaintainedResource): Add support for resource part views

477 of 4476 branches covered (10.66%)

Branch coverage included in aggregate %.

1255 of 8368 relevant lines covered (15.0%)

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/MaintainedResource.java
1
package com.knowledgepixels.nanodash;
2

3
import java.io.Serializable;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Set;
9

10
import org.eclipse.rdf4j.model.IRI;
11
import org.nanopub.Nanopub;
12
import org.nanopub.extra.services.ApiResponse;
13
import org.nanopub.extra.services.ApiResponseEntry;
14
import org.nanopub.extra.services.QueryRef;
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17

18
public class MaintainedResource implements Serializable {
19

20
    private static final Logger logger = LoggerFactory.getLogger(MaintainedResource.class);
×
21

22
    private static List<MaintainedResource> resourceList;
23
    private static Map<String, MaintainedResource> resourcesById;
24
    private static Map<String, MaintainedResource> resourcesByNamespace;
25
    private static Map<Space, List<MaintainedResource>> resourcesBySpace;
26
    private static boolean loaded = false;
×
27

28
    public static synchronized void refresh(ApiResponse resp) {
29
        System.err.println("REFRESH...");
×
30
        resourceList = new ArrayList<>();
×
31
        resourcesById = new HashMap<>();
×
32
        resourcesBySpace = new HashMap<>();
×
33
        resourcesByNamespace = new HashMap<>();
×
34
        for (ApiResponseEntry entry : resp.getData()) {
×
35
            Space space = Space.get(entry.get("space"));
×
36
            if (space == null) continue;
×
37
            MaintainedResource resource = new MaintainedResource(entry, space);
×
38
            if (resourcesById.containsKey(resource.getId())) continue;
×
39
            resourceList.add(resource);
×
40
            resourcesById.put(resource.getId(), resource);
×
41
            resourcesBySpace.computeIfAbsent(space, k -> new ArrayList<>()).add(resource);
×
42
            if (resource.getNamespace() != null) {
×
43
                // TODO Handle conflicts when two resources claim the same namespace:
44
                resourcesByNamespace.put(resource.getNamespace(), resource);
×
45
            }
46
        }
×
47
        loaded = true;
×
48
    }
×
49

50
    /**
51
     * Check if the resources have been loaded.
52
     *
53
     * @return true if loaded, false otherwise.
54
     */
55
    public static boolean isLoaded() {
56
        return loaded;
×
57
    }
58

59
    /**
60
     * Ensure that the resources are loaded, fetching them from the API if necessary.
61
     */
62
    public static void ensureLoaded() {
63
        if (resourceList == null) {
×
64
            refresh(QueryApiAccess.forcedGet(new QueryRef("get-maintained-resources")));
×
65
        }
66
    }
×
67

68
    /**
69
     * Get the list of all maintained resources.
70
     *
71
     * @return List of resources.
72
     */
73
    public static List<MaintainedResource> getResourceList() {
74
        ensureLoaded();
×
75
        return resourceList;
×
76
    }
77

78
    public static List<MaintainedResource> getResourcesBySpace(Space space) {
79
        return resourcesBySpace.computeIfAbsent(space, k -> new ArrayList<>());
×
80
    }
81

82
    /**
83
     * Get a maintained resource by its id.
84
     *
85
     * @param id The id of the resource.
86
     * @return The corresponding MaintainedResource object, or null if not found.
87
     */
88
    public static MaintainedResource get(String id) {
89
        ensureLoaded();
×
90
        return resourcesById.get(id);
×
91
    }
92

93
    public static MaintainedResource getByNamespace(String namespace) {
94
        return resourcesByNamespace.get(namespace);
×
95
    }
96

97
    public static String getNamespace(Object stringOrIri) {
98
        return stringOrIri.toString().replaceFirst("([#/])[^#/]+$", "$1");
×
99
    }
100

101
    public static void refresh() {
102
        ensureLoaded();
×
103
        for (MaintainedResource resource : resourceList) {
×
104
            resource.dataNeedsUpdate = true;
×
105
        }
×
106
    }
×
107

108
    private String id, label, nanopubId, namespace;
109
    private Space space;
110
    private Nanopub nanopub;
111
    private ResourceData data = new ResourceData();
×
112

113
    private boolean dataInitialized = false;
×
114
    private boolean dataNeedsUpdate = true;
×
115

116
    private static class ResourceData implements Serializable {
×
117
        List<ResourceView> views = new ArrayList<>();
×
118
    }
119

120
    private MaintainedResource(ApiResponseEntry resp, Space space) {
×
121
        this.space = space;
×
122
        this.id = resp.get("resource");
×
123
        this.label = resp.get("label");
×
124
        this.nanopubId = resp.get("np");
×
125
        this.namespace = resp.get("namespace");
×
126
        if (namespace != null && namespace.isBlank()) namespace = null;
×
127
        this.nanopub = Utils.getAsNanopub(nanopubId);
×
128
    }
×
129

130
    public Space getSpace() {
131
        return space;
×
132
    }
133

134
    public String getId() {
135
        return id;
×
136
    }
137

138
    public String getNanopubId() {
139
        return nanopubId;
×
140
    }
141

142
    public Nanopub getNanopub() {
143
        return nanopub;
×
144
    }
145

146
    public String getLabel() {
147
        return label;
×
148
    }
149

150
    public String getNamespace() {
151
        return namespace;
×
152
    }
153

154
    public boolean isDataInitialized() {
155
        triggerDataUpdate();
×
156
        return dataInitialized;
×
157
    }
158

159
    public List<ResourceView> getTopLevelViews() {
160
        triggerDataUpdate();
×
161
        List<ResourceView> views = new ArrayList<>();
×
162
        for (ResourceView v : data.views) {
×
163
            if (v.isTopLevelView()) views.add(v);
×
164
        }
×
165
        return views;
×
166
    }
167

168
    public List<ResourceView> getPartLevelViews(Set<IRI> classes) {
169
        triggerDataUpdate();
×
170
        List<ResourceView> views = new ArrayList<>();
×
171
        for (ResourceView v : data.views) {
×
172
            if (v.isTopLevelView()) continue;
×
173
            if (v.hasTargetClasses()) {
×
174
                for (IRI c : classes) {
×
175
                    if (v.hasTargetClass(c)) {
×
176
                        views.add(v);
×
177
                        break;
×
178
                    }
179
                }
×
180
            } else {
181
                views.add(v);
×
182
            }
183
        }
×
184
        return views;
×
185
    }
186

187
    private synchronized void triggerDataUpdate() {
188
        if (dataNeedsUpdate) {
×
189
            new Thread(() -> {
×
190
                try {
191
                    ResourceData newData = new ResourceData();
×
192

193
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-view-displays", "resource", id)).getData()) {
×
194
                        if (!space.isAdminPubkey(r.get("pubkey"))) continue;
×
195
                        ResourceView view = ResourceView.get(r.get("view"));
×
196
                        if (view == null) continue;
×
197
                        newData.views.add(view);
×
198
                    }
×
199
                    data = newData;
×
200
                    dataInitialized = true;
×
201
                } catch (Exception ex) {
×
202
                    logger.error("Error while trying to update space data: {}", ex);
×
203
                    dataNeedsUpdate = true;
×
204
                }
×
205
            }).start();
×
206
            dataNeedsUpdate = false;
×
207
        }
208
    }
×
209

210
    @Override
211
    public String toString() {
212
        return id;
×
213
    }
214

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