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

knowledgepixels / nanodash / 28663628697

03 Jul 2026 01:27PM UTC coverage: 28.551% (-0.02%) from 28.569%
28663628697

push

github

web-flow
Merge pull request #531 from knowledgepixels/feat/space-governed-views

feat: resolve space-governed view versions via gen:governedBy

1798 of 7095 branches covered (25.34%)

Branch coverage included in aggregate %.

3680 of 12092 relevant lines covered (30.43%)

4.52 hits per line

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

9.36
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.apache.commons.lang3.tuple.Pair;
7
import org.eclipse.rdf4j.model.IRI;
8
import org.eclipse.rdf4j.model.Literal;
9
import org.eclipse.rdf4j.model.Statement;
10
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
11
import org.eclipse.rdf4j.model.vocabulary.RDF;
12
import org.eclipse.rdf4j.model.vocabulary.RDFS;
13
import org.nanopub.Nanopub;
14
import org.nanopub.NanopubUtils;
15
import org.nanopub.extra.services.ApiResponse;
16
import org.nanopub.extra.services.QueryRef;
17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19

20
import com.google.common.cache.Cache;
21
import com.google.common.cache.CacheBuilder;
22
import com.google.common.collect.ArrayListMultimap;
23
import com.google.common.collect.Multimap;
24

25
import java.io.Serializable;
26
import java.util.*;
27
import java.util.concurrent.ConcurrentHashMap;
28
import java.util.concurrent.TimeUnit;
29

30
/**
31
 * A class representing a Resource View.
32
 */
33
public class View implements Serializable {
34

35
    private static final Logger logger = LoggerFactory.getLogger(View.class);
9✔
36
    private static final Set<IRI> supportedViewTypes = Set.of(
21✔
37
            KPXL_TERMS.TABULAR_VIEW,
38
            KPXL_TERMS.LIST_VIEW,
39
            KPXL_TERMS.PLAIN_PARAGRAPH_VIEW,
40
            KPXL_TERMS.NANOPUB_SET_VIEW,
41
            KPXL_TERMS.ITEM_LIST_VIEW
42
    );
43

44
    static Map<IRI, Integer> columnWidths = new HashMap<>();
12✔
45

46
    static {
47
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_1_OF_12, 1);
18✔
48
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_2_OF_12, 2);
18✔
49
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_3_OF_12, 3);
18✔
50
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_4_OF_12, 4);
18✔
51
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_5_OF_12, 5);
18✔
52
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_6_OF_12, 6);
18✔
53
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_7_OF_12, 7);
18✔
54
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_8_OF_12, 8);
18✔
55
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_9_OF_12, 9);
18✔
56
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_10_OF_12, 10);
18✔
57
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_11_OF_12, 11);
18✔
58
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_12_OF_12, 12);
18✔
59
    }
60

61
    private static final Cache<String, View> views = CacheBuilder.newBuilder()
6✔
62
        .maximumSize(5_000)
9✔
63
        .expireAfterAccess(24, TimeUnit.HOURS)
3✔
64
        .build();
6✔
65

66
    /**
67
     * Memo of latest-version resolutions: view id (as passed to {@link #get(String)})
68
     * to the resolution time and the View it resolved to. Entries are served as
69
     * long as they live; one older than {@link #REFRESH_RESOLUTION_AFTER_MS} is
70
     * served stale while a background re-resolution runs (stale-while-revalidate,
71
     * like {@link ApiCache}), so a superseding view nanopub is picked up on a
72
     * later render without {@link #get(String)} ever blocking on the network
73
     * once an entry exists.
74
     */
75
    private static final Cache<String, Pair<Long, View>> latestResolvedViews = CacheBuilder.newBuilder()
6✔
76
        .maximumSize(5_000)
9✔
77
        .expireAfterAccess(24, TimeUnit.HOURS)
3✔
78
        .build();
6✔
79

80
    /**
81
     * Age after which a memoized latest-version resolution is re-resolved in the
82
     * background; mirrors the freshness window of
83
     * {@link QueryApiAccess#getLatestVersionId(String)}.
84
     */
85
    private static final long REFRESH_RESOLUTION_AFTER_MS = 1000 * 60;
