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

knowledgepixels / nanodash / 20299457275

17 Dec 2025 10:20AM UTC coverage: 14.401% (-0.9%) from 15.279%
20299457275

push

github

tkuhn
fix: Use API result cache for all requests

546 of 5004 branches covered (10.91%)

Branch coverage included in aggregate %.

1496 of 9176 relevant lines covered (16.3%)

2.13 hits per line

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

35.71
src/main/java/com/knowledgepixels/nanodash/template/TemplateData.java
1
package com.knowledgepixels.nanodash.template;
2

3
import java.io.Serializable;
4
import java.util.ArrayList;
5
import java.util.Comparator;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Set;
9
import java.util.concurrent.ConcurrentHashMap;
10
import java.util.concurrent.ConcurrentMap;
11

12
import org.eclipse.rdf4j.model.IRI;
13
import org.eclipse.rdf4j.model.Statement;
14
import org.nanopub.Nanopub;
15
import org.nanopub.extra.services.ApiResponse;
16
import org.nanopub.extra.services.ApiResponseEntry;
17
import org.nanopub.extra.services.QueryRef;
18
import org.nanopub.vocabulary.NTEMPLATE;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

22
import com.knowledgepixels.nanodash.ApiCache;
23

24
import net.trustyuri.TrustyUriUtils;
25

26
/**
27
 * Singleton class that manages templates data.
28
 */
29
public class TemplateData implements Serializable {
30

31
    private static final Logger logger = LoggerFactory.getLogger(TemplateData.class);
9✔
32

33
    private static TemplateData instance;
34

35
    /**
36
     * Refreshes the templates data by creating a new instance of TemplateData.
37
     */
38
    public static synchronized void refreshTemplates() {
39
        instance = new TemplateData();
12✔
40
    }
3✔
41

42
    /**
43
     * Ensures that the TemplateData instance is loaded.
44
     */
45
    public static synchronized void ensureLoaded() {
46
        if (instance == null) refreshTemplates();
9✔
47
    }
3✔
48

49
    /**
50
     * Gets the singleton instance of TemplateData.
51
     *
52
     * @return the TemplateData instance
53
     */
54
    public static TemplateData get() {
55
        ensureLoaded();
3✔
56
        return instance;
6✔
57
    }
58

59
    private List<ApiResponseEntry> assertionTemplates, provenanceTemplates, pubInfoTemplates;
60
    private ConcurrentMap<String, Template> templateMap;
61

62
    /**
63
     * Constructor to initialize the TemplateData instance.
64
     */
65
    public TemplateData() {
6✔
66
        assertionTemplates = new ArrayList<>();
15✔
67
        provenanceTemplates = new ArrayList<>();
15✔
68
        pubInfoTemplates = new ArrayList<>();
15✔
69
        templateMap = new ConcurrentHashMap<>();
15✔
70
        refreshTemplates(assertionTemplates, "get-assertion-templates");
15✔
71
        refreshTemplates(provenanceTemplates, "get-provenance-templates");
15✔
72
        refreshTemplates(pubInfoTemplates, "get-pubinfo-templates");
15✔
73
    }
3✔
74

75
    private void refreshTemplates(List<ApiResponseEntry> templates, String queryId) {
76
        ApiResponse templateEntries = ApiCache.retrieveResponseSync(new QueryRef(queryId), true);
21✔
77
        String previousId = null;
6✔
78
        logger.info("Loading templates...");
9✔
79
        for (ApiResponseEntry entry : templateEntries.getData()) {
33✔
80
            if ("true".equals(entry.get("unlisted"))) continue;
21✔
81
            if (!entry.get("np").equals(previousId)) {
18✔
82
                templates.add(entry);
12✔
83
            }
84
            previousId = entry.get("np");
12✔
85
        }
3✔
86
        templates.sort(templateComparator);
9✔
87
    }
3✔
88

89
    /**
90
     * Returns the list of assertion templates.
91
     *
92
     * @return a list of assertion templates
93
     */
94
    public List<ApiResponseEntry> getAssertionTemplates() {
95
        return assertionTemplates;
×
96
    }
97

98
    /**
99
     * Returns the list of provenance templates.
100
     *
101
     * @return a list of provenance templates
102
     */
103
    public List<ApiResponseEntry> getProvenanceTemplates() {
104
        return provenanceTemplates;
×
105
    }
106

107
    /**
108
     * Returns the list of publication information templates.
109
     *
110
     * @return a list of publication information templates
111
     */
112
    public List<ApiResponseEntry> getPubInfoTemplates() {
113
        return pubInfoTemplates;
×
114
    }
115

116
    /**
117
     * Returns a Template object for the given template ID.
118
     *
119
     * @param id the ID of the template
120
     * @return the Template object if found, or null if not found or invalid
121
     */
122
    public Template getTemplate(String id) {
123
        Template template = templateMap.get(id);
18✔
124
        if (template != null) return template;
6!
125
        if (TrustyUriUtils.isPotentialTrustyUri(id)) {
9!
126
            try {
127
                Template t = new Template(id);
15✔
128
                templateMap.put(id, t);
18✔
129
                return t;
6✔
130
            } catch (Exception ex) {
×
131
                logger.error("Exception: {}", ex.getMessage());
×
132
                return null;
×
133
            }
134
        }
135
        return null;
×
136
    }
137

138
    /**
139
     * Returns a Template object for the template of the given Nanopub.
140
     *
141
     * @param np the Nanopub from which to extract the template
142
     * @return the Template object if found, or null if not found or invalid
143
     */
144
    public Template getTemplate(Nanopub np) {
145
        IRI templateId = getTemplateId(np);
×
146
        if (templateId == null) return null;
×
147
        return getTemplate(templateId.stringValue());
×
148
    }
149

150
    /**
151
     * Returns a Template object for the provenance template of the given Nanopub.
152
     *
153
     * @param np the Nanopub from which to extract the provenance template
154
     * @return the Template object if found, or null if not found or invalid
155
     */
156
    public Template getProvenanceTemplate(Nanopub np) {
157
        IRI templateId = getProvenanceTemplateId(np);
×
158
        if (templateId == null) return null;
×
159
        return getTemplate(templateId.stringValue());
×
160
    }
161

162
    /**
163
     * Returns a set of Template objects for the publication information templates of the given Nanopub.
164
     *
165
     * @param np the Nanopub from which to extract the publication information templates
166
     * @return a set of Template objects
167
     */
168
    public Set<Template> getPubinfoTemplates(Nanopub np) {
169
        Set<Template> templates = new HashSet<>();
×
170
        for (IRI id : getPubinfoTemplateIds(np)) {
×
171
            templates.add(getTemplate(id.stringValue()));
×
172
        }
×
173
        return templates;
×
174
    }
175

176
    /**
177
     * Returns the template ID of the given Nanopub.
178
     *
179
     * @param nanopub the Nanopub from which to extract the template ID
180
     * @return the IRI of the template ID, or null if not found
181
     */
182
    public IRI getTemplateId(Nanopub nanopub) {
183
        for (Statement st : nanopub.getPubinfo()) {
×
184
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
185
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_TEMPLATE)) continue;
×
186
            if (!(st.getObject() instanceof IRI)) continue;
×
187
            return (IRI) st.getObject();
×
188
        }
