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

knowledgepixels / nanodash / 19765999519

28 Nov 2025 02:02PM UTC coverage: 14.481% (+0.2%) from 14.321%
19765999519

push

github

tkuhn
feat(ViewDisplay): Add plain 'appliesTo' predicate support

547 of 4898 branches covered (11.17%)

Branch coverage included in aggregate %.

1459 of 8955 relevant lines covered (16.29%)

0.73 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
    /**
117
     * Creates a plain minimal view display without attached view object.
118
     *
119
     * @param pageSize the page size of the view display
120
     */
121
    public ViewDisplay(Integer pageSize) {
×
122
        this.pageSize = pageSize;
×
123
    }
×
124

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

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

149
    public boolean appliesToClasses() {
150
        if (appliesToClasses.isEmpty()) {
×
151
            return view.appliesToClasses();
×
152
        } else {
153
            return true;
×
154
        }
155
    }
156

157
    public boolean appliesToClass(IRI targetClass) {
158
        if (appliesToClasses.isEmpty()) {
×
159
            return view.appliesToClasses();
×
160
        } else {
161
            return appliesToClasses.contains(targetClass);
×
162
        }
163
    }
164

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

175
    public Integer getPageSize() {
176
        if (pageSize != null) return pageSize;
×
177
        if (view == null) return 10;
×
178
        if (view.getPageSize() != null) return view.getPageSize();
×
179
        return 10;
×
180
    }
181

182
    public Integer getDisplayWidth() {
183
        if (displayWidth != null) return displayWidth;
×
184
        if (view == null) return 12;
×
185
        if (view.getDisplayWidth() != null) return view.getDisplayWidth();
×
186
        return 12;
×
187
    }
188

189
    public String getStructuralPosition() {
190
        if (structuralPosition != null) return structuralPosition;
×
191
        if (view == null) return "5.5.default";
×
192
        if (view.getStructuralPosition() != null) return view.getStructuralPosition();
×
193
        return "5.5.default";
×
194
    }
195

196
    public String getTitle() {
197
        if (title != null) return title;
×
198
        if (view != null) return view.getTitle();
×
199
        return null;
×
200
    }
201

202
    @Override
203
    public int compareTo(ViewDisplay other) {
204
        return this.getStructuralPosition().compareTo(other.getStructuralPosition());
×
205
    }
206

207
    
208

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