86

87
    /**
88
     * Ids whose latest-version resolution is currently being refreshed in the
89
     * background, so concurrent renders don't pile up duplicate refreshes.
90
     */
91
    private static final Set<String> refreshingViews = ConcurrentHashMap.newKeySet();
9✔
92

93
    /**
94
     * Indicates whether {@link #get(String)} would currently return without
95
     * network access, i.e. a latest-version resolution is memoized for this id
96
     * (possibly stale, in which case get() serves it while refreshing in the
97
     * background). Used to decide between constructing a view-based panel
98
     * directly and deferring it to a lazy-loading AJAX request.
99
     *
100
     * @param id the ID of the View
101
     * @return true if {@link #get(String)} currently returns without blocking
102
     */
103
    public static boolean isCached(String id) {
104
        return latestResolvedViews.getIfPresent(id) != null;
×
105
    }
106

107
    /**
108
     * Get a View by its ID, resolving to the latest version (following the
109
     * supersedes chain).
110
     *
111
     * @param id the ID of the View
112
     * @return the View object
113
     */
114
    public static View get(String id) {
115
        return get(id, true);
×
116
    }
117

118
    /**
119
     * Get a View by its ID.
120
     *
121
     * @param id            the ID of the View
122
     * @param resolveLatest if true, follow the supersedes chain to load the latest
123
     *                      version of the view; if false, load exactly the given
124
     *                      version without a latest-version lookup. Pass false when
125
     *                      the caller already holds a latest-resolved IRI (e.g. from
126
     *                      the get-view-displays query, which now resolves it
127
     *                      server-side) to avoid a redundant network round-trip.
128
     *                      A version declaring {@code gen:governedBy} still gets the
129
     *                      space-based resolution here even with false: its float is
130
     *                      not supersedes-based, so the caller's server-side
131
     *                      resolution doesn't cover it.
132
     * @return the View object
133
     */
134
    public static View get(String id, boolean resolveLatest) {
135
        String npId = id.replaceFirst("^(.*[^A-Za-z0-9-_]RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$1");
×
136
        if (!resolveLatest) {
×
137
            View exact = getExactVersion(id, npId);
×
138
            if (exact == null || exact.getGoverningSpace() == null || exact.getViewKindIri() == null) {
×
139
                return exact;
×
140
            }
141
            // fall through to the memoized latest path, which resolves a governed
142
            // version space-based (never supersedes-based) for this pin
143
        }
144
        Pair<Long, View> memo = latestResolvedViews.getIfPresent(id);
×
145
        if (memo != null) {
×
146
            if (System.currentTimeMillis() - memo.getLeft() > REFRESH_RESOLUTION_AFTER_MS) {
×
147
                triggerResolutionRefresh(id, npId);
×
148
            }
149
            return memo.getRight();
×
150
        }
151
        View resolved = resolveLatestVersion(id, npId);
×
152
        if (resolved != null) {
×
153
            latestResolvedViews.put(id, Pair.of(System.currentTimeMillis(), resolved));
×
154
        }
155
        return resolved;
×
156
    }
157

158
    /**
159
     * Resolves a view id to the latest version of its view definition, falling
160
     * back to the exact given version if the lookup fails or doesn't yield a
161
     * single embedded view IRI. This is the network-touching part of
162
     * {@link #get(String)}. A version that declares {@code gen:governedBy}
163
     * resolves space-based (authority-scoped latest-wins within its
164
     * {@code (kind, space)} pair); one that doesn't follows the supersedes
165
     * chain as before. See docs/views-and-presets-as-maintained-resources.md.
166
     */
