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

knowledgepixels / nanodash / 19666099531

25 Nov 2025 10:19AM UTC coverage: 13.938% (-0.1%) from 14.072%
19666099531

push

github

tkuhn
refactor(ResourceView): Consistent naming

523 of 4830 branches covered (10.83%)

Branch coverage included in aggregate %.

1390 of 8895 relevant lines covered (15.63%)

0.69 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> targetClasses = new HashSet<>();
×
81
    private Set<IRI> elementNamespaces = 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.HAS_ELEMENT_NAMESPACE) && st.getObject() instanceof IRI objIri) {
×
115
                    elementNamespaces.add(objIri);
×
116
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_VIEW_TARGET_CLASS) && st.getObject() instanceof IRI objIri) {
×
117
                    targetClasses.add(objIri);
×
118
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_PAGE_SIZE) && st.getObject() instanceof Literal objL) {
×
119
                    try {
120
                        pageSize = Integer.parseInt(objL.stringValue());
×
121
                    } catch (NumberFormatException ex) {
×
122
                        logger.error("Invalid page size value: " + objL.stringValue(), ex);
×
123
                    }
×
124
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_DISPLAY_WIDTH) && st.getObject() instanceof IRI objIri) {
×
125
                    displayWidth = columnWidths.get(objIri);
×
126
                } else if (st.getPredicate().equals(KPXL_TERMS.HAS_STRUCTURAL_POSITION) && st.getObject() instanceof Literal objL) {
×
127
                    structuralPosition = objL.stringValue();
×
128
                }
129
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE)) {
×
130
                Template template = TemplateData.get().getTemplate(st.getObject().stringValue());
×
131
                actionTemplateMap.put((IRI) st.getSubject(), template);
×
132
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_TARGET_FIELD)) {
×
133
                actionTemplateTargetFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
134
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_PART_FIELD)) {
×
135
                actionTemplatePartFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
136
            } else if (st.getPredicate().equals(KPXL_TERMS.HAS_ACTION_TEMPLATE_QUERY_MAPPING)) {
×
137
                actionTemplateQueryMappingMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
138
            } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
139
                labelMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
140
            } else if (st.getPredicate().equals(RDF.TYPE)) {
×
141
                if (st.getObject().equals(KPXL_TERMS.VIEW_ACTION) || st.getObject().equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
142
                    actionTemplateTypeMap.put((IRI) st.getSubject(), (IRI) st.getObject());
×
143
                }
144
            }
145
        }
×
146
        for (IRI actionIri : actionList) {
×
147
            if (actionTemplateTypeMap.containsKey(actionIri) && actionTemplateTypeMap.get(actionIri).equals(KPXL_TERMS.VIEW_ENTRY_ACTION)) {
×
148
                viewEntryActionList.add(actionIri);
×
149
            } else {
150
                viewResultActionList.add(actionIri);
×
151
            }
152
        }
×
153
        if (!resourceViewTypeFound) throw new IllegalArgumentException("Not a proper resource view nanopub: " + id);
×
154
        if (query == null) throw new IllegalArgumentException("Query not found: " + id);
×
155
    }
×
156

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

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

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

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

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

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

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

220
    public Integer getDisplayWidth() {
221
        return displayWidth;
×
222
    }
223

224
    public String getStructuralPosition() {
225
        return structuralPosition;
×
226
    }
227

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

237
    public List<IRI> getViewEntryActionList() {
238
        return viewEntryActionList;
×
239
    }
240

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

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

261
    public String getTemplatePartFieldForAction(IRI actionIri) {
262
        return actionTemplatePartFieldMap.get(actionIri);
×
263
    }
264

265
    public String getTemplateQueryMapping(IRI actionIri) {
266
        return actionTemplateQueryMappingMap.get(actionIri);
×
267
    }
268

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

279
    public boolean coversElement(String elementId) {
280
        for (IRI namespace : elementNamespaces) {
×
281
            if (elementId.startsWith(namespace.stringValue())) return true;
×
282
        }
×
283
        return false;
×
284
    }
285

286
    /**
287
     * Checks if the ResourceView has target classes.
288
     *
289
     * @return true if the ResourceView has target classes, false otherwise
290
     */
291
    public boolean hasTargetClasses() {
292
        return !targetClasses.isEmpty();
×
293
    }
294

295
    /**
296
     * Checks if the ResourceView has a specific target class.
297
     *
298
     * @param targetClass the target class IRI
299
     * @return true if the ResourceView has the target class, false otherwise
300
     */
301
    public boolean hasTargetClass(IRI targetClass) {
302
        return targetClasses.contains(targetClass);
×
303
    }
304

305
    @Override
306
    public String toString() {
307
        return id;
×
308
    }
309

310
    /**
311
     * Gets the view type of the ResourceView.
312
     *
313
     * @return the view type mode IRI
314
     */
315
    public IRI getViewType() {
316
        return viewType;
×
317
    }
318

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