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

knowledgepixels / nanodash / 22630596072

03 Mar 2026 03:41PM UTC coverage: 15.949% (-0.08%) from 16.03%
22630596072

Pull #369

github

web-flow
Merge 6951bc4cf into 85e0af2dc
Pull Request #369: Replace "^" for view displays with dropdown menu

699 of 5317 branches covered (13.15%)

Branch coverage included in aggregate %.

1721 of 9856 relevant lines covered (17.46%)

2.4 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 String title;
28
    private Integer pageSize;
29
    private Integer displayWidth;
30
    private String structuralPosition;
31
    private Set<IRI> types = new HashSet<>();
×
32
    private Set<String> appliesTo = new HashSet<>();
×
33
    private Set<IRI> appliesToClasses = new HashSet<>();
×
34
    private Set<IRI> appliesToNamespaces = new HashSet<>();
×
35
    private IRI resource;
36

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

48
    /**
49
     * Get a View by its ID.
50
     *
51
     * @param id the ID of the View
52
     * @return the View object
53
     */
54
    public static ViewDisplay get(String id) throws IllegalArgumentException {
55
        try {
56
            Nanopub np = Utils.getAsNanopub(id.replaceFirst("^(.*[^A-Za-z0-9-_])?(RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$2"));
×
57
            return new ViewDisplay(id, np);
×
58
        } catch (Exception ex) {
×
59
            logger.error("Couldn't load nanopub for resource: {}", id, ex);
×
60
            throw new IllegalArgumentException("invalid view value " + id);
×
61
        }
62
    }
63

64
    /**
65
     * Constructor for ViewDisplay.
66
     *
67
     * @param id      the ID of the ViewDisplay
68
     * @param nanopub the Nanopub containing the data for this ViewDisplay
69
     */
70
    private ViewDisplay(String id, Nanopub nanopub) {
×
71
        this.id = id;
×
72
        this.nanopub = nanopub;
×
73

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

118
    public String getId() {
119
        return id;
×
120
    }
121

122
    public boolean hasType(IRI type) {
123
        return types.contains(type);
×
124
    }
125

126
    /**
127
     * Creates a plain minimal view display without attached view object.
128
     *
129
     * @param pageSize the page size of the view display
130
     */
131
    public ViewDisplay(Integer pageSize) {
×
132
        this.pageSize = pageSize;
×
133
    }
×
134

135
    /**
136
     * Gets the View associated with this ViewDisplay.
137
     *
138
     * @return the View
139
     */
140
    public View getView() {
141
        return view;
×
142
    }
143

144
    public IRI getViewKindIri() {
145
        return view.getViewKindIri();
×
146
    }
147

148
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
149
        if (appliesTo.contains(resourceId)) return true;
×
150
        if (!appliesTo.isEmpty()) return false;
×
151
        if (appliesToNamespaces.isEmpty() && appliesToClasses.isEmpty()) {
×
152
            return view.appliesTo(resourceId, classes);
×
153
        } else {
154
            for (IRI namespace : appliesToNamespaces) {
×
155
                if (resourceId.startsWith(namespace.stringValue())) return true;
×
156
            }
×
157
            if (classes != null) {
×
158
                for (IRI c : classes) {
×
159
                    if (appliesToClasses.contains(c)) return true;
×
160
                }
×
161
            }
162
        }
163
        return false;
×
164
    }
165

166
    public boolean appliesToClasses() {
167
        if (appliesToClasses.isEmpty()) {
×
168
            return view.appliesToClasses();
×
169
        } else {
170
            return true;
×
171
        }
172
    }
173

174
    public boolean appliesToClass(IRI targetClass) {
175
        if (appliesToClasses.isEmpty()) {
×
176
            return view.appliesToClasses();
×
177
        } else {
178
            return appliesToClasses.contains(targetClass);
×
179
        }
180
    }
181

182
    /**
183
     * Gets the nanopub ID associated with this ViewDisplay
184
     *
185
     * @return the nanopub ID
186
     */
187
    public IRI getNanopubId() {
188
        if (nanopub == null) {
×
189
            return null;
×
190
        }
191
        return nanopub.getUri();
×
192
    }
193

194
    /**
195
     * Gets the nanopub associated with this ViewDisplay
196
     *
197
     * @return the nanopub, or null if not available
198
     */
199
    public Nanopub getNanopub() {
200
        return nanopub;
×
201
    }
202

203
    public Integer getPageSize() {
204
        if (pageSize != null) return pageSize;
×
205
        if (view == null) return 10;
×
206
        if (view.getPageSize() != null) return view.getPageSize();
×
207
        return 10;
×
208
    }
209

210
    public Integer getDisplayWidth() {
211
        if (displayWidth != null) return displayWidth;
×
212
        if (view == null) return 12;
×
213
        if (view.getDisplayWidth() != null) return view.getDisplayWidth();
×
214
        return 12;
×
215
    }
216

217
    public String getStructuralPosition() {
218
        if (structuralPosition != null) return structuralPosition;
×
219
        if (view == null) return "5.5.default";
×
220
        if (view.getStructuralPosition() != null) return view.getStructuralPosition();
×
221
        return "5.5.default";
×
222
    }
223

224
    public String getTitle() {
225
        if (title != null) return title;
×
226
        if (view != null) return view.getTitle();
×
227
        return null;
×
228
    }
229

230
    @Override
231
    public int compareTo(ViewDisplay other) {
232
        return this.getStructuralPosition().compareTo(other.getStructuralPosition());
×
233
    }
234

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