File indexing completed on 2024-04-28 16:30:32

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 #ifndef SKGOBJECTBASE_H
0007 #define SKGOBJECTBASE_H
0008 /** @file
0009  * This file defines classes SKGObjectBase.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 #include <qlist.h>
0014 #include <qstring.h>
0015 #include <qvariant.h>
0016 
0017 #include "skgdefine.h"
0018 #include "skgerror.h"
0019 #include "skgservices.h"
0020 
0021 class SKGDocument;
0022 class SKGPropertyObject;
0023 class SKGObjectBasePrivate;
0024 
0025 /**
0026  * This class is the base class for all objects.
0027  * This class is a generic way to manipulate objects.
0028  * Some important rules:
0029  * R1: Each table must have a view named v_[tablename] where [tablename] is the name of the table.
0030  * R2: Each computed attribute of the view must have a name starting with v_
0031  */
0032 class SKGBASEMODELER_EXPORT SKGObjectBase
0033 {
0034     /**
0035      * Unique ide  of the object
0036      */
0037     Q_PROPERTY(QString uniqueID READ getUniqueID CONSTANT)
0038     /**
0039      * Id of the object
0040      */
0041     Q_PROPERTY(int ID READ getID CONSTANT)
0042     /**
0043      * Table of the object
0044      */
0045     Q_PROPERTY(QString table READ getTable CONSTANT)
0046 
0047 public:
0048     /**
0049      * A list of SKGObjectBase ==> SKGListSKGObjectBase
0050      */
0051     using SKGListSKGObjectBase = QVector<SKGObjectBase>;
0052 
0053     /**
0054      * An iterator for SKGListSKGObjectBase
0055      */
0056     using SKGListSKGObjectBaseIterator = QVector<SKGObjectBase>::Iterator;
0057 
0058     /**
0059      * Default constructor
0060      */
0061     explicit SKGObjectBase();
0062 
0063     /**
0064      * Constructor
0065      * @param iDocument the document containing the object
0066      * @param iTable the table of the object
0067      * @param iID the identifier in @p iTable of the object
0068      */
0069     explicit SKGObjectBase(SKGDocument* iDocument, const QString& iTable = QString(), int iID = 0);
0070 
0071     /**
0072      * Copy constructor
0073      * @param iObject the object to copy
0074      */
0075     SKGObjectBase(const SKGObjectBase& iObject);
0076 
0077     /**
0078      * Move constructor
0079      * @param iObject the object to copy
0080      */
0081     SKGObjectBase(SKGObjectBase&& iObject) noexcept;
0082 
0083     /**
0084      * Operator comparison
0085      * @param iObject The object to compare
0086      */
0087     virtual bool operator==(const SKGObjectBase& iObject) const;
0088 
0089     /**
0090      * Operator comparison
0091      * @param iObject The object to compare
0092      */
0093     virtual bool operator!=(const SKGObjectBase& iObject) const;
0094 
0095     /**
0096      * Operator comparison
0097      * @param iObject The object to compare
0098      */
0099     virtual bool operator<(const SKGObjectBase& iObject) const;
0100 
0101     /**
0102      * Operator comparison
0103      * @param iObject The object to compare
0104      */
0105     virtual bool operator>(const SKGObjectBase& iObject) const;
0106 
0107     /**
0108      * Operator affectation
0109      * @param iObject the object to copy
0110      */
0111     SKGObjectBase& operator= (const SKGObjectBase& iObject);
0112 
0113     /**
0114      * Clone this object into a same document. The id is not kept. The cloned object is not saved into the target document
0115      */
0116     virtual SKGObjectBase cloneInto();
0117 
0118     /**
0119      * Clone this object into a new document. The id is not kept. The cloned object is not saved into the target document
0120      * @param iDocument the target document.
0121      */
0122     virtual SKGObjectBase cloneInto(SKGDocument* iDocument);
0123 
0124     /**
0125      * Destructor
0126      */
0127     virtual ~SKGObjectBase();
0128 
0129     /**
0130      * Return the unique id of this object
0131      * @return unique id of this object
0132      */
0133     virtual QString getUniqueID() const;
0134 
0135     /**
0136      * Return the id of this object
0137      * @return id of this object
0138      */
0139     virtual int getID() const;
0140 
0141     /**
0142      * Return the name of the object for the display
0143      * @return name of the object
0144      */
0145     virtual QString getDisplayName() const;
0146 
0147     /**
0148      * Reset the ID of this object.
0149      * It is used to create a new object based on an existing one.
0150      * By reseting the ID, the save will try an INSERT instead of an UPDATE.
0151      * @return an object managing the error
0152      *   @see SKGError
0153      */
0154     virtual SKGError resetID();
0155 
0156     /**
0157      * Return the table name of this object
0158      * @return table name of this object
0159      */
0160     virtual QString getTable() const;
0161 
0162     /**
0163      * Return the real table name of this object.
0164      * This is useful for modification (UPDATE, DELETE)
0165      * @return real table name of this object
0166      */
0167     virtual QString getRealTable() const;
0168 
0169     /**
0170      * Return the document of this object
0171      * @return document of this object
0172      */
0173     SKGDocument* getDocument() const;
0174 
0175     /**
0176      * Return the attributes of this object
0177      * @return attributes of this object
0178      */
0179     virtual SKGQStringQStringMap getAttributes() const;
0180 
0181     /**
0182      * Return the number of attributes
0183      * @return number of attributes
0184      */
0185     virtual int getNbAttributes() const;
0186 
0187     /**
0188      * Get the value for an attribute
0189      * @param iName the name of the attribute.
0190      * This can be:
0191      *   The name of an attribute (eg. t_name)
0192      *   The name of a property (eg. p_myproperty)
0193      *   The name of an attribute on a marked object (eg. rd_node_id.(v_node)t_name)
0194      * @return the value of this attribute
0195      */
0196     virtual QString getAttribute(const QString& iName) const;
0197 
0198     /**
0199      * Get the value for an attribute for another view
0200      * @param iView the name of the view
0201      * @param iName the name of the attribute
0202      * @return the value of this attribute
0203      */
0204     virtual QString getAttributeFromView(const QString& iView, const QString& iName) const;
0205 
0206     /**
0207      * Set the value for an attribute
0208      * @param iName the name of the attribute
0209      * @param iValue the value of the attribute
0210      * @return an object managing the error
0211      *   @see SKGError
0212      */
0213     virtual SKGError setAttribute(const QString& iName, const QString& iValue);
0214 
0215     /**
0216      * Set the values for attributes
0217      * @param iNames list of attributes
0218      * @param iValues list of values
0219      * @return an object managing the error
0220      *   @see SKGError
0221      */
0222     virtual SKGError setAttributes(const QStringList& iNames, const QStringList& iValues);
0223 
0224     /**
0225      * Get the list of properties
0226      * @return the list of properties
0227      */
0228     virtual QStringList getProperties() const;
0229 
0230     /**
0231      * Get the value for a property
0232      * @param iName the name of the property
0233      * @return the value of this property
0234      */
0235     virtual QString getProperty(const QString& iName) const;
0236 
0237     /**
0238      * Get the property
0239      * @param iName the name of the property
0240      * @return the property
0241      */
0242     virtual SKGObjectBase getPropertyObject(const QString& iName) const;
0243 
0244     /**
0245      * Get the blob for a property
0246      * @param iName the name of the property
0247      * @return the blob of this property
0248      */
0249     virtual QVariant getPropertyBlob(const QString& iName) const;
0250 
0251     /**
0252      * Set the value for a property
0253      * @param iName the property name
0254      * @param iValue the property value
0255      * @param iFileName the file name.
0256      * @param oObjectCreated the property object created. Can be nullptr
0257      * @return an object managing the property
0258      *   @see SKGError
0259      */
0260     virtual SKGError setProperty(const QString& iName, const QString& iValue,
0261                                  const QString& iFileName,
0262                                  SKGPropertyObject* oObjectCreated = nullptr) const;
0263 
0264     /**
0265      * Set the value for a property
0266      * @param iName the property name
0267      * @param iValue the property value
0268      * @param iBlob the property blob
0269      * @param oObjectCreated the property object created. Can be nullptr
0270      * @return an object managing the property
0271      *   @see SKGError
0272      */
0273     virtual SKGError setProperty(const QString& iName, const QString& iValue,
0274                                  const QVariant& iBlob = QVariant(),
0275                                  SKGPropertyObject* oObjectCreated = nullptr) const;
0276 
0277 
0278     /**
0279      * To know if an object exists or not
0280      * @return "true" if the object exists else "false".
0281      */
0282     virtual bool exist() const;
0283 
0284     /**
0285      * load or reload the object from the database
0286      * @return an object managing the error
0287      *   @see SKGError
0288      */
0289     SKGError load();
0290 
0291     /**
0292      * save the object into the database
0293      * @param iInsertOrUpdate the save mode.
0294      *    true: try an insert, if the insert failed then try an update.
0295      *    false: try an insert, if the insert failed then return an error.
0296      * @param iReloadAfterSave to reload the object after save. Set false if you are sure that you will not use this object after save
0297      * @return an object managing the error
0298      *   @see SKGError
0299      */
0300     virtual SKGError save(bool iInsertOrUpdate = true, bool iReloadAfterSave = true);
0301 
0302     /**
0303      * delete the object into the database
0304      * @param iSendMessage to send message when the object is deleted
0305      * @param iForce to force the deletion
0306      * @return an object managing the error
0307      *   @see SKGError
0308      */
0309     virtual SKGError remove(bool iSendMessage = true, bool iForce = false) const;
0310 
0311     /**
0312      * dump the object
0313      * @return an object managing the error
0314      *   @see SKGError
0315      */
0316     virtual SKGError dump() const;
0317 
0318 protected:
0319     /**
0320      * Copy from an existing object
0321      * @param iObject the object to copy
0322      */
0323     void copyFrom(const SKGObjectBase& iObject);
0324 
0325     /**
0326      * Get where clause needed to identify objects.
0327      * For this class, the whereclause is based on id
0328      * @return the where clause
0329      */
0330     virtual QString getWhereclauseId() const;
0331 
0332 private:
0333     Q_GADGET
0334     SKGObjectBasePrivate* d;
0335 };
0336 /**
0337  * Declare the meta type
0338  */
0339 Q_DECLARE_METATYPE(SKGObjectBase)
0340 
0341 /**
0342  * Declare the class
0343  */
0344 Q_DECLARE_TYPEINFO(SKGObjectBase, Q_MOVABLE_TYPE);
0345 
0346 /**
0347  * Declare the meta type
0348  */
0349 Q_DECLARE_METATYPE(SKGObjectBase::SKGListSKGObjectBase)
0350 #endif