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

knowledgepixels / nanodash / 19964803313

05 Dec 2025 01:42PM UTC coverage: 15.393% (+0.02%) from 15.37%
19964803313

push

github

tkuhn
fix(ViewDisplay): Correctly consider gen:appliesTo values

594 of 4974 branches covered (11.94%)

Branch coverage included in aggregate %.

1570 of 9084 relevant lines covered (17.28%)

0.77 hits per line

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

16.2
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);
3✔
17

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

22
    private static List<ProfiledResource> instances = new ArrayList<>();
5✔
23

24
    public static void refresh() {
25
        for (ProfiledResource r : instances) {
10✔
26
            r.setDataNeedsUpdate();
2✔
27
        }
1✔
28
    }
1✔
29

30
    private String id;
31
    private Space space;
32
    private ResourceData data = new ResourceData();
5✔
33
    private boolean dataInitialized = false;
3✔
34
    private boolean dataNeedsUpdate = true;
3✔
35
    private Long runUpdateAfter = null;
3✔
36

37
    protected ProfiledResource(String id) {
2✔
38
        this.id = id;
3✔
39
        instances.add(this);
4✔
40
    }
1✔
41

42
    protected void initSpace(Space space) {
43
        this.space = space;
3✔
44
    }
1✔
45

46
    public String getId() {
47
        return id;
3✔
48
    }
49

50
    protected synchronized Thread triggerDataUpdate() {
51
        if (dataNeedsUpdate) {
×
52
            Thread thread = new Thread(() -> {
×
53
                try {
54
                    if (runUpdateAfter != null) {
×
55
                        while (System.currentTimeMillis() < runUpdateAfter) {
×
56
                            Thread.sleep(100);
×
57
                        }
58
                        runUpdateAfter = null;
×
59
                    }
60

61
                    ResourceData newData = new ResourceData();
×
62

63
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-view-displays", "resource", id)).getData()) {
×
64
                        if (space != null && !space.isAdminPubkey(r.get("pubkey"))) continue;
×
65
                        try {
66
                            newData.viewDisplays.add(ViewDisplay.get(r.get("display")));
×
67
                        } catch (IllegalArgumentException ex) {
×
68
                            logger.error("Couldn't generate view display object", ex);
×
69
                        }
×
70
                    }
×
71
                    data = newData;
×
72
                    dataInitialized = true;
×
73
                } catch (Exception ex) {
×
74
                    logger.error("Error while trying to update space data: {}", ex);
×
75
                    dataNeedsUpdate = true;
×
76
                }
×
77
            });
×
78
            thread.start();
×
79
            dataNeedsUpdate = false;
×
80
            return thread;
×
81
        }
82
        return null;
×
83
    }
84

85
    public void forceRefresh(long waitMillis) {
86
        dataNeedsUpdate = true;
×
87
        dataInitialized = false;
×
88
        runUpdateAfter = System.currentTimeMillis() + waitMillis;
×
89
    }
×
90

91
    public Long getRunUpdateAfter() {
92
        return runUpdateAfter;
×
93
    }
94

95
    public Space getSpace() {
96
        return space;
×
97
    }
98

99
    public String getNanopubId() {
100
        return null;
×
101
    }
102

103
    public Nanopub getNanopub() {
104
        return null;
×
105
    }
106

107
    public String getNamespace() {
108
        return null;
×
109
    }
110

111
    public void setDataNeedsUpdate() {
112
        dataNeedsUpdate = true;
3✔
113
    }
1✔
114

115
    public boolean isDataInitialized() {
116
        triggerDataUpdate();
×
117
        return dataInitialized;
×
118
    }
119

120
    public List<ViewDisplay> getViewDisplays() {
121
        return data.viewDisplays;
×
122
    }
123

124
    public List<ViewDisplay> getTopLevelViewDisplays() {
125
        return getViewDisplays(true, getId(), null);
×
126
    }
127

128
    public List<ViewDisplay> getPartLevelViewDisplays(String resourceId, Set<IRI> classes) {
129
        return getViewDisplays(false, resourceId, classes);
×
130
    }
131

132
    private List<ViewDisplay> getViewDisplays(boolean toplevel, String resourceId, Set<IRI> classes) {
133
        triggerDataUpdate();
×
134
        List<ViewDisplay> viewDisplays = new ArrayList<>();
×
135
        Set<IRI> viewKinds = new HashSet<>();
×
136

137
        for (ViewDisplay vd : getViewDisplays()) {
×
138
            IRI kind = vd.getViewKindIri();
×
139
            if (kind != null) {
×
140
                if (viewKinds.contains(kind)) continue;
×
141
                viewKinds.add(vd.getViewKindIri());
×
142
            }
143
            if (vd.hasType(KPXL_TERMS.DEACTIVATED_VIEW_DISPLAY)) continue;
×
144

145
            if (!toplevel && vd.hasType(KPXL_TERMS.TOP_LEVEL_VIEW_DISPLAY)) {
×
146
                // Deprecated
147
                // do nothing
148
            } else if (vd.appliesTo(resourceId, classes)) {
×
149
                viewDisplays.add(vd);
×
150
            } else if (toplevel && vd.hasType(KPXL_TERMS.TOP_LEVEL_VIEW_DISPLAY)) {
×
151
                // Deprecated
152
                viewDisplays.add(vd);
×
153
            }
154
        }
×
155

156
        Collections.sort(viewDisplays);
×
157
        return viewDisplays;
×
158
    }
159

160
    public String getLabel() {
161
        return space.getLabel();
×
162
    }
163

164
    @Override
165
    public String toString() {
166
        return id;
×
167
    }
168

169
    /**
170
     * Gets the chain of superspaces from the current space up to the root space.
171
     *
172
     * @return the list of superspaces from the given space to the root space
173
     */
174
    public List<ProfiledResource> getAllSuperSpacesUntilRoot() {
175
        List<ProfiledResource> chain = new ArrayList<>();
×
176
        Set<String> visited = new HashSet<>();
×
177
        collectAncestors(space, chain, visited);
×
178
        Collections.reverse(chain);
×
179
        return chain;
×
180
    }
181

182
    private void collectAncestors(Space current, List<ProfiledResource> chain, Set<String> visited) {
183
        if (current == null) {
×
184
            return;
×
185
        }
186
        List<Space> parents = current.getSuperspaces();
×
187
        if (parents == null || parents.isEmpty()) {
×
188
            return;
×
189
        }
190
        Space parent = parents.getFirst();
×
191
        if (parent == null) {
×
192
            return;
×
193
        }
194
        String pid = parent.getId();
×
195
        if (pid == null || !visited.add(pid)) {
×
196
            return;
×
197
        }
198
        chain.add(parent);
×
199
        collectAncestors(parent, chain, visited);
×
200
    }
×
201

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