Java: add servedby to Entity
[smartapi.git] / Common / Java / SmartAPI / src / smartapi / model / Entity.java
1 package smartapi.model;
2
3 import java.net.URL;
4 import java.sql.Time;
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.List;
8
9 import org.apache.jena.rdf.model.Model;
10 import org.apache.jena.rdf.model.Resource;
11 import org.apache.jena.rdf.model.Statement;
12
13 import smartapi.common.PROPERTY;
14 import smartapi.common.RESOURCE;
15 import smartapi.rdf.Variant;
16
17 public class Entity extends Obj
18 {
19
20         public Entity()
21         {
22                 super();
23                 this.setType(RESOURCE.ENTITY);
24         }
25
26         public Entity(String identifierUri)
27         {
28                 super(identifierUri);
29                 this.setType(RESOURCE.ENTITY);
30         }
31
32         public Resource getSerialization(Model model)
33         {
34                 Resource resource = super.getSerialization(model);
35                 return resource;
36         }
37         
38         public void parse(Statement stmt)
39         {
40                 // get predicate
41                 int propertyIndex = PROPERTY.add(stmt.getPredicate().toString());
42
43                 switch (propertyIndex) {
44                         case PROPERTY.ISMANAGEDBY:
45                                 if ( hasProperties(stmt) ) {
46                                         this.setManagedBy(parseObjectProperty(stmt)); return;
47                                 } else {
48                                         this.setManagedBy(parseObjectPropertyAsURL(stmt)); return;
49                                 }
50                         
51                         case PROPERTY.MANAGES:
52                                 this.addManagedEntity(parseObjectPropertyAsURL(stmt)); return;
53                         
54                         case PROPERTY.ISSERVEDBY:
55                                 if ( hasProperties(stmt) ) {
56                                         this.setServedBy(parseObjectProperty(stmt)); return;
57                                 } else {
58                                         this.setServedBy(parseObjectPropertyAsURL(stmt)); return;
59                                 }
60                         default:
61                                 // pass on to Obj
62                                 super.parse(stmt);
63                 }
64         }
65
66         public boolean hasAddress() { return has(PROPERTY.HASADDRESS); }
67         public Address getAddress() { return getFirstAs(Address.class, PROPERTY.HASADDRESS); }
68         public void setAddress(Address address) { set(PROPERTY.HASADDRESS, address); }
69
70         public boolean hasWebsite() { return has(PROPERTY.HASURL); }
71         public List<Variant> getWebsites() { return getAsVariant(PROPERTY.HASURL); }
72         public void addWebsite(String url) { add(PROPERTY.HASURL, new Variant(url)); }
73         
74         public boolean hasLogo() { return has(PROPERTY.HASLOGO); }
75         public List<Variant> getLogos() { return getAsVariant(PROPERTY.HASLOGO); }
76         public void addLogo(String logo) { add(PROPERTY.HASLOGO, new Variant(logo)); }
77         
78         public boolean hasPhoto() { return has(PROPERTY.HASPHOTO); }
79         public List<Variant> getPhotos() { return getAsVariant(PROPERTY.HASPHOTO); }
80         public void addPhoto(String photo) { add(PROPERTY.HASPHOTO, new Variant(photo)); }
81         
82         public boolean hasSound() { return has(PROPERTY.HASSOUND); }
83         public List<Variant> getSounds() { return getAsVariant(PROPERTY.HASSOUND); }
84         public void addSound(String sound) { add(PROPERTY.HASSOUND, new Variant(sound)); }
85
86         public boolean hasCreator() { return has(PROPERTY.DC_CREATOR); }
87         public List<Entity> getCreators() { return getAs(Entity.class, PROPERTY.DC_CREATOR); }
88         public void addCreator(Entity creator) { add(PROPERTY.DC_CREATOR, creator); }
89
90         public boolean hasOwner() { return has(PROPERTY.OWNER); }
91         public List<Entity> getOwners() { return getAs(Entity.class, PROPERTY.OWNER); }
92         public void addOwner(Entity owner) { add(PROPERTY.OWNER, owner); }
93
94         public boolean hasControllability() { return has(PROPERTY.HASEVALUATION); }
95         public List<Controllability> getControllabilities() { return getAs(Controllability.class, PROPERTY.HASEVALUATION); }
96         public void addControllability(Controllability controllability) { add(PROPERTY.HASEVALUATION, controllability); }
97
98         public boolean hasAvailability() { return has(PROPERTY.HASAVAILABILITY); }
99         public Availability getAvailability() { return getFirstAs(Availability.class, PROPERTY.HASAVAILABILITY); }
100         public void setAvailability(Availability availability) { add(PROPERTY.HASAVAILABILITY, availability); }
101
102         public void addAvailability(Availability availability)
103         {
104                 if ( this.hasAvailability() ) {
105                         this.getAvailability().addAvailability(availability);
106                 } else {
107                         this.setAvailability(availability);
108                 }
109         }
110
111         public void addAvailability(TemporalContext temporalContext)
112         {
113                 if ( this.hasAvailability() ) {
114                         this.getAvailability().addAvailability(temporalContext);
115                 } else {
116                         this.setAvailability(new Availability(temporalContext));
117                 }
118         }
119         
120         public void addAvailability(Date start, Date end)
121         {
122                 if ( this.hasAvailability() ) {
123                         this.getAvailability().addAvailability(start, end);
124                 } else {
125                         this.setAvailability(new Availability(start, end));
126                 }
127         }
128         
129         public void addAvailability(Date start, Date end, String during)
130         {
131                 if ( this.hasAvailability() ) {
132                         this.getAvailability().addAvailability(start, end, during);
133                 } else {
134                         this.setAvailability(new Availability(start, end, during));
135                 }
136         }
137         
138         public void addAvailability(Time start, Time end)
139         {
140                 if ( this.hasAvailability() ) {
141                         this.getAvailability().addAvailability(start, end);
142                 } else {
143                         this.setAvailability(new Availability(start, end));
144                 }
145         }
146         
147         public void addAvailability(Time start, Time end, String during)
148         {
149                 if ( this.hasAvailability() ) {
150                         this.getAvailability().addAvailability(start, end, during);
151                 } else {
152                         this.setAvailability(new Availability(start, end, during));
153                 }
154         }
155         
156         public void addAvailability(Address address)
157         {
158                 if ( this.hasAvailability() ) {
159                         this.getAvailability().addAvailability(address);
160                 } else {
161                         this.setAvailability(new Availability(address));
162                 }
163         }
164         
165         public void addAvailability(Ring ring)
166         {
167                 if ( this.hasAvailability() ) {
168                         this.getAvailability().addAvailability(ring);
169                 } else {
170                         this.setAvailability(new Availability(ring));
171                 }
172         }
173         
174         public boolean hasDataAvailability() { return has(PROPERTY.HASDATAAVAILABILITY); }      
175         public List<Availability> getDataAvailabilities() { return getAs(Availability.class, PROPERTY.HASDATAAVAILABILITY); }
176         public void addDataAvailability(Availability availability) { add(PROPERTY.HASDATAAVAILABILITY, availability); }
177
178         public boolean hasCapability() { return has(PROPERTY.HASCAPABILITY); }  
179         public List<Activity> getCapabilities() { return getAs(Activity.class, PROPERTY.HASCAPABILITY); }
180         public void addCapability(Activity capability) { add(PROPERTY.HASCAPABILITY, capability); }
181         public void setCapabilities(List<Activity> capabilities) { set(PROPERTY.HASCAPABILITY, capabilities); }
182
183         public void addCapability(String uri)
184         {
185                 Activity activity = new Activity(uri);
186                 activity.clearTypes();
187                 addCapability(activity);
188         }
189
190         public boolean hasManagedBy() { return has(PROPERTY.ISMANAGEDBY); }
191         public String getManagedBy() { return getFirstAsIdentifier(PROPERTY.ISMANAGEDBY); }
192         public void setManagedBy(URL url) { set(PROPERTY.ISMANAGEDBY, url); }
193         public void setManagedBy(Obj obj) { set(PROPERTY.ISMANAGEDBY, obj); }
194         public void setManagedBy(String uri) { setAsUrl(PROPERTY.ISMANAGEDBY, uri); }
195
196         @Deprecated
197         public boolean hasManagingSystemUri()
198         {
199                 return this.hasManagedBy();
200         }
201
202         @Deprecated
203         public void setManagingSystemUri(String uri)
204         {
205                 this.setManagedBy(uri);
206         }
207
208         @Deprecated
209         public void setManagingSystemUri(Obj obj)
210         {
211                 this.setManagedBy(obj);
212         }
213
214         @Deprecated
215         public String getManagingSystemUri()
216         {
217                 return this.getManagedBy();
218         }
219
220         public boolean hasServedBy() { return has(PROPERTY.ISSERVEDBY); }
221         public String getServedBy() { return getFirstAsIdentifier(PROPERTY.ISSERVEDBY); }
222         public void setServedBy(URL url) { set(PROPERTY.ISSERVEDBY, url); }
223         public void setServedBy(Obj obj) { set(PROPERTY.ISSERVEDBY, obj); }
224         public void setServedBy(String uri) { setAsUrl(PROPERTY.ISSERVEDBY, uri); }
225         
226         public boolean hasInterface() { return has(PROPERTY.INTERFACE); }
227         public List<InterfaceAddress> getInterfaces() { return getAs(InterfaceAddress.class, PROPERTY.INTERFACE); }
228         public void addInterface(InterfaceAddress interfaceAddress) { add(PROPERTY.INTERFACE, interfaceAddress); }
229         public void setInterface(InterfaceAddress interfaceAddress) { set(PROPERTY.INTERFACE, interfaceAddress); }
230
231         public boolean hasManagedEntities() { return has(PROPERTY.MANAGES); }
232         public List<String> getManagedEntities() { return getAsIdentifier(PROPERTY.MANAGES); }
233         public void addManagedEntity(URL e) { add(PROPERTY.MANAGES, e); }
234         public void addManagedEntity(String e)
235         {
236                 try {
237                         add(PROPERTY.MANAGES, new URL(e)); 
238                 } catch ( Exception ex ) {
239                         ex.printStackTrace();
240                 }
241         }
242         public void addManagedEntity(Obj e)
243         {
244                 try {
245                         add(PROPERTY.MANAGES, new URL(e.getIdentifierUri())); 
246                 } catch ( Exception ex ) {
247                         ex.printStackTrace();
248                 }
249         }
250         public void setManagedEntities(List<Variant> managedEntities) { set(PROPERTY.MANAGES, managedEntities); }
251
252         public boolean hasCoordinates() { return has(PROPERTY.LOCATION); }      
253         public Coordinates getCoordinates() { return getFirstAs(Coordinates.class, PROPERTY.LOCATION); }
254         public void setCoordinates(Coordinates coordinates) { set(PROPERTY.LOCATION, coordinates); }
255
256         public void setCoordinates(double latitude, double longitude)
257         {
258                 if ( !this.hasCoordinates() ) {
259                         setCoordinates(new Coordinates(latitude, longitude));
260                 } else {
261                         getCoordinates().set(latitude, longitude);
262                 }
263         }
264
265         public boolean hasZone() { return has(PROPERTY.ZONE); } 
266         public Zone getZone() { return getFirstAs(Zone.class, PROPERTY.ZONE); }
267         public void setZone(Zone zone) { set(PROPERTY.ZONE, zone); }
268
269         public boolean hasCapacity() { return has(PROPERTY.CAPACITY); } 
270         public List<Capacity> getCapacities() { return getAs(Capacity.class, PROPERTY.CAPACITY); }
271         public void addCapacity(Capacity capacity) { add(PROPERTY.CAPACITY, capacity); }
272         public void setCapacities(List<Capacity> capacity) { set(PROPERTY.CAPACITY, capacity); }
273
274         public boolean hasValueObject() { return has(PROPERTY.VALUEOBJECT); }   
275         public List<ValueObject> getValueObjects() { return getAs(ValueObject.class, PROPERTY.VALUEOBJECT); }
276         public void addValueObject(ValueObject valueObject) { add(PROPERTY.VALUEOBJECT, valueObject); }
277         public void setValueObjects(List<ValueObject> valueObject) { set(PROPERTY.VALUEOBJECT, valueObject); }
278         
279         public ValueObject getFirstValueObjectByQuantity(String quantity)
280         {
281                 List<ValueObject> list = getValueObjectByQuantity(quantity);
282                 if ( list.size() > 0 ) {
283                         return list.get(0);
284                 } else {
285                         return null;
286                 }
287         }
288         
289         public List<ValueObject> getValueObjectByQuantity(String quantity)
290         {
291                 List<ValueObject> ret = new ArrayList<ValueObject>();
292                 if ( quantity != null ) {
293                         for ( ValueObject vo : this.getValueObjects() ) {
294                                 if ( vo.hasQuantity() && vo.getQuantity().equals(quantity) ) {
295                                         ret.add(vo);
296                                 }
297                         }
298                 } else {
299                         for ( ValueObject vo : this.getValueObjects() ) {
300                                 if ( !vo.hasQuantity() ) {
301                                         ret.add(vo);
302                                 }
303                         }
304                 }
305                 return ret;
306         }
307         
308         public ValueObject getFirstValueObjectByUnit(String unit)
309         {
310                 List<ValueObject> list = getValueObjectByUnit(unit);
311                 if ( list.size() > 0 ) {
312                         return list.get(0);
313                 } else {
314                         return null;
315                 }
316         }
317         
318         public List<ValueObject> getValueObjectByUnit(String unit)
319         {
320                 List<ValueObject> ret = new ArrayList<ValueObject>();
321                 if ( unit != null ) {
322                         for ( ValueObject vo : this.getValueObjects() ) {
323                                 if ( vo.hasUnit() && vo.getUnit().equals(unit) ) {
324                                         ret.add(vo);
325                                 }
326                         }
327                 } else {
328                         for ( ValueObject vo : this.getValueObjects() ) {
329                                 if ( !vo.hasUnit() ) {
330                                         ret.add(vo);
331                                 }
332                         }
333                 }
334                 return ret;
335         }
336         
337         public ValueObject getValueObjectByIdentifier(String identifier)
338         {
339                 if ( identifier == null ) {
340                         return null;
341                 } else {
342                         for ( ValueObject vo : this.getValueObjects() ) {
343                                 if ( vo.hasIdentifierUri(identifier) ) {
344                                         return vo;
345                                 }
346                         }
347                         return null;
348                 }
349         }
350         
351         public ValueObject newValueObject()
352         {
353                 ValueObject valueObject = new ValueObject();
354                 this.addValueObject(valueObject);
355                 return valueObject;
356         }
357
358         public ValueObject newValueObject(String identifier)
359         {
360                 ValueObject valueObject = new ValueObject(identifier);
361                 this.addValueObject(valueObject);
362                 return valueObject;
363         }
364
365         public ValueObject newValueObject(String quantity, String unit, String dataType, String description, String... types)
366         {
367                 ValueObject valueObject = new ValueObject(quantity, unit, dataType, description, types);
368                 this.addValueObject(valueObject);
369                 return valueObject;
370         }
371         
372         public boolean hasHeartbeat() { return has(PROPERTY.HEARTBEAT); }
373         public Heartbeat getHeartbeat() { return getFirstAs(Heartbeat.class, PROPERTY.HEARTBEAT); }
374         public void setHeartbeat(Heartbeat heartbeat) { set(PROPERTY.HEARTBEAT, heartbeat); }
375
376         // route accessories
377         public boolean hasRoute() { return has(PROPERTY.ROUTE); }
378         public Route getRoute() { return getFirstAs(Route.class, PROPERTY.ROUTE); }
379         public void setRoute(Route route) { set(PROPERTY.ROUTE, route); }
380 }