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

knowledgepixels / nanodash / 30517559333

30 Jul 2026 05:46AM UTC coverage: 34.157% (-0.05%) from 34.211%
30517559333

push

github

web-flow
Merge pull request #574 from knowledgepixels/view-displays-unresolved-query

feat(views): switch Content-tab displays to unresolved query with client-side version resolution

2458 of 7970 branches covered (30.84%)

Branch coverage included in aggregate %.

4797 of 13270 relevant lines covered (36.15%)

5.59 hits per line

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

0.0
src/main/java/com/knowledgepixels/nanodash/ViewDisplay.java
1
package com.knowledgepixels.nanodash;
2

3
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
4
import org.eclipse.rdf4j.model.IRI;
5
import org.eclipse.rdf4j.model.Literal;
6
import org.eclipse.rdf4j.model.Statement;
7
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
8
import org.eclipse.rdf4j.model.vocabulary.RDF;
9
import org.nanopub.Nanopub;
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12

13
import java.io.Serializable;
14
import java.util.HashSet;
15
import java.util.Set;
16

17
/**
18
 * A class representing the display of a resource view associated with a Space.
19
 */
20
public class ViewDisplay implements Serializable, Comparable<ViewDisplay> {
21

22
    private static final Logger logger = LoggerFactory.getLogger(ViewDisplay.class);
×
23

24
    private String id;
25
    private Nanopub nanopub;
26
    private View view;
27
    private IRI viewIri;
28
    private String title;
29
    private Integer pageSize;
30
    private Integer displayWidth;
31
    private String structuralPosition;
32
    private Set<IRI> types = new HashSet<>();
×
33
    private Set<String> appliesTo = new HashSet<>();
×
34
    private Set<IRI> appliesToClasses = new HashSet<>();
×
35
    private Set<IRI> appliesToNamespaces = new HashSet<>();
×
36
    private IRI resource;
37

38
    /**
39
     * Constructor for ViewDisplay with only a View. This is used for temporary view displays used in profiles as defaults.
40
     *
41
     * @param view the View associated with this ViewDisplay
42
     */
43
    public ViewDisplay(View view) {
×
44
        this.id = null;
×
45
        this.nanopub = view.getNanopub();
×
46
        this.view = view;
×
47
    }
×
48

49
    /**
50
     * Get a View by its ID.
51
     *
52
     * @param id the ID of the View
53
     * @return the View object
54
     */
55
    public static ViewDisplay get(String id) throws IllegalArgumentException {
56
        return get(id, null);
×
57
    }
58

59
    /**
60
     * Get a ViewDisplay by its ID, using a pre-resolved latest view IRI.
61
     *
62
     * <p>The display nanopub is loaded directly, without a per-display latest-version
63
     * lookup: the get-view-displays query already returns only current display
64
     * nanopubs (its {@code npx:invalidates} filter excludes superseded ones, since
65
     * superseding a display also invalidates it under the same signing key). The
66
     * displayed view is likewise loaded directly from {@code latestViewIri}, which the
67
     * query resolved to its latest version server-side. The result: no one-by-one
68
     * latest-version network round-trips when building the view displays for a page.</p>
69
     *
70
     * @param id            the ID of the ViewDisplay (a current display nanopub)
71
     * @param latestViewIri the already latest-resolved IRI of the displayed view (from
72
     *                      the get-view-displays query), or null to resolve the view's
73
     *                      latest version separately
74
     * @return the ViewDisplay object
75
     */
76
    public static ViewDisplay get(String id, String latestViewIri) throws IllegalArgumentException {
77
        try {
78
            Nanopub np = Utils.getAsNanopub(id.replaceFirst("^(.*[^A-Za-z0-9-_])?(RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$2"));
×
79
            return new ViewDisplay(id, np, latestViewIri);
×
80
        } catch (Exception ex) {
×
81
            logger.error("Couldn't load nanopub for resource: {}", id, ex);
×
82
            throw new IllegalArgumentException("invalid view value " + id);
×
83
        }
84
    }
85

86
    /**
87
     * Builds a view display derived from a preset assignment (issue #302).
88
     *
89
     * <p>Used for the preset-derived rows emitted by the {@code get-view-displays}
90
     * query: instead of a standalone view-display nanopub, the row carries the
91
     * resolved view IRI plus the assignment's activation state. The resulting
92
     * object behaves like a top-level view display for {@code resourceId}, so it
93
     * flows through the same latest-wins / deactivation aggregation in
94
     * {@link com.knowledgepixels.nanodash.domain.AbstractResourceWithProfile} as
95
     * standalone displays.</p>
96
     *
97
     * @param resourceId    the resource the preset is assigned to
98
     * @param viewIri       the view IRI (a {@code gen:hasView} /
99
     *                      {@code gen:hasTopLevelView} target of the preset)
100
     * @param topLevel      whether this came from {@code gen:hasTopLevelView} (shown
101
     *                      at the top level of the resource page) vs. {@code gen:hasView}
102
     *                      (shown at the part level, for parts matching the view's own
103
     *                      class/namespace targeting)
104
     * @param deactivated   whether the underlying preset assignment is deactivated
105
     * @param resolveLatest whether to resolve {@code viewIri} to its latest version
106
     *                      here; pass false when the query already resolved it
107
     *                      server-side, true for the unresolved query variant
108
     * @return the ViewDisplay, or null if the view could not be resolved
109
     */
110
    public static ViewDisplay forPresetView(String resourceId, String viewIri, boolean topLevel, boolean deactivated, boolean resolveLatest) {
111
        View view = View.get(viewIri, resolveLatest);
×
112
        if (view == null) {
×
113
            logger.error("Couldn't resolve preset view: {}", viewIri);
×
114
            return null;
×
115
        }
116
        return new ViewDisplay(resourceId, view, topLevel, deactivated);
×
117
    }
118

119
    private ViewDisplay(String resourceId, View view, boolean topLevel, boolean deactivated) {
×
120
        this.id = null;
×
121
        this.nanopub = view.getNanopub();
×
122
        this.view = view;
×
123
        if (topLevel) {
×
124
            // gen:hasTopLevelView: pin to the resource's own page (top level).
125
            this.appliesTo.add(resourceId);
×
126
        }
127
        // else gen:hasView: leave appliesTo empty so applicability falls back to the
128
        // view's own class/namespace targeting -> shown at the part level for matching
129
        // parts, not at the resource's top level.
130
        if (deactivated) {
×
131
            this.types.add(KPXL_TERMS.DEACTIVATED_VIEW_DISPLAY);
×
132
        }
133
    }
×
134

135
    /**
136
     * Constructor for ViewDisplay.
137
     *
138
     * @param id      the ID of the ViewDisplay
139
     * @param nanopub the Nanopub containing the data for this ViewDisplay
140
     */
141
    private ViewDisplay(String id, Nanopub nanopub) {
142
        this(id, nanopub, null);
×
143
    }
×
144

145
    /**
146
     * Constructor for ViewDisplay.
147
     *
148
     * @param id            the ID of the ViewDisplay
149
     * @param nanopub       the Nanopub containing the data for this ViewDisplay
150
     * @param latestViewIri the already latest-resolved IRI of the displayed view, or
151
     *                      null to resolve the view's latest version separately. When
152
     *                      given, the view is loaded directly (no extra round-trip).
153
     */
154
    private ViewDisplay(String id, Nanopub nanopub, String latestViewIri) {
×
155
        this.id = id;
×
156
        this.nanopub = nanopub;
×
157

158
        boolean viewDisplayTypeFound = false;
×
159
        for (Statement st : nanopub.getAssertion()) {
×
160
            if (st.getSubject().stringValue().equals(id)) {
×
161
                if (st.getPredicate().equals(RDF.TYPE)) {
×
162
                    if (st.getObject().equals(KPXL_TERMS.VIEW_DISPLAY) || st.getObject().equals(KPXL_TERMS.DEACTIVATED_VIEW_DISPLAY)) {
×
163
                        viewDisplayTypeFound = true;
×
164
                    }
165
                    if (st.getObject() instanceof IRI objIri && !st.getObject().equals(KPXL_TERMS.VIEW_DISPLAY)) {
×
166
                        types.add(objIri);
×
167
                    }
168
                } else if (st.getPredicate().equals(DCTERMS.TITLE)) {
×
169
                    title = st.getObject().stringValue();
×
170
                } else if (st.getPredicate().equals(KPXL_TERMS.IS_DISPLAY_OF_VIEW) && st.getObject() instanceof IRI objIri) {
×
171
                    if (view != null) {
×
172
                        throw new IllegalArgumentException("View already set: " + objIri);
×
173
                    }
174
                    viewIri = objIri;
×
175
                    view = (latestViewIri != null && !latestViewIri.isEmpty())
×
176
                            ? View.get(latestViewIri, false)
×
177
                            : View.get(objIri.stringValue());
×
178
                } else if (st.getPredicate().equals(KPXL_TERMS.IS_DISPLAY_FOR) && st.getObject() instanceof IRI objIri) {
×
179
                    if (resource != null) {
×
180
                        throw new IllegalArgumentException("Resource already set: " + objIri);
×
181
                    }
182
                    resource = objIri;
×
183
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_PAGE_SIZE) && st.getObject() instanceof Literal objL) {
×
184
                    try {
185
                        pageSize = Integer.parseInt(objL.stringValue());
×
186
                    } catch (NumberFormatException ex) {
×
187
                        logger.error("Invalid page size value: {}", objL.stringValue(), ex);
×
188
                    }
×
189
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_DISPLAY_WIDTH) && st.getObject() instanceof IRI objIri) {
×
190
                    displayWidth = View.columnWidths.get(objIri);
×
191
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_STRUCTURAL_POSITION) && st.getObject() instanceof Literal objL) {
×
192
                    structuralPosition = objL.stringValue();
×
193
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_NAMESPACE) && st.getObject() instanceof IRI objIri) {
×
194
                    appliesToNamespaces.add(objIri);
×
195
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_INSTANCES_OF) && st.getObject() instanceof IRI objIri) {
×
196
                    appliesToClasses.add(objIri);
×
197
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO) && st.getObject() instanceof IRI objIri) {
×
198
                    appliesTo.add(objIri.stringValue());
×
199
                }
