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

knowledgepixels / nanodash / 19767873064

28 Nov 2025 03:21PM UTC coverage: 13.909% (-0.1%) from 14.034%
19767873064

push

github

tkuhn
refactor(MaintainedResource): Move ResourceData to ProfiledResource

525 of 4892 branches covered (10.73%)

Branch coverage included in aggregate %.

1399 of 8941 relevant lines covered (15.65%)

0.69 hits per line

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

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

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

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

19
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
20

21
public class MaintainedResource extends ProfiledResource {
22

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

25
    private static List<MaintainedResource> resourceList;
26
    private static Map<String, MaintainedResource> resourcesById;
27
    private static Map<String, MaintainedResource> resourcesByNamespace;
28
    private static Map<Space, List<MaintainedResource>> resourcesBySpace;
29
    private static boolean loaded = false;
2✔
30
    private static Long runRootUpdateAfter = null;
3✔
31

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

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

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

81
    public static void forceRootRefresh(long waitMillis) {
82
        resourceList = null;
×
83
        runRootUpdateAfter = System.currentTimeMillis() + waitMillis;
×
84
    }
×
85

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

96
    public static List<MaintainedResource> getResourcesBySpace(Space space) {
97
        return resourcesBySpace.computeIfAbsent(space, k -> new ArrayList<>());
×
98
    }
99

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

111
    public static MaintainedResource getByNamespace(String namespace) {
112
        return resourcesByNamespace.get(namespace);
×
113
    }
114

115
    public static String getNamespace(Object stringOrIri) {
116
        return stringOrIri.toString().replaceFirst("([#/])[^#/]+$", "$1");
×
117
    }
118

119
    public static void refresh() {
120
        refresh(QueryApiAccess.forcedGet(new QueryRef("get-maintained-resources")));
6✔
121
        for (MaintainedResource resource : resourceList) {
10✔
122
            resource.setDataNeedsUpdate();
2✔
123
        }
1✔
124
    }
1✔
125

126
    private String label, nanopubId, namespace;
127
    private Space space;
128
    private Nanopub nanopub;
129

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

140
    public Space getSpace() {
141
        return space;
×
142
    }
143

144
    public String getNanopubId() {
145
        return nanopubId;
×
146
    }
147

148
    public Nanopub getNanopub() {
149
        return nanopub;
×
150
    }
151

152
    public String getLabel() {
153
        return label;
×
154
    }
155

156
    public String getNamespace() {
157
        return namespace;
3✔
158
    }
159

160
    public List<ViewDisplay> getViewDisplays(boolean toplevel, Set<IRI> classes) {
161
        triggerDataUpdate();
×
162
        List<ViewDisplay> viewDisplays = new ArrayList<>();
×
163
        Set<IRI> viewKinds = new HashSet<>();
×
164

165
        for (ViewDisplay vd : getViewDisplays()) {
×
166
            IRI kind = vd.getViewKindIri();
×
167
            if (kind != null) {
×
168
                if (viewKinds.contains(kind)) continue;
×
169
                viewKinds.add(vd.getViewKindIri());
×
170
            }
171
            if (vd.hasType(KPXL_TERMS.DEACTIVATED_VIEW_DISPLAY)) continue;
×
172

173
            if (vd.appliesTo(getId(), null)) {
×
174
                viewDisplays.add(vd);
×
175
            } else if (toplevel && vd.hasType(KPXL_TERMS.TOP_LEVEL_VIEW_DISPLAY)) {
×
176
                // Deprecated
177
                viewDisplays.add(vd);
×
178
            }
179
        }
×
180

181
        Collections.sort(viewDisplays);
×
182
        return viewDisplays;
×
183
    }
184

185
    public boolean appliesTo(String elementId, Set<IRI> classes) {
186
        triggerDataUpdate();
×
187
        for (ViewDisplay v : getViewDisplays()) {
×
188
            if (v.appliesTo(elementId, classes)) return true;
×
189
        }
×
190
        return false;
×
191
    }
192

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