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

knowledgepixels / nanodash / 19473636483

18 Nov 2025 04:35PM UTC coverage: 14.513% (+0.2%) from 14.288%
19473636483

push

github

ashleycaselli
ci(deps): update action actions/setup-java to v5.0.0

542 of 4792 branches covered (11.31%)

Branch coverage included in aggregate %.

1437 of 8844 relevant lines covered (16.25%)

0.72 hits per line

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

39.38
src/main/java/com/knowledgepixels/nanodash/MaintainedResource.java
1
package com.knowledgepixels.nanodash;
2

3
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
4
import org.eclipse.rdf4j.model.IRI;
5
import org.nanopub.Nanopub;
6
import org.nanopub.extra.services.ApiResponse;
7
import org.nanopub.extra.services.ApiResponseEntry;
8
import org.nanopub.extra.services.QueryRef;
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11

12
import java.io.Serializable;
13
import java.util.*;
14

15
public class MaintainedResource implements Serializable {
16

17
    private static final Logger logger = LoggerFactory.getLogger(MaintainedResource.class);
3✔
18

19
    private static List<MaintainedResource> resourceList;
20
    private static Map<String, MaintainedResource> resourcesById;
21
    private static Map<String, MaintainedResource> resourcesByNamespace;
22
    private static Map<Space, List<MaintainedResource>> resourcesBySpace;
23
    private static boolean loaded = false;
2✔
24
    private static Long runRootUpdateAfter = null;
3✔
25

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

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

56
    /**
57
     * Ensure that the resources are loaded, fetching them from the API if necessary.
58
     */
59
    public static void ensureLoaded() {
60
        if (resourceList == null) {
2✔
61
            try {
62
                if (runRootUpdateAfter != null) {
2!
63
                    while (System.currentTimeMillis() < runRootUpdateAfter) {
×
64
                        Thread.sleep(100);
×
65
                    }
66
                    runRootUpdateAfter = null;
×
67
                }
68
            } catch (InterruptedException ex) {
×
69
                logger.error("Interrupted", ex);
×
70
            }
1✔
71
            refresh(QueryApiAccess.forcedGet(new QueryRef("get-maintained-resources")));
6✔
72
        }
73
    }
1✔
74

75
    public static void forceRootRefresh(long waitMillis) {
76
        resourceList = null;
×
77
        runRootUpdateAfter = System.currentTimeMillis() + waitMillis;
×
78
    }
×
79

80
    /**
81
     * Get the list of all maintained resources.
82
     *
83
     * @return List of resources.
84
     */
85
    public static List<MaintainedResource> getResourceList() {
86
        ensureLoaded();
×
87
        return resourceList;
×
88
    }
89

90
    public static List<MaintainedResource> getResourcesBySpace(Space space) {
91
        return resourcesBySpace.computeIfAbsent(space, k -> new ArrayList<>());
×
92
    }
93

94
    /**
95
     * Get a maintained resource by its id.
96
     *
97
     * @param id The id of the resource.
98
     * @return The corresponding MaintainedResource object, or null if not found.
99
     */
100
    public static MaintainedResource get(String id) {
101
        ensureLoaded();
1✔
102
        return resourcesById.get(id);
5✔
103
    }
104

105
    public static MaintainedResource getByNamespace(String namespace) {
106
        return resourcesByNamespace.get(namespace);
×
107
    }
108

109
    public static String getNamespace(Object stringOrIri) {
110
        return stringOrIri.toString().replaceFirst("([#/])[^#/]+$", "$1");
×
111
    }
112

113
    public static void refresh() {
114
        refresh(QueryApiAccess.forcedGet(new QueryRef("get-maintained-resources")));
6✔
115
        for (MaintainedResource resource : resourceList) {
10✔
116
            resource.dataNeedsUpdate = true;
3✔
117
        }
1✔
118
    }
1✔
119

120
    private String id, label, nanopubId, namespace;
121
    private Space space;
122
    private Nanopub nanopub;
123
    private ResourceData data = new ResourceData();
5✔
124

125
    private boolean dataInitialized = false;
3✔
126
    private boolean dataNeedsUpdate = true;
3✔
127

128
    private static class ResourceData implements Serializable {
2✔
129
        List<ViewDisplay> topLevelViews = new ArrayList<>();
5✔
130
        List<ViewDisplay> partLevelViews = new ArrayList<>();
6✔
131
    }
132

133
    private MaintainedResource(ApiResponseEntry resp, Space space) {
2✔
134
        this.space = space;
3✔
135
        this.id = resp.get("resource");
5✔
136
        this.label = resp.get("label");
5✔
137
        this.nanopubId = resp.get("np");
5✔
138
        this.namespace = resp.get("namespace");
5✔
139
        if (namespace != null && namespace.isBlank()) namespace = null;
10!
140
        this.nanopub = Utils.getAsNanopub(nanopubId);
5✔
141
    }
1✔
142

143
    public Space getSpace() {
144
        return space;
×
145
    }
146

147
    public String getId() {
148
        return id;
3✔
149
    }
150

151
    public String getNanopubId() {
152
        return nanopubId;
×
153
    }
154

155
    public Nanopub getNanopub() {
156
        return nanopub;
×
157
    }
158

159
    public String getLabel() {
160
        return label;
×
161
    }
162

163
    public String getNamespace() {
164
        return namespace;
3✔
165
    }
166

167
    public boolean isDataInitialized() {
168
        triggerDataUpdate();
×
169
        return dataInitialized;
×
170
    }
171

172
    public List<ViewDisplay> getTopLevelViews() {
173
        triggerDataUpdate();
×
174
        // TODO Check for compliance with target classes here too:
175
        return data.topLevelViews;
×
176
    }
177

178
    public List<ViewDisplay> getPartLevelViews(Set<IRI> classes) {
179
        triggerDataUpdate();
×
180
        List<ViewDisplay> viewDisplays = new ArrayList<>();
×
181
        for (ViewDisplay v : data.partLevelViews) {
×
182
            if (v.getView().hasTargetClasses()) {
×
183
                for (IRI c : classes) {
×
184
                    if (v.getView().hasTargetClass(c)) {
×
185
                        viewDisplays.add(v);
×
186
                        break;
×
187
                    }
188
                }
×
189
            } else {
190
                viewDisplays.add(v);
×
191
            }
192
        }
×
193
        return viewDisplays;
×
194
    }
195

196
    public boolean coversElement(String elementId) {
197
        triggerDataUpdate();
×
198
        for (ViewDisplay v : data.topLevelViews) {
×
199
            if (v.getView().coversElement(elementId)) return true;
×
200
        }
×
201
        for (ViewDisplay v : data.partLevelViews) {
×
202
            if (v.getView().coversElement(elementId)) return true;
×
203
        }
×
204
        return false;
×
205
    }
206

207
    private synchronized void triggerDataUpdate() {
208
        if (dataNeedsUpdate) {
×
209
            new Thread(() -> {
×
210
                try {
211
                    ResourceData newData = new ResourceData();
×
212

213
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-view-displays", "resource", id)).getData()) {
×
214
                        if (!space.isAdminPubkey(r.get("pubkey"))) continue;
×
215
                        try {
216
                            ViewDisplay vd = ViewDisplay.get(r.get("display"));
×
217
                            if (KPXL_TERMS.PART_LEVEL_VIEW_DISPLAY.stringValue().equals(r.get("displayType"))) {
×
218
                                newData.partLevelViews.add(vd);
×
219
                            } else {
220
                                newData.topLevelViews.add(vd);
×
221
                            }
222
                        } catch (IllegalArgumentException ex) {
×
223
                            logger.error("Couldn't generate view display object", ex);
×
224
                        }
×
225
                    }
×
226
                    Collections.sort(newData.topLevelViews);
×
227
                    Collections.sort(newData.partLevelViews);
×
228
                    data = newData;
×
229
                    dataInitialized = true;
×
230
                } catch (Exception ex) {
×
231
                    logger.error("Error while trying to update space data: {}", ex);
×
232
                    dataNeedsUpdate = true;
×
233
                }
×
234
            }).start();
×
235
            dataNeedsUpdate = false;
×
236
        }
237
    }
×
238

239
    @Override
240
    public String toString() {
241
        return id;
×
242
    }
243

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