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

knowledgepixels / nanodash / 19640315218

24 Nov 2025 03:48PM UTC coverage: 14.394% (-0.2%) from 14.6%
19640315218

push

github

tkuhn
feat(ResourceView): Allow for entry-level actions

542 of 4828 branches covered (11.23%)

Branch coverage included in aggregate %.

1433 of 8893 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/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.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

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

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

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

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

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

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

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

63
    private String id;
64
    private Nanopub nanopub;
65
    private String label;
66
    private String title = "View";
×
67
    private GrlcQuery query;
68
    private String queryField = "resource";
×
69
    private Integer pageSize;
70
    private Integer displayWidth;
71
    private String structuralPosition;
72
    private List<IRI> viewActionList = new ArrayList<>();
×
73
    private List<IRI> entryActionList = new ArrayList<>();
×
74
    private Set<IRI> targetClasses = new HashSet<>();
×
75
    private Set<IRI> elementNamespaces = new HashSet<>();
×
76
    private Map<IRI, Template> actionTemplateMap = new HashMap<>();
×
77
    private Map<IRI, String> actionTemplateTargetFieldMap = new HashMap<>();
×
78
    private Map<IRI, IRI> actionTemplateTypeMap = new HashMap<>();
×
79
    private Map<IRI, String> actionTemplatePartFieldMap = new HashMap<>();
×
80
    private Map<IRI, String> actionTemplateQueryMappingMap = new HashMap<>();
×
81
    private Map<IRI, String> labelMap = new HashMap<>();
×
82
    private IRI viewType;
83

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

151
    /**
152
     * Gets the ID of the ResourceView.
153
     *
154
     * @return the ID of the ResourceView
155
     */
156
    public String getId() {
157
        return id;
×
158
    }
159

160
    /**
161
     * Gets the Nanopub defining this ResourceView.
162
     *
163
     * @return the Nanopub defining this ResourceView
164
     */
165
    public Nanopub getNanopub() {
166
        return nanopub;
×
167
    }
168

169
    /**
170
     * Gets the label of the ResourceView.
171
     *
172
     * @return the label of the ResourceView
173
     */
174
    public String getLabel() {
175
        return label;
×
176
    }
177

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

187
    /**
188
     * Gets the GrlcQuery associated with the ResourceView.
189
     *
190
     * @return the GrlcQuery associated with the ResourceView
191
     */
192
    public GrlcQuery getQuery() {
193
        return query;
×
194
    }
195

196
    /**
197
     * Gets the query field of the ResourceView.
198
     *
199
     * @return the query field
200
     */
201
    public String getQueryField() {
202
        return queryField;
×
203
    }
204

205
    /**
206
     * Returns the preferred page size.
207
     *
208
     * @return page size (0 = everything on first page)
209
     */
210
    public Integer getPageSize() {
211
        return pageSize;
×
212
    }
213

214
    public Integer getDisplayWidth() {
215
        return displayWidth;
×
216
    }
217

218
    public String getStructuralPosition() {
219
        return structuralPosition;
×
220
    }
221

222
    /**
223
     * Gets the list of action IRIs associated with the ResourceView.
224
     *
225
     * @return the list of action IRIs
226
     */
227
    public List<IRI> getViewActionList() {
228
        return viewActionList;
×
229
    }
230

231
    public List<IRI> getEntryActionList() {
232
        return entryActionList;
×
233
    }
234

235
    /**
236
     * Gets the Template for a given action IRI.
237
     *
238
     * @param actionIri the action IRI
239
     * @return the Template for the action IRI
240
     */
241
    public Template getTemplateForAction(IRI actionIri) {
242
        return actionTemplateMap.get(actionIri);
×
243
    }
244

245
    /**
246
     * Gets the template field for a given action IRI.
247
     *
248
     * @param actionIri the action IRI
249
     * @return the template field for the action IRI
250
     */
251
    public String getTemplateTargetFieldForAction(IRI actionIri) {
252
        return actionTemplateTargetFieldMap.get(actionIri);
×
253
    }
254

255
    public String getTemplatePartFieldForAction(IRI actionIri) {
256
        return actionTemplatePartFieldMap.get(actionIri);
×
257
    }
258

259
    public String getTemplateQueryMapping(IRI actionIri) {
260
        return actionTemplateQueryMappingMap.get(actionIri);
×
261
    }
262

263
    /**
264
     * Gets the label for a given action IRI.
265
     *
266
     * @param actionIri the action IRI
267
     * @return the label for the action IRI
268
     */
269
    public String getLabelForAction(IRI actionIri) {
270
        return labelMap.get(actionIri);
×
271
    }
272

273
    public boolean coversElement(String elementId) {
274
        for (IRI namespace : elementNamespaces) {
×
275
            if (elementId.startsWith(namespace.stringValue())) return true;
×
276
        }
×
277
        return false;
×
278
    }
279

280
    /**
281
     * Checks if the ResourceView has target classes.
282
     *
283
     * @return true if the ResourceView has target classes, false otherwise
284
     */
285
    public boolean hasTargetClasses() {
286
        return !targetClasses.isEmpty();
×
287
    }
288

289
    /**
290
     * Checks if the ResourceView has a specific target class.
291
     *
292
     * @param targetClass the target class IRI
293
     * @return true if the ResourceView has the target class, false otherwise
294
     */
295
    public boolean hasTargetClass(IRI targetClass) {
296
        return targetClasses.contains(targetClass);
×
297
    }
298

299
    @Override
300
    public String toString() {
301
        return id;
×
302
    }
303

304
    /**
305
     * Gets the view type of the ResourceView.
306
     *
307
     * @return the view type mode IRI
308
     */
309
    public IRI getViewType() {
310
        return viewType;
×
311
    }
312

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