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

knowledgepixels / nanodash / 19765556252

28 Nov 2025 01:43PM UTC coverage: 14.068% (-0.3%) from 14.373%
19765556252

push

github

tkuhn
feat(ResourceView): Support new appliesToInstancesOf/Namespace preds

522 of 4868 branches covered (10.72%)

Branch coverage included in aggregate %.

1419 of 8929 relevant lines covered (15.89%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
src/main/java/com/knowledgepixels/nanodash/ResourceView.java
1
package com.knowledgepixels.nanodash;
2

3
import java.io.Serializable;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10

11
import org.eclipse.rdf4j.model.IRI;
12
import org.eclipse.rdf4j.model.Literal;
13
import org.eclipse.rdf4j.model.Statement;
14
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
15
import org.eclipse.rdf4j.model.vocabulary.RDF;
16
import org.eclipse.rdf4j.model.vocabulary.RDFS;
17
import org.nanopub.Nanopub;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

21
import com.knowledgepixels.nanodash.template.Template;
22
import com.knowledgepixels.nanodash.template.TemplateData;
23
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
24

25
/**
26
 * A class representing a Resource View.
27
 */
28
public class ResourceView implements Serializable {
29

30
    private static final Logger logger = LoggerFactory.getLogger(ResourceView.class);
×
31

32
    static Map<IRI, Integer> columnWidths = new HashMap<>();
×
33

34
    static {
35
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_1_OF_12, 1);
×
36
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_2_OF_12, 2);
×
37
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_3_OF_12, 3);
×
38
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_4_OF_12, 4);
×
39
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_5_OF_12, 5);
×
40
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_6_OF_12, 6);
×
41
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_7_OF_12, 7);
×
42
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_8_OF_12, 8);
×
43
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_9_OF_12, 9);
×
44
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_10_OF_12, 10);
×
45
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_11_OF_12, 11);
×
46
        columnWidths.put(KPXL_TERMS.COLUMN_WIDTH_12_OF_12, 12);
×
47
    }
48

49
    private static Map<String, ResourceView> resourceViews = new HashMap<>();
×
50

51
    /**
52
     * Get a ResourceView by its ID.
53
     *
54
     * @param id the ID of the ResourceView
55
     * @return the ResourceView object
56
     */
57
    public static ResourceView get(String id) {
58
        if (!resourceViews.containsKey(id)) {
×
59
            try {
60
                Nanopub np = Utils.getAsNanopub(id.replaceFirst("^(.*[^A-Za-z0-9-_])?(RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$2"));
×
61
                resourceViews.put(id, new ResourceView(id, np));
×
62
            } catch (Exception ex) {
×
63
                logger.error("Couldn't load nanopub for resource: " + id, ex);
×
64
            }
×
65
        }
66
        return resourceViews.get(id);
×
67
    }
68

69
    private String id;
70
    private Nanopub nanopub;
71
    private String label;
72
    private String title = "View";
×
73
    private GrlcQuery query;
74
    private String queryField = "resource";
×
75
    private Integer pageSize;
76
    private Integer displayWidth;
77
    private String structuralPosition;
78
    private List<IRI> viewResultActionList = new ArrayList<>();
×
79
    private List<IRI> viewEntryActionList = new ArrayList<>();
×
80
    private Set<IRI> appliesToClasses = new HashSet<>();
×
81
    private Set<IRI> appliesToNamespaces = new HashSet<>();
×
82
    private Map<IRI, Template> actionTemplateMap = new HashMap<>();
×
83
    private Map<IRI, String> actionTemplateTargetFieldMap = new HashMap<>();
×
84
    private Map<IRI, IRI> actionTemplateTypeMap = new HashMap<>();
×
85
    private Map<IRI, String> actionTemplatePartFieldMap = new HashMap<>();
×
86
    private Map<IRI, String> actionTemplateQueryMappingMap = new HashMap<>();
×
87
    private Map<IRI, String> labelMap = new HashMap<>();
×
88
    private IRI viewType;
89

