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

knowledgepixels / nanodash / 17837235071

18 Sep 2025 05:58PM UTC coverage: 13.87%. Remained the same
17837235071

push

github

tkuhn
chore: Remove serialVersionUIDs

443 of 4022 branches covered (11.01%)

Branch coverage included in aggregate %.

1133 of 7341 relevant lines covered (15.43%)

0.68 hits per line

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

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

3
import com.knowledgepixels.nanodash.QueryApiAccess;
4
import net.trustyuri.TrustyUriUtils;
5
import org.eclipse.rdf4j.model.IRI;
6
import org.eclipse.rdf4j.model.Statement;
7
import org.nanopub.Nanopub;
8
import org.nanopub.extra.services.ApiResponse;
9
import org.nanopub.extra.services.ApiResponseEntry;
10
import org.nanopub.vocabulary.NTEMPLATE;
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13

14
import java.io.Serializable;
15
import java.util.*;
16
import java.util.concurrent.ConcurrentHashMap;
17
import java.util.concurrent.ConcurrentMap;
18

19
/**
20
 * Singleton class that manages templates data.
21
 */
22
public class TemplateData implements Serializable {
23

24
    private static final Logger logger = LoggerFactory.getLogger(TemplateData.class);
3✔
25

26
    private static TemplateData instance;
27

28
    /**
29
     * Refreshes the templates data by creating a new instance of TemplateData.
30
     */
31
    public static synchronized void refreshTemplates() {
32
        instance = new TemplateData();
4✔
33
    }
1✔
34

35
    /**
36
     * Ensures that the TemplateData instance is loaded.
37
     */
38
    public static synchronized void ensureLoaded() {
39
        if (instance == null) refreshTemplates();
3✔
40
    }
1✔
41

42
    /**
43
     * Gets the singleton instance of TemplateData.
44
     *
45
     * @return the TemplateData instance
46
     */
47
    public static TemplateData get() {
48
        ensureLoaded();
1✔
49
        return instance;
2✔
50
    }
51

52
    private List<ApiResponseEntry> assertionTemplates, provenanceTemplates, pubInfoTemplates;
53
    private ConcurrentMap<String, Template> templateMap;
54

55
    /**
56
     * Constructor to initialize the TemplateData instance.
57
     */
58
    public TemplateData() {
2✔
59
        assertionTemplates = new ArrayList<>();
5✔
60
        provenanceTemplates = new ArrayList<>();
5✔
61
        pubInfoTemplates = new ArrayList<>();
5✔
62
        templateMap = new ConcurrentHashMap<>();
5✔
63
        refreshTemplates(assertionTemplates, "get-assertion-templates");
5✔
64
        refreshTemplates(provenanceTemplates, "get-provenance-templates");
5✔
65
        refreshTemplates(pubInfoTemplates, "get-pubinfo-templates");
5✔
66
    }
1✔
67

68
    private void refreshTemplates(List<ApiResponseEntry> templates, String queryId) {
69
        ApiResponse templateEntries = QueryApiAccess.forcedGet(queryId);
3✔
70
        String previousId = null;
2✔
71
        logger.info("Loading templates...");
3✔
72
        for (ApiResponseEntry entry : templateEntries.getData()) {
11✔
73
            if ("true".equals(entry.get("unlisted"))) continue;
7✔
74
            if (!entry.get("np").equals(previousId)) {
6✔
75
                templates.add(entry);
4✔
76
            }
77
            previousId = entry.get("np");
4✔
78
        }
1✔
79
        templates.sort(templateComparator);
3✔
80
    }
1✔
81

82
    /**
83
     * Returns the list of assertion templates.
84
     *
85
     * @return a list of assertion templates
86
     */
87
    public List<ApiResponseEntry> getAssertionTemplates() {
88
        return assertionTemplates;
×
89
    }
90

91
    /**
92
     * Returns the list of provenance templates.
93
     *
94
     * @return a list of provenance templates
95
     */
96
    public List<ApiResponseEntry> getProvenanceTemplates() {
97
        return provenanceTemplates;
×
98
    }
99

100
    /**
101
     * Returns the list of publication information templates.
102
     *
103
     * @return a list of publication information templates
104
     */
105
    public List<ApiResponseEntry> getPubInfoTemplates() {
106
        return pubInfoTemplates;
×
107
    }
108

109
    /**
110
     * Returns a Template object for the given template ID.
111
     *
112
     * @param id the ID of the template
113
     * @return the Template object if found, or null if not found or invalid
114
     */
115
    public Template getTemplate(String id) {
116
        Template template = templateMap.get(id);
×
117
        if (template != null) return template;
×
118
        if (TrustyUriUtils.isPotentialTrustyUri(id)) {
×
119
            try {
120
                Template t = new Template(id);
×
121
                templateMap.put(id, t);
×
122
                return t;
×
123
            } catch (Exception ex) {
×
124
                logger.error("Exception: {}", ex.getMessage());
×
125
                return null;
×
126
            }
127
        }
128
        return null;
×
129
    }
130

131
    public Template getTemplate(Nanopub np) {
132
        IRI templateId = getTemplateId(np);
×
133
        if (templateId == null) return null;
×
134
        return getTemplate(templateId.stringValue());
×
135
    }
136

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

149
    /**
150
     * Returns a set of Template objects for the publication information templates of the given Nanopub.
151
     *
152
     * @param np the Nanopub from which to extract the publication information templates
153
     * @return a set of Template objects
154
     */
155
    public Set<Template> getPubinfoTemplates(Nanopub np) {
156
        Set<Template> templates = new HashSet<>();
×
157
        for (IRI id : getPubinfoTemplateIds(np)) {
×
158
            templates.add(getTemplate(id.stringValue()));
×
159
        }
×
160
        return templates;
×
161
    }
162

163
    /**
164
     * Returns the template ID of the given Nanopub.
165
     *
166
     * @param nanopub the Nanopub from which to extract the template ID
167
     * @return the IRI of the template ID, or null if not found
168
     */
169
    public IRI getTemplateId(Nanopub nanopub) {
170
        for (Statement st : nanopub.getPubinfo()) {
×
171
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
172
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_TEMPLATE)) continue;
×
173
            if (!(st.getObject() instanceof IRI)) continue;
×
174
            return (IRI) st.getObject();
×
175
        }
176
        return null;
×
177
    }
178

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

195
    /**
196
     * Returns the set of publication information template IDs for the given Nanopub.
197
     *
198
     * @param nanopub the Nanopub from which to extract the publication information template IDs
199
     * @return a set of IRI objects representing the publication information template IDs
200
     */
201
    public Set<IRI> getPubinfoTemplateIds(Nanopub nanopub) {
202
        Set<IRI> iriSet = new HashSet<>();
×
203
        for (Statement st : nanopub.getPubinfo()) {
×
204
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
205
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_PUBINFO_TEMPLATE)) continue;
×
206
            if (!(st.getObject() instanceof IRI)) continue;
×
207
            iriSet.add((IRI) st.getObject());
×
208
        }
×
209
        return iriSet;
×
210
    }
211

212

213
    public static List<Template> getTemplateList(ApiResponse apiResponse) {
214
        List<Template> templates = new ArrayList<>();
×
215
        for (ApiResponseEntry e : apiResponse.getData()) {
×
216
            String templateNpId = e.get("template_np");
×
217
            if (templateNpId == null) templateNpId = e.get("np");
×
218
            templates.add(TemplateData.get().getTemplate(templateNpId));
×
219
        }
×
220
        return templates;
×
221
    }
222

223
    private static final TemplateComparator templateComparator = new TemplateComparator();
5✔
224

225
    private static class TemplateComparator implements Comparator<ApiResponseEntry>, Serializable {
226

227
        /**
228
         * Compares two Template objects based on their labels.
229
         *
230
         * @param o1 the first object to be compared.
231
         * @param o2 the second object to be compared.
232
         * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
233
         */
234
        @Override
235
        public int compare(ApiResponseEntry o1, ApiResponseEntry o2) {
236
            return o1.get("label").compareTo(o2.get("label"));
8✔
237
        }
238

239
    }
240

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