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

knowledgepixels / nanodash / 27135940635

08 Jun 2026 11:56AM UTC coverage: 20.712% (-0.2%) from 20.924%
27135940635

Pull #479

github

web-flow
Merge 480993615 into 77e002f9b
Pull Request #479: About pages for spaces, maintained resources, and users (#478)

1055 of 6425 branches covered (16.42%)

Branch coverage included in aggregate %.

2643 of 11429 relevant lines covered (23.13%)

3.31 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 Map<IRI, Set<IRI>> actionVisibleToMap = new HashMap<>();
×
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
                }
190
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE)) {
×
191
                Template template = TemplateData.get().getTemplate(st.getObject().stringValue());
×
192
                actionTemplateMap.put((IRI) st.getSubject(), template);
×
193
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_TARGET_FIELD)) {
×
194
                actionTemplateTargetFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
195
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_PART_FIELD)) {
×
196
                actionTemplatePartFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
197
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_QUERY_MAPPING)) {
×
198
                actionTemplateQueryMappingMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
199
            } else if (st.getPredicate().equals(KPXL_TERMS.IS_VISIBLE_TO) && st.getObject() instanceof IRI objIri) {
×
200
                // Per-action visibility: gen:isVisibleTo on an action node restricts
201
                // that action button to viewers holding the given role tier or
202
                // specific role. See docs/role-specific-views.md.
203
                actionVisibleToMap.computeIfAbsent((IRI) st.getSubject(), k -> new HashSet<>()).add(objIri);
×
204
            } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
205
                labelMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
206
            } else if (st.getPredicate().equals(RDF.TYPE)) {
×
207
                if (st.getObject().equals(KPXL_TERMS.VIEW_ACTION) || st.getObject().equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
208
                    actionTemplateTypeMap.put((IRI) st.getSubject(), (IRI) st.getObject());
×
209
                }
210
            }
211
        }
×
212
        for (IRI actionIri : actionList) {
×
213
            if (actionTemplateTypeMap.containsKey(actionIri) && actionTemplateTypeMap.get(actionIri).equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
214
                viewEntryActionList.add(actionIri);
×
215
            } else {
216
                viewResultActionList.add(actionIri);
×
217
            }
218
        }
×
219
        if (!viewTypeFound) throw new IllegalArgumentException("Not a proper resource view nanopub: " + id);
×
220
        if (query == null) throw new IllegalArgumentException("Query not found: " + id);
×
221
    }
×
222

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

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

241
    public IRI getViewKindIri() {
242
        return viewKind;
×
243
    }
244

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

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

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

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

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

290
    public Integer getDisplayWidth() {
291
        return displayWidth;
×
292
    }
293

294
    public String getStructuralPosition() {
295
        return structuralPosition;
×
296
    }
297

298
    /**
299
     * Gets the visibility restriction declared on a given action node via
300
     * {@code gen:isVisibleTo}: the set of role-tier or specific-role IRIs a viewer
301
     * must hold for that action button to be shown. An empty set means the action
302
     * is visible to everyone (subject to the existing button-list routing).
303
     *
304
     * @param actionIri the action IRI (a result or entry action of this view)
305
     * @return the set of {@code gen:isVisibleTo} IRIs for that action (never null)
306
     */
307
    public Set<IRI> getActionVisibleTo(IRI actionIri) {
308
        return actionVisibleToMap.getOrDefault(actionIri, Collections.emptySet());
×
309
    }
310

311
    /**
312
     * Gets the list of action IRIs associated with the View.
313
     *
314
     * @return the list of action IRIs
315
     */
316
    public List<IRI> getViewResultActionList() {
317
        return viewResultActionList;
×
318
    }
319

320
    public List<IRI> getViewEntryActionList() {
321
        return viewEntryActionList;
×
322
    }
323

324
    /**
325
     * Gets the Template for a given action IRI.
326
     *
327
     * @param actionIri the action IRI
328
     * @return the Template for the action IRI
329
     */
330
    public Template getTemplateForAction(IRI actionIri) {
331
        return actionTemplateMap.get(actionIri);
×
332
    }
333

334
    /**
335
     * Gets the template field for a given action IRI.
336
     *
337
     * @param actionIri the action IRI
338
     * @return the template field for the action IRI
339
     */
340
    public String getTemplateTargetFieldForAction(IRI actionIri) {
341
        return actionTemplateTargetFieldMap.get(actionIri);
×
342
    }
343

344
    public String getTemplatePartFieldForAction(IRI actionIri) {
345
        return actionTemplatePartFieldMap.get(actionIri);
×
346
    }
347

348
    public String getTemplateQueryMapping(IRI actionIri) {
349
        return actionTemplateQueryMappingMap.get(actionIri);
×
350
    }
351

352
    /**
353
     * Gets the label for a given action IRI.
354
     *
355
     * @param actionIri the action IRI
356
     * @return the label for the action IRI
357
     */
358
    public String getLabelForAction(IRI actionIri) {
359
        return labelMap.get(actionIri);
×
360
    }
361

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

374
    /**
375
     * Checks if the View has target classes.
376
     *
377
     * @return true if the View has target classes, false otherwise
378
     */
379
    public boolean appliesToClasses() {
380
        return !appliesToClasses.isEmpty();
×
381
    }
382

383
    /**
384
     * Checks if the View has a specific target class.
385
     *
386
     * @param targetClass the target class IRI
387
     * @return true if the View has the target class, false otherwise
388
     */
389
    public boolean appliesToClass(IRI targetClass) {
390
        return appliesToClasses.contains(targetClass);
×
391
    }
392

393
    @Override
394
    public String toString() {
395
        return id;
×
396
    }
397

398
    /**
399
     * Gets the view type of the View.
400
     *
401
     * @return the view type mode IRI
402
     */
403
    public IRI getViewType() {
404
        return viewType;
×
405
    }
406

407
    /**
408
     * Get the supported view types.
409
     *
410
     * @return a set of supported view type IRIs
411
     */
412
    public static Set<IRI> getSupportedViewTypes() {
413
        return supportedViewTypes;
×
414
    }
415

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