189
        return null;
×
190
    }
191

192
    /**
193
     * Returns the provenance template ID of the given Nanopub.
194
     *
195
     * @param nanopub the Nanopub from which to extract the provenance template ID
196
     * @return the IRI of the provenance template ID, or null if not found
197
     */
198
    public IRI getProvenanceTemplateId(Nanopub nanopub) {
199
        for (Statement st : nanopub.getPubinfo()) {
×
200
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
201
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_PROVENANCE_TEMPLATE)) continue;
×
202
            if (!(st.getObject() instanceof IRI)) continue;
×
203
            return (IRI) st.getObject();
×
204
        }
205
        return null;
×
206
    }
207

208
    /**
209
     * Returns the set of publication information template IDs for the given Nanopub.
210
     *
211
     * @param nanopub the Nanopub from which to extract the publication information template IDs
212
     * @return a set of IRI objects representing the publication information template IDs
213
     */
214
    public Set<IRI> getPubinfoTemplateIds(Nanopub nanopub) {
215
        Set<IRI> iriSet = new HashSet<>();
×
216
        for (Statement st : nanopub.getPubinfo()) {
×
217
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
218
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_PUBINFO_TEMPLATE)) continue;
×
219
            if (!(st.getObject() instanceof IRI)) continue;
×
220
            iriSet.add((IRI) st.getObject());
×
221
        }
×
222
        return iriSet;
×
223
    }
224

225
    /**
226
     * Returns a list of Template objects from the given ApiResponse.
227
     *
228
     * @param apiResponse the ApiResponse containing template entries
229
     * @return a list of Template objects
230
     */
231
    public static List<Template> getTemplateList(ApiResponse apiResponse) {
232
        List<Template> templates = new ArrayList<>();
×
233
        for (ApiResponseEntry e : apiResponse.getData()) {
×
234
            String templateNpId = e.get("template_np");
×
235
            if (templateNpId == null) templateNpId = e.get("np");
×
236
            templates.add(TemplateData.get().getTemplate(templateNpId));
×
237
        }
×
238
        return templates;
×
239
    }
240

241
    private static final TemplateComparator templateComparator = new TemplateComparator();
15✔
242

243
    private static class TemplateComparator implements Comparator<ApiResponseEntry>, Serializable {
244

245
        /**
246
         * Compares two Template objects based on their labels.
247
         *
248
         * @param o1 the first object to be compared.
249
         * @param o2 the second object to be compared.
250
         * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
251
         */
252
        @Override
253
        public int compare(ApiResponseEntry o1, ApiResponseEntry o2) {
254
            return o1.get("label").compareTo(o2.get("label"));
24✔
255
        }
256

257
    }
258

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