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

knowledgepixels / nanodash / 19818701198

01 Dec 2025 10:02AM UTC coverage: 13.36% (-0.5%) from 13.901%
19818701198

push

github

tkuhn
feat(ResourceView): Automatically apply latest version of views

503 of 4898 branches covered (10.27%)

Branch coverage included in aggregate %.

1347 of 8949 relevant lines covered (15.05%)

0.67 hits per line

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

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

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

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

17
public class MaintainedResource extends ProfiledResource {
18

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

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

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

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

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

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

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

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

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

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

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

115
    public static void refresh() {
116
        refresh(QueryApiAccess.forcedGet(new QueryRef("get-maintained-resources")));
×
117
        for (MaintainedResource resource : resourceList) {
×
118
            resource.setDataNeedsUpdate();
×
119
        }
×
120
    }
×
121

122
    private String label, nanopubId, namespace;
123
    private Nanopub nanopub;
124

125
    private MaintainedResource(ApiResponseEntry resp, Space space) {
126
        super(resp.get("resource"));
×
127
        initSpace(space);
×
128
        this.label = resp.get("label");
×
129
        this.nanopubId = resp.get("np");
×
130
        this.namespace = resp.get("namespace");
×
131
        if (namespace != null && namespace.isBlank()) namespace = null;
×
132
        this.nanopub = Utils.getAsNanopub(nanopubId);
×
133
    }
×
134

135
    @Override
136
    public String getNanopubId() {
137
        return nanopubId;
×
138
    }
139

140
    @Override
141
    public Nanopub getNanopub() {
142
        return nanopub;
×
143
    }
144

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

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

154
    public boolean appliesTo(String elementId, Set<IRI> classes) {
155
        triggerDataUpdate();
×
156
        for (ViewDisplay v : getViewDisplays()) {
×
157
            if (v.appliesTo(elementId, classes)) return true;
×
158
        }
×
159
        return false;
×
160
    }
161

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