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

knowledgepixels / nanodash / 19765742917

28 Nov 2025 01:51PM UTC coverage: 14.321% (+0.3%) from 14.068%
19765742917

push

github

tkuhn
feat(ViewDisplay): Support appliesToInstancesOf/Namespace overriding

540 of 4890 branches covered (11.04%)

Branch coverage included in aggregate %.

1442 of 8950 relevant lines covered (16.11%)

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<IRI> appliesToClasses = new HashSet<>();
×
35
    private Set<IRI> appliesToNamespaces = new HashSet<>();
×
36
    private IRI resource;
37

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

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

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

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

109
    public String getId() {
110
        return id;
×
111
    }
112

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

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

131
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
132
        if (appliesToNamespaces.isEmpty()) {
×
133
            return view.appliesTo(resourceId, classes);
×
134
        } else {
135
            for (IRI namespace : appliesToNamespaces) {
×
136
                if (resourceId.startsWith(namespace.stringValue())) return true;
×
137
            }
×
138
            for (IRI c : classes) {
×
139
                if (appliesToClasses.contains(c)) return true;
×
140
            }
×
141
        }
142
        return false;
×
143
    }
144

145
    public boolean appliesToClasses() {
146
        if (appliesToClasses.isEmpty()) {
×
147
            return view.appliesToClasses();
×
148
        } else {
149
            return true;
×
150
        }
151
    }
152

153
    public boolean appliesToClass(IRI targetClass) {
154
        if (appliesToClasses.isEmpty()) {
×
155
            return view.appliesToClasses();
×
156
        } else {
157
            return appliesToClasses.contains(targetClass);
×
158
        }
159
    }
160

161
    /**
162
     * Gets the nanopub ID associated with this ViewDisplay.
163
     *
164
     * @return the nanopub ID
165
     */
166
    public String getNanopubId() {
167
        if (nanopub == null) return null;
×
168
        return nanopub.getUri().stringValue();
×
169
    }
170

171
    public Integer getPageSize() {
172
        if (pageSize != null) return pageSize;
×
173
        if (view == null) return 10;
×
174
        if (view.getPageSize() != null) return view.getPageSize();
×
175
        return 10;
×
176
    }
177

178
    public Integer getDisplayWidth() {
179
        if (displayWidth != null) return displayWidth;
×
180
        if (view == null) return 12;
×
181
        if (view.getDisplayWidth() != null) return view.getDisplayWidth();
×
182
        return 12;
×
183
    }
184

185
    public String getStructuralPosition() {
186
        if (structuralPosition != null) return structuralPosition;
×
187
        if (view == null) return "5.5.default";
×
188
        if (view.getStructuralPosition() != null) return view.getStructuralPosition();
×
189
        return "5.5.default";
×
190
    }
191

192
    public String getTitle() {
193
        if (title != null) return title;
×
194
        if (view != null) return view.getTitle();
×
195
        return null;
×
196
    }
197

198
    @Override
199
    public int compareTo(ViewDisplay other) {
200
        return this.getStructuralPosition().compareTo(other.getStructuralPosition());
×
201
    }
202

203
    
204

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