457134569f22120ffbe9534a2f1d6ac2cff2d71f
[smartapi.git] / Common / C++ / SmartAPI / smartapi / model / Entity.cpp
1 #include "Entity.h"
2 #include <QtDebug>
3
4 #include "smartapi/model/Capacity.h"
5 #include "smartapi/model/Availability.h"
6
7
8 Entity::Entity(QString uri) : 
9         Obj(uri)
10 {
11         INIT_PROPERTY(mManagedBy)
12         INIT_PROPERTY(mCoordinates)
13         INIT_PROPERTY(mAddress)
14         INIT_PROPERTY(mZone)
15         INIT_PROPERTY(mHeartBeat)
16         INIT_PROPERTY(mRoute)
17         INIT_PROPERTYLIST(mAvailabilities,PROPERTY__HASAVAILABILITY);
18         INIT_PROPERTYLIST(mManagedEntities,PROPERTY__MANAGES);
19         INIT_PROPERTYLIST(mInterfaces,PROPERTY__INTERFACE);
20         INIT_PROPERTYLIST(mControllabilities,PROPERTY__HASEVALUATION);
21         INIT_PROPERTYLIST(mDataAvailabilities,PROPERTY__HASDATAAVAILABILITY);
22         INIT_PROPERTYLIST(mCapabilities,PROPERTY__HASCAPABILITY);
23         INIT_PROPERTYLIST(mCapacities,PROPERTY__CAPACITY);
24         INIT_PROPERTYLIST(mCreators,PROPERTY__DC_CREATOR);
25         INIT_PROPERTYLIST(mOwners,PROPERTY__OWNER);
26         INIT_PROPERTYLIST(mValueObjects,PROPERTY__VALUEOBJECT);
27
28         setType(RESOURCE__ENTITY);
29 }
30
31 Entity::Entity(Entity* e) : Obj(e)
32 {
33         SET_PROPERTY_REFERENCE(PROPERTY__ISMANAGEDBY, mManagedBy, GraphItem)
34         SET_PROPERTY_REFERENCE(PROPERTY__LOCATION, mCoordinates, Coordinates)
35         SET_PROPERTY_REFERENCE(PROPERTY__HASADDRESS, mAddress, Address)
36         SET_PROPERTY_REFERENCE(PROPERTY__ZONE, mZone, Zone)
37         SET_PROPERTY_REFERENCE(PROPERTY__HEARTBEAT, mHeartBeat, HeartBeat)
38     SET_PROPERTY_REFERENCE(PROPERTY__ROUTE, mRoute, Route)
39         SET_PROPERTYLIST_REFERENCE(PROPERTY__HASAVAILABILITY, mAvailabilities, Availability)
40         SET_PROPERTYLIST_REFERENCE(PROPERTY__HASURL, mWebsites, Variant)
41         SET_PROPERTYLIST_REFERENCE(PROPERTY__HASLOGO, mLogos, Variant)
42         SET_PROPERTYLIST_REFERENCE(PROPERTY__HASPHOTO, mPhotos, Variant)
43         SET_PROPERTYLIST_REFERENCE(PROPERTY__HASSOUND, mSounds, Variant)
44         SET_PROPERTYLIST_REFERENCE(PROPERTY__DC_CREATOR, mCreators, Entity)
45         SET_PROPERTYLIST_REFERENCE(PROPERTY__OWNER, mOwners, Entity)
46         SET_PROPERTYLIST_REFERENCE(PROPERTY__HASEVALUATION, mControllabilities, Controllability)
47         SET_PROPERTYLIST_REFERENCE(PROPERTY__HASDATAAVAILABILITY, mDataAvailabilities, Availability)
48         SET_PROPERTYLIST_REFERENCE(PROPERTY__HASCAPABILITY, mCapabilities, Activity)
49         SET_PROPERTYLIST_REFERENCE(PROPERTY__INTERFACE, mInterfaces, InterfaceAddress)
50         SET_PROPERTYLIST_REFERENCE(PROPERTY__MANAGES, mManagedEntities, Entity)
51         SET_PROPERTYLIST_REFERENCE(PROPERTY__CAPACITY, mCapacities, Capacity)
52         SET_PROPERTYLIST_REFERENCE(PROPERTY__VALUEOBJECT, mValueObjects, ValueObject)
53         setType(RESOURCE__ENTITY);
54 }
55
56 Entity::~Entity() 
57 {
58 }
59
60 void Entity::clearReferences(QSet<GraphItem*>* trash)
61 {
62         INIT_CLEAR(Obj,trash);
63         FINISH_CLEAR(Obj,trash);
64 }
65
66 Resource* Entity::serialize(Model* model)
67 {
68         INIT_SERIALIZE(Obj,model)
69         FINISH_SERIALIZE(Obj)
70 }
71
72 void Entity::parse(Statement* statement)
73 {
74         INIT_PARSE(statement)
75
76         PARSE_PROPERTY(PROPERTY__HASADDRESS, setAddress, Address)
77         PARSE_PROPERTY(PROPERTY__LOCATION, setCoordinates, Coordinates)
78         PARSE_PROPERTY(PROPERTY__ZONE, setZone, Zone)
79         PARSE_PROPERTY(PROPERTY__HEARTBEAT, setHeartBeat, HeartBeat)
80         PARSE_PROPERTY(PROPERTY__ISMANAGEDBY, setManagedBy, GraphItem)
81         PARSE_PROPERTY(PROPERTY__HASAVAILABILITY, addAvailability, Availability)
82         PARSE_PROPERTY(PROPERTY__HASURL, addWebsite, Variant)
83         PARSE_PROPERTY(PROPERTY__HASLOGO, addLogo, Variant)
84         PARSE_PROPERTY(PROPERTY__HASPHOTO, addPhoto, Variant)
85         PARSE_PROPERTY(PROPERTY__HASSOUND, addSound, Variant)
86         PARSE_PROPERTY(PROPERTY__DC_CREATOR, addCreator, Entity)
87         PARSE_PROPERTY(PROPERTY__OWNER, addOwner, Entity)
88         PARSE_PROPERTY(PROPERTY__HASEVALUATION, addControllability, Controllability)
89         PARSE_PROPERTY(PROPERTY__HASDATAAVAILABILITY, addDataAvailability, Availability)
90         PARSE_PROPERTY(PROPERTY__HASCAPABILITY, addCapability, Activity)
91         PARSE_PROPERTY(PROPERTY__INTERFACE, addInterface, InterfaceAddress)
92         PARSE_PROPERTY(PROPERTY__MANAGES, addManagedEntity, Entity)
93         PARSE_PROPERTY(PROPERTY__CAPACITY, addCapacity, Capacity)
94         PARSE_PROPERTY(PROPERTY__VALUEOBJECT, addValueObject, ValueObject)
95     PARSE_PROPERTY(PROPERTY__ROUTE, setRoute, Route)
96
97         FINISH_PARSE(Obj)
98 }
99
100 void Entity::setManagedBy(Entity* e)
101 {
102         if (e != NULL && e->hasIdentifierUri())
103                 setManagedBy(e->getIdentifierUri());
104 }
105
106 void Entity::addAvailability(Availability* availability)
107 {
108         setAvailability(availability);
109 }
110
111 void Entity::addAvailability(TemporalContext* temporalContext)
112 {
113         setAvailability(new Availability(temporalContext));
114 }
115
116 void Entity::addAvailability(QDateTime start, QDateTime end)
117 {
118         setAvailability(new Availability(start, end));
119 }
120
121 void Entity::addAvailability(Address* address)
122 {
123         setAvailability(new Availability(address));
124 }
125
126 void Entity::addAvailability(Ring* ring)
127 {
128         setAvailability(new Availability(ring));
129 }
130
131 ValueObject* Entity::getValueObject(QString quantity)
132 {
133         for (int i = 0; i < mValueObjects.length(); i++) {
134                 ValueObject* v = mValueObjects.at(i);
135                 if (quantity.isEmpty()) {
136                         if (!v->hasQuantity()) {
137                                 return v;
138                         }
139                 } else if (v->hasQuantity() && v->getQuantity() == quantity) {
140                         return v;
141                 }
142         }
143
144         return NULL;
145 }
146
147 ValueObject* Entity::newValueObject(QString identifier)
148 {
149         ValueObject* valueObject = new ValueObject(identifier);
150         addValueObject(valueObject);
151         return valueObject;
152 }
153
154 ValueObject* Entity::newValueObject(QString quantity, QString unit, QString dataType, QString description, QStringList types)
155 {
156         ValueObject* valueObject = new ValueObject(quantity, unit, dataType, description, types);
157         addValueObject(valueObject);
158         return valueObject;
159 }