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

knowledgepixels / nanodash / 17852532121

19 Sep 2025 08:10AM UTC coverage: 13.568% (-0.3%) from 13.87%
17852532121

push

github

tkuhn
feat: Switch to QueryRef provided by nanopub, using multimap

428 of 4008 branches covered (10.68%)

Branch coverage included in aggregate %.

1104 of 7283 relevant lines covered (15.16%)

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 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.QueryApiAccess;
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);
3✔
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();
4✔
40
    }
1✔
41

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

49
    /**
50
     * Gets the singleton instance of TemplateData.
51
     *
52
     * @return the TemplateData instance
53
     */
54
    public static TemplateData get() {
55
        ensureLoaded();
1✔
56
        return instance;
2✔
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() {
2✔
66
        assertionTemplates = new ArrayList<>();
5✔
67
        provenanceTemplates = new ArrayList<>();
5✔
68
        pubInfoTemplates = new ArrayList<>();
5✔
69
        templateMap = new ConcurrentHashMap<>();
5✔
70
        refreshTemplates(assertionTemplates, "get-assertion-templates");
5✔
71
        refreshTemplates(provenanceTemplates, "get-provenance-templates");
5✔
72
        refreshTemplates(pubInfoTemplates, "get-pubinfo-templates");
5✔
73
    }
1✔
74

75
    private void refreshTemplates(List<ApiResponseEntry> templates, String queryId) {
76
        ApiResponse templateEntries = QueryApiAccess.forcedGet(new QueryRef(queryId));
6✔
77
        String previousId = null;
2✔
78
        logger.info("Loading templates...");
3✔
79
        for (ApiResponseEntry entry : templateEntries.getData()) {
11✔
80
            if ("true".equals(entry.get("unlisted"))) continue;
7✔
81
            if (!entry.get("np").equals(previousId)) {
6✔
82
                templates.add(entry);
4✔
83
            }
84
            previousId = entry.get("np");
4✔
85
        }
1✔
86
        templates.sort(templateComparator);
3✔
87
    }
1✔
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);
×
124
        if (template != null) return template;
×
125
        if (TrustyUriUtils.isPotentialTrustyUri(id)) {
×
126
            try {
127
                Template t = new Template(id);
×
128
                templateMap.put(id, t);
×
129
                return t;
×
130
            } catch (Exception ex) {
×
131
                logger.error("Exception: {}", ex.getMessage());
×
132
                return null;
×
133
            }
134
        }
135
        return null;
×
136
    }
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
    public static List<Template> getTemplateList(ApiResponse apiResponse) {
221
        List<Template> templates = new ArrayList<>();
×
222
        for (ApiResponseEntry e : apiResponse.getData()) {
×
223
            String templateNpId = e.get("template_np");
×
224
            if (templateNpId == null) templateNpId = e.get("np");
×
225
            templates.add(TemplateData.get().getTemplate(templateNpId));
×
226
        }
×
227
        return templates;
×
228
    }
229

230
    private static final TemplateComparator templateComparator = new TemplateComparator();
5✔
231

232
    private static class TemplateComparator implements Comparator<ApiResponseEntry>, Serializable {
233

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

246
    }
247

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