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

knowledgepixels / nanodash / 20268226543

16 Dec 2025 12:39PM UTC coverage: 14.107% (-1.3%) from 15.358%
20268226543

push

github

ashleycaselli
refactor: replace VocabUtils with the ones defined in the nanopub-java library

526 of 4946 branches covered (10.63%)

Branch coverage included in aggregate %.

1452 of 9075 relevant lines covered (16.0%)

2.11 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 com.knowledgepixels.nanodash.template.Template;
4
import com.knowledgepixels.nanodash.template.TemplateData;
5
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
6
import org.eclipse.rdf4j.model.IRI;
7
import org.eclipse.rdf4j.model.Literal;
8
import org.eclipse.rdf4j.model.Statement;
9
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
10
import org.eclipse.rdf4j.model.vocabulary.RDF;
11
import org.eclipse.rdf4j.model.vocabulary.RDFS;
12
import org.nanopub.Nanopub;
13
import org.nanopub.NanopubUtils;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16

17
import java.io.Serializable;
18
import java.util.*;
19

20
/**
21
 * A class representing a Resource View.
22
 */
23
public class ResourceView implements Serializable {
24

25
    private static final Logger logger = LoggerFactory.getLogger(ResourceView.class);
×
26

27
    static Map<IRI, Integer> columnWidths = new HashMap<>();
×
28

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

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

46
    /**
47
     * Get a ResourceView by its ID.
48
     *
49
     * @param id the ID of the ResourceView
50
     * @return the ResourceView object
51
     */
52
    public static ResourceView get(String id) {
53
        String npId = id.replaceFirst("^(.*[^A-Za-z0-9-_]RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$1");
×
54
        // Automatically selecting latest version of view definition:
55
        // TODO This should be made configurable at some point, so one can make it a fixed version.
56
        String latestNpId = QueryApiAccess.getLatestVersionId(npId);
×
57
        String latestId = id;
×
58
        Nanopub np = Utils.getAsNanopub(latestNpId);
×
59
        if (!latestNpId.equals(npId)) {
×
60
            Set<String> embeddedIris = NanopubUtils.getEmbeddedIriIds(np);
×
61
            if (embeddedIris.size() == 1) {
×
62
                latestId = embeddedIris.iterator().next();
×
63
            } else {
64
                latestNpId = npId;
×
65
                np = Utils.getAsNanopub(npId);
×
66
            }
67
        }
68
        if (!resourceViews.containsKey(latestId)) {
×
69
            try {
70
                resourceViews.put(latestId, new ResourceView(latestId, np));
×
71
            } catch (Exception ex) {
×
72
                logger.error("Couldn't load nanopub for resource: " + id, ex);
×
73
            }
×
74
        }
75
        return resourceViews.get(latestId);
×
76
    }
77

78
    private String id;
79
    private Nanopub nanopub;
80
    private IRI viewKind;
81
    private String label;
82
    private String title = "View";
×
83
    private GrlcQuery query;
84
    private String queryField = "resource";
×
85
    private Integer pageSize;
86
    private Integer displayWidth;
87
    private String structuralPosition;
88
    private List<IRI> viewResultActionList = new ArrayList<>();
×
89
    private List<IRI> viewEntryActionList = new ArrayList<>();
×
90
    private Set<IRI> appliesToClasses = new HashSet<>();
×
91
    private Set<IRI> appliesToNamespaces = new HashSet<>();
×
92
    private Map<IRI, Template> actionTemplateMap = new HashMap<>();
×
93
    private Map<IRI, String> actionTemplateTargetFieldMap = new HashMap<>();
×
94
    private Map<IRI, IRI> actionTemplateTypeMap = new HashMap<>();
×
95
    private Map<IRI, String> actionTemplatePartFieldMap = new HashMap<>();
×
96
    private Map<IRI, String> actionTemplateQueryMappingMap = new HashMap<>();
×
97
    private Map<IRI, String> labelMap = new HashMap<>();
×
98
    private IRI viewType;
99

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

172
    /**
173
     * Gets the ID of the ResourceView.
174
     *
175
     * @return the ID of the ResourceView
176
     */
177
    public String getId() {
178
        return id;
×
179
    }
180

181
    /**
182
     * Gets the Nanopub defining this ResourceView.
183
     *
184
     * @return the Nanopub defining this ResourceView
185
     */
186
    public Nanopub getNanopub() {
187
        return nanopub;
×
188
    }
189

190
    public IRI getViewKindIri() {
191
        return viewKind;
×
192
    }
193

194
    /**
195
     * Gets the label of the ResourceView.
196
     *
197
     * @return the label of the ResourceView
198
     */
199
    public String getLabel() {
200
        return label;
×
201
    }
202

203
    /**
204
     * Gets the title of the ResourceView.
205
     *
206
     * @return the title of the ResourceView
207
     */
208
    public String getTitle() {
209
        return title;
×
210
    }
211

212
    /**
213
     * Gets the GrlcQuery associated with the ResourceView.
214
     *
215
     * @return the GrlcQuery associated with the ResourceView
216
     */
217
    public GrlcQuery getQuery() {
218
        return query;
×
219
    }
220

221
    /**
222
     * Gets the query field of the ResourceView.
223
     *
224
     * @return the query field
225
     */
226
    public String getQueryField() {
227
        return queryField;
×
228
    }
229

230
    /**
231
     * Returns the preferred page size.
232
     *
233
     * @return page size (0 = everything on first page)
234
     */
235
    public Integer getPageSize() {
236
        return pageSize;
×
237
    }
238

239
    public Integer getDisplayWidth() {
240
        return displayWidth;
×
241
    }
242

243
    public String getStructuralPosition() {
244
        return structuralPosition;
×
245
    }
246

247
    /**
248
     * Gets the list of action IRIs associated with the ResourceView.
249
     *
250
     * @return the list of action IRIs
251
     */
252
    public List<IRI> getViewResultActionList() {
253
        return viewResultActionList;
×
254
    }
255

256
    public List<IRI> getViewEntryActionList() {
257
        return viewEntryActionList;
×
258
    }
259

260
    /**
261
     * Gets the Template for a given action IRI.
262
     *
263
     * @param actionIri the action IRI
264
     * @return the Template for the action IRI
265
     */
266
    public Template getTemplateForAction(IRI actionIri) {
267
        return actionTemplateMap.get(actionIri);
×
268
    }
269

270
    /**
271
     * Gets the template field for a given action IRI.
272
     *
273
     * @param actionIri the action IRI
274
     * @return the template field for the action IRI
275
     */
276
    public String getTemplateTargetFieldForAction(IRI actionIri) {
277
        return actionTemplateTargetFieldMap.get(actionIri);
×
278
    }
279

280
    public String getTemplatePartFieldForAction(IRI actionIri) {
281
        return actionTemplatePartFieldMap.get(actionIri);
×
282
    }
283

284
    public String getTemplateQueryMapping(IRI actionIri) {
285
        return actionTemplateQueryMappingMap.get(actionIri);
×
286
    }
287

288
    /**
289
     * Gets the label for a given action IRI.
290
     *
291
     * @param actionIri the action IRI
292
     * @return the label for the action IRI
293
     */
294
    public String getLabelForAction(IRI actionIri) {
295
        return labelMap.get(actionIri);
×
296
    }
297

298
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
299
        for (IRI namespace : appliesToNamespaces) {
×
300
            if (resourceId.startsWith(namespace.stringValue())) return true;
×
301
        }
×
302
        if (classes != null) {
×
303
            for (IRI c : classes) {
×
304
                if (appliesToClasses.contains(c)) return true;
×
305
            }
×
306
        }
307
        return false;
×
308
    }
309

310
    /**
311
     * Checks if the ResourceView has target classes.
312
     *
313
     * @return true if the ResourceView has target classes, false otherwise
314
     */
315
    public boolean appliesToClasses() {
316
        return !appliesToClasses.isEmpty();
×
317
    }
318

319
    /**
320
     * Checks if the ResourceView has a specific target class.
321
     *
322
     * @param targetClass the target class IRI
323
     * @return true if the ResourceView has the target class, false otherwise
324
     */
325
    public boolean appliesToClass(IRI targetClass) {
326
        return appliesToClasses.contains(targetClass);
×
327
    }
328

329
    @Override
330
    public String toString() {
331
        return id;
×
332
    }
333

334
    /**
335
     * Gets the view type of the ResourceView.
336
     *
337
     * @return the view type mode IRI
338
     */
339
    public IRI getViewType() {
340
        return viewType;
×
341
    }
342

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