200
            }
201
        }
×
202
        if (!viewDisplayTypeFound) throw new IllegalArgumentException("Not a proper view display nanopub: " + id);
×
203
        if (view == null) throw new IllegalArgumentException("View not found: " + id);
×
204
    }
×
205

206
    public String getId() {
207
        return id;
×
208
    }
209

210
    public boolean hasType(IRI type) {
211
        return types.contains(type);
×
212
    }
213

214
    /**
215
     * Creates a plain minimal view display without attached view object.
216
     *
217
     * @param pageSize the page size of the view display
218
     */
219
    public ViewDisplay(Integer pageSize) {
×
220
        this.pageSize = pageSize;
×
221
    }
×
222

223
    /**
224
     * Gets the View associated with this ViewDisplay.
225
     *
226
     * @return the View
227
     */
228
    public View getView() {
229
        return view;
×
230
    }
231

232
    public IRI getViewIri() {
233
        return viewIri;
×
234
    }
235

236
    public IRI getViewKindIri() {
237
        IRI kind = view.getViewKindIri();
×
238
        if (kind != null) return kind;
×
239
        return viewIri;
×
240
    }
241

242
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
243
        if (appliesTo.contains(resourceId)) return true;
×
244
        if (appliesToNamespaces.isEmpty() && appliesToClasses.isEmpty()) {
×
245
            if (!appliesTo.isEmpty()) return false;
×
246
            return view.appliesTo(resourceId, classes);
×
247
        } else {
248
            for (IRI namespace : appliesToNamespaces) {
×
249
                if (resourceId.startsWith(namespace.stringValue())) return true;
×
250
            }
×
251
            if (classes != null) {
×
252
                for (IRI c : classes) {
×
253
                    if (appliesToClasses.contains(c)) return true;
×
254
                }
×
255
            }
256
        }
