1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package cz.zcu.mre.data.rdf;
23
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import cz.zcu.mre.config.ApplicationConfiguration;
26 import java.io.Serializable;
27 import org.apache.jena.ontology.Individual;
28 import org.apache.jena.ontology.OntClass;
29 import org.apache.jena.ontology.OntProperty;
30 import org.apache.jena.rdf.model.Resource;
31 import org.apache.jena.rdf.model.ResourceFactory;
32 import org.apache.jena.vocabulary.RDFS;
33
34
35
36
37
38
39 public class ResourceWrapper implements Serializable {
40
41 private Resource resource;
42
43 private String label;
44
45 private int intOrder = 0;
46 private String stringOrder = "";
47
48 public ResourceWrapper(Resource resource) {
49
50 if (resource == null) {
51 throw new IllegalArgumentException("Null resource is not allowed.");
52 }
53
54 this.resource = resource;
55 }
56
57 public ResourceWrapper(String resource) {
58
59 if (resource == null) {
60 throw new IllegalArgumentException("Null resource is not allowed.");
61 }
62
63 setURI(resource);
64 }
65
66 public Resource getResource() {
67 return resource;
68 }
69
70 public void setResource(Resource resource) {
71 this.resource = resource;
72 }
73
74 @JsonProperty("resource")
75 public String getURI() {
76 return this.resource.getURI();
77 }
78
79 @JsonProperty("resource")
80 public final void setURI(String uri) {
81
82 this.resource = ResourceFactory.createResource(uri);
83 }
84
85 @Override
86 public String toString() {
87 return getURI();
88 }
89
90 public String getLabel() {
91
92 if (label != null) {
93 return label;
94 }
95
96
97 String language = ApplicationConfiguration.DEFAULT_LANGUAGE;
98 if (resource instanceof Individual) {
99 label = ((Individual) resource).getLabel(language);
100 if (label != null) {
101 return label;
102 }
103
104 label = ((Individual) resource).getLabel(null);
105 if (label != null) {
106 return label;
107 }
108
109 return resource.getLocalName();
110
111 } else if (resource instanceof OntClass) {
112 label = ((OntClass) resource).getLabel(language);
113 if (label != null) {
114 return label;
115 }
116
117 label = ((OntClass) resource).getLabel(null);
118 if (label != null) {
119 return label;
120 }
121
122 return resource.getLocalName();
123
124 } else if (resource instanceof OntProperty) {
125 label = ((OntProperty) resource).getLabel(language);
126 if (label != null) {
127 return label;
128 }
129
130 label = ((OntProperty) resource).getLabel(null);
131 if (label != null) {
132 return label;
133 }
134
135 return resource.getLocalName();
136 }
137
138 if (resource.getModel() != null) {
139 Resource r = this.resource.getPropertyResourceValue(RDFS.label);
140 if (r != null) {
141 label = r.asLiteral().getString();
142 return label;
143 }
144 }
145
146
147 label = "form.instance.empty";
148 return label;
149 }
150
151 public void setLabel(String label) {
152 this.label = label;
153 }
154
155 public int getIntOrder() {
156 return intOrder;
157 }
158
159 public void setIntOrder(int intOrder) {
160 this.intOrder = intOrder;
161 }
162
163 public String getStringOrder() {
164 return stringOrder;
165 }
166
167 public void setStringOrder(String stringOrder) {
168 this.stringOrder = stringOrder;
169 }
170
171 }