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

knowledgepixels / nanodash / 27127668106

08 Jun 2026 09:14AM UTC coverage: 20.467% (-0.5%) from 20.924%
27127668106

Pull #479

github

web-flow
Merge f10bc9fe9 into 5556185a4
Pull Request #479: About pages for spaces, maintained resources, and users (#478)

1024 of 6405 branches covered (15.99%)

Branch coverage included in aggregate %.

2624 of 11419 relevant lines covered (22.98%)

3.29 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 com.google.common.cache.Cache;
18
import com.google.common.cache.CacheBuilder;
19

20
import java.io.Serializable;
21
import java.util.*;
22
import java.util.concurrent.TimeUnit;
23

24
/**
25
 * A class representing a Resource View.
26
 */
27
public class View implements Serializable {
28

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

38
    static Map<IRI, Integer> columnWidths = new HashMap<>();
×
39

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

55
    private static final Cache<String, View> views = CacheBuilder.newBuilder()
×
56
        .maximumSize(5_000)
×
57
        .expireAfterAccess(24, TimeUnit.HOURS)
×
58
        .build();
×
59

60
    /**
61
     * Get a View by its ID, resolving to the latest version (following the
62
     * supersedes chain).
63
     *
64
     * @param id the ID of the View
65
     * @return the View object
66
     */
67
    public static View get(String id) {
68
        return get(id, true);
×
69
    }
70

71
    /**
72
     * Get a View by its ID.
73
     *
74
     * @param id            the ID of the View
75
     * @param resolveLatest if true, follow the supersedes chain to load the latest
76
     *                      version of the view; if false, load exactly the given
77
     *                      version without a latest-version lookup. Pass false when
78
     *                      the caller already holds a latest-resolved IRI (e.g. from
79
     *                      the get-view-displays query, which now resolves it
80
     *                      server-side) to avoid a redundant network round-trip.
81
     * @return the View object
82
     */
83
    public static View get(String id, boolean resolveLatest) {
84
        String npId = id.replaceFirst("^(.*[^A-Za-z0-9-_]RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$1");
×
85
        if (resolveLatest) {
×
86
            // Automatically selecting latest version of view definition:
87
            // TODO This should be made configurable at some point, so one can make it a fixed version.
88
            try {
89
                String latestNpId = QueryApiAccess.getLatestVersionId(npId);
×
90
                if (!latestNpId.equals(npId)) {
×
91
                    Nanopub np = Utils.getAsNanopub(latestNpId);
×
92
                    if (np != null) {
×
93
                        Set<String> embeddedIris = NanopubUtils.getEmbeddedIriIds(np);
×
94
                        if (embeddedIris.size() == 1) {
×
95
                            String latestId = embeddedIris.iterator().next();
×
96
                            View cached = views.getIfPresent(latestId);
×
97
                            if (cached == null) {
×
98
                                cached = new View(latestId, np);
×
99
                                views.put(latestId, cached);
×
100
                            }
101
                            return cached;
×
102
                        }
103
                    }
104
                }
105
            } catch (Exception ex) {
×
106
                logger.error("Error resolving latest version for view: {}", id, ex);
×
107
            }
×
108
        }
109
        // Fall back to loading the nanopub as given:
110
        Nanopub np = Utils.getAsNanopub(npId);
×
111
        View cached = views.getIfPresent(id);
×
112
        if (cached == null) {
×
113
            try {
114
                cached = new View(id, np);
×
115
                views.put(id, cached);
×
116
            } catch (Exception ex) {
×
117
                logger.error("Couldn't load nanopub for resource: {}", id, ex);
×
118
            }
×
119
        }
120
        return cached;
×
121
    }
122

123
    private String id;
124
    private Nanopub nanopub;
125
    private IRI viewKind;
126
    private String label;
127
    private String title = "View";
×
128
    private GrlcQuery query;
129
    private String queryField = "resource";
×
130
    private Integer pageSize;
131
    private Integer displayWidth;
132
    private String structuralPosition;
133
    private List<IRI> viewResultActionList = new ArrayList<>();
×
134
    private List<IRI> viewEntryActionList = new ArrayList<>();
×
135
    private Set<IRI> appliesToClasses = new HashSet<>();
×
136
    private Set<IRI> appliesToNamespaces = new HashSet<>();
×
137
    private Map<IRI, Template> actionTemplateMap = new HashMap<>();
×
138
    private Map<IRI, String> actionTemplateTargetFieldMap = new HashMap<>();
×
139
    private Map<IRI, IRI> actionTemplateTypeMap = new HashMap<>();
×
140
    private Map<IRI, String> actionTemplatePartFieldMap = new HashMap<>();
×
141
    private Map<IRI, String> actionTemplateQueryMappingMap = new HashMap<>();
×
142
    private Map<IRI, String> labelMap = new HashMap<>();
×
143
    private IRI viewType;
144
    private Set<IRI> visibleTo = new HashSet<>();
×
145

146
    private View(String id, Nanopub nanopub) {
×
147
        this.id = id;
×
148
        this.nanopub = nanopub;
×
149
        List<IRI> actionList = new ArrayList<>();
×
150
        boolean viewTypeFound = false;
×
151
        for (Statement st : nanopub.getAssertion()) {
×
152
            if (st.getSubject().stringValue().equals(id)) {
×
153
                if (st.getPredicate().equals(RDF.TYPE)) {
×
154
                    if (st.getObject().equals(KPXL_TERMS.RESOURCE_VIEW)) {
×
155
                        viewTypeFound = true;
×
156
                    }
157
                    if (st.getObject() instanceof IRI objIri && supportedViewTypes.contains(objIri)) {
×
158
                        viewType = objIri;
×
159
                    }
160
                } else if (st.getPredicate().equals(DCTERMS.IS_VERSION_OF) && st.getObject() instanceof IRI objIri) {
×
161
                    viewKind = objIri;
×
162
                } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
163
                    label = st.getObject().stringValue();
×
164
                } else if (st.getPredicate().equals(DCTERMS.TITLE)) {
×
165
                    title = st.getObject().stringValue();
×
166
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_QUERY)) {
×
167
                    query = GrlcQuery.get(st.getObject().stringValue());
×
168
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_QUERY_TARGET_FIELD)) {
×
169
                    queryField = st.getObject().stringValue();
×
170
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_ACTION) && st.getObject() instanceof IRI objIri) {
×
171
                    actionList.add(objIri);
×
172
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_NAMESPACE) && st.getObject() instanceof IRI objIri) {
×
173
                    appliesToNamespaces.add(objIri);
×
174
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_INSTANCES_OF) && st.getObject() instanceof IRI objIri) {
×
175
                    appliesToClasses.add(objIri);
×
176
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_TARGET_CLASS) && st.getObject() instanceof IRI objIri) {
×
177
                    // Deprecated
178
                    appliesToClasses.add(objIri);
×
179
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_PAGE_SIZE) && st.getObject() instanceof Literal objL) {
×
180
                    try {
181
                        pageSize = Integer.parseInt(objL.stringValue());
×
182
                    } catch (NumberFormatException ex) {
×
183
                        logger.error("Invalid page size value: {}", objL.stringValue(), ex);
×
184
                    }
×
185
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_DISPLAY_WIDTH) && st.getObject() instanceof IRI objIri) {
×
186
                    displayWidth = columnWidths.get(objIri);
×
187
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_STRUCTURAL_POSITION) && st.getObject() instanceof Literal objL) {
×
188
                    structuralPosition = objL.stringValue();
×
189
                } else if (st.getPredicate().equals(KPXL_TERMS.IS_VISIBLE_TO) && st.getObject() instanceof IRI objIri) {
×
190
                    visibleTo.add(objIri);
×
191
                }
