C++: setTypes method to Obj
[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 setTypes(QList<QString> types);
118         void addType(QString type);
119         QList<QString> getTypes()                                                                       { return mTypes; }
120
121         bool hasName()                                                                                          { return !mName.isEmpty(); }
122         void setName(QString name)                                                                      { mName = name; }
123         QString getName()                                                                                       { return mName; }
124         
125         bool hasDescription()                                                                           { return !mDescription.isEmpty(); }
126         void setDescription(QString d)                                                          { mDescription = d; }
127         QString getDescription()                                                                        { return mDescription; }
128
129         bool hasProvenance()                                                                            { return !mProvenances.isEmpty(); }
130         PropertyList<Provenance*> getProvenances()                                      { return mProvenances; }
131         void addProvenance(Provenance* provenance)                                      { mProvenances.append(provenance); }
132         void setProvenances(PropertyList<Provenance*> provenances);
133         void setProvenances(QList<Provenance*> provenances);
134
135         bool hasRestriction()                                                                           { return !mRestrictions.isEmpty(); }
136         PropertyList<Restriction*> getRestrictions()                            { return mRestrictions; }
137         void addRestriction(Restriction* restriction)                           { mRestrictions.append(restriction); }
138         void setRestrictions(PropertyList<Restriction*> restrictions);
139         void setRestrictions(QList<Restriction*> restrictions);
140
141         bool hasOffering()                                                                                      { return !mOfferings.isEmpty(); }
142         PropertyList<Offering*> getOfferings()                                          { return mOfferings; }
143         Offering* firstOffering();
144         void addOffering(Offering* offering)                                            { mOfferings.append(offering); }
145         void setOfferings(PropertyList<Offering*> offerings);
146         void setOfferings(QList<Offering*> offerings);
147         void addOffering(Offering* offering, float amount,
148                         QString unitOfmeasurement, QString name,
149                         QString description);
150
151         void addError(Error* e)                                                                         { mErrors.append(e); }
152         PropertyList<Error*> getErrors()                                                        { return mErrors; }
153         bool hasErrors()                                                                                        { return (mErrors.length() > 0); }
154
155         bool hasHashCode()                                                                                      { return !mHashCode.isNull(); }
156         QString getHashCode()                                                                           { return mHashCode; }
157         void setHashCode(QString hashCode)                                                      { mHashCode = hashCode; }
158
159         bool hasSignature()                                                                                     { return !mSignature.isNull(); }
160         QString getSignature()                                                                          { return mSignature; }
161         void setSignature(QString signature)                                            { mSignature = signature; }
162
163         bool hasSessionKey()                                                                            { return !mSessionKey.isNull(); }
164         QString getSessionKey()                                                                         { return mSessionKey; }
165         void setSessionKey(QString sessionKey)                                          { mSessionKey = sessionKey; }
166
167         bool hasEncryptionKeyType()                                                                     { return !mEncryptionKeyType.isNull(); }
168         void setEncryptionKeyType(QString encryptionKeyType)            { mEncryptionKeyType = encryptionKeyType; }
169         QString getEncryptionKeyType()                                                          { return mEncryptionKeyType; }
170
171         bool hasNotary()                                                                                        { return (!mNotary.isEmpty() && mNotary.toString().length() > 0); }
172         void setNotary(QString notary)                                                          { mNotary = QUrl(notary); }
173         QString getNotary()                                                                                     { return mNotary.toString(); }
174
175         bool hasStringRepresentation()                                                          { return !mStringRepresentation.isEmpty(); }
176         QByteArray getStringRepresentation()                                            { return mStringRepresentation; }
177         void setStringRepresentation(QByteArray stringRepresentation)  { mStringRepresentation = stringRepresentation; }
178
179         bool hasGeneratedBy()                                                                           { return (!mGeneratedBy.isEmpty() && mGeneratedBy.toString().length() > 0); }
180         QUrl getGeneratedBy()                                                                           { return mGeneratedBy; }
181         void setGeneratedBy(QString generatedBy)                                        { mGeneratedBy = QUrl(generatedBy); }
182         void setGeneratedBy(QUrl generatedBy)                                           { mGeneratedBy = generatedBy; }
183
184         bool hasGeneratedAt()                                                                           { return mGeneratedAt.isValid(); }
185         QDateTime getGeneratedAt()                                                                      { return mGeneratedAt; }
186         void setGeneratedAt(QDateTime generatedAt)                                      { mGeneratedAt = generatedAt; }
187
188         bool hasStatus()                                                                                        { return (mStatus != NULL); }
189         Status* getStatus()                                                                                     { return mStatus; }
190         void setStatus(Status* s)                                                                       { mStatus = s; }
191
192         QMap<QString, PropertyList<GraphItem*>* > getAllProperties();
193
194         void addOptionalPropertyDescription(QStringList dataRanges, QString property);
195         void addOptionalPropertyDescription(QStringList dataRanges, QStringList properties);
196         void addOptionalPropertyDescription(QString dataRange, QString property);
197         void addOptionalPropertyDescription(QString dataRange, QStringList properties);
198         void addOptionalPropertyDescription(int numOfValues, QStringList dataRanges, QString property);
199         void addOptionalPropertyDescription(int numOfValues, QStringList dataRanges, QStringList properties);
200         void addOptionalPropertyDescription(int numOfValues, QString dataRange, QString property);
201         void addOptionalPropertyDescription(int numOfValues, QString dataRange, QStringList properties);
202         void addOptionalPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QStringList dataRanges, QString property);
203         void addOptionalPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QStringList dataRanges, QStringList properties);
204         void addOptionalPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QString dataRange, QString property);
205         void addOptionalPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QString dataRange, QStringList properties);
206
207         void addRequiredPropertyDescription(QStringList dataRanges, QString property);
208         void addRequiredPropertyDescription(QStringList dataRanges, QStringList properties);
209         void addRequiredPropertyDescription(QString dataRange, QString property);
210         void addRequiredPropertyDescription(QString dataRange, QStringList properties);
211         void addRequiredPropertyDescription(int numOfValues, QStringList dataRanges, QString property);
212         void addRequiredPropertyDescription(int numOfValues, QStringList dataRanges, QStringList properties);
213         void addRequiredPropertyDescription(int numOfValues, QString dataRange, QString property);
214         void addRequiredPropertyDescription(int numOfValues, QString dataRange, QStringList properties);
215         void addRequiredPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QStringList dataRanges, QString property);
216         void addRequiredPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QStringList dataRanges, QStringList properties);
217         void addRequiredPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QString dataRange, QString property);
218         void addRequiredPropertyDescription(Variant* minNumOfValues, Variant* maxNumOfValues, QString dataRange, QStringList properties);
219
220         PropertyList<GraphItem*>* getAll(QString property);
221         GraphItem* get(QString property);
222         template<typename T> T* get(QString property);
223         template<typename T> T* getAt(QString property, int index);
224         QVariant getValue(QString property);
225         QString getAsQString(QString property);
226
227         bool has(QString property);
228
229         void add(QUrl property, GraphItem* v);
230         void add(QString property, GraphItem* obj);
231         void add(QString property, QVariant obj);
232         void add(QString property, QList<Obj*> obj);
233         void add(QString property, QList<Map*> obj);
234         void add(QString property, PropertyList<GraphItem*> obj);
235         void add(QString property, PropertyList<Variant*> obj);
236         void set(QString property, GraphItem* v);
237         void remove(QString property, GraphItem* obj);
238         
239         QList<QString> propertyNames();
240         QList<QString> activePropertyNames();
241         void clearProperties();
242         bool hasProperties()                                                                            { return !mVariantMap.isEmpty(); }
243         void setProperties(QMap<QString, PropertyList<GraphItem*>* > properties);
244         QMap<QString, PropertyList<GraphItem*>* > getProperties()       { return mVariantMap; }
245         PropertyList<GraphItem*>* getProperty(QString prop)                     { return mVariantMap.value(prop); }
246
247         EVP_PKEY* getSignKey()                                                                          { return mSignKey; }
248         EVP_PKEY* getEncryptKey()                                                                       { return mEncryptPKey; }
249         QByteArray getSecretKey()                                                                       { return mEncryptSecretKey; }
250         QString getNotaryAddress()                                                                      { return getNotary(); }
251
252         QString asTurtle();
253         void turtlePrint();
254         void printOut(bool destroyAfterPrinting = false);
255         void explain(bool destroyAfterPrinting = false);
256
257 private:
258         bool replaceThisObjectWith(QString str);
259         void cleanList(PropertyList<GraphItem*>* l);
260         void cleanList(PropertyList<GraphItem*>* l, GraphItem* obj);
261
262         // FIXME QList<Variant*> copyList(QList<Variant*> l);
263         void addProperty(Model* model, Resource* resource, QString property, QVariant propObj);
264
265         QString getIdentifierUriPart(int index);
266
267         QString mName;
268         QString mDescription;
269         QDateTime mGeneratedAt;
270         QUrl mGeneratedBy;
271         QUrl mIdentifierUri;
272
273         Transaction* mTransaction;
274         Obj* mSameAs;
275         Status* mStatus;
276
277         QList<QString> mTypes;
278         
279         PropertyList<Error*> mErrors;
280         PropertyList<Provenance*> mProvenances;
281
282         // map for property-variant pairs
283         QMap<QString, PropertyList<GraphItem*>* > mVariantMap;
284
285         // restrictions (for entity descriptions)
286         PropertyList<Restriction*> mRestrictions;
287         PropertyList<Offering*> mOfferings;
288
289         // object is signed when serialized
290         bool mSign;
291         EVP_PKEY* mSignKey;
292
293         // object is encrypted when serialized
294         bool mEncrypt;
295         EVP_PKEY* mEncryptPKey;                         // mEncryptPKey and mEncryptSecretKey are mutually exclusive
296         QByteArray mEncryptSecretKey;
297         bool mIsDecrypted;
298
299         // reference object related properties
300         QString mHashCode;
301         QString mSignature;
302         QString mSessionKey;
303         QString mEncryptionKeyType;
304         QString mChecksum;
305         QUrl mNotary;
306
307         // canonical or encrypted form
308         QByteArray mStringRepresentation;
309
310         // if this object is to be encrypted or signed
311         bool mSerializeAsReference;
312
313         // prefixes for generated references and transactions
314         QString mReferenceUriPrefix;
315 };
316
317 struct PropertyObjectPair {
318         PropertyObjectPair() : object(NULL), propertyIdentifier(QString()) {}
319         PropertyObjectPair(QString i, Obj* o) : object(o), propertyIdentifier(i) {}
320         Obj* object;
321         QString propertyIdentifier;
322 };
323
324
325 #include "Obj.tcc"
326
327 #endif // _OBJ_H_