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

knowledgepixels / nanodash / 21595502811

02 Feb 2026 03:10PM UTC coverage: 14.286% (-0.3%) from 14.572%
21595502811

push

github

web-flow
Merge pull request #348 from knowledgepixels/336-implement-nanopub-lists-as-views

Implement Nanopubs list/grid as `ResourceView`

569 of 5258 branches covered (10.82%)

Branch coverage included in aggregate %.

1552 of 9589 relevant lines covered (16.19%)

2.1 hits per line

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

0.0
src/main/java/com/knowledgepixels/nanodash/ResourceView.java
1
package com.knowledgepixels.nanodash;
2

3
import com.knowledgepixels.nanodash.template.Template;
4
import com.knowledgepixels.nanodash.template.TemplateData;
5
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
6
import org.eclipse.rdf4j.model.IRI;
7
import org.eclipse.rdf4j.model.Literal;
8
import org.eclipse.rdf4j.model.Statement;
9
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
10
import org.eclipse.rdf4j.model.vocabulary.RDF;
11
import org.eclipse.rdf4j.model.vocabulary.RDFS;
12
import org.nanopub.Nanopub;
13
import org.nanopub.NanopubUtils;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16

17
import java.io.Serializable;
18
import java.util.*;
19

20
/**
21
 * A class representing a Resource View.
22
 */
23
public class ResourceView implements Serializable {
24

25
    private static final Logger logger = LoggerFactory.getLogger(ResourceView.class);
×
26
    private static final Set<IRI> supportedViewTypes = Set.of(KPXL_TERMS.TABULAR_VIEW, KPXL_TERMS.LIST_VIEW, KPXL_TERMS.PLAIN_PARAGRAPH_VIEW, KPXL_TERMS.NANOPUB_SET_VIEW);
×
27

28
    static Map<IRI, Integer> columnWidths = new HashMap<>();
×
29

30
    static {
31
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_1_OF_12, 1);
×
32
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_2_OF_12, 2);
×
33
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_3_OF_12, 3);
×
34
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_4_OF_12, 4);
×
35
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_5_OF_12, 5);
×
36
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_6_OF_12, 6);
×
37
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_7_OF_12, 7);
×
38
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_8_OF_12, 8);
×
39
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_9_OF_12, 9);
×
40
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_10_OF_12, 10);
×
41
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_11_OF_12, 11);
×
42
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_12_OF_12, 12);
×
43
    }
44

45
    private static Map<String, ResourceView> resourceViews = new HashMap<>();
×
46

47
    /**
48
     * Get a ResourceView by its ID.
49
     *
50
     * @param id the ID of the ResourceView
51
     * @return the ResourceView object
52
     */
53
    public static ResourceView get(String id) {
54
        String npId = id.replaceFirst("^(.*[^A-Za-z0-9-_]RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$1");
×
55
        // Automatically selecting latest version of view definition:
56
        // TODO This should be made configurable at some point, so one can make it a fixed version.
57
        String latestNpId = QueryApiAccess.getLatestVersionId(npId);
×
58
        String latestId = id;
×
59
        Nanopub np = Utils.getAsNanopub(latestNpId);
×
60
        if (!latestNpId.equals(npId)) {
×
61
            Set<String> embeddedIris = NanopubUtils.getEmbeddedIriIds(np);
×
62
            if (embeddedIris.size() == 1) {
×
63
                latestId = embeddedIris.iterator().next();
×
64
            } else {
65
                latestNpId = npId;
×
66
                np = Utils.getAsNanopub(npId);
×
67
            }
68
        }
69
        if (!resourceViews.containsKey(latestId)) {
×
70
            try {
71
                resourceViews.put(latestId, new ResourceView(latestId, np));
×
72
            } catch (Exception ex) {
×
73
                logger.error("Couldn't load nanopub for resource: " + id, ex);
×
74
            }
×
75
        }
76
        return resourceViews.get(latestId);
×
77
    }
78

79
    private String id;
80
    private Nanopub nanopub;
81
    private IRI viewKind;
82
    private String label;
83
    private String title = "View";
×
84
    private GrlcQuery query;
85
    private String queryField = "resource";
×
86
    private Integer pageSize;
87
    private Integer displayWidth;
88
    private String structuralPosition;
89
    private List<IRI> viewResultActionList = new ArrayList<>();
×
90
    private List<IRI> viewEntryActionList = new ArrayList<>();
×
91
    private Set<IRI> appliesToClasses = new HashSet<>();
×
92
    private Set<IRI> appliesToNamespaces = new HashSet<>();
×
93
    private Map<IRI, Template> actionTemplateMap = new HashMap<>();
×
94
    private Map<IRI, String> actionTemplateTargetFieldMap = new HashMap<>();