167
    private static View resolveLatestVersion(String id, String npId) {
168
        View pinned = getExactVersion(id, npId);
×
169
        if (pinned != null && pinned.getGoverningSpace() != null && pinned.getViewKindIri() != null) {
×
170
            return resolveGovernedVersion(pinned);
×
171
        }
172
        // Automatically selecting latest version of view definition:
173
        // TODO This should be made configurable at some point, so one can make it a fixed version.
174
        try {
175
            String latestNpId = QueryApiAccess.getLatestVersionId(npId);
×
176
            if (!latestNpId.equals(npId)) {
×
177
                Nanopub np = Utils.getAsNanopub(latestNpId);
×
178
                if (np != null) {
×
179
                    Set<String> embeddedIris = NanopubUtils.getEmbeddedIriIds(np);
×
180
                    if (embeddedIris.size() == 1) {
×
181
                        String latestId = embeddedIris.iterator().next();
×
182
                        View cached = views.getIfPresent(latestId);
×
183
                        if (cached == null) {
×
184
                            cached = new View(latestId, np);
×
185
                            views.put(latestId, cached);
×
186
                        }
187
                        return cached;
×
188
                    }
189
                }
190
            }
191
        } catch (Exception ex) {
×
192
            logger.error("Error resolving latest version for view: {}", id, ex);
×
193
        }
×
194
        return pinned;
×
195
    }
196

197
    /**
198
     * Resolves the latest space-governed version of the pinned view's
199
     * {@code (kind, space)} pair: the newest version declaring the same kind and
200
     * governing space, signed by a current member+ of that space, with the kind
201
     * validated as maintained by the space — all checked server-side by the
202
     * {@link QueryApiAccess#GET_LATEST_GOVERNED_VERSION} query. The pin is the
203
     * floor: on an empty result (or any failure) the pinned version stands,
204
     * un-revalidated.
205
     */
206
    private static View resolveGovernedVersion(View pinned) {
207
        try {
208
            Multimap<String, String> params = ArrayListMultimap.create();
×
209
            params.put("kind", pinned.getViewKindIri().stringValue());
×
210
            params.put("space", pinned.getGoverningSpace().stringValue());
×
211
            ApiResponse resp = ApiCache.retrieveResponseSync(new QueryRef(QueryApiAccess.GET_LATEST_GOVERNED_VERSION, params), false);
×
212
            if (resp != null && !resp.getData().isEmpty()) {
×
213
                String latestId = resp.getData().get(0).get("version");
×
214
                if (latestId != null && !latestId.isEmpty() && !latestId.equals(pinned.getId())) {
×
215
                    String latestNpId = latestId.replaceFirst("^(.*[^A-Za-z0-9-_]RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$1");
×
216
                    View resolved = getExactVersion(latestId, latestNpId);
×
217
                    if (resolved != null) return resolved;
×
218
                }
219
            }
220
        } catch (Exception ex) {
×
221
            logger.error("Error resolving governed version for view: {}", pinned.getId(), ex);
×
222
        }
×
223
        return pinned;
×
224
    }
225

226
    /**
227
     * Loads the view exactly as given, without a latest-version lookup.
228
     */
229
    private static View getExactVersion(String id, String npId) {
230
        Nanopub np = Utils.getAsNanopub(npId);
×
231
        View cached = views.getIfPresent(id);
×
232
        if (cached == null) {
×
233
            try {
234
                cached = new View(id, np);
×
235
                views.put(id, cached);
×
236
            } catch (Exception ex) {
×
237
                logger.error("Couldn't load nanopub for resource: {}", id, ex);
×
238
            }
×
239
        }
240
        return cached;
×
241
    }
242

243
    /**
244
     * Re-resolves a stale memoized latest-version resolution in the background.
245
     * The stale value keeps being served meanwhile; on failure it is re-stamped
246
     * with the current time, so failing lookups are retried at most once per
247
     * {@link #REFRESH_RESOLUTION_AFTER_MS} rather than on every render.
248
     */
249
    private static void triggerResolutionRefresh(String id, String npId) {
250
        if (!refreshingViews.add(id)) return;
×
251
        NanodashThreadPool.submit(() -> {
×
252
            try {
253
                View resolved = resolveLatestVersion(id, npId);
×
254
                if (resolved == null) {
×
255
                    Pair<Long, View> previous = latestResolvedViews.getIfPresent(id);
×
256
                    resolved = (previous == null ? null : previous.getRight());
×
257
                }
258
                if (resolved != null) {
×
259
                    latestResolvedViews.put(id, Pair.of(System.currentTimeMillis(), resolved));
×
260
                }
261
            } finally {
262
                refreshingViews.remove(id);
×
263
            }
264
        });
×
265
    }
