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

knowledgepixels / nanodash / 21642716107

03 Feb 2026 06:31PM UTC coverage: 14.255% (-0.08%) from 14.333%
21642716107

push

github

ashleycaselli
feat(KPXL_TERMS): add ITEM_LIST_VIEW to supported view types

582 of 5240 branches covered (11.11%)

Branch coverage included in aggregate %.

1522 of 9520 relevant lines covered (15.99%)

2.09 hits per line

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

0.0
src/main/java/com/knowledgepixels/nanodash/View.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 View implements Serializable {
24

25
    private static final Logger logger = LoggerFactory.getLogger(View.class);
×
26
    private static final Set<IRI> supportedViewTypes = Set.of(
×
27
            KPXL_TERMS.TABULAR_VIEW,
28
            KPXL_TERMS.LIST_VIEW,
29
            KPXL_TERMS.PLAIN_PARAGRAPH_VIEW,
30
            KPXL_TERMS.NANOPUB_SET_VIEW,
31
            KPXL_TERMS.ITEM_LIST_VIEW
32
    );
33

34
    static Map<IRI, Integer> columnWidths = new HashMap<>();
×
35

36
    static {
37
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_1_OF_12, 1);
×
38
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_2_OF_12, 2);
×
39
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_3_OF_12, 3);
×
40
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_4_OF_12, 4);
×
41
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_5_OF_12, 5);
×
42
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_6_OF_12, 6);
×
43
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_7_OF_12, 7);
×
44
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_8_OF_12, 8);
×
45
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_9_OF_12, 9);
×
46
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_10_OF_12, 10);
×
47
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_11_OF_12, 11);
×
48
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_12_OF_12, 12);
×
49
    }
50

51
    private static Map<String, View> views = new HashMap<>();
×
52

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

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

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

179
    /**
180
     * Gets the ID of the View.
181
     *
182
     * @return the ID of the View
183
     */
184
    public String getId() {
185
        return id;
×
186
    }
187

188
    /**
189
     * Gets the Nanopub defining this View.
190
     *
191
     * @return the Nanopub defining this View
192
     */
193
    public Nanopub getNanopub() {
194
        return nanopub;
×
195
    }
196

197
    public IRI getViewKindIri() {
198
        return viewKind;
×
199
    }
200

201
    /**
202
     * Gets the label of the View.
203
     *
204
     * @return the label of the View
205
     */
206
    public String getLabel() {
207
        return label;
×
208
    }
209

210
    /**
211
     * Gets the title of the View.
212
     *
213
     * @return the title of the View
214
     */
215
    public String getTitle() {
216
        return title;
×
217
    }
218

219
    /**
220
     * Gets the GrlcQuery associated with the View.
221
     *
222
     * @return the GrlcQuery associated with the View
223
     */
224
    public GrlcQuery getQuery() {
225
        return query;
×
226
    }
227

228
    /**
229
     * Gets the query field of the View.
230
     *
231
     * @return the query field
232
     */
233
    public String getQueryField() {
234
        return queryField;
×
235
    }
236

237
    /**
238
     * Returns the preferred page size.
239
     *
240
     * @return page size (0 = everything on first page)
241
     */
242
    public Integer getPageSize() {
243
        return pageSize;
×
244
    }
245

246
    public Integer getDisplayWidth() {
247
        return displayWidth;
×
248
    }
249

250
    public String getStructuralPosition() {
251
        return structuralPosition;
×
252
    }
253

254
    /**
255
     * Gets the list of action IRIs associated with the View.
256
     *
257
     * @return the list of action IRIs
258
     */
259
    public List<IRI> getViewResultActionList() {
260
        return viewResultActionList;
×
261
    }
262

263
    public List<IRI> getViewEntryActionList() {
264
        return viewEntryActionList;
×
265
    }
266

267
    /**
268
     * Gets the Template for a given action IRI.
269
     *
270
     * @param actionIri the action IRI
271
     * @return the Template for the action IRI
272
     */
273
    public Template getTemplateForAction(IRI actionIri) {
274
        return actionTemplateMap.get(actionIri);
×
275
    }
276

277
    /**
278
     * Gets the template field for a given action IRI.
279
     *
280
     * @param actionIri the action IRI
281
     * @return the template field for the action IRI
282
     */
283
    public String getTemplateTargetFieldForAction(IRI actionIri) {
284
        return actionTemplateTargetFieldMap.get(actionIri);
×
285
    }
286

287
    public String getTemplatePartFieldForAction(IRI actionIri) {
288
        return actionTemplatePartFieldMap.get(actionIri);
×
289
    }
290

291
    public String getTemplateQueryMapping(IRI actionIri) {
292
        return actionTemplateQueryMappingMap.get(actionIri);
×
293
    }
294

295
    /**
296
     * Gets the label for a given action IRI.
297
     *
298
     * @param actionIri the action IRI
299
     * @return the label for the action IRI
300
     */
301
    public String getLabelForAction(IRI actionIri) {
302
        return labelMap.get(actionIri);
×
303
    }
304

305
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
306
        for (IRI namespace : appliesToNamespaces) {
×
307
            if (resourceId.startsWith(namespace.stringValue())) return true;
×
308
        }
×
309
        if (classes != null) {
×
310
            for (IRI c : classes) {
×
311
                if (appliesToClasses.contains(c)) return true;
×
312
            }
×
313
        }
314
        return false;
×
315
    }
316

317
    /**
318
     * Checks if the View has target classes.
319
     *
320
     * @return true if the View has target classes, false otherwise
321
     */
322
    public boolean appliesToClasses() {
323
        return !appliesToClasses.isEmpty();
×
324
    }
325

326
    /**
327
     * Checks if the View has a specific target class.
328
     *
329
     * @param targetClass the target class IRI
330
     * @return true if the View has the target class, false otherwise
331
     */
332
    public boolean appliesToClass(IRI targetClass) {
333
        return appliesToClasses.contains(targetClass);
×
334
    }
335

336
    @Override
337
    public String toString() {
338
        return id;
×
339
    }
340

341
    /**
342
     * Gets the view type of the View.
343
     *
344
     * @return the view type mode IRI
345
     */
346
    public IRI getViewType() {
347
        return viewType;
×
348
    }
349

350
    /**
351
     * Get the supported view types.
352
     *
353
     * @return a set of supported view type IRIs
354
     */
355
    public static Set<IRI> getSupportedViewTypes() {
356
        return supportedViewTypes;
×
357
    }
358

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