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

knowledgepixels / nanodash / 19767008036

28 Nov 2025 02:44PM UTC coverage: 14.4% (-0.08%) from 14.481%
19767008036

push

github

tkuhn
feat(Space): Use new "appliesTo..." fields to calculate view displays

548 of 4898 branches covered (11.19%)

Branch coverage included in aggregate %.

1446 of 8949 relevant lines covered (16.16%)

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

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

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

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

181
    public IRI getViewKindIri() {
182
        return viewKind;
×
183
    }
184

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

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

203
    /**
204
     * Gets the GrlcQuery associated with the ResourceView.
205
     *
206
     * @return the GrlcQuery associated with the ResourceView
207
     */
208
    public GrlcQuery getQuery() {
209
        return query;
×
210
    }
211

212
    /**
213
     * Gets the query field of the ResourceView.
214
     *
215
     * @return the query field
216
     */
217
    public String getQueryField() {
218
        return queryField;
×
219
    }
220

221
    /**
222
     * Returns the preferred page size.
223
     *
224
     * @return page size (0 = everything on first page)
225
     */
226
    public Integer getPageSize() {
227
        return pageSize;
×
228
    }
229

230
    public Integer getDisplayWidth() {
231
        return displayWidth;
×
232
    }
233

234
    public String getStructuralPosition() {
235
        return structuralPosition;
×
236
    }
237

238
    /**
239
     * Gets the list of action IRIs associated with the ResourceView.
240
     *
241
     * @return the list of action IRIs
242
     */
243
    public List<IRI> getViewResultActionList() {
244
        return viewResultActionList;
×
245
    }
246

247
    public List<IRI> getViewEntryActionList() {
248
        return viewEntryActionList;
×
249
    }
250

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

261
    /**
262
     * Gets the template field for a given action IRI.
263
     *
264
     * @param actionIri the action IRI
265
     * @return the template field for the action IRI
266
     */
267
    public String getTemplateTargetFieldForAction(IRI actionIri) {
268
        return actionTemplateTargetFieldMap.get(actionIri);
×
269
    }
270

271
    public String getTemplatePartFieldForAction(IRI actionIri) {
272
        return actionTemplatePartFieldMap.get(actionIri);
×
273
    }
274

275
    public String getTemplateQueryMapping(IRI actionIri) {
276
        return actionTemplateQueryMappingMap.get(actionIri);
×
277
    }
278

279
    /**
280
     * Gets the label for a given action IRI.
281
     *
282
     * @param actionIri the action IRI
283
     * @return the label for the action IRI
284
     */
285
    public String getLabelForAction(IRI actionIri) {
286
        return labelMap.get(actionIri);
×
287
    }
288

289
    public boolean appliesTo(String resourceId, Set<IRI> classes) {
290
        for (IRI namespace : appliesToNamespaces) {
×
291
            if (resourceId.startsWith(namespace.stringValue())) return true;
×
292
        }
×
293
        if (classes != null) {
×
294
            for (IRI c : classes) {
×
295
                if (appliesToClasses.contains(c)) return true;
×
296
            }
×
297
        }
298
        return false;
×
299
    }
300

301
    /**
302
     * Checks if the ResourceView has target classes.
303
     *
304
     * @return true if the ResourceView has target classes, false otherwise
305
     */
306
    public boolean appliesToClasses() {
307
        return !appliesToClasses.isEmpty();
×
308
    }
309

310
    /**
311
     * Checks if the ResourceView has a specific target class.
312
     *
313
     * @param targetClass the target class IRI
314
     * @return true if the ResourceView has the target class, false otherwise
315
     */
316
    public boolean appliesToClass(IRI targetClass) {
317
        return appliesToClasses.contains(targetClass);
×
318
    }
319

320
    @Override
321
    public String toString() {
322
        return id;
×
323
    }
324

325
    /**
326
     * Gets the view type of the ResourceView.
327
     *
328
     * @return the view type mode IRI
329
     */
330
    public IRI getViewType() {
331
        return viewType;
×
332
    }
333

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