192
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE)) {
×
193
                Template template = TemplateData.get().getTemplate(st.getObject().stringValue());
×
194
                actionTemplateMap.put((IRI) st.getSubject(), template);
×
195
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_TARGET_FIELD)) {
×
196
                actionTemplateTargetFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
197
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_PART_FIELD)) {
×
198
                actionTemplatePartFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
199
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_QUERY_MAPPING)) {
×
200
                actionTemplateQueryMappingMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
201
            } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
202
                labelMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
203
            } else if (st.getPredicate().equals(RDF.TYPE)) {
×
204
                if (st.getObject().equals(KPXL_TERMS.VIEW_ACTION) || st.getObject().equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
205
                    actionTemplateTypeMap.put((IRI) st.getSubject(), (IRI) st.getObject());
×
206
                }
207
            }
208
        }
×
209
        for (IRI actionIri : actionList) {
×
210
            if (actionTemplateTypeMap.containsKey(actionIri) && actionTemplateTypeMap.get(actionIri).equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
211
                viewEntryActionList.add(actionIri);
×
212
            } else {
213
                viewResultActionList.add(actionIri);
×
214
            }
215
        }
×
216
        if (!viewTypeFound) throw new IllegalArgumentException("Not a proper resource view nanopub: " + id);
×
217
        if (query == null) throw new IllegalArgumentException("Query not found: " + id);
×
218
    }
×
219

220
    /**
221
     * Gets the ID of the View.
222
     *
223
     * @return the ID of the View
224
     */
225
    public String getId() {
226
        return id;
×
227
    }
228

229
    /**
230
     * Gets the Nanopub defining this View.
231
     *
232
     * @return the Nanopub defining this View
233
     */
234
    public Nanopub getNanopub() {
235
        return nanopub;
×
236
    }
237

