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

knowledgepixels / nanodash / 19964803313

05 Dec 2025 01:42PM UTC coverage: 15.393% (+0.02%) from 15.37%
19964803313

push

github

tkuhn
fix(ViewDisplay): Correctly consider gen:appliesTo values

594 of 4974 branches covered (11.94%)

Branch coverage included in aggregate %.

1570 of 9084 relevant lines covered (17.28%)

0.77 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 java.io.Serializable;
4
import java.util.HashSet;
5
import java.util.Set;
6

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.nanopub.Nanopub;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

16
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
17

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

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

25
    private String id;
26
    private Nanopub nanopub;
27
    private ResourceView view;
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
     * Get a ResourceView by its ID.
40
     *
41
     * @param id the ID of the ResourceView
42
     * @return the ResourceView object
43
     */
44
    public static ViewDisplay get(String id) {
45
        try {
46
            Nanopub np = Utils.getAsNanopub(id.replaceFirst("^(.*[^A-Za-z0-9-_])?(RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$2"));
×
47
            return new ViewDisplay(id, np);
×
48
        } catch (Exception ex) {
×
49
            logger.error("Couldn't load nanopub for resource: " + id, ex);
×
50
        }
51
        return null;
×
52
    }
53

54
    /**
55
     * Constructor for ViewDisplay.
56
     *
57
     * @param entry an ApiResponseEntry containing the view and nanopub ID.
58
     */
59
    private ViewDisplay(String id, Nanopub nanopub) {
×
60
        this.id = id;
×
61
        this.nanopub = nanopub;
×
62

63
        boolean viewDisplayTypeFound = false;
×
64
        for (Statement st : nanopub.getAssertion()) {
×
65
            if (st.getSubject().stringValue().equals(id)) {
×
66
                if (st.getPredicate().equals(RDF.TYPE)) {
×
67
                    if (st.getObject().equals(KPXL_TERMS.VIEW_DISPLAY)) {
×
68
                        viewDisplayTypeFound = true;
×
69
                    } else if (st.getObject() instanceof IRI objIri) {
×
70
                        types.add(objIri);
×
71
                    }
72
                } else if (st.getPredicate().equals(DCTERMS.TITLE)) {
×
73
                    title = st.getObject().stringValue();
×
74
                } else if (st.getPredicate().equals(KPXL_TERMS.IS_DISPLAY_OF_VIEW) && st.getObject() instanceof IRI objIri) {
×
75
                    if (view != null) {
×
76
                        throw new IllegalArgumentException("View already set: " + objIri);
×
77
                    }
78
                    view = ResourceView.get(objIri.stringValue());
×
79
                } else if (st.getPredicate().equals(KPXL_TERMS.IS_DISPLAY_FOR) && st.getObject() instanceof IRI objIri) {
×
80
                    if (resource != null) {
×
81
                        throw new IllegalArgumentException("Resource already set: " + objIri);
×
82
                    }
83
                    resource = objIri;
×
84
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_PAGE_SIZE) && st.getObject() instanceof Literal objL) {
×
85
                    try {
86
                        pageSize = Integer.parseInt(objL.stringValue());
×
87
                    } catch (NumberFormatException ex) {
×
88
                        logger.error("Invalid page size value: " + objL.stringValue(), ex);
×
89
                    }
×
90
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_DISPLAY_WIDTH) && st.getObject() instanceof IRI objIri) {
×
91
                    displayWidth = ResourceView.columnWidths.get(objIri);
×
92
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_DISPLAY_WIDTH) && st.getObject() instanceof Literal objL) {
×
93
                    structuralPosition = objL.stringValue();
×
94
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_NAMESPACE) && st.getObject() instanceof IRI objIri) {
×
95
                    appliesToNamespaces.add(objIri);
×
96
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_INSTANCES_OF) && st.getObject() instanceof IRI objIri) {
×
97
                    appliesToClasses.add(objIri);
×
98
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO) && st.getObject() instanceof IRI objIri) {
×
99
                    appliesTo.add(objIri.stringValue());
×
100
                }
101
            }
102
        }
×
103
        if (!viewDisplayTypeFound) throw new IllegalArgumentException("Not a proper view display nanopub: " + id);
×
104
        if (view == null) throw new IllegalArgumentException("View not found: " + id);
×
105
    }
×
106

107
    public String getId() {
108
        return id;
×
109
    }
110

111
    public boolean hasType(IRI type) {
112
        return types.contains(type);
×
113
    }
114

115
    /**
116
     * Creates a plain minimal view display without attached view object.
117
     *
118
     * @param pageSize the page size of the view display
119
     */
120
    public ViewDisplay(Integer pageSize) {
×
121
        this.pageSize = pageSize;
×
122
    }
×
123

124
    /**
125
     * Gets the ResourceView associated with this ViewDisplay.
126
     *
127
     * @return the ResourceView
128
     */
129
    public ResourceView getView() {
130
        return view;
×
131
    }
132

133
    public IRI getViewKindIri() {
134
        return view.getViewKindIri();
×
135
    }
136

137
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
138
        if (appliesTo.contains(resourceId)) return true;
×
139
        if (!appliesTo.isEmpty()) return false;
×
140
        if (appliesToNamespaces.isEmpty() && appliesToClasses.isEmpty()) {
×
141
            return view.appliesTo(resourceId, classes);
×
142
        } else {
143
            for (IRI namespace : appliesToNamespaces) {
×
144
                if (resourceId.startsWith(namespace.stringValue())) return true;
×
145
            }
×
146
            if (classes != null) {
×
147
                for (IRI c : classes) {
×
148
                    if (appliesToClasses.contains(c)) return true;
×
149
                }
×
150
            }
151
        }
152
        return false;
×
153
    }
154

155
    public boolean appliesToClasses() {
156
        if (appliesToClasses.isEmpty()) {
×
157
            return view.appliesToClasses();
×
158
        } else {
159
            return true;
×
160
        }
161
    }
162

163
    public boolean appliesToClass(IRI targetClass) {
164
        if (appliesToClasses.isEmpty()) {
×
165
            return view.appliesToClasses();
×
166
        } else {
167
            return appliesToClasses.contains(targetClass);
×
168
        }
169
    }
170

171
    /**
172
     * Gets the nanopub ID associated with this ViewDisplay.
173
     *
174
     * @return the nanopub ID
175
     */
176
    public String getNanopubId() {
177
        if (nanopub == null) return null;
×
178
        return nanopub.getUri().stringValue();
×
179
    }
180

181
    public Integer getPageSize() {
182
        if (pageSize != null) return pageSize;
×
183
        if (view == null) return 10;
×
184
        if (view.getPageSize() != null) return view.getPageSize();
×
185
        return 10;
×
186
    }
187

188
    public Integer getDisplayWidth() {
189
        if (displayWidth != null) return displayWidth;
×
190
        if (view == null) return 12;
×
191
        if (view.getDisplayWidth() != null) return view.getDisplayWidth();
×
192
        return 12;
×
193
    }
194

195
    public String getStructuralPosition() {
196
        if (structuralPosition != null) return structuralPosition;
×
197
        if (view == null) return "5.5.default";
×
198
        if (view.getStructuralPosition() != null) return view.getStructuralPosition();
×
199
        return "5.5.default";
×
200
    }
201

202
    public String getTitle() {
203
        if (title != null) return title;
×
204
        if (view != null) return view.getTitle();
×
205
        return null;
×
206
    }
207

208
    @Override
209
    public int compareTo(ViewDisplay other) {
210
        return this.getStructuralPosition().compareTo(other.getStructuralPosition());
×
211
    }
212

213
    
214

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