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

knowledgepixels / nanodash / 19235243637

10 Nov 2025 02:36PM UTC coverage: 13.754% (-0.6%) from 14.373%
19235243637

push

github

tkuhn
feat(ViewDisplay): Support for specifying width and structural position

514 of 4708 branches covered (10.92%)

Branch coverage included in aggregate %.

1329 of 8692 relevant lines covered (15.29%)

0.68 hits per line

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

40.69
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.Collections;
6
import java.util.HashMap;
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
public class MaintainedResource implements Serializable {
20

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

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

29
    public static synchronized void refresh(ApiResponse resp) {
30
        resourceList = new ArrayList<>();
4✔
31
        resourcesById = new HashMap<>();
4✔
32
        resourcesBySpace = new HashMap<>();
4✔
33
        resourcesByNamespace = new HashMap<>();
4✔
34
        for (ApiResponseEntry entry : resp.getData()) {
11✔
35
            Space space = Space.get(entry.get("space"));
5✔
36
            if (space == null) continue;
2!
37
            MaintainedResource resource = new MaintainedResource(entry, space);
6✔
38
            if (resourcesById.containsKey(resource.getId())) continue;
5!
39
            resourceList.add(resource);
4✔
40
            resourcesById.put(resource.getId(), resource);
6✔
41
            resourcesBySpace.computeIfAbsent(space, k -> new ArrayList<>()).add(resource);
12✔
42
            if (resource.getNamespace() != null) {
3✔
43
                // TODO Handle conflicts when two resources claim the same namespace:
44
                resourcesByNamespace.put(resource.getNamespace(), resource);
6✔
45
            }
46
        }
1✔
47
        loaded = true;
2✔
48
    }
1✔
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) {
2✔
64
            refresh(QueryApiAccess.forcedGet(new QueryRef("get-maintained-resources")));
6✔
65
        }
66
    }
1✔
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();
1✔
90
        return resourcesById.get(id);
5✔
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
        refresh(QueryApiAccess.forcedGet(new QueryRef("get-maintained-resources")));
6✔
103
        for (MaintainedResource resource : resourceList) {
10✔
104
            resource.dataNeedsUpdate = true;
3✔
105
        }
1✔
106
    }
1✔
107

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

113
    private boolean dataInitialized = false;
3✔
114
    private boolean dataNeedsUpdate = true;
3✔
115

116
    private static class ResourceData implements Serializable {
2✔
117
        List<ViewDisplay> topLevelViews = new ArrayList<>();
5✔
118
        List<ViewDisplay> partLevelViews = new ArrayList<>();
6✔
119
    }
120

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

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

135
    public String getId() {
136
        return id;
3✔
137
    }
138

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

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

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

151
    public String getNamespace() {
152
        return namespace;
3✔
153
    }
154

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

160
    public List<ViewDisplay> getTopLevelViews() {
161
        triggerDataUpdate();
×
162
        // TODO Check for compliance with target classes here too:
163
        return data.topLevelViews;
×
164
    }
165

166
    public List<ViewDisplay> getPartLevelViews(Set<IRI> classes) {
167
        triggerDataUpdate();
×
168
        List<ViewDisplay> viewDisplays = new ArrayList<>();
×
169
        for (ViewDisplay v : data.partLevelViews) {
×
170
            if (v.getView().hasTargetClasses()) {
×
171
                for (IRI c : classes) {
×
172
                    if (v.getView().hasTargetClass(c)) {
×
173
                        viewDisplays.add(v);
×
174
                        break;
×
175
                    }
176
                }
×
177
            } else {
178
                viewDisplays.add(v);
×
179
            }
180
        }
×
181
        return viewDisplays;
×
182
    }
183

184
    public boolean coversElement(String elementId) {
185
        triggerDataUpdate();
×
186
        for (ViewDisplay v : data.topLevelViews) {
×
187
            if (v.getView().coversElement(elementId)) return true;
×
188
        }
×
189
        for (ViewDisplay v : data.partLevelViews) {
×
190
            if (v.getView().coversElement(elementId)) return true;
×
191
        }
×
192
        return false;
×
193
    }
194

195
    private synchronized void triggerDataUpdate() {
196
        if (dataNeedsUpdate) {
×
197
            new Thread(() -> {
×
198
                try {
199
                    ResourceData newData = new ResourceData();
×
200

201
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-view-displays", "resource", id)).getData()) {
×
202
                        if (!space.isAdminPubkey(r.get("pubkey"))) continue;
×
203
                        try {
204
                            ViewDisplay vd = ViewDisplay.get(r.get("display"));
×
205
                            if (ResourceView.PART_LEVEL_VIEW_DISPLAY.stringValue().equals(r.get("displayType"))) {
×
206
                                newData.partLevelViews.add(vd);
×
207
                            } else {
208
                                newData.topLevelViews.add(vd);
×
209
                            }
210
                        } catch (IllegalArgumentException ex) {
×
211
                            logger.error("Couldn't generate view display object", ex);
×
212
                        }
×
213
                    }
×
214
                    Collections.sort(newData.topLevelViews);
×
215
                    Collections.sort(newData.partLevelViews);
×
216
                    data = newData;
×
217
                    dataInitialized = true;
×
218
                } catch (Exception ex) {
×
219
                    logger.error("Error while trying to update space data: {}", ex);
×
220
                    dataNeedsUpdate = true;
×
221
                }
×
222
            }).start();
×
223
            dataNeedsUpdate = false;
×
224
        }
225
    }
×
226

227
    @Override
228
    public String toString() {
229
        return id;
×
230
    }
231

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