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

knowledgepixels / nanodash / 19229148803

10 Nov 2025 10:52AM UTC coverage: 13.973% (-0.8%) from 14.759%
19229148803

push

github

web-flow
Merge pull request #280 from knowledgepixels/276-news-items

276 news items

514 of 4630 branches covered (11.1%)

Branch coverage included in aggregate %.

1333 of 8588 relevant lines covered (15.52%)

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

14
import java.io.Serializable;
15
import java.util.*;
16

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

22
    private static final Logger logger = LoggerFactory.getLogger(ResourceView.class);
×
23

24
    public static final IRI RESOURCE_VIEW = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/ResourceView");
×
25
    public static final IRI TOP_LEVEL_VIEW_DISPLAY = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/TopLevelViewDisplay");
×
26
    public static final IRI PART_LEVEL_VIEW_DISPLAY = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/PartLevelViewDisplay");
×
27
    public static final IRI HAS_VIEW_QUERY = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/hasViewQuery");
×
28
    public static final IRI HAS_VIEW_QUERY_TARGET_FIELD = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/hasViewQueryTargetField");
×
29
    public static final IRI HAS_VIEW_TARGET_CLASS = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/hasViewTargetClass");
×
30
    public static final IRI HAS_ELEMENT_NAMESPACE = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/hasElementNamespace");
×
31
    public static final IRI HAS_VIEW_ACTION = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/hasViewAction");
×
32
    public static final IRI HAS_ACTION_TEMPLATE = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/hasActionTemplate");
×
33
    public static final IRI HAS_ACTION_TEMPLATE_TARGET_FIELD = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/hasActionTemplateTargetField");
×
34
    public static final IRI TABULAR_VIEW = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/TabularView");
×
35
    public static final IRI LIST_VIEW = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/ListView");
×
36
    public static final IRI HAS_ACTION_TEMPLATE_PART_FIELD = Utils.vf.createIRI("https://w3id.org/kpxl/gen/terms/hasActionTemplatePartField");
×
37

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

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

58
    private String id;
59
    private Nanopub nanopub;
60
    private String label;
61
    private String title = "View";
×
62
    private GrlcQuery query;
63
    private String queryField = "resource";
×
64
    private List<IRI> actionList = new ArrayList<>();
×
65
    private Set<IRI> targetClasses = new HashSet<>();
×
66
    private Set<IRI> elementNamespaces = new HashSet<>();
×
67
    private Map<IRI, Template> actionTemplateMap = new HashMap<>();
×
68
    private Map<IRI, String> actionTemplateTargetFieldMap = new HashMap<>();
×
69
    private Map<IRI, String> actionTemplatePartFieldMap = new HashMap<>();
×
70
    private Map<IRI, String> labelMap = new HashMap<>();
×
71
    private IRI viewType;
72

73
    private ResourceView(String id, Nanopub nanopub) {
×
74
        this.id = id;
×
75
        this.nanopub = nanopub;
×
76
        boolean resourceViewTypeFound = false;
×
77
        for (Statement st : nanopub.getAssertion()) {
×
78
            if (st.getSubject().stringValue().equals(id)) {
×
79
                if (st.getPredicate().equals(RDF.TYPE)) {
×
80
                    if (st.getObject().equals(RESOURCE_VIEW)) {
×
81
                        resourceViewTypeFound = true;
×
82
                    }
83
                    if (st.getObject().equals(TABULAR_VIEW) || st.getObject().equals(LIST_VIEW)) {
×
84
                        viewType = (IRI) st.getObject();
×
85
                    }
86
                } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
87
                    label = st.getObject().stringValue();
×
88
                } else if (st.getPredicate().equals(DCTERMS.TITLE)) {
×
89
                    title = st.getObject().stringValue();
×
90
                } else if (st.getPredicate().equals(HAS_VIEW_QUERY)) {
×
91
                    query = GrlcQuery.get(st.getObject().stringValue());
×
92
                } else if (st.getPredicate().equals(HAS_VIEW_QUERY_TARGET_FIELD)) {
×
93
                    queryField = st.getObject().stringValue();
×
94
                } else if (st.getPredicate().equals(HAS_VIEW_ACTION) && st.getObject() instanceof IRI objIri) {
×
95
                    actionList.add(objIri);
×
96
                } else if (st.getPredicate().equals(HAS_ELEMENT_NAMESPACE) && st.getObject() instanceof IRI objIri) {
×
97
                    elementNamespaces.add(objIri);
×
98
                } else if (st.getPredicate().equals(HAS_VIEW_TARGET_CLASS) && st.getObject() instanceof IRI objIri) {
×
99
                    targetClasses.add(objIri);
×
100
                }
101
            } else if (st.getPredicate().equals(HAS_ACTION_TEMPLATE)) {
×
102
                Template template = TemplateData.get().getTemplate(st.getObject().stringValue());
×
103
                actionTemplateMap.put((IRI) st.getSubject(), template);
×
104
            } else if (st.getPredicate().equals(HAS_ACTION_TEMPLATE_TARGET_FIELD)) {
×
105
                actionTemplateTargetFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
106
            } else if (st.getPredicate().equals(HAS_ACTION_TEMPLATE_PART_FIELD)) {
×
107
                actionTemplatePartFieldMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
108
            } else if (st.getPredicate().equals(RDFS.LABEL)) {
×
109
                labelMap.put((IRI) st.getSubject(), st.getObject().stringValue());
×
110
            }
111
        }
