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

knowledgepixels / nanodash / 19369164032

14 Nov 2025 03:23PM UTC coverage: 13.303% (-0.8%) from 14.107%
19369164032

push

github

tkuhn
feat(SpacePage): Show source links also for roles

502 of 4788 branches covered (10.48%)

Branch coverage included in aggregate %.

1309 of 8825 relevant lines covered (14.83%)

0.66 hits per line

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

0.0
src/main/java/com/knowledgepixels/nanodash/Project.java
1
package com.knowledgepixels.nanodash;
2

3
import com.knowledgepixels.nanodash.template.Template;
4
import com.knowledgepixels.nanodash.template.TemplateData;
5
import org.eclipse.rdf4j.model.IRI;
6
import org.eclipse.rdf4j.model.Literal;
7
import org.eclipse.rdf4j.model.Statement;
8
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
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.nanopub.vocabulary.NTEMPLATE;
14

15
import java.io.Serializable;
16
import java.util.ArrayList;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.Set;
20
import java.util.concurrent.ConcurrentHashMap;
21
import java.util.concurrent.ConcurrentMap;
22

23
import static com.knowledgepixels.nanodash.Utils.vf;
24

25
/**
26
 * Class representing a Nanodash project.
27
 */
28
public class Project implements Serializable {
29

30
    private static List<Project> projectList = null;
×
31
    private static ConcurrentMap<String, Project> projectsByCoreInfo = new ConcurrentHashMap<>();
×
32
    private static ConcurrentMap<String, Project> projectsById = new ConcurrentHashMap<>();
×
33

34
    /**
35
     * Refresh the list of projects from the given API response.
36
     *
37
     * @param resp The API response containing project data.
38
     */
39
    public static synchronized void refresh(ApiResponse resp) {
40
        projectList = new ArrayList<>();
×
41
        ConcurrentMap<String, Project> prevProjectsByCoreInfoPrev = projectsByCoreInfo;
×
42
        projectsByCoreInfo = new ConcurrentHashMap<>();
×
43
        projectsById.clear();
×
44
        for (ApiResponseEntry entry : resp.getData()) {
×
45
            Project project = new Project(entry.get("project"), entry.get("label"), entry.get("np"));
×
46
            Project prevProject = prevProjectsByCoreInfoPrev.get(project.getCoreInfoString());
×
47
            if (prevProject != null) project = prevProject;
×
48
            projectList.add(project);
×
49
            projectsByCoreInfo.put(project.getCoreInfoString(), project);
×
50
            projectsById.put(project.getId(), project);
×
51
        }
×
52
    }
×
53

54
    /**
55
     * Ensure that the project list is loaded. If not, it fetches the data from the API.
56
     */
57
    public static void ensureLoaded() {
58
        if (projectList == null) {
×
59
            refresh(QueryApiAccess.forcedGet(new QueryRef("get-projects")));
×
60
        }
61
    }
×
62

63
    /**
64
     * Get the list of all projects.
65
     */
66
    public static List<Project> getProjectList() {
67
        ensureLoaded();
×
68
        return projectList;
×
69
    }
70

71
    /**
72
     * Get a project by its ID.
73
     *
74
     * @param id The ID of the project.
75
     * @return The project with the given ID, or null if not found.
76
     */
77
    public static Project get(String id) {
78
        ensureLoaded();
×
79
        return projectsById.get(id);
×
80
    }
81

82
    /**
83
     * Mark all projects as needing data refresh.
84
     */
85
    public static void refresh() {
86
        ensureLoaded();
×
87
        for (Project project : projectList) {
×
88
            project.dataNeedsUpdate = true;
×
89
        }
×
90
    }
×
91

92
    private String id, label, rootNanopubId;
93
    private Nanopub rootNanopub = null;
×
94

95
    private String description = null;
×
96
    private List<IRI> owners = new ArrayList<>();
×
97
    private List<IRI> members = new ArrayList<>();
×
98
    private ConcurrentMap<String, IRI> ownerPubkeyMap = new ConcurrentHashMap<>();
×
99
    private List<Template> templates = new ArrayList<>();
×
100
    private Set<String> templateTags = new HashSet<>();
×
101
    private ConcurrentMap<String, List<Template>> templatesPerTag = new ConcurrentHashMap<>();
×
102
    private List<IRI> queryIds = new ArrayList<>();
×
103
    private IRI defaultProvenance = null;
×
104

105
    private boolean dataInitialized = false;
×
106
    private boolean dataNeedsUpdate = true;
×
107

108
    public static final IRI HAS_OWNER = vf.createIRI("https://w3id.org/kpxl/gen/terms/hasOwner");
×
109

110
    private Project(String id, String label, String rootNanopubId) {
×
111
        this.id = id;
×
112
        this.label = label;
×
113
        this.rootNanopubId = rootNanopubId;
×
114
        this.rootNanopub = Utils.getAsNanopub(rootNanopubId);
×
115

116
        for (Statement st : rootNanopub.getAssertion()) {
×
117
            if (st.getSubject().stringValue().equals(getId())) {
×
118
                if (st.getPredicate().equals(DCTERMS.DESCRIPTION)) {
×
119
                    description = st.getObject().stringValue();
×
120
                } else if (st.getPredicate().equals(HAS_OWNER) && st.getObject() instanceof IRI obj) {
×
121
                    addOwner(obj);
×
122
                } else if (st.getPredicate().equals(Space.HAS_PINNED_TEMPLATE) && st.getObject() instanceof IRI obj) {
×
123
                    templates.add(TemplateData.get().getTemplate(obj.stringValue()));
×
124
                } else if (st.getPredicate().equals(Space.HAS_PINNED_QUERY) && st.getObject() instanceof IRI obj) {
×
125
                    queryIds.add(obj);
×
126
                } else if (st.getPredicate().equals(NTEMPLATE.HAS_DEFAULT_PROVENANCE) && st.getObject() instanceof IRI obj) {
×
127
                    defaultProvenance = obj;
×
128
                }
129
            } else if (st.getPredicate().equals(NTEMPLATE.HAS_TAG) && st.getObject() instanceof Literal l) {
×
130
                templateTags.add(l.stringValue());
×
131
                List<Template> list = templatesPerTag.get(l.stringValue());
×
132
                if (list == null) {
×
133
                    list = new ArrayList<>();
×
134
                    templatesPerTag.put(l.stringValue(), list);
×
135
                }
136
                list.add(TemplateData.get().getTemplate(st.getSubject().stringValue()));
×
137
            }
138
        }
×
139

140
    }
×
141

142
    private void addOwner(IRI owner) {
143
        // TODO This isn't efficient for long owner lists:
144
        if (owners.contains(owner)) return;
×
145
        owners.add(owner);
×
146
        UserData ud = User.getUserData();
×
147
        for (String pubkeyhash : ud.getPubkeyhashes(owner, true)) {
×
148
            ownerPubkeyMap.put(pubkeyhash, owner);
×
149
        }
×
150
    }
×
151

152
    /**
153
     * Get the ID of the project.
154
     *
155
     * @return The project ID.
156
     */
157
    public String getId() {
158
        return id;
×
159
    }
160

161
    /**
162
     * Get the root nanopublication ID of the project.
163
     *
164
     * @return The root nanopublication ID.
165
     */
166
    public String getRootNanopubId() {
167
        return rootNanopubId;
×
168
    }
169

170
    /**
171
     * Get a string containing the core information of the project (ID and root nanopub ID).
172
     *
173
     * @return A string with the project ID and root nanopub ID.
174
     */
175
    public String getCoreInfoString() {
176
        return id + " " + rootNanopubId;
×
177
    }
178

179
    /**
180
     * Get the root nanopublication of the project.
181
     *
182
     * @return The root nanopublication.
183
     */
184
    public Nanopub getRootNanopub() {
185
        return rootNanopub;
×
186
    }
187

188
    /**
189
     * Get the label of the project.
190
     *
191
     * @return The project label.
192
     */
193
    public String getLabel() {
194
        return label;
×
195
    }
196

197
    /**
198
     * Get the description of the project.
199
     *
200
     * @return The project description.
201
     */
202
    public String getDescription() {
203
        return description;
×
204
    }
205

206
    /**
207
     * Check if the project data has been initialized.
208
     *
209
     * @return True if the data is initialized, false otherwise.
210
     */
211
    public boolean isDataInitialized() {
212
        triggerDataUpdate();
×
213
        return dataInitialized;
×
214
    }
215

216
    /**
217
     * Get the list of owners of the project.
218
     *
219
     * @return A list of IRIs representing the owners.
220
     */
221
    public List<IRI> getOwners() {
222
        triggerDataUpdate();
×
223
        return owners;
×
224
    }
225

226
    /**
227
     * Get the list of members of the project.
228
     *
229
     * @return A list of IRIs representing the members.
230
     */
231
    public List<IRI> getMembers() {
232
        triggerDataUpdate();
×
233
        return members;
×
234
    }
235

236
    /**
237
     * Get the list of templates associated with the project.
238
     *
239
     * @return A list of templates.
240
     */
241
    public List<Template> getTemplates() {
242
        return templates;
×
243
    }
244

245
    /**
246
     * Get the set of template tags associated with the project.
247
     *
248
     * @return A set of template tags.
249
     */
250
    public Set<String> getTemplateTags() {
251
        return templateTags;
×
252
    }
253

254
    /**
255
     * Get a map of templates categorized by their tags.
256
     *
257
     * @return A concurrent map where keys are tags and values are lists of templates.
258
     */
259
    public ConcurrentMap<String, List<Template>> getTemplatesPerTag() {
260
        return templatesPerTag;
×
261
    }
262

263
    /**
264
     * Get the list of query IDs associated with the project.
265
     *
266
     * @return A list of IRIs representing the query IDs.
267
     */
268
    public List<IRI> getQueryIds() {
269
        return queryIds;
×
270
    }
271

272
    /**
273
     * Get the default provenance IRI for the project.
274
     *
275
     * @return The default provenance IRI.
276
     */
277
    public IRI getDefaultProvenance() {
278
        return defaultProvenance;
×
279
    }
280

281
    private synchronized void triggerDataUpdate() {
282
        if (dataNeedsUpdate) {
×
283
            new Thread(() -> {
×
284
                for (ApiResponseEntry r : QueryApiAccess.forcedGet(new QueryRef("get-owners", "unit", id)).getData()) {
×
285
                    String pubkeyhash = r.get("pubkeyhash");
×
286
                    if (ownerPubkeyMap.containsKey(pubkeyhash)) {
×
287
                        addOwner(Utils.vf.createIRI(r.get("owner")));
×
288
                    }
289
                }
×
290
                members = new ArrayList<>();
×
291
                for (ApiResponseEntry r : QueryApiAccess.forcedGet(new QueryRef("get-members", "unit", id)).getData()) {
×
292
                    IRI memberId = Utils.vf.createIRI(r.get("member"));
×
293
                    // TODO These checks are inefficient for long member lists:
294
                    if (owners.contains(memberId)) continue;
×
295
                    if (members.contains(memberId)) continue;
×
296
                    members.add(memberId);
×
297
                }
×
298
                owners.sort(User.getUserData().userComparator);
×
299
                members.sort(User.getUserData().userComparator);
×
300
                dataInitialized = true;
×
301
            }).start();
×
302
            dataNeedsUpdate = false;
×
303
        }
304
    }
×
305

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