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

knowledgepixels / nanodash / 28875494786

07 Jul 2026 02:49PM UTC coverage: 28.201% (-0.3%) from 28.465%
28875494786

Pull #543

github

web-flow
Merge f137c9052 into f1a7efda7
Pull Request #543: feat: query-form views (gen:QueryFormView) with view-results page

1813 of 7289 branches covered (24.87%)

Branch coverage included in aggregate %.

3717 of 12320 relevant lines covered (30.17%)

4.48 hits per line

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

9.22
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 boolean queryForm = false;
×
290
    private Map<IRI, Set<IRI>> actionVisibleToMap = new HashMap<>();
×
291

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

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

393
    /**
394
     * Gets the ID of the View.
395
     *
396
     * @return the ID of the View
397
     */
398
    public String getId() {
399
        return id;
×
400
    }
401

402
    /**
403
     * Gets the Nanopub defining this View.
404
     *
405
     * @return the Nanopub defining this View
406
     */
407
    public Nanopub getNanopub() {
408
        return nanopub;
×
409
    }
410

411
    public IRI getViewKindIri() {
412
        return viewKind;
×
413
    }
414

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

426
    /**
427
     * Gets the label of the View.
428
     *
429
     * @return the label of the View
430
     */
431
    public String getLabel() {
432
        return label;
×
433
    }
434

435
    /**
436
     * Gets the title of the View.
437
     *
438
     * @return the title of the View
439
     */
440
    public String getTitle() {
441
        return title;
×
442
    }
443

444
    /**
445
     * Gets the GrlcQuery associated with the View.
446
     *
447
     * @return the GrlcQuery associated with the View
448
     */
449
    public GrlcQuery getQuery() {
450
        return query;
×
451
    }
452

453
    /**
454
     * Gets the query field of the View.
455
     *
456
     * @return the query field
457
     */
458
    public String getQueryField() {
459
        return queryField;
×
460
    }
461

462
    /**
463
     * Returns the preferred page size.
464
     *
465
     * @return page size (0 = everything on first page)
466
     */
467
    public Integer getPageSize() {
468
        return pageSize;
×
469
    }
470

471
    public Integer getDisplayWidth() {
472
        return displayWidth;
×
473
    }
474

475
    public String getStructuralPosition() {
476
        return structuralPosition;
×
477
    }
478

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

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

501
    public List<IRI> getViewEntryActionList() {
502
        return viewEntryActionList;
×
503
    }
504

505
    /**
506
     * Gets the Template for a given action IRI.
507
     *
508
     * @param actionIri the action IRI
509
     * @return the Template for the action IRI
510
     */
511
    public Template getTemplateForAction(IRI actionIri) {
512
        return actionTemplateMap.get(actionIri);
×
513
    }
514

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

525
    public String getTemplatePartFieldForAction(IRI actionIri) {
526
        return actionTemplatePartFieldMap.get(actionIri);
×
527
    }
528

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

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

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

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

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

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

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

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

642
    @Override
643
    public String toString() {
644
        return id;
×
645
    }
646

647
    /**
648
     * Gets the view type of the View.
649
     *
650
     * @return the view type mode IRI
651
     */
652
    public IRI getViewType() {
653
        return viewType;
×
654
    }
655

656
    /**
657
     * Whether this view is additionally typed {@code gen:QueryFormView}: on a resource
658
     * page it renders as a form for the query placeholders not auto-filled from the
659
     * page context, whose submission leads to the full results page. Orthogonal to
660
     * {@link #getViewType()}, which then determines how those results render.
661
     *
662
     * @return true if this is a query-form view
663
     */
664
    public boolean hasQueryForm() {
665
        return queryForm;
×
666
    }
667

668
    /**
669
     * Get the supported view types.
670
     *
671
     * @return a set of supported view type IRIs
672
     */
673
    public static Set<IRI> getSupportedViewTypes() {
674
        return supportedViewTypes;
×
675
    }
676

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