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

knowledgepixels / nanodash / 17211438187

25 Aug 2025 02:13PM UTC coverage: 12.474% (+0.03%) from 12.446%
17211438187

push

github

ashleycaselli
build(deps): update org.apache.maven.plugins:maven-compiler-plugin to v3.14.0

330 of 3766 branches covered (8.76%)

Branch coverage included in aggregate %.

982 of 6752 relevant lines covered (14.54%)

0.64 hits per line

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

32.17
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.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12

13
import java.io.Serializable;
14
import java.util.*;
15

16
/**
17
 * Singleton class that manages templates data.
18
 */
19
public class TemplateData implements Serializable {
20

21
    private static final long serialVersionUID = 1L;
22
    private static final Logger logger = LoggerFactory.getLogger(TemplateData.class);
3✔
23

24
    private static TemplateData instance;
25

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

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

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

50
    private List<ApiResponseEntry> assertionTemplates, provenanceTemplates, pubInfoTemplates;
51
    private Map<String, Template> templateMap;
52

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

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

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

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

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

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

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

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

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

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

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

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

210

211
    private static final TemplateComparator templateComparator = new TemplateComparator();
5✔
212

213
    private static class TemplateComparator implements Comparator<ApiResponseEntry>, Serializable {
214

215
        private static final long serialVersionUID = 1L;
216

217
        /**
218
         * Compares two Template objects based on their labels.
219
         *
220
         * @param o1 the first object to be compared.
221
         * @param o2 the second object to be compared.
222
         * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
223
         */
224
        @Override
225
        public int compare(ApiResponseEntry o1, ApiResponseEntry o2) {
226
            return o1.get("label").compareTo(o2.get("label"));
8✔
227
        }
228

229
    }
230

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