×
112
        if (!resourceViewTypeFound) throw new IllegalArgumentException("Not a proper resource view nanopub: " + id);
×
113
        if (query == null) throw new IllegalArgumentException("Query not found: " + id);
×
114
    }
×
115

116
    /**
117
     * Gets the ID of the ResourceView.
118
     *
119
     * @return the ID of the ResourceView
120
     */
121
    public String getId() {
122
        return id;
×
123
    }
124

125
    /**
126
     * Gets the Nanopub defining this ResourceView.
127
     *
128
     * @return the Nanopub defining this ResourceView
129
     */
130
    public Nanopub getNanopub() {
131
        return nanopub;
×
132
    }
133

134
    /**
135
     * Gets the label of the ResourceView.
136
     *
137
     * @return the label of the ResourceView
138
     */
139
    public String getLabel() {
140
        return label;
×
141
    }
142

143
    /**
144
     * Gets the title of the ResourceView.
145
     *
146
     * @return the title of the ResourceView
147
     */
148
    public String getTitle() {
149
        return title;
×
150
    }
151

152
    /**
153
     * Gets the GrlcQuery associated with the ResourceView.
154
     *
155
     * @return the GrlcQuery associated with the ResourceView
156
     */
157
    public GrlcQuery getQuery() {
158
        return query;
×
159
    }
160

161
    /**
162
     * Gets the query field of the ResourceView.
163
     *
164
     * @return the query field
165
     */
166
    public String getQueryField() {
167
        return queryField;
×
168
    }
169

170
    /**
171
     * Gets the list of action IRIs associated with the ResourceView.
172
     *
173
     * @return the list of action IRIs
174
     */
175
    public List<IRI> getActionList() {
176
        return actionList;
×
177
    }
178

179
    /**
180
     * Gets the Template for a given action IRI.
181
     *
182
     * @param actionIri the action IRI
183
     * @return the Template for the action IRI
184
     */
185
    public Template getTemplateForAction(IRI actionIri) {
186
        return actionTemplateMap.get(actionIri);
×
187
    }
188

189
    /**
190
     * Gets the template field for a given action IRI.
191
     *
192
     * @param actionIri the action IRI
193
     * @return the template field for the action IRI
194
     */
195
    public String getTemplateTargetFieldForAction(IRI actionIri) {
196
        return actionTemplateTargetFieldMap.get(actionIri);
×
197
    }
198

199
    public String getTemplatePartFieldForAction(IRI actionIri) {
200
        return actionTemplatePartFieldMap.get(actionIri);
×
201
    }
202

203
    /**
204
     * Gets the label for a given action IRI.
205
     *
206
     * @param actionIri the action IRI
207
     * @return the label for the action IRI
208
     */
209
    public String getLabelForAction(IRI actionIri) {
210
        return labelMap.get(actionIri);
×
211
    }
212

213
    public boolean coversElement(String elementId) {
214
        for (IRI namespace : elementNamespaces) {
×
215
            if (elementId.startsWith(namespace.stringValue())) return true;
×
216
        }
×
217
        return false;
×
218
    }
219

220
    /**
221
     * Checks if the ResourceView has target classes.
222
     *
223
     * @return true if the ResourceView has target classes, false otherwise
224
     */
225
    public boolean hasTargetClasses() {
226
        return !targetClasses.isEmpty();
×
227
    }
228

229
    /**
230
     * Checks if the ResourceView has a specific target class.
231
     *
232
     * @param targetClass the target class IRI
233
     * @return true if the ResourceView has the target class, false otherwise
234
     */
235
    public boolean hasTargetClass(IRI targetClass) {
236
        return targetClasses.contains(targetClass);
×
237
    }
238

239
    @Override
240
    public String toString() {
241
        return id;
×
242
    }
243

244
    /**
245
     * Gets the view type of the ResourceView.
246
     *
247
     * @return the view type mode IRI
248
     */
249
    public IRI getViewType() {
250
        return viewType;
×
251
    }
252

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