561e24f0188d6061a281042dd794a77c95663272
[smartapi.git] / Common / C++ / SmartAPI / smartapi / model / Obj.h
1 #ifndef _OBJ_H_
2 #define _OBJ_H_
3
4 #define START_SERIALIZE if (hasSpecialSerialization(model)) return getSpecialSerialization(model);
5
6 #include "smartapi/rdf/GraphItem.h"
7 #include "smartapi/rdf/Literal.h"
8 #include "smartapi/rdf/Model.h"
9 #include "smartapi/rdf/Statement.h"
10 #include "smartapi/rdf/Variant.h"
11 #include "smartapi/rdf/PropertyList.h"
12 #include "smartapi/common/Tools.h"
13
14 #include <openssl/evp.h>
15 #include <QString>
16
17 class Activity;
18 class Map;
19 class OntologyCache;
20 class Parameter;
21 class Provenance;
22 class Error;
23 class Status;
24 class Offering;
25 class Restriction;
26 class Transaction;
27
28 class Obj : public GraphItem
29 {
30 public:
31         Obj(QUrl identifierUri);
32         Obj(QString identifierUri = QString());
33         Obj(Obj*);
34         virtual ~Obj();
35
36         //bool hasSpecialSerialization(Model* model);
37         //Resource* getSpecialSerialization()                                                   { return mSpecialSerialization; }
38         bool hasSpecialSerialization(Model *model);
39         Resource* getSpecialSerialization(Model *model);
40
41         bool getSerializeAsReference()                                                          { return mSerializeAsReference; }
42         void setSerializeAsReference(bool value)                                        { mSerializeAsReference = value; }
43
44         bool isPropertyList()                                                                           { return false; }
45         bool isVariant()                                                                                        { return false; }
46         bool isSigned()                                                                                         { return (!mSignature.isNull()); }
47         bool isToBeSigned()                                                                                     { return mSign; }
48         bool isToBeEncrypted()                                                                          { return mEncrypt; }
49         bool isToBeSerializedAsReference()                                                      { return mSerializeAsReference; }
50         bool isReference()                                                                                      { return (mSign || mEncrypt || hasType(RESOURCE__REFERENCE)); }
51         bool isEncrypted()                                                                                      { return hasType(RESOURCE__ENCRYPTEDREFERENCE); }
52         bool isDecrypted()                                                                                      { return mIsDecrypted; }
53
54         bool fetchKeyFromNotary(QString identity, QString notaryServer, EVP_PKEY* privKey);
55         bool verifyHash();
56         bool sign(EVP_PKEY* key);
57         bool verifySignature(EVP_PKEY* key);
58         bool decrypt(QByteArray secretKey);
59         bool decrypt(EVP_PKEY* privateKey);
60         bool encrypt(QByteArray secretKey);
61         QByteArray encrypt(EVP_PKEY* publicKey);
62         bool encrypt(EVP_PKEY* publicKey, QByteArray secretKey);
63         void encryptAndNotarize(QString notaryAddress, EVP_PKEY* publicKey, EVP_PKEY* privateKey = NULL);
64         Obj* fromStringRepresentation(Model* model);
65         bool confirmContentValidityAndFetchKey(QString identity, QString notaryAddress, EVP_PKEY* privateKey);
66
67         QString generateTemporaryIdentifier();
68         QString generateChecksum();
69         void setChecksum(QString checksum) { mChecksum = checksum; }
70         bool getCheckResult();
71
72         void setReferenceUriPrefix(QString prefix)                                      { mReferenceUriPrefix = prefix; }
73         QString getReferenceUriPrefix()                                                         { return mReferenceUriPrefix; }
74
75         virtual Resource* serialize(Model* model);
76         Resource* serializeToReference(Model* model);
77         template<typename T> static T* parse(Resource* resource);
78         virtual void parse(Statement* statement);
79         virtual void parse(QList<Statement*> statements);
80         virtual void clearReferences(QSet<GraphItem*>* trash);
81         
82         virtual QVariantMap toNude();
83         virtual void fromNude(QVariantMap n);
84
85         QString getId()                                                                                         { return getIdentifierUri(); }
86         bool is(QString identifierUri)                                                          { return hasIdentifierUri() && (getIdentifierUri() == identifierUri); }
87         bool isReadable();
88         bool isWritable();
89         bool hasIdentifierUri()                                                                         { return !mIdentifierUri.isEmpty() && mIdentifierUri.toString().length() > 0; }  // IMPORTANT: the length must also be tested, an empty "" will cause errors in the library
90         bool hasIdentifierUri(QString uri)                                                      { return is(uri); }
91         void setIdentifierUri(QString uri);
92         void setIdentifierUri(QUrl uri);
93         QString getIdentifierUri()                                                                      { if (!mIdentifierUri.isEmpty()) return mIdentifierUri.toString(QUrl::None); else return QString(); }
94         QUrl getIdentifierUrl()                                                                         { return mIdentifierUri; }
95         QString toString()                                                                                      { return getIdentifierUri(); }
96         QString asString()                                                                                      { return toString(); }
97
98         QString getDomain()                                                                                     { return getIdentifierUriPart(0); }
99         QString getSystemIdentifier()                                                           { return getIdentifierUriPart(1); }
100         QString getObjectIdentifier()                                                           { return getIdentifierUriPart(2); }
101         QString getObjectIdentifier(int index)                                          { return getIdentifierUriPart(1 + index); }
102
103         bool hasTransaction()                                                                           { return (mTransaction != NULL); }
104         Transaction* getTransaction()                                                           { return mTransaction; }
105         void setTransaction(Transaction* transaction)                           { mTransaction = transaction; }
106
107         bool hasSameAs()                                                                                        { return (mSameAs != NULL); }
108         void setSameAs(QString uri);
109         void setSameAs(Obj* sameAs)                                                                     { delete mSameAs; mSameAs = sameAs; }
110         Obj* getSameAs()                                                                                        { return mSameAs; }
111
112         bool hasType()                                                                                          { return !mTypes.isEmpty(); }
113         bool hasType(QString type);
114         bool removeType(QString type);
115         void clearTypes()                                                                                       { mTypes.clear(); }
116         void setType(QString type)                                                                      { clearTypes(); addType(type); }
117         void addType(QString type);
118         QList<QString> getTypes()                                                                       { return mTypes; }
119
120         bool hasName()                                                                                          { return !mName.isEmpty(); }
121         void setName(QString name)                                                                      { mName = name; }
122         QString getName()                                                                                       { return mName; }
123         
124         bool hasDescription()                                                                           { return !mDescription.isEmpty(); }
125         void setDescription(QString d)                                                          { mDescription = d; }
126         QString getDescription()                                                                        { return mDescription; }
127
128         bool hasProvenance()                                                                            { return !mProvenances.isEmpty(); }
129         PropertyList<Provenance*> getProvenances()                                      { return mProvenances; }
130         void addProvenance(Provenance* provenance)                                      { mProvenances.append(provenance); }
131         void setProvenances(PropertyList<Provenance*> provenances);
132         void setProvenances(QList<Provenance*> provenances);
133
134         bool hasRestriction()                                                                           { return !mRestrictions.isEmpty(); }
135         PropertyList<Restriction*> getRestrictions()                            { return mRestrictions; }
136         void addRestriction(Restriction* restriction)                           { mRestrictions.append(restriction); }
137         void setRestrictions(PropertyList<Restriction*> restrictions);
138         void setRestrictions(QList<Restriction*> restrictions);
139
140         bool hasOffering()                                                                                      { return !mOfferings.isEmpty(); }
141         PropertyList<Offering*> getOfferings()                                          { return mOfferings; }
142         Offering* firstOffering();
143         void addOffering(Offering* offering)                                            { mOfferings.append(offering); }
144         void setOfferings(PropertyList<Offering*> offerings);
145         void setOfferings(QList<Offering*> offerings);
146         void addOffering(Offering* offering, float amount,
147                         QString unitOfmeasurement, QString name,
148                         QString description);
149
150         void addError(Error* e)                                                                         { mErrors.append(e); }
151         PropertyList<Error*> getErrors()                                                        { return mErrors; }
152         bool hasErrors()                                                                                        { return (mErrors.length() > 0); }
153
154         bool hasHashCode()                                                                                      { return !mHashCode.isNull(); }
155         QString getHashCode()                                                                           { return mHashCode; }
156         void setHashCode(QString hashCode)                                                      { mHashCode = hashCode; }
157
158         bool hasSignature()                                                                                     { return !mSignature.isNull(); }
159         QString getSignature()                                                                          { return mSignature; }
160         void setSignature(QString signature)                                            { mSignature = signature; }
161
162         bool hasSessionKey()                                                                            { return !mSessionKey.isNull(); }
163         QString getSessionKey()                                                                         { return mSessionKey; }
164         void setSessionKey(QString sessionKey)                                          { mSessionKey = sessionKey; }
165
166         bool hasEncryptionKeyType()                                                                     { return !mEncryptionKeyType.isNull(); }
167         void setEncryptionKeyType(QString encryptionKeyType)            { mEncryptionKeyType = encryptionKeyType; }
168         QString getEncryptionKeyType()                                                          { return mEncryptionKeyType; }
169
170         bool hasNotary()                                                                                        { return (!mNotary.isEmpty() && mNotary.toString().length() > 0); }
171         void setNotary(QString notary)                                                          { mNotary = QUrl(notary); }
172         QString getNotary()                                                                                     { return mNotary.toString(); }
173
174         bool hasStringRepresentation()                                                          { return !mStringRepresentation.isEmpty(); }
175         QByteArray getStringRepresentation()                                            { return mStringRepresentation; }
176         void setStringRepresentation(QByteArray stringRepresentation)  { mStringRepresentation = stringRepresentation; }
177
178         bool hasGeneratedBy()                                                                           { return (!mGeneratedBy.isEmpty() && mGeneratedBy.toString().length() > 0); }
179         QUrl getGeneratedBy()                                                                           { return mGeneratedBy; }
180         void setGeneratedBy(QString generatedBy)                                        { mGeneratedBy = QUrl(generatedBy); }
181         void setGeneratedBy(QUrl generatedBy)                                           { mGeneratedBy = generatedBy; }
182
183         bool hasGeneratedAt()                                                                           { return mGeneratedAt.isValid(); }
184         QDateTime getGeneratedAt()                                                                      { return mGeneratedAt; }
185         void setGeneratedAt(QDateTime generatedAt)                                      { mGeneratedAt = generatedAt; }
186
187         bool hasStatus()                                                                                        { return (mStatus != NULL); }
188         Status* getStatus()                                                                                     { return mStatus; }
189         void setStatus(Status* s)                                                                       { mStatus = s; }
190
191         QMap<QString, PropertyList<GraphItem*>* > getAllProperties();
192
193         void addOptionalPropertyDescription(QStringList dataRanges, QString property);
194         void addOptionalPropertyDescription(QStringList dataRanges, QStringList properties);
195         void addOptionalPropertyDescription(QString dataRange, QString property);
196         void addOptionalPropertyDescription(QString dataRange, QStringList properties);
197         void addOptionalPropertyDescription(int numOfValues, QStringList dataRanges, QString property);
198         void addOptionalPropertyDescription(int numOfValues, QStringList dataRanges, QStringList properties);
199         void addOptionalPropertyDescription(int numOfValues, QString dataRange, QString property);
200         void addOptionalPropertyDescription(int numOfValues, QString dataRange, QStringList properties);
201         void addOptionalPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QStringList dataRanges, QString property);
202         void addOptionalPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QStringList dataRanges, QStringList properties);
203         void addOptionalPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QString dataRange, QString property);
204         void addOptionalPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QString dataRange, QStringList properties);
205
206         void addRequiredPropertyDescription(QStringList dataRanges, QString property);
207         void addRequiredPropertyDescription(QStringList dataRanges, QStringList properties);
208         void addRequiredPropertyDescription(QString dataRange, QString property);
209         void addRequiredPropertyDescription(QString dataRange, QStringList properties);
210         void addRequiredPropertyDescription(int numOfValues, QStringList dataRanges, QString property);
211         void addRequiredPropertyDescription(int numOfValues, QStringList dataRanges, QStringList properties);
212         void addRequiredPropertyDescription(int numOfValues, QString dataRange, QString property);
213         void addRequiredPropertyDescription(int numOfValues, QString dataRange, QStringList properties);
214         void addRequiredPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QStringList dataRanges, QString property);
215         void addRequiredPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QStringList dataRanges, QStringList properties);
216         void addRequiredPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QString dataRange, QString property);
217         void addRequiredPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QString dataRange, QStringList properties);
218
219         PropertyList<GraphItem*>* getAll(QString property);
220         GraphItem* get(QString property);
221         template<typename T> T* get(QString property);
222         template<typename T> T* getAt(QString property, int index);
223         QVariant getValue(QString property);
224         QString getAsQString(QString property);
225
226         bool has(QString property);
227
228         void add(QUrl property, GraphItem* v);
229         void add(QString property, GraphItem* obj);
230         void add(QString property, QVariant obj);
231         void add(QString property, QList<Obj*> obj);
232         void add(QString property, QList<Map*> obj);
233         void add(QString property, PropertyList<GraphItem*> obj);
234         void add(QString property, PropertyList<Variant*> obj);
235         void set(QString property, GraphItem* v);
236         void remove(QString property, GraphItem* obj);
237         
238         QList<QString> propertyNames();
239         QList<QString> activePropertyNames();
240         void clearProperties();
241         bool hasProperties()                                                                            { return !mVariantMap.isEmpty(); }
242         void setProperties(QMap<QString, PropertyList<GraphItem*>* > properties);
243         QMap<QString, PropertyList<GraphItem*>* > getProperties()       { return mVariantMap; }
244         PropertyList<GraphItem*>* getProperty(QString prop)                     { return mVariantMap.value(prop); }
245
246         EVP_PKEY* getSignKey()                                                                          { return mSignKey; }
247         EVP_PKEY* getEncryptKey()                                                                       { return mEncryptPKey; }
248         QByteArray getSecretKey()                                                                       { return mEncryptSecretKey; }
249         QString getNotaryAddress()                                                                      { return getNotary(); }
250
251         QString asTurtle();
252         void turtlePrint();
253         void printOut(bool destroyAfterPrinting = false);
254         void explain(bool destroyAfterPrinting = false);
255
256 private:
257         bool replaceThisObjectWith(QString str);
258         void cleanList(PropertyList<GraphItem*>* l);
259         void cleanList(PropertyList<GraphItem*>* l, GraphItem* obj);
260
261         // FIXME QList<Variant*> copyList(QList<Variant*> l);
262         void addProperty(Model* model, Resource* resource, QString property, QVariant propObj);
263
264         QString getIdentifierUriPart(int index);
265
266         QString mName;
267         QString mDescription;
268         QDateTime mGeneratedAt;
269         QUrl mGeneratedBy;
270         QUrl mIdentifierUri;
271
272         Transaction* mTransaction;
273         Obj* mSameAs;
274         Status* mStatus;
275
276         QList<QString> mTypes;
277         
278         PropertyList<Error*> mErrors;
279         PropertyList<Provenance*> mProvenances;
280
281         // map for property-variant pairs
282         QMap<QString, PropertyList<GraphItem*>* > mVariantMap;
283
284         // restrictions (for entity descriptions)
285         PropertyList<Restriction*> mRestrictions;
286         PropertyList<Offering*> mOfferings;
287
288         // object is signed when serialized
289         bool mSign;
290         EVP_PKEY* mSignKey;
291
292         // object is encrypted when serialized
293         bool mEncrypt;
294         EVP_PKEY* mEncryptPKey;                         // mEncryptPKey and mEncryptSecretKey are mutually exclusive
295         QByteArray mEncryptSecretKey;
296         bool mIsDecrypted;
297
298         // reference object related properties
299         QString mHashCode;
300         QString mSignature;
301         QString mSessionKey;
302         QString mEncryptionKeyType;
303         QString mChecksum;
304         QUrl mNotary;
305
306         // canonical or encrypted form
307         QByteArray mStringRepresentation;
308
309         // if this object is to be encrypted or signed
310         bool mSerializeAsReference;
311
312         // prefixes for generated references and transactions
313         QString mReferenceUriPrefix;
314 };
315
316 struct PropertyObjectPair {
317         PropertyObjectPair() : object(NULL), propertyIdentifier(QString()) {}
318         PropertyObjectPair(QString i, Obj* o) : object(o), propertyIdentifier(i) {}
319         Obj* object;
320         QString propertyIdentifier;
321 };
322
323
324 #include "Obj.tcc"
325
326 #endif // _OBJ_H_