×
266

267
    private String id;
268
    private Nanopub nanopub;
269
    private IRI viewKind;
270
    private IRI governingSpace;
271
    private String label;
272
    private String title = "View";
×
273
    private GrlcQuery query;
274
    private String queryField = "resource";
×
275
    private Integer pageSize;
276
    private Integer displayWidth;
277
    private String structuralPosition;
278
    private List<IRI> viewResultActionList = new ArrayList<>();
×
279
    private List<IRI> viewEntryActionList = new ArrayList<>();
×
280
    private Set<IRI> appliesToClasses = new HashSet<>();
×
281
    private Set<IRI> appliesToNamespaces = new HashSet<>();
×
282
    private Map<IRI, Template> actionTemplateMap = new HashMap<>();
×
283
    private Map<IRI, String> actionTemplateTargetFieldMap = new HashMap<>();
×
284
    private Map<IRI, IRI> actionTemplateTypeMap = new HashMap<>();
×
285
    private Map<IRI, String> actionTemplatePartFieldMap = new HashMap<>();
×
286
    private Map<IRI, List<String>> actionTemplateQueryMappingsMap = new HashMap<>();
×
287
    private Map<IRI, String> labelMap = new HashMap<>();
×
288
    private IRI viewType;
289
    private Map<IRI, Set<IRI>> actionVisibleToMap = new HashMap<>();
×
290

291
    private View(String id, Nanopub nanopub) {
×
292
        this.id = id;
×
293
        this.nanopub = nanopub;
×
294
        List<IRI> actionList = new ArrayList<>();
×
295
        boolean viewTypeFound = false;
×
296
        for (Statement st : nanopub.getAssertion()) {
×
297
            if (st.getSubject().stringValue().equals(id)) {
×
298
                if (st.getPredicate().equals(RDF.TYPE)) {
×
299
                    if (st.getObject().equals(KPXL_TERMS.RESOURCE_VIEW)) {
×
300
                        viewTypeFound = true;
×
301
                    }
302
                    if (st.getObject() instanceof IRI objIri && supportedViewTypes.contains(objIri)) {
×
303
                        viewType = objIri;
×
304
                    }
305
                } else if (st.getPredicate().equals(DCTERMS.IS_VERSION_OF) && st.getObject() instanceof IRI objIri) {
×
306
                    viewKind = objIri;
×
307
                } else if (st.getPredicate().equals(KPXL_TERMS.GOVERNED_BY) && st.getObject() instanceof IRI objIri) {
×
308
                    governingSpace = objIri;
×
309
                } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
310
                    label = st.getObject().stringValue();
×
311
                } else if (st.getPredicate().equals(DCTERMS.TITLE)) {
×
312
                    title = st.getObject().stringValue();
×
313
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_QUERY)) {
×
314
                    query = GrlcQuery.get(st.getObject().stringValue());
×
315
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_QUERY_TARGET_FIELD)) {
×
316
                    queryField = st.getObject().stringValue();
×
317
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_ACTION) && st.getObject() instanceof IRI objIri) {
×
318
                    actionList.add(objIri);
×
319
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_NAMESPACE) && st.getObject() instanceof IRI objIri) {
×
320
                    appliesToNamespaces.add(objIri);
×
321
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_INSTANCES_OF) && st.getObject() instanceof IRI objIri) {
×
322
                    appliesToClasses.add(objIri);
×
323
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_TARGET_CLASS) && st.getObject() instanceof IRI objIri) {
×
324
                    // Deprecated
325
                    appliesToClasses.add(objIri);
×
326
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_PAGE_SIZE) && st.getObject() instanceof Literal objL) {
×
327
                    try {
328
                        pageSize = Integer.parseInt(objL.stringValue());
×
329
                    } catch (NumberFormatException ex) {
×
330
                        logger.error("Invalid page size value: {}", objL.stringValue(), ex);
×
331
                    }
×
332
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_DISPLAY_WIDTH) && st.getObject() instanceof IRI objIri) {
×
333
                    displayWidth = columnWidths.get(objIri);
×
334
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_STRUCTURAL_POSITION) && st.getObject() instanceof Literal objL) {
×
335
                    structuralPosition = objL.stringValue();
×
336
                }
