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

knowledgepixels / nanodash / 18155160356

01 Oct 2025 07:42AM UTC coverage: 13.788% (-0.03%) from 13.817%
18155160356

push

github

ashleycaselli
docs: add missing or update Javadoc annotations

445 of 4084 branches covered (10.9%)

Branch coverage included in aggregate %.

1155 of 7520 relevant lines covered (15.36%)

0.69 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.extra.services.QueryRef;
11
import org.nanopub.vocabulary.NTEMPLATE;
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14

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

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

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

27
    private static TemplateData instance;
28

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

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

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

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

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

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

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

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

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

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

132
    /**
133
     * Returns a Template object for the template of the given Nanopub.
134
     *
135
     * @param np the Nanopub from which to extract the template
136
     * @return the Template object if found, or null if not found or invalid
137
     */
138
    public Template getTemplate(Nanopub np) {
139
        IRI templateId = getTemplateId(np);
×
140
        if (templateId == null) return null;
×
141
        return getTemplate(templateId.stringValue());
×
142
    }
143

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

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

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

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

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

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

235
    private static final TemplateComparator templateComparator = new TemplateComparator();
5✔
236

237
    private static class TemplateComparator implements Comparator<ApiResponseEntry>, Serializable {
238

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

251
    }
252

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