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

knowledgepixels / nanodash / 27126000317

08 Jun 2026 08:41AM UTC coverage: 20.384% (-0.5%) from 20.924%
27126000317

Pull #479

github

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

1004 of 6333 branches covered (15.85%)

Branch coverage included in aggregate %.

2603 of 11362 relevant lines covered (22.91%)

3.28 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

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

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

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

235
    public IRI getViewKindIri() {
236
        return viewKind;
×
237
    }
238

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

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

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

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

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

284
    public Integer getDisplayWidth() {
285
        return displayWidth;
×
286
    }
287

288
    public String getStructuralPosition() {
289
        return structuralPosition;
×
290
    }
291

292
    /**
293
     * Gets the list of action IRIs associated with the View.
294
     *
295
     * @return the list of action IRIs
296
     */
297
    public List<IRI> getViewResultActionList() {
298
        return viewResultActionList;
×
299
    }
300

301
    public List<IRI> getViewEntryActionList() {
302
        return viewEntryActionList;
×
303
    }
304

305
    /**
306
     * Gets the Template for a given action IRI.
307
     *
308
     * @param actionIri the action IRI
309
     * @return the Template for the action IRI
310
     */
311
    public Template getTemplateForAction(IRI actionIri) {
312
        return actionTemplateMap.get(actionIri);
×
313
    }
314

315
    /**
316
     * Gets the template field for a given action IRI.
317
     *
318
     * @param actionIri the action IRI
319
     * @return the template field for the action IRI
320
     */
321
    public String getTemplateTargetFieldForAction(IRI actionIri) {
322
        return actionTemplateTargetFieldMap.get(actionIri);
×
323
    }
324

325
    public String getTemplatePartFieldForAction(IRI actionIri) {
326
        return actionTemplatePartFieldMap.get(actionIri);
×
327
    }
328

329
    public String getTemplateQueryMapping(IRI actionIri) {
330
        return actionTemplateQueryMappingMap.get(actionIri);
×
331
    }
332

333
    /**
334
     * Gets the label for a given action IRI.
335
     *
336
     * @param actionIri the action IRI
337
     * @return the label for the action IRI
338
     */
339
    public String getLabelForAction(IRI actionIri) {
340
        return labelMap.get(actionIri);
×
341
    }
342

343
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
344
        for (IRI namespace : appliesToNamespaces) {
×
345
            if (resourceId.startsWith(namespace.stringValue())) return true;
×
346
        }
×
347
        if (classes != null) {
×
348
            for (IRI c : classes) {
×
349
                if (appliesToClasses.contains(c)) return true;
×
350
            }
×
351
        }
352
        return false;
×
353
    }
354

355
    /**
356
     * Checks if the View has target classes.
357
     *
358
     * @return true if the View has target classes, false otherwise
359
     */
360
    public boolean appliesToClasses() {
361
        return !appliesToClasses.isEmpty();
×
362
    }
363

364
    /**
365
     * Checks if the View has a specific target class.
366
     *
367
     * @param targetClass the target class IRI
368
     * @return true if the View has the target class, false otherwise
369
     */
370
    public boolean appliesToClass(IRI targetClass) {
371
        return appliesToClasses.contains(targetClass);
×
372
    }
373

374
    @Override
375
    public String toString() {
376
        return id;
×
377
    }
378

379
    /**
380
     * Gets the view type of the View.
381
     *
382
     * @return the view type mode IRI
383
     */
384
    public IRI getViewType() {
385
        return viewType;
×
386
    }
387

388
    /**
389
     * Get the supported view types.
390
     *
391
     * @return a set of supported view type IRIs
392
     */
393
    public static Set<IRI> getSupportedViewTypes() {
394
        return supportedViewTypes;
×
395
    }
396

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