337
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE)) {
×
338
                Template template = TemplateData.get().getTemplate(st.getObject().stringValue());
×
339
                actionTemplateMap.put((IRI) st.getSubject(), template);
×
340
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_TARGET_FIELD)) {
×
341
                putUnlessVoid(actionTemplateTargetFieldMap, (IRI) st.getSubject(), st.getObject().stringValue());
×
342
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_PART_FIELD)) {
×
343
                putUnlessVoid(actionTemplatePartFieldMap, (IRI) st.getSubject(), st.getObject().stringValue());
×
344
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_QUERY_MAPPING)) {
×
345
                // Repeatable: an action may declare several query mappings (e.g. derive
346
                // maps both the row np and the local pubkey). "void" means "none".
347
                String mapping = st.getObject().stringValue();
×
348
                if (!"void".equals(mapping)) {
×
349
                    actionTemplateQueryMappingsMap.computeIfAbsent((IRI) st.getSubject(), k -> new ArrayList<>()).add(mapping);
×
350
                }
351
            } else if (st.getPredicate().equals(KPXL_TERMS.IS_VISIBLE_TO) && st.getObject() instanceof IRI objIri) {
×
352
                // Per-action visibility: gen:isVisibleTo on an action node restricts
353
                // that action button to viewers holding the given role tier or
354
                // specific role. See docs/role-specific-views.md.
355
                actionVisibleToMap.computeIfAbsent((IRI) st.getSubject(), k -> new HashSet<>()).add(objIri);
×
356
            } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
357
                labelMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
358
            } else if (st.getPredicate().equals(RDF.TYPE)) {
×
359
                if (st.getObject().equals(KPXL_TERMS.VIEW_ACTION) || st.getObject().equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
360
                    actionTemplateTypeMap.put((IRI) st.getSubject(), (IRI) st.getObject());
×
361
                }
362
            }
363
        }
×
364
        for (IRI actionIri : actionList) {
×
365
            if (actionTemplateTypeMap.containsKey(actionIri) && actionTemplateTypeMap.get(actionIri).equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
366
                viewEntryActionList.add(actionIri);
×
367
            } else {
368
                viewResultActionList.add(actionIri);
×
369
            }
370
        }
×
371
        if (!viewTypeFound) throw new IllegalArgumentException("Not a proper resource view nanopub: " + id);
×
372
        if (query == null) throw new IllegalArgumentException("Query not found: " + id);
×
373
    }
×
374

375
    /**
376
     * Stores an action-field value unless it is the {@code "void"} sentinel.
377
     * View-creation templates can't leave a statement optional inside a repeated
378
     * action group, so views carry every action field, with {@code "void"} for the
379
     * not-applicable ones (its presence is what lets Nanodash repopulate the action
380
     * group when superseding a view). It is treated here as absent — so e.g. a
381
     * "void" part field never becomes a bogus {@code param_void}.
382
     */
383
    private static void putUnlessVoid(Map<IRI, String> map, IRI key, String value) {
384
        if (value != null && !value.equals("void")) {
×
385
            map.put(key, value);
×
386
        }
387
    }
×
388

389
    /**
390
     * Gets the ID of the View.
391
     *
392
     * @return the ID of the View
393
     */
394
    public String getId() {
395
        return id;
×
396
    }
397

398
    /**
399
     * Gets the Nanopub defining this View.
400
     *
401
     * @return the Nanopub defining this View
402
     */
403
    public Nanopub getNanopub() {
404
        return nanopub;
×
405
    }
406

407
    public IRI getViewKindIri() {
408
        return viewKind;
×
409
    }
410

411
    /**
412
     * Gets the space governing this view version's {@code (kind, space)} pair via
413
     * {@code gen:governedBy}, or null if the version doesn't opt into space
414
     * governance (in which case latest-version resolution stays supersedes-based).
415
     *
416
     * @return the governing space IRI, or null
417
     */
418
    public IRI getGoverningSpace() {
419
        return governingSpace;
×
420
    }