238
    public IRI getViewKindIri() {
239
        return viewKind;
×
240
    }
241

242
    /**
243
     * Gets the label of the View.
244
     *
245
     * @return the label of the View
246
     */
247
    public String getLabel() {
248
        return label;
×
249
    }
250

251
    /**
252
     * Gets the title of the View.
253
     *
254
     * @return the title of the View
255
     */
256
    public String getTitle() {
257
        return title;
×
258
    }
259

260
    /**
261
     * Gets the GrlcQuery associated with the View.
262
     *
263
     * @return the GrlcQuery associated with the View
264
     */
265
    public GrlcQuery getQuery() {
266
        return query;
×
267
    }
268

269
    /**
270
     * Gets the query field of the View.
271
     *
272
     * @return the query field
273
     */
274
    public String getQueryField() {
275
        return queryField;
×
276
    }
277

278
    /**
279
     * Returns the preferred page size.
280
     *
281
     * @return page size (0 = everything on first page)
282
     */
283
    public Integer getPageSize() {
284
        return pageSize;
×
285
    }
286

287
    public Integer getDisplayWidth() {
288
        return displayWidth;
×
289
    }
290

291
    public String getStructuralPosition() {
292
        return structuralPosition;
×
293
    }
294

295
    /**
296
     * Gets the default visibility restriction of this view: the set of role-tier
297
     * or specific-role IRIs a viewer must hold to see it. Empty means visible to
298
     * everyone. A {@link ViewDisplay} overrides this with its own restriction.
299
     *
300
     * @return the set of {@code gen:isVisibleTo} IRIs (never null)
301
     */
302
    public Set<IRI> getVisibleTo() {
303
        return visibleTo;
×
304
    }
305

306
    /**
307
     * Gets the list of action IRIs associated with the View.
308
     *
309
     * @return the list of action IRIs
310
     */
311
    public List<IRI> getViewResultActionList() {
312
        return viewResultActionList;
×
313
    }
314

315
    public List<IRI> getViewEntryActionList() {
316
        return viewEntryActionList;
×
317
    }
318

319
    /**
320
     * Gets the Template for a given action IRI.
321
     *
322
     * @param actionIri the action IRI
323
     * @return the Template for the action IRI
324
     */
325
    public Template getTemplateForAction(IRI actionIri) {
326
        return actionTemplateMap.get(actionIri);
×
327
    }
328

329
    /**
330
     * Gets the template field for a given action IRI.
331
     *
332
     * @param actionIri the action IRI
333
     * @return the template field for the action IRI
334
     */
335
    public String getTemplateTargetFieldForAction(IRI actionIri) {
336
        return actionTemplateTargetFieldMap.get(actionIri);
×
337
    }
338

339
    public String getTemplatePartFieldForAction(IRI actionIri) {
340
        return actionTemplatePartFieldMap.get(actionIri);
×
341
    }
342

343
    public String getTemplateQueryMapping(IRI actionIri) {
344
        return actionTemplateQueryMappingMap.get(actionIri);
×
345
    }
346

347
    /**
348
     * Gets the label for a given action IRI.
349
     *
350
     * @param actionIri the action IRI
351
     * @return the label for the action IRI
352
     */
353
    public String getLabelForAction(IRI actionIri) {
354
        return labelMap.get(actionIri);
×
355
    }
356

357
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
358
        for (IRI namespace : appliesToNamespaces) {
×
359
            if (resourceId.startsWith(namespace.stringValue())) return true;
×
360
        }
×
361
        if (classes != null) {
×
362
            for (IRI c : classes) {
×
363
                if (appliesToClasses.contains(c)) return true;
×
364
            }
×
365
        }
366
        return false;
×
367
    }
368

369
    /**
370
     * Checks if the View has target classes.
371
     *
372
     * @return true if the View has target classes, false otherwise
373
     */
374
    public boolean appliesToClasses() {
375
        return !appliesToClasses.isEmpty();
×
376
    }
377

378
    /**
379
     * Checks if the View has a specific target class.
380
     *
381
     * @param targetClass the target class IRI
382
     * @return true if the View has the target class, false otherwise
383
     */
384
    public boolean appliesToClass(IRI targetClass) {
385
        return appliesToClasses.contains(targetClass);
×
386
    }
387

388
    @Override
389
    public String toString() {
390
        return id;
×
391
    }
392

393
    /**
394
     * Gets the view type of the View.
395
     *
396
     * @return the view type mode IRI
397
     */
398
    public IRI getViewType() {
399
        return viewType;
×
400
    }
401

402
    /**
403
     * Get the supported view types.
404
     *
405
     * @return a set of supported view type IRIs
406
     */
407
    public static Set<IRI> getSupportedViewTypes() {
408
        return supportedViewTypes;
×
409
    }
410

411
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc