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

knowledgepixels / nanodash / 19767008036

28 Nov 2025 02:44PM UTC coverage: 14.4% (-0.08%) from 14.481%
19767008036

push

github

tkuhn
feat(Space): Use new "appliesTo..." fields to calculate view displays

548 of 4898 branches covered (11.19%)

Branch coverage included in aggregate %.

1446 of 8949 relevant lines covered (16.16%)

0.72 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.HashMap;
5
import java.util.HashSet;
6
import java.util.Map;
7
import java.util.Set;
8

9
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
10
import org.eclipse.rdf4j.model.IRI;
11
import org.eclipse.rdf4j.model.Literal;
12
import org.eclipse.rdf4j.model.Statement;
13
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
14
import org.eclipse.rdf4j.model.vocabulary.RDF;
15
import org.nanopub.Nanopub;
16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
18

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

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

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

39
    private static Map<String, ViewDisplay> viewDisplays = new HashMap<>();
×
40

41
    /**
42
     * Get a ResourceView by its ID.
43
     *
44
     * @param id the ID of the ResourceView
45
     * @return the ResourceView object
46
     */
47
    public static ViewDisplay get(String id) {
48
        if (!viewDisplays.containsKey(id)) {
×
49
            try {
50
                Nanopub np = Utils.getAsNanopub(id.replaceFirst("^(.*[^A-Za-z0-9-_])?(RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$2"));
×
51
                viewDisplays.put(id, new ViewDisplay(id, np));
×
52
            } catch (Exception ex) {
×
53
                logger.error("Couldn't load nanopub for resource: " + id, ex);
×
54
            }
×
55
        }
56
        return viewDisplays.get(id);
×
57
    }
58

59
    /**
60
     * Constructor for ViewDisplay.
61
     *
62
     * @param entry an ApiResponseEntry containing the view and nanopub ID.
63
     */
64
    private ViewDisplay(String id, Nanopub nanopub) {
×
65
        this.id = id;
×
66
        this.nanopub = nanopub;
×
67

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

112
    public String getId() {
113
        return id;
×
114
    }
115

116
    public boolean hasType(IRI type) {
117
        return types.contains(type);
×
118
    }
119

120
    /**
121
     * Creates a plain minimal view display without attached view object.
122
     *
123
     * @param pageSize the page size of the view display
124
     */
125
    public ViewDisplay(Integer pageSize) {
×
126
        this.pageSize = pageSize;
×
127
    }
×
128

129
    /**
130
     * Gets the ResourceView associated with this ViewDisplay.
131
     *
132
     * @return the ResourceView
133
     */
134
    public ResourceView getView() {
135
        return view;
×
136
    }
137

138
    public IRI getViewKindIri() {
139
        return view.getViewKindIri();
×
140
    }
141

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

159
    public boolean appliesToClasses() {
160
        if (appliesToClasses.isEmpty()) {
×
161
            return view.appliesToClasses();
×
162
        } else {
163
            return true;
×
164
        }
165
    }
166

167
    public boolean appliesToClass(IRI targetClass) {
168
        if (appliesToClasses.isEmpty()) {
×
169
            return view.appliesToClasses();
×
170
        } else {
171
            return appliesToClasses.contains(targetClass);
×
172
        }
173
    }
174

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

185
    public Integer getPageSize() {
186
        if (pageSize != null) return pageSize;
×
187
        if (view == null) return 10;
×
188
        if (view.getPageSize() != null) return view.getPageSize();
×
189
        return 10;
×
190
    }
191

192
    public Integer getDisplayWidth() {
193
        if (displayWidth != null) return displayWidth;
×
194
        if (view == null) return 12;
×
195
        if (view.getDisplayWidth() != null) return view.getDisplayWidth();
×
196
        return 12;
×
197
    }
198

199
    public String getStructuralPosition() {
200
        if (structuralPosition != null) return structuralPosition;
×
201
        if (view == null) return "5.5.default";
×
202
        if (view.getStructuralPosition() != null) return view.getStructuralPosition();
×
203
        return "5.5.default";
×
204
    }
205

206
    public String getTitle() {
207
        if (title != null) return title;
×
208
        if (view != null) return view.getTitle();
×
209
        return null;
×
210
    }
211

212
    @Override
213
    public int compareTo(ViewDisplay other) {
214
        return this.getStructuralPosition().compareTo(other.getStructuralPosition());
×
215
    }
216

217
    
218

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