421

422
    /**
423
     * Gets the label of the View.
424
     *
425
     * @return the label of the View
426
     */
427
    public String getLabel() {
428
        return label;
×
429
    }
430

431
    /**
432
     * Gets the title of the View.
433
     *
434
     * @return the title of the View
435
     */
436
    public String getTitle() {
437
        return title;
×
438
    }
439

440
    /**
441
     * Gets the GrlcQuery associated with the View.
442
     *
443
     * @return the GrlcQuery associated with the View
444
     */
445
    public GrlcQuery getQuery() {
446
        return query;
×
447
    }
448

449
    /**
450
     * Gets the query field of the View.
451
     *
452
     * @return the query field
453
     */
454
    public String getQueryField() {
455
        return queryField;
×
456
    }
457

458
    /**
459
     * Returns the preferred page size.
460
     *
461
     * @return page size (0 = everything on first page)
462
     */
463
    public Integer getPageSize() {
464
        return pageSize;
×
465
    }
466

467
    public Integer getDisplayWidth() {
468
        return displayWidth;
×
469
    }
470

471
    public String getStructuralPosition() {
472
        return structuralPosition;
×
473
    }
474

475
    /**
476
     * Gets the visibility restriction declared on a given action node via
477
     * {@code gen:isVisibleTo}: the set of role-tier or specific-role IRIs a viewer
478
     * must hold for that action button to be shown. An empty set means the action
479
     * is visible to everyone (subject to the existing button-list routing).
480
     *
481
     * @param actionIri the action IRI (a result or entry action of this view)
482
     * @return the set of {@code gen:isVisibleTo} IRIs for that action (never null)
483
     */
484
    public Set<IRI> getActionVisibleTo(IRI actionIri) {
485
        return actionVisibleToMap.getOrDefault(actionIri, Collections.emptySet());
×
486
    }
487

488
    /**
489
     * Gets the list of action IRIs associated with the View.
490
     *
491
     * @return the list of action IRIs
492
     */
493
    public List<IRI> getViewResultActionList() {
494
        return viewResultActionList;
×
495
    }
496

497
    public List<IRI> getViewEntryActionList() {
498
        return viewEntryActionList;
×
499
    }
500

501
    /**
502
     * Gets the Template for a given action IRI.
503
     *
504
     * @param actionIri the action IRI
505
     * @return the Template for the action IRI
506
     */
507
    public Template getTemplateForAction(IRI actionIri) {
508
        return actionTemplateMap.get(actionIri);
×
509
    }
510

511
    /**
512
     * Gets the template field for a given action IRI.
513
     *
514
     * @param actionIri the action IRI
515
     * @return the template field for the action IRI
516
     */
517
    public String getTemplateTargetFieldForAction(IRI actionIri) {
518
        return actionTemplateTargetFieldMap.get(actionIri);
×
519
    }
520

521
    public String getTemplatePartFieldForAction(IRI actionIri) {
522
        return actionTemplatePartFieldMap.get(actionIri);
×
523
    }
524

525
    /**
526
     * Gets the query mappings declared for an action: each is {@code "col:target"},
527
     * mapping result column {@code col} to template parameter {@code param_target}
528
     * — or, when {@code target} begins with {@code @}, to the raw URL parameter
529
     * {@code target} (without the {@code param_} prefix), used for fill-mode keys
530
     * such as {@code @derive-a} / {@code @supersede}. An entry action applies all
531
     * of these per row; see docs/magic-query-params.md.
532
     *
533
     * @param actionIri the action IRI
534
     * @return the list of mappings (never null; empty if none)
535
     */
536
    public List<String> getTemplateQueryMappings(IRI actionIri) {
537
        List<String> result = new ArrayList<>();
×
538
        for (String literal : actionTemplateQueryMappingsMap.getOrDefault(actionIri, Collections.emptyList())) {
×
539
            result.addAll(parseMappingLiteral(literal));
×
540
        }
×
541
        return result;
×
542
    }
543

