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

knowledgepixels / nanodash / 22618039211

03 Mar 2026 10:05AM UTC coverage: 16.058% (+0.2%) from 15.884%
22618039211

Pull #365

github

web-flow
Merge 1e7e700f0 into a8c4b4a77
Pull Request #365: Refactor of `ResourceWithProfile` and related classes

699 of 5287 branches covered (13.22%)

Branch coverage included in aggregate %.

1721 of 9783 relevant lines covered (17.59%)

2.41 hits per line

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

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

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

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

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

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

28
    private static TemplateData instance;
29

30
    /**
31
     * Refreshes the templates data by creating a new instance of TemplateData.
32
     */
33
    public static synchronized void refreshTemplates() {
34
        logger.info("Refreshing templates...");
9✔
35
        instance = new TemplateData();
12✔
36
    }
3✔
37

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

45
    /**
46
     * Gets the singleton instance of TemplateData.
47
     *
48
     * @return the TemplateData instance
49
     */
50
    public static TemplateData get() {
51
        ensureLoaded();
3✔
52
        return instance;
6✔
53
    }
54

55
    private List<ApiResponseEntry> assertionTemplates, provenanceTemplates, pubInfoTemplates;
56
    private ConcurrentMap<String, Template> templateMap;
57

58
    /**
59
     * Constructor to initialize the TemplateData instance.
60
     */
61
    public TemplateData() {
6✔
62
        assertionTemplates = new ArrayList<>();
15✔
63
        provenanceTemplates = new ArrayList<>();
15✔
64
        pubInfoTemplates = new ArrayList<>();
15✔
65
        templateMap = new ConcurrentHashMap<>();
15✔
66
        refreshTemplates(assertionTemplates, QueryApiAccess.GET_ASSERTION_TEMPLATES);
15✔
67
        refreshTemplates(provenanceTemplates, QueryApiAccess.GET_PROVENANCE_TEMPLATES);
15✔
68
        refreshTemplates(pubInfoTemplates, QueryApiAccess.GET_PUBINFO_TEMPLATES);
15✔
69
    }
3✔
70

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

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

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

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

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

134
    /**
135
     * Registers a template from a Nanopub object directly, without fetching it from the registry.
136
     * This is useful for previewing templates that have not yet been published.
137
     *
138
     * @param np the Nanopub containing the template definition
139
     * @return the Template object, or null if the nanopub is not a valid template
140
     */
141
    public Template registerTemplate(Nanopub np) {
142
        String id = np.getUri().stringValue();
×
143
        Template template = templateMap.get(id);
×
144
        if (template != null) return template;
×
145
        try {
146
            Template t = new Template(np);
×
147
            templateMap.put(id, t);
×
148
            return t;
×
149
        } catch (Exception ex) {
×
150
            logger.error("Exception registering template from nanopub: {}", ex.getMessage());
×
151
            return null;
×
152
        }
153
    }
154

155
    /**
156
     * Returns a Template object for the template of the given Nanopub.
157
     *
158
     * @param np the Nanopub from which to extract the template
159
     * @return the Template object if found, or null if not found or invalid
160
     */
161
    public Template getTemplate(Nanopub np) {
162
        IRI templateId = getTemplateId(np);
×
163
        if (templateId == null) return null;
×
164
        return getTemplate(templateId.stringValue());
×
165
    }
166

167
    /**
168
     * Returns a Template object for the provenance template of the given Nanopub.
169
     *
170
     * @param np the Nanopub from which to extract the provenance template
171
     * @return the Template object if found, or null if not found or invalid
172
     */
173
    public Template getProvenanceTemplate(Nanopub np) {
174
        IRI templateId = getProvenanceTemplateId(np);
×
175
        if (templateId == null) return null;
×
176
        return getTemplate(templateId.stringValue());
×
177
    }
178

179
    /**
180
     * Returns a set of Template objects for the publication information templates of the given Nanopub.
181
     *
182
     * @param np the Nanopub from which to extract the publication information templates
183
     * @return a set of Template objects
184
     */
185
    public Set<Template> getPubinfoTemplates(Nanopub np) {
186
        Set<Template> templates = new HashSet<>();
×
187
        for (IRI id : getPubinfoTemplateIds(np)) {
×
188
            templates.add(getTemplate(id.stringValue()));
×
189
        }
×
190
        return templates;
×
191
    }
192

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

209
    /**
210
     * Returns the provenance template ID of the given Nanopub.
211
     *
212
     * @param nanopub the Nanopub from which to extract the provenance template ID
213
     * @return the IRI of the provenance template ID, or null if not found
214
     */
215
    public IRI getProvenanceTemplateId(Nanopub nanopub) {
216
        for (Statement st : nanopub.getPubinfo()) {
×
217
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
218
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_PROVENANCE_TEMPLATE)) continue;
×
219
            if (!(st.getObject() instanceof IRI)) continue;
×
220
            return (IRI) st.getObject();
×
221
        }
222
        return null;
×
223
    }
224

225
    /**
226
     * Returns the set of publication information template IDs for the given Nanopub.
227
     *
228
     * @param nanopub the Nanopub from which to extract the publication information template IDs
229
     * @return a set of IRI objects representing the publication information template IDs
230
     */
231
    public Set<IRI> getPubinfoTemplateIds(Nanopub nanopub) {
232
        Set<IRI> iriSet = new HashSet<>();
×
233
        for (Statement st : nanopub.getPubinfo()) {
×
234
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
235
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_PUBINFO_TEMPLATE)) continue;
×
236
            if (!(st.getObject() instanceof IRI)) continue;
×
237
            iriSet.add((IRI) st.getObject());
×
238
        }
×
239
        return iriSet;
×
240
    }
241

242
    /**
243
     * Returns a list of Template objects from the given ApiResponse.
244
     *
245
     * @param apiResponse the ApiResponse containing template entries
246
     * @return a list of Template objects
247
     */
248
    public static List<Template> getTemplateList(ApiResponse apiResponse) {
249
        List<Template> templates = new ArrayList<>();
×
250
        for (ApiResponseEntry e : apiResponse.getData()) {
×
251
            String templateNpId = e.get("template_np");
×
252
            if (templateNpId == null) templateNpId = e.get("np");
×
253
            templates.add(TemplateData.get().getTemplate(templateNpId));
×
254
        }
×
255
        return templates;
×
256
    }
257

258
    private static final TemplateComparator templateComparator = new TemplateComparator();
15✔
259

260
    private static class TemplateComparator implements Comparator<ApiResponseEntry>, Serializable {
261

262
        /**
263
         * Compares two Template objects based on their labels.
264
         *
265
         * @param o1 the first object to be compared.
266
         * @param o2 the second object to be compared.
267
         * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
268
         */
269
        @Override
270
        public int compare(ApiResponseEntry o1, ApiResponseEntry o2) {
271
            return o1.get("label").compareTo(o2.get("label"));
24✔
272
        }
273

274
    }
275

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