View Javadoc
1   /*
2    * Copyright 2018-2022 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * Author Petr Vcelak (vcelak@kiv.zcu.cz).
7    *
8    * This file is part of MRECore project.
9    *
10   * MRECore is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as published by
12   * the Free Software Foundation, either version 3 of the License.
13   *
14   * MRECore is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with MRECore. If not, see <http://www.gnu.org/licenses/>.
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   * Wrapper for RDF Resource.
36   *
37   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
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          /* get label -- it is quite slow */
97          String language = ApplicationConfiguration.DEFAULT_LANGUAGE; // TODO get language from the session
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         // use localized 'empty' message
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 }