544
    /**
545
     * Splits a query-mapping literal into individual {@code "col:target"} mappings
546
     * on whitespace. Multiple mappings share a single literal because a
547
     * view-creation template cannot repeat a statement inside its repeated action
548
     * group — e.g. {@code "np:nanopubToBeRetracted"} or
549
     * {@code "derive_target:@derive-a local_pubkey:public-key__.1"}.
550
     *
551
     * @param literal the mapping literal (may be null/blank/"void")
552
     * @return the individual mappings (never null; empty if none)
553
     */
554
    public static List<String> parseMappingLiteral(String literal) {
555
        List<String> mappings = new ArrayList<>();
12✔
556
        if (literal == null || literal.isBlank()) return mappings;
21✔
557
        for (String m : literal.trim().split("\\s+")) {
57✔
558
            if (!m.isEmpty() && !"void".equals(m)) mappings.add(m);
33!
559
        }
560
        return mappings;
6✔
561
    }
562

563
    /**
564
     * Gets the set of query result columns that serve only as <em>sources</em> for
565
     * this view's action query mappings (the {@code col} part of each
566
     * {@code "col:target"} mapping, across all actions). These columns carry
567
     * action data — conditional targets, the local-key bundle — not row content, so
568
     * the result builders skip them when rendering visible columns. A column that
569
     * happens to be both a display column and a mapping source would also be hidden;
570
     * map a duplicated/aliased column instead if you need to show one.
571
     *
572
     * @return the set of mapping-source column names (never null)
573
     */
574
    public Set<String> getActionMappingSourceColumns() {
575
        Set<String> columns = new HashSet<>();
×
576
        for (IRI actionIri : actionTemplateQueryMappingsMap.keySet()) {
×
577
            for (String mapping : getTemplateQueryMappings(actionIri)) {
×
578
                int idx = mapping.indexOf(':');
×
579
                if (idx > 0) columns.add(mapping.substring(0, idx));
×
580
            }
×
581
        }
×
582
        return columns;
×
583
    }
584

585
    /**
586
     * Gets the first query mapping for an action, or null. Kept for result-action
587
     * callers that pass a single {@code values-from-query-mapping}.
588
     *
589
     * @param actionIri the action IRI
590
     * @return the first mapping, or null
591
     */
592
    public String getTemplateQueryMapping(IRI actionIri) {
593
        List<String> mappings = actionTemplateQueryMappingsMap.get(actionIri);
×
594
        return (mappings == null || mappings.isEmpty()) ? null : mappings.get(0);
×
595
    }
596

597
    /**
598
     * Gets the label for a given action IRI.
599
     *
600
     * @param actionIri the action IRI
601
     * @return the label for the action IRI
602
     */
603
    public String getLabelForAction(IRI actionIri) {
604
        return labelMap.get(actionIri);
×
605
    }
606

607
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
608
        for (IRI namespace : appliesToNamespaces) {
×
609
            if (resourceId.startsWith(namespace.stringValue())) return true;
×
610
        }
×
611
        if (classes != null) {
×
612
            for (IRI c : classes) {
×
613
                if (appliesToClasses.contains(c)) return true;
×
614
            }
×
615
        }
616
        return false;
×
617
    }
618

619
    /**
620
     * Checks if the View has target classes.
621
     *
622
     * @return true if the View has target classes, false otherwise
623
     */
624
    public boolean appliesToClasses() {
625
        return !appliesToClasses.isEmpty();
×
626
    }
627

628
    /**
629
     * Checks if the View has a specific target class.
630
     *
631
     * @param targetClass the target class IRI
632
     * @return true if the View has the target class, false otherwise
633
     */
634
    public boolean appliesToClass(IRI targetClass) {
635
        return appliesToClasses.contains(targetClass);
×
636
    }
637

638
    @Override
639
    public String toString() {
640
        return id;
×
641
    }
642

643
    /**
644
     * Gets the view type of the View.
645
     *
646
     * @return the view type mode IRI
647
     */
648
    public IRI getViewType() {
649
        return viewType;
×
650
    }
651

652
    /**
653
     * Get the supported view types.
654
     *
655
     * @return a set of supported view type IRIs
656
     */
657
    public static Set<IRI> getSupportedViewTypes() {
658
        return supportedViewTypes;
×
659
    }
660

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