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

knowledgepixels / nanodash / 20268226543

16 Dec 2025 12:39PM UTC coverage: 14.107% (-1.3%) from 15.358%
20268226543

push

github

ashleycaselli
refactor: replace VocabUtils with the ones defined in the nanopub-java library

526 of 4946 branches covered (10.63%)

Branch coverage included in aggregate %.

1452 of 9075 relevant lines covered (16.0%)

2.11 hits per line

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

10.14
src/main/java/com/knowledgepixels/nanodash/ProfiledResource.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.ApiResponseEntry;
7
import org.nanopub.extra.services.QueryRef;
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10

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

14
public class ProfiledResource implements Serializable {
15

16
    private static final Logger logger = LoggerFactory.getLogger(ProfiledResource.class);
9✔
17

18
    protected static class ResourceData implements Serializable {
6✔
19
        List<ViewDisplay> viewDisplays = new ArrayList<>();
18✔
20
    }
21

22
    private static Map<String,ProfiledResource> instances = new HashMap<>();
15✔
23

24
    public static void refresh() {
25
        for (ProfiledResource r : instances.values()) {
×
26
            r.setDataNeedsUpdate();
×
27
        }
×
28
    }
×
29

30
    public static void forceRefresh(String id, long waitMillis) {
31
        if (isProfiledResource(id)) {
×
32
            instances.get(id).forceRefresh(waitMillis);
×
33
        }
34
    }
×
35

36
    public static boolean isProfiledResource(String id) {
37
        return instances.containsKey(id);
×
38
    }
39

40
    private String id;
41
    private Space space;
42
    private ResourceData data = new ResourceData();
15✔
43
    private boolean dataInitialized = false;
9✔
44
    private boolean dataNeedsUpdate = true;
9✔
45
    private Long runUpdateAfter = null;
9✔
46

47
    protected ProfiledResource(String id) {
6✔
48
        this.id = id;
9✔
49
        instances.put(id, this);
15✔
50
    }
3✔
51

52
    protected void initSpace(Space space) {
53
        this.space = space;
9✔
54
    }
3✔
55

56
    public String getId() {
57
        return id;
9✔
58
    }
59

60
    protected synchronized Thread triggerDataUpdate() {
61
        if (dataNeedsUpdate) {
×
62
            Thread thread = new Thread(() -> {
×
63
                try {
64
                    if (runUpdateAfter != null) {
×
65
                        while (System.currentTimeMillis() < runUpdateAfter) {
×
66
                            Thread.sleep(100);
×
67
                        }
68
                        runUpdateAfter = null;
×
69
                    }
70

71
                    ResourceData newData = new ResourceData();
×
72

73
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-view-displays", "resource", id)).getData()) {
×
74
                        if (space != null && !space.isAdminPubkey(r.get("pubkey"))) continue;
×
75
                        try {
76
                            newData.viewDisplays.add(ViewDisplay.get(r.get("display")));
×
77
                        } catch (IllegalArgumentException ex) {
×
78
                            logger.error("Couldn't generate view display object", ex);
×
79
                        }
×
80
                    }
×
81
                    data = newData;
×
82
                    dataInitialized = true;
×
83
                } catch (Exception ex) {
×
84
                    logger.error("Error while trying to update space data: {}", ex);
×
85
                    dataNeedsUpdate = true;
×
86
                }
×
87
            });
×
88
            thread.start();
×
89
            dataNeedsUpdate = false;
×
90
            return thread;
×
91
        }
92
        return null;
×
93
    }
94

95
    public void forceRefresh(long waitMillis) {
96
        dataNeedsUpdate = true;
×
97
        dataInitialized = false;
×
98
        runUpdateAfter = System.currentTimeMillis() + waitMillis;
×
99
    }
×
100

101
    public Long getRunUpdateAfter() {
102
        return runUpdateAfter;
×
103
    }
104

105
    public Space getSpace() {
106
        return space;
×
107
    }
108

109
    public String getNanopubId() {
110
        return null;
×
111
    }
112

113
    public Nanopub getNanopub() {
114
        return null;
×
115
    }
116

117
    public String getNamespace() {
118
        return null;
×
119
    }
120

121
    public void setDataNeedsUpdate() {
122
        dataNeedsUpdate = true;
×
123
    }
×
124

125
    public boolean isDataInitialized() {
126
        triggerDataUpdate();
×
127
        return dataInitialized;
×
128
    }
129

130
    public List<ViewDisplay> getViewDisplays() {
131
        return data.viewDisplays;
×
132
    }
133

134
    public List<ViewDisplay> getTopLevelViewDisplays() {
135
        return getViewDisplays(true, getId(), null);
×
136
    }
137

138
    public List<ViewDisplay> getPartLevelViewDisplays(String resourceId, Set<IRI> classes) {
139
        return getViewDisplays(false, resourceId, classes);
×
140
    }
141

142
    private List<ViewDisplay> getViewDisplays(boolean toplevel, String resourceId, Set<IRI> classes) {
143
        triggerDataUpdate();
×
144
        List<ViewDisplay> viewDisplays = new ArrayList<>();
×
145
        Set<IRI> viewKinds = new HashSet<>();
×
146

147
        for (ViewDisplay vd : getViewDisplays()) {
×
148
            IRI kind = vd.getViewKindIri();
×
149
            if (kind != null) {
×
150
                if (viewKinds.contains(kind)) continue;
×
151
                viewKinds.add(vd.getViewKindIri());
×
152
            }
153
            if (vd.hasType(KPXL_TERMS.DEACTIVATED_VIEW_DISPLAY)) continue;
×
154

155
            if (!toplevel && vd.hasType(KPXL_TERMS.TOP_LEVEL_VIEW_DISPLAY)) {
×
156
                // Deprecated
157
                // do nothing
158
            } else if (vd.appliesTo(resourceId, classes)) {
×
159
                viewDisplays.add(vd);
×
160
            } else if (toplevel && vd.hasType(KPXL_TERMS.TOP_LEVEL_VIEW_DISPLAY)) {
×
161
                // Deprecated
162
                viewDisplays.add(vd);
×
163
            }
164
        }
×
165

166
        Collections.sort(viewDisplays);
×
167
        return viewDisplays;
×
168
    }
169

170
    public String getLabel() {
171
        return space.getLabel();
×
172
    }
173

174
    @Override
175
    public String toString() {
176
        return id;
×
177
    }
178

179
    /**
180
     * Gets the chain of superspaces from the current space up to the root space.
181
     *
182
     * @return the list of superspaces from the given space to the root space
183
     */
184
    public List<ProfiledResource> getAllSuperSpacesUntilRoot() {
185
        List<ProfiledResource> chain = new ArrayList<>();
×
186
        Set<String> visited = new HashSet<>();
×
187
        collectAncestors(space, chain, visited);
×
188
        Collections.reverse(chain);
×
189
        return chain;
×
190
    }
191

192
    private void collectAncestors(Space current, List<ProfiledResource> chain, Set<String> visited) {
193
        if (current == null) {
×
194
            return;
×
195
        }
196
        List<Space> parents = current.getSuperspaces();
×
197
        if (parents == null || parents.isEmpty()) {
×
198
            return;
×
199
        }
200
        Space parent = parents.getFirst();
×
201
        if (parent == null) {
×
202
            return;
×
203
        }
204
        String pid = parent.getId();
×
205
        if (pid == null || !visited.add(pid)) {
×
206
            return;
×
207
        }
208
        chain.add(parent);
×
209
        collectAncestors(parent, chain, visited);
×
210
    }
×
211

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