90
    private ResourceView(String id, Nanopub nanopub) {
×
91
        this.id = id;
×
92
        this.nanopub = nanopub;
×
93
        List<IRI> actionList = new ArrayList<>();
×
94
        boolean resourceViewTypeFound = false;
×
95
        for (Statement st : nanopub.getAssertion()) {
×
96
            if (st.getSubject().stringValue().equals(id)) {
×
97
                if (st.getPredicate().equals(RDF.TYPE)) {
×
98
                    if (st.getObject().equals(KPXL_TERMS.RESOURCE_VIEW)) {
×
99
                        resourceViewTypeFound = true;
×
100
                    }
101
                    if (st.getObject().equals(KPXL_TERMS.TABULAR_VIEW) || st.getObject().equals(KPXL_TERMS.LIST_VIEW)) {
×
102
                        viewType = (IRI) st.getObject();
×
103
                    }
104
                } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
105
                    label = st.getObject().stringValue();
×
106
                } else if (st.getPredicate().equals(DCTERMS.TITLE)) {
×
107
                    title = st.getObject().stringValue();
×
108
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_QUERY)) {
×
109
                    query = GrlcQuery.get(st.getObject().stringValue());
×
110
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_QUERY_TARGET_FIELD)) {
×
111
                    queryField = st.getObject().stringValue();
×
112
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_ACTION) && st.getObject() instanceof IRI objIri) {
×
113
                    actionList.add(objIri);
×
114
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_NAMESPACE) && st.getObject() instanceof IRI objIri) {
×
115
                    appliesToNamespaces.add(objIri);
×
116
                } else if (st.getPredicate().equals(KPXL_TERMS.APPLIES_TO_INSTANCES_OF) && st.getObject() instanceof IRI objIri) {
×
117
                    appliesToClasses.add(objIri);
×
118
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_TARGET_CLASS) && st.getObject() instanceof IRI objIri) {
×
119
                    // Deprecated
120
                    appliesToClasses.add(objIri);
×
121
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_PAGE_SIZE) && st.getObject() instanceof Literal objL) {
×
122
                    try {
123
                        pageSize = Integer.parseInt(objL.stringValue());
×
124
                    } catch (NumberFormatException ex) {
×
125
                        logger.error("Invalid page size value: " + objL.stringValue(), ex);
×
126
                    }
×
127
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_DISPLAY_WIDTH) && st.getObject() instanceof IRI objIri) {
×
128
                    displayWidth = columnWidths.get(objIri);
×
129
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_STRUCTURAL_POSITION) && st.getObject() instanceof Literal objL) {
×
130
                    structuralPosition = objL.stringValue();
×
131
                }
132
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE)) {
×
133
                Template template = TemplateData.get().getTemplate(st.getObject().stringValue());
×
134
                actionTemplateMap.put((IRI) st.getSubject(), template);
×
135
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_TARGET_FIELD)) {
×
136
                actionTemplateTargetFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
137
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_PART_FIELD)) {
×
138
                actionTemplatePartFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
139
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_QUERY_MAPPING)) {
×
140
                actionTemplateQueryMappingMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
141
            } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
142
                labelMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
143
            } else if (st.getPredicate().equals(RDF.TYPE)) {
×
144
                if (st.getObject().equals(KPXL_TERMS.VIEW_ACTION) || st.getObject().equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
145
                    actionTemplateTypeMap.put((IRI) st.getSubject(), (IRI) st.getObject());
×
146
                }
147
            }
148
        }
×
149
        for (IRI actionIri : actionList) {
×
150
            if (actionTemplateTypeMap.containsKey(actionIri) && actionTemplateTypeMap.get(actionIri).equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
151
                viewEntryActionList.add(actionIri);
×
152
            } else {
153
                viewResultActionList.add(actionIri);
×
154
            }
155
        }
×
156
        if (!resourceViewTypeFound) throw new IllegalArgumentException("Not a proper resource view nanopub: " + id);
×
157
        if (query == null) throw new IllegalArgumentException("Query not found: " + id);
×
158
    }
×
159

160
    /**
161
     * Gets the ID of the ResourceView.
162
     *
163
     * @return the ID of the ResourceView
164
     */
165
    public String getId() {
166
        return id;
×
167
    }
168

169
    /**
170
     * Gets the Nanopub defining this ResourceView.
171
     *
172
     * @return the Nanopub defining this ResourceView
173
     */