257
        return false;
×
258
    }
259

260
    public boolean appliesToClasses() {
261
        if (appliesToClasses.isEmpty()) {
×
262
            return view.appliesToClasses();
×
263
        } else {
264
            return true;
×
265
        }
266
    }
267

268
    public boolean appliesToClass(IRI targetClass) {
269
        if (appliesToClasses.isEmpty()) {
×
270
            return view.appliesToClasses();
×
271
        } else {
272
            return appliesToClasses.contains(targetClass);
×
273
        }
274
    }
275

276
    /**
277
     * Gets the nanopub ID associated with this ViewDisplay
278
     *
279
     * @return the nanopub ID
280
     */
281
    public IRI getNanopubId() {
282
        if (nanopub == null) {
×
283
            return null;
×
284
        }
285
        return nanopub.getUri();
×
286
    }
287

288
    /**
289
     * Gets the nanopub associated with this ViewDisplay
290
     *
291
     * @return the nanopub, or null if not available
292
     */
293
    public Nanopub getNanopub() {
294
        return nanopub;
×
295
    }
296

297
    public Integer getPageSize() {
298
        if (pageSize != null) return pageSize;
×
299
        if (view == null) return 10;
×
300
        if (view.getPageSize() != null) return view.getPageSize();
×
301
        return 10;
×
302
    }
303

304
    public Integer getDisplayWidth() {
305
        if (displayWidth != null) return displayWidth;
×
306
        if (view == null) return 12;
×
307
        if (view.getDisplayWidth() != null) return view.getDisplayWidth();
×
308
        return 12;
×
309
    }
310

311
    public ViewDisplay withDisplayWidth(int width) {
312
        this.displayWidth = width;
×
313
        return this;
×
314
    }
315

316
    /**
317
     * Overrides the title shown by the result components. An empty string hides the
318
     * title row entirely (used on the view-results page, where the query form above
319
     * already shows the view's icon+title).
320
     *
321
     * @param title the title to show, or "" to hide it
322
     * @return this view display, for chaining
323
     */
324
    public ViewDisplay withTitle(String title) {
325
        this.title = title;
×
326
        return this;
×
327
    }
328

329
    public String getStructuralPosition() {
330
        if (structuralPosition != null) return structuralPosition;
×
331
        if (view == null) return "5.5.default";
×
332
        if (view.getStructuralPosition() != null) return view.getStructuralPosition();
×
333
        return "5.5.default";
×
334
    }
335

336
    public String getTitle() {
337
        if (title != null) return title;
×
338
        if (view != null) return view.getTitle();
×
339
        return null;
×
340
    }
341

342
    @Override
343
    public int compareTo(ViewDisplay other) {
344
        return this.getStructuralPosition().compareTo(other.getStructuralPosition());
×
345
    }
346

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