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

knowledgepixels / nanodash / 18834771957

27 Oct 2025 08:37AM UTC coverage: 13.674% (+0.01%) from 13.662%
18834771957

push

github

tkuhn
feat(MaintainedResources): Add resource part pages

481 of 4418 branches covered (10.89%)

Branch coverage included in aggregate %.

1255 of 8278 relevant lines covered (15.16%)

0.68 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

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

16
public class MaintainedResource implements Serializable {
17

18
    private static final Logger logger = LoggerFactory.getLogger(MaintainedResource.class);
×
19

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

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

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

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

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

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

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

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

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

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

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

111
    private boolean dataInitialized = false;
×
112
    private boolean dataNeedsUpdate = true;
×
113

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

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

128
    public Space getSpace() {
129
        return space;
×
130
    }
131

132
    public String getId() {
133
        return id;
×
134
    }
135

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

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

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

148
    public String getNamespace() {
149
        return namespace;
×
150
    }
151

152
    public boolean isDataInitialized() {
153
        triggerDataUpdate();
×
154
        return dataInitialized;
×
155
    }
156

157
    public List<ResourceView> getViews() {
158
        triggerDataUpdate();
×
159
        return data.views;
×
160
    }
161

162
    private synchronized void triggerDataUpdate() {
163
        if (dataNeedsUpdate) {
×
164
            new Thread(() -> {
×
165
                try {
166
                    ResourceData newData = new ResourceData();
×
167

168
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-view-displays", "resource", id)).getData()) {
×
169
                        if (!space.isAdminPubkey(r.get("pubkey"))) continue;
×
170
                        ResourceView view = ResourceView.get(r.get("view"));
×
171
                        if (view == null) continue;
×
172
                        newData.views.add(view);
×
173
                    }
×
174
                    data = newData;
×
175
                    dataInitialized = true;
×
176
                } catch (Exception ex) {
×
177
                    logger.error("Error while trying to update space data: {}", ex);
×
178
                    dataNeedsUpdate = true;
×
179
                }
×
180
            }).start();
×
181
            dataNeedsUpdate = false;
×
182
        }
183
    }
×
184

185
    @Override
186
    public String toString() {
187
        return id;
×
188
    }
189

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