174
    public Nanopub getNanopub() {
175
        return nanopub;
×
176
    }
177

178
    /**
179
     * Gets the label of the ResourceView.
180
     *
181
     * @return the label of the ResourceView
182
     */
183
    public String getLabel() {
184
        return label;
×
185
    }
186

187
    /**
188
     * Gets the title of the ResourceView.
189
     *
190
     * @return the title of the ResourceView
191
     */
192
    public String getTitle() {
193
        return title;
×
194
    }
195

196
    /**
197
     * Gets the GrlcQuery associated with the ResourceView.
198
     *
199
     * @return the GrlcQuery associated with the ResourceView
200
     */
201
    public GrlcQuery getQuery() {
202
        return query;
×
203
    }
204

205
    /**
206
     * Gets the query field of the ResourceView.
207
     *
208
     * @return the query field
209
     */
210
    public String getQueryField() {
211
        return queryField;
×
212
    }
213

214
    /**
215
     * Returns the preferred page size.
216
     *
217
     * @return page size (0 = everything on first page)
218
     */
219
    public Integer getPageSize() {
220
        return pageSize;
×
221
    }
222

223
    public Integer getDisplayWidth() {
224
        return displayWidth;
×
225
    }
226

227
    public String getStructuralPosition() {
228
        return structuralPosition;
×
229
    }
230

231
    /**
232
     * Gets the list of action IRIs associated with the ResourceView.
233
     *
234
     * @return the list of action IRIs
235
     */
236
    public List<IRI> getViewResultActionList() {
237
        return viewResultActionList;
×
238
    }
239

240
    public List<IRI> getViewEntryActionList() {
241
        return viewEntryActionList;
×
242
    }
243

244
    /**
245
     * Gets the Template for a given action IRI.
246
     *
247
     * @param actionIri the action IRI
248
     * @return the Template for the action IRI
249
     */
250
    public Template getTemplateForAction(IRI actionIri) {
251
        return actionTemplateMap.get(actionIri);
×
252
    }
253

254
    /**
255
     * Gets the template field for a given action IRI.
256
     *
257
     * @param actionIri the action IRI
258
     * @return the template field for the action IRI
259
     */
260
    public String getTemplateTargetFieldForAction(IRI actionIri) {
261
        return actionTemplateTargetFieldMap.get(actionIri);
×
262
    }
263

264
    public String getTemplatePartFieldForAction(IRI actionIri) {
265
        return actionTemplatePartFieldMap.get(actionIri);
×
266
    }
267

268
    public String getTemplateQueryMapping(IRI actionIri) {
269
        return actionTemplateQueryMappingMap.get(actionIri);
×
270
    }
271

272
    /**
273
     * Gets the label for a given action IRI.
274
     *
275
     * @param actionIri the action IRI
276
     * @return the label for the action IRI
277
     */
278
    public String getLabelForAction(IRI actionIri) {
279
        return labelMap.get(actionIri);
×
280
    }
281

282
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
283
        for (IRI namespace : appliesToNamespaces) {
×
284
            if (resourceId.startsWith(namespace.stringValue())) return true;
×
285
        }
×
286
        for (IRI c : classes) {
×
287
            if (appliesToClasses.contains(c)) return true;
×
288
        }
×
289
        return false;
×
290
    }
291

292
    /**
293
     * Checks if the ResourceView has target classes.
294
     *
295
     * @return true if the ResourceView has target classes, false otherwise
296
     */
297
    public boolean appliesToClasses() {
298
        return !appliesToClasses.isEmpty();
×
299
    }
300

301
    /**
302
     * Checks if the ResourceView has a specific target class.
303
     *
304
     * @param targetClass the target class IRI
305
     * @return true if the ResourceView has the target class, false otherwise
306
     */
307
    public boolean appliesToClass(IRI targetClass) {
308
        return appliesToClasses.contains(targetClass);
×
309
    }
310

311
    @Override
312
    public String toString() {
313
        return id;
×
314
    }
315

316
    /**
317
     * Gets the view type of the ResourceView.
318
     *
319
     * @return the view type mode IRI
320
     */
321
    public IRI getViewType() {
322
        return viewType;
×
323
    }
324

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