×
95
    private Map<IRI, IRI> actionTemplateTypeMap = new HashMap<>();
×
96
    private Map<IRI, String> actionTemplatePartFieldMap = new HashMap<>();
×
97
    private Map<IRI, String> actionTemplateQueryMappingMap = new HashMap<>();
×
98
    private Map<IRI, String> labelMap = new HashMap<>();
×
99
    private IRI viewType;
100

101
    private ResourceView(String id, Nanopub nanopub) {
×
102
        this.id = id;
×
103
        this.nanopub = nanopub;
×
104
        List<IRI> actionList = new ArrayList<>();
×
105
        boolean resourceViewTypeFound = false;
×
106
        for (Statement st : nanopub.getAssertion()) {
×
107
            if (st.getSubject().stringValue().equals(id)) {
×
108
                if (st.getPredicate().equals(RDF.TYPE)) {
×
109
                    if (st.getObject().equals(KPXL_TERMS.RESOURCE_VIEW)) {
×
110
                        resourceViewTypeFound = true;
×
111
                    }
112
                    if (st.getObject() instanceof IRI objIri && supportedViewTypes.contains(objIri)) {
×
113
                        viewType = objIri;
×
114
                    }
115
                } else if (st.getPredicate().equals(DCTERMS.IS_VERSION_OF) && st.getObject() instanceof IRI objIri) {
×
116
                    viewKind = objIri;
×
117
                } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
118
                    label = st.getObject().stringValue();
×
119
                } else if (st.getPredicate().equals(DCTERMS.TITLE)) {
×
120
                    title = st.getObject().stringValue();
×
121
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_QUERY)) {
×
122
                    query = GrlcQuery.get(st.getObject().stringValue());
×
123
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_QUERY_TARGET_FIELD)) {
×
124
                    queryField = st.getObject().stringValue();
×
125
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_ACTION) && st.getObject() instanceof IRI objIri) {
×
126
                    actionList.add(objIri);
×
127
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_NAMESPACE) && st.getObject() instanceof IRI objIri) {
×
128
                    appliesToNamespaces.add(objIri);
×
129
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_INSTANCES_OF) && st.getObject() instanceof IRI objIri) {
×
130
                    appliesToClasses.add(objIri);
×
131
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_TARGET_CLASS) && st.getObject() instanceof IRI objIri) {
×
132
                    // Deprecated
133
                    appliesToClasses.add(objIri);
×
134
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_PAGE_SIZE) && st.getObject() instanceof Literal objL) {
×
135
                    try {
136
                        pageSize = Integer.parseInt(objL.stringValue());
×
137
                    } catch (NumberFormatException ex) {
×
138
                        logger.error("Invalid page size value: " + objL.stringValue(), ex);
×
139
                    }
×
140
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_DISPLAY_WIDTH) && st.getObject() instanceof IRI objIri) {
×
141
                    displayWidth = columnWidths.get(objIri);
×
142
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_STRUCTURAL_POSITION) && st.getObject() instanceof Literal objL) {
×
143
                    structuralPosition = objL.stringValue();
×
144
                }
145
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE)) {
×
146
                Template template = TemplateData.get().getTemplate(st.getObject().stringValue());
×
147
                actionTemplateMap.put((IRI) st.getSubject(), template);
×
148
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_TARGET_FIELD)) {
×
149
                actionTemplateTargetFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
150
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_PART_FIELD)) {
×
151
                actionTemplatePartFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
152
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_QUERY_MAPPING)) {
×
153
                actionTemplateQueryMappingMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
154
            } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
155
                labelMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
156
            } else if (st.getPredicate().equals(RDF.TYPE)) {
×
157
                if (st.getObject().equals(KPXL_TERMS.VIEW_ACTION) || st.getObject().equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
158
                    actionTemplateTypeMap.put((IRI) st.getSubject(), (IRI) st.getObject());
×
159
                }
160
            }
161
        }
×
162
        for (IRI actionIri : actionList) {
×
163
            if (actionTemplateTypeMap.containsKey(actionIri) && actionTemplateTypeMap.get(actionIri).equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
164
                viewEntryActionList.add(actionIri);
×
165
            } else {
166
                viewResultActionList.add(actionIri);
×
167
            }
168
        }
×
169
        if (!resourceViewTypeFound) throw new IllegalArgumentException("Not a proper resource view nanopub: " + id);
×
170
        if (query == null) throw new IllegalArgumentException("Query not found: " + id);
×
171
    }
×
172

173
    /**
174
     * Gets the ID of the ResourceView.
175
     *
176
     * @return the ID of the ResourceView
177
     */
178
    public String getId() {
179
        return id;
×
180
    }
181

182
    /**
183
     * Gets the Nanopub defining this ResourceView.
184
     *
185
     * @return the Nanopub defining this ResourceView
186
     */
187
    public Nanopub getNanopub() {
188
        return nanopub;
×
189
    }
190

191
    public IRI getViewKindIri() {
192
        return viewKind;
×
193
    }
194

195
    /**
196
     * Gets the label of the ResourceView.
197
     *
198
     * @return the label of the ResourceView
199
     */
200
    public String getLabel() {
201
        return label;
×
202
    }
203

204
    /**
205
     * Gets the title of the ResourceView.
206
     *
207
     * @return the title of the ResourceView
208
     */
209
    public String getTitle() {
210
        return title;
×
211
    }
212

213
    /**
214
     * Gets the GrlcQuery associated with the ResourceView.
215
     *
216
     * @return the GrlcQuery associated with the ResourceView
217
     */
218
    public GrlcQuery getQuery() {
219
        return query;
×
220
    }
221

222
    /**
223
     * Gets the query field of the ResourceView.
224
     *
225
     * @return the query field
226
     */
227
    public String getQueryField() {
228
        return queryField;
×
229
    }
230

231
    /**
232
     * Returns the preferred page size.
233
     *
234
     * @return page size (0 = everything on first page)
235
     */
236
    public Integer getPageSize() {
237
        return pageSize;
×
238
    }
239

240
    public Integer getDisplayWidth() {
241
        return displayWidth;
×
242
    }
243

244
    public String getStructuralPosition() {
245
        return structuralPosition;
×
246
    }
247

248
    /**
249
     * Gets the list of action IRIs associated with the ResourceView.
250
     *
251
     * @return the list of action IRIs
252
     */
253
    public List<IRI> getViewResultActionList() {
254
        return viewResultActionList;
×
255
    }
256

257
    public List<IRI> getViewEntryActionList() {
258
        return viewEntryActionList;
×
259
    }
260

261
    /**
262
     * Gets the Template for a given action IRI.
263
     *
264
     * @param actionIri the action IRI
265
     * @return the Template for the action IRI
266
     */
267
    public Template getTemplateForAction(IRI actionIri) {
268
        return actionTemplateMap.get(actionIri);
×
269
    }
270

271
    /**
272
     * Gets the template field for a given action IRI.
273
     *
274
     * @param actionIri the action IRI
275
     * @return the template field for the action IRI
276
     */
277
    public String getTemplateTargetFieldForAction(IRI actionIri) {
278
        return actionTemplateTargetFieldMap.get(actionIri);
×
279
    }
280

281
    public String getTemplatePartFieldForAction(IRI actionIri) {
282
        return actionTemplatePartFieldMap.get(actionIri);
×
283
    }
284

285
    public String getTemplateQueryMapping(IRI actionIri) {
286
        return actionTemplateQueryMappingMap.get(actionIri);
×
287
    }
288

289
    /**
290
     * Gets the label for a given action IRI.
291
     *
292
     * @param actionIri the action IRI
293
     * @return the label for the action IRI
294
     */
295
    public String getLabelForAction(IRI actionIri) {
296
        return labelMap.get(actionIri);
×
297
    }
298

299
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
300
        for (IRI namespace : appliesToNamespaces) {
×
301
            if (resourceId.startsWith(namespace.stringValue())) return true;
×
302
        }
×
303
        if (classes != null) {
×
304
            for (IRI c : classes) {
×
305
                if (appliesToClasses.contains(c)) return true;
×
306
            }
×
307
        }
308
        return false;
×
309
    }
310

311
    /**
312
     * Checks if the ResourceView has target classes.
313
     *
314
     * @return true if the ResourceView has target classes, false otherwise
315
     */
316
    public boolean appliesToClasses() {
317
        return !appliesToClasses.isEmpty();
×
318
    }
319

320
    /**
321
     * Checks if the ResourceView has a specific target class.
322
     *
323
     * @param targetClass the target class IRI
324
     * @return true if the ResourceView has the target class, false otherwise
325
     */
326
    public boolean appliesToClass(IRI targetClass) {
327
        return appliesToClasses.contains(targetClass);
×
328
    }
329

330
    @Override
331
    public String toString() {
332
        return id;
×
333
    }
334

335
    /**
336
     * Gets the view type of the ResourceView.
337
     *
338
     * @return the view type mode IRI
339
     */
340
    public IRI getViewType() {
341
        return viewType;
×
342
    }
343

344
    /**
345
     * Get the supported view types.
346
     *
347
     * @return a set of supported view type IRIs
348
     */
349
    public static Set<IRI> getSupportedViewTypes() {
350
        return supportedViewTypes;
×
351
    }
352

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