Moved ContactField record assignment from construction to initialization stage 02/58502/4
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Mon, 1 Feb 2016 15:17:48 +0000 (17:17 +0200)
committerEugene Kurzberg <i.kurtsberg@samsung.com>
Tue, 2 Feb 2016 08:14:03 +0000 (10:14 +0200)
to allow reuse of initialization logic in update from database feature.

Change-Id: I2ea575c2992dc1c1f2963e22ddf094e4365f0f61
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
18 files changed:
lib-contact/inc/Contacts/Model/ContactArray.h
lib-contact/inc/Contacts/Model/ContactDateField.h
lib-contact/inc/Contacts/Model/ContactEnumField.h
lib-contact/inc/Contacts/Model/ContactField.h
lib-contact/inc/Contacts/Model/ContactFieldFactory.h
lib-contact/inc/Contacts/Model/ContactObject.h
lib-contact/inc/Contacts/Model/ContactTextField.h
lib-contact/inc/Contacts/Model/ContactTypedObject.h
lib-contact/src/Contacts/Model/Contact.cpp
lib-contact/src/Contacts/Model/ContactArray.cpp
lib-contact/src/Contacts/Model/ContactDateField.cpp
lib-contact/src/Contacts/Model/ContactEnumField.cpp
lib-contact/src/Contacts/Model/ContactField.cpp
lib-contact/src/Contacts/Model/ContactFieldContainer.cpp
lib-contact/src/Contacts/Model/ContactFieldFactory.cpp
lib-contact/src/Contacts/Model/ContactObject.cpp
lib-contact/src/Contacts/Model/ContactTextField.cpp
lib-contact/src/Contacts/Model/ContactTypedObject.cpp

index 1d1c053683c73ff82127a1dd07e6b31dd2e0445b..a01b6c1fb0bf1455d6449bb4cad9700b7b57f8d0 100644 (file)
@@ -34,11 +34,6 @@ namespace Contacts
                public:
                        using ContactFieldContainer::ContactFieldContainer;
 
-                       /**
-                        * @see ContactField::initialize()
-                        */
-                       virtual void initialize() override;
-
                        /**
                         * @see ContactField::isChanged()
                         */
@@ -56,9 +51,18 @@ namespace Contacts
                         */
                        void removeField(ContactField &field);
 
-               private:
+               protected:
+                       /**
+                        * @return Array type metadata.
+                        */
                        const ContactArrayMetadata &getArrayMetadata() const;
 
+                       /**
+                        * @see ContactField::onInitialize()
+                        */
+                       virtual void onInitialize(contacts_record_h record) override;
+
+               private:
                        size_t m_InitialCount;
                };
        }
index 74bfcd7d9489a0e420ed2ff8d9ac4f76c7fa8a83..50c37949f90a7f5943b71fb118f9980916cf2e18 100644 (file)
@@ -35,11 +35,6 @@ namespace Contacts
                public:
                        using ContactField::ContactField;
 
-                       /**
-                        * @see ContactField::initialize()
-                        */
-                       virtual void initialize() override;
-
                        /**
                         * @see ContactInputField::reset()
                         */
@@ -61,6 +56,17 @@ namespace Contacts
                         */
                        void setValue(tm date);
 
+               protected:
+                       /**
+                        * @brief Get field value from the given record.
+                        */
+                       tm getValue(contacts_record_h record) const;
+
+                       /**
+                        * @see ContactField::onInitialize()
+                        */
+                       virtual void onInitialize(contacts_record_h record) override;
+
                private:
                        static tm convertDate(int value);
                        static int convertDate(const tm &date);
index 67159672090f2fa3a6edda5eb42455ac747cf2d2..53a17b8528d9415acd839846c6176b2d055a8dae 100644 (file)
@@ -38,11 +38,6 @@ namespace Contacts
                public:
                        using ContactField::ContactField;
 
-                       /**
-                        * @see ContactField::initialize()
-                        */
-                       virtual void initialize() override;
-
                        /**
                         * @see ContactField::reset()
                         */
@@ -79,9 +74,23 @@ namespace Contacts
                         */
                        bool hasCustomValue() const;
 
-               private:
+               protected:
+                       /**
+                        * @brief Get field value from the given record.
+                        */
+                       int getValue(contacts_record_h record) const;
+
+                       /**
+                        * @brief Enum type metadata.
+                        */
                        const ContactEnumMetadata &getEnumMetadata() const;
 
+                       /**
+                        * @see ContactField::onInitialize()
+                        */
+                       virtual void onInitialize(contacts_record_h record) override;
+
+               private:
                        int m_InitialValue;
                };
        }
index 34e5fce33da2651e429bf55863fc9bcb546f5704..2fb460d5499c7f4780aabfb8ba3bf505f2664f65 100644 (file)
@@ -48,18 +48,16 @@ namespace Contacts
 
                        /**
                         * @brief Create contact field.
-                        * @param[in]   record      Record containing the field
                         * @param[in]   metadata    Field metadata
                         */
-                       ContactField(contacts_record_h record,
-                                       const ContactFieldMetadata &metadata);
+                       ContactField(const ContactFieldMetadata &metadata);
                        virtual ~ContactField() { }
 
                        /**
-                        * @brief Initialize the adapter.
-                        * @remark This method should be called before using the object
+                        * @brief Initialize the adapter with database record.
+                        * @param[in]   record      Record containing the field
                         */
-                       virtual void initialize() { }
+                       void initialize(contacts_record_h record);
 
                        /**
                         * @brief Reset field values to default.
@@ -139,18 +137,6 @@ namespace Contacts
                        unsigned getPropertyId() const;
 
                protected:
-                       /**
-                        * @brief Create contact field.
-                        * @param[in]   metadata    Field metadata
-                        */
-                       ContactField(const ContactFieldMetadata &metadata);
-
-                       /**
-                        * @brief Set database record containing the field.
-                        * @param[in]   record      Record containing the field
-                        */
-                       void setRecord(contacts_record_h record);
-
                        /**
                         * @return Field metadata.
                         */
@@ -162,6 +148,12 @@ namespace Contacts
                         */
                        void onFilled(bool isFilled);
 
+                       /**
+                        * @brief Called when field is being initialized.
+                        * @param[in]   record      Record that initializes the field
+                        */
+                       virtual void onInitialize(contacts_record_h record) { }
+
                private:
                        contacts_record_h m_Record;
                        const ContactFieldMetadata &m_Metadata;
index a4b30428248d09b8e8d61537dde7723ac73c3d71..c178d8e0f6a09445cce4c866fb5a8bd93f510e95 100644 (file)
@@ -32,16 +32,10 @@ namespace Contacts
                public:
                        /**
                         * @brief Create record property adaptor.
-                        * @param[in]   record      Record to which property belongs
                         * @param[in]   metadata    Adaptor field metadata
                         * @return Adaptor field.
                         */
-                       static ContactFieldPtr createField(contacts_record_h record,
-                                       const ContactFieldMetadata &metadata);
-
-               private:
-                       static contacts_record_h getObjectRecord(contacts_record_h record,
-                                       const ContactFieldMetadata &metadata);
+                       static ContactFieldPtr createField(const ContactFieldMetadata &metadata);
                };
        }
 }
index da0bc7118288c31523be4b6f6a764659d131d18b..cbd88fab0d2321a33e818c9ef07ee2f1bb716463 100644 (file)
@@ -35,11 +35,6 @@ namespace Contacts
                public:
                        using ContactFieldContainer::ContactFieldContainer;
 
-                       /**
-                        * @see ContactField::initialize()
-                        */
-                       virtual void initialize() override;
-
                        /**
                         * @brief Get child field by id.
                         * @param[in]   id      Child field id
@@ -53,7 +48,19 @@ namespace Contacts
                        int getRecordId() const;
 
                protected:
+                       /**
+                        * @return Object type metadata.
+                        */
                        const ContactObjectMetadata &getObjectMetadata() const;
+
+                       /**
+                        * @see ContactField::onInitialize()
+                        */
+                       virtual void onInitialize(contacts_record_h record) override;
+
+               private:
+                       static contacts_record_h getChildRecord(contacts_record_h record,
+                                       const ContactFieldMetadata &metadata);
                };
        }
 }
index 20e88e4689b8ad0c9713aa0c3f6c47c703cbf4e6..aa066c6b24fc2eda19a3aaddeb7761db42314156 100644 (file)
@@ -35,11 +35,6 @@ namespace Contacts
                public:
                        using ContactField::ContactField;
 
-                       /**
-                        * @see ContactField::initialize()
-                        */
-                       virtual void initialize() override;
-
                        /**
                         * @see ContactField::reset()
                         */
@@ -65,6 +60,17 @@ namespace Contacts
                         */
                        void setValue(const char *value);
 
+               protected:
+                       /**
+                        * @brief Get field value from the given record.
+                        */
+                       const char *getValue(contacts_record_h record) const;
+
+                       /**
+                        * @see ContactField::onInitialize()
+                        */
+                       virtual void onInitialize(contacts_record_h record) override;
+
                private:
                        std::string m_InitialValue;
                };
index 4a5be6305076ef133a7c24d506aedf37686c6eab..9fa33cc3bf8b4895dd736aa87674a9da87b5464c 100644 (file)
@@ -37,11 +37,6 @@ namespace Contacts
                public:
                        using ContactObject::ContactObject;
 
-                       /**
-                        * @see ContactField::initialize()
-                        */
-                       virtual void initialize() override;
-
                        /**
                         * @see ContactField::reset()
                         */
@@ -62,9 +57,18 @@ namespace Contacts
                         */
                        ContactTextField &getLabelField() const;
 
-               private:
+               protected:
+                       /**
+                        * @return Typed object metadata.
+                        */
                        const ContactTypedObjectMetadata &getTypedObjectMetadata() const;
 
+                       /**
+                        * @see ContactField::onInitialize()
+                        */
+                       virtual void onInitialize(contacts_record_h record) override;
+
+               private:
                        ContactFieldPtr m_TypeField;
                        ContactFieldPtr m_LabelField;
                };
index dafa5c364903117ecf0827e786e66458028e9d01..2bda2a3c2dd6f404d73406e0cdf9bfad490c0eae 100644 (file)
@@ -43,9 +43,7 @@ int Contact::initialize(int recordId)
                err = contacts_db_get_record(uri, recordId, &record);
        }
 
-       setRecord(record);
-       ContactObject::initialize();
-
+       ContactObject::initialize(record);
        return err;
 }
 
index 0eb35d4af74de391d9695f2cd6043dbd61a3ccf4..a003c65ea6c0809af6d17547f297a33780a827c2 100644 (file)
 
 using namespace Contacts::Model;
 
-void ContactArray::initialize()
-{
-       int count = 0;
-       contacts_record_get_child_record_count(getRecord(), getPropertyId(), &count);
-
-       for (int i = 0; i < count; ++i) {
-               contacts_record_h record = nullptr;
-               contacts_record_get_child_record_at_p(getRecord(), getPropertyId(), i, &record);
-               ContactFieldContainer::addField(record, getArrayMetadata().element);
-       }
-
-       m_InitialCount = getFieldCount();
-}
-
 bool ContactArray::isChanged() const
 {
        if (getFieldCount() < m_InitialCount) {
@@ -70,3 +56,17 @@ const ContactArrayMetadata &ContactArray::getArrayMetadata() const
 {
        return *(const ContactArrayMetadata *) ContactField::getMetadata().typeMetadata;
 }
+
+void ContactArray::onInitialize(contacts_record_h record)
+{
+       int count = 0;
+       contacts_record_get_child_record_count(record, getPropertyId(), &count);
+
+       for (int i = 0; i < count; ++i) {
+               contacts_record_h childRecord = nullptr;
+               contacts_record_get_child_record_at_p(record, getPropertyId(), i, &childRecord);
+               ContactFieldContainer::addField(childRecord, getArrayMetadata().element);
+       }
+
+       m_InitialCount = getFieldCount();
+}
index ec45bda2d37c43e9ca5c055d6cb42807dd8c9f39..8a4c0037b4831565fffdf8ea0202037eac817f8e 100644 (file)
 
 using namespace Contacts::Model;
 
-void ContactDateField::initialize()
-{
-       tm value = getValue();
-       m_InitialValue = mktime(&value);
-}
-
 void ContactDateField::reset()
 {
        time_t now = time(nullptr);
@@ -44,9 +38,7 @@ bool ContactDateField::isChanged() const
 
 tm ContactDateField::getValue() const
 {
-       int date = 0;
-       contacts_record_get_int(getRecord(), getPropertyId(), &date);
-       return convertDate(date);
+       return getValue(getRecord());
 }
 
 void ContactDateField::setValue(tm date)
@@ -54,6 +46,19 @@ void ContactDateField::setValue(tm date)
        contacts_record_set_int(getRecord(), getPropertyId(), convertDate(date));
 }
 
+tm ContactDateField::getValue(contacts_record_h record) const
+{
+       int date = 0;
+       contacts_record_get_int(record, getPropertyId(), &date);
+       return convertDate(date);
+}
+
+void ContactDateField::onInitialize(contacts_record_h record)
+{
+       tm value = getValue(record);
+       m_InitialValue = mktime(&value);
+}
+
 tm ContactDateField::convertDate(int value)
 {
        struct tm date = { 0 };
index 603b8d9c852d2f2dfab9698edbac3c0b3e678c1b..03dbd79b0ac7e948fee8d7001c687c9c6a5c65a9 100644 (file)
 
 using namespace Contacts::Model;
 
-void ContactEnumField::initialize()
-{
-       m_InitialValue = getValue();
-}
-
 void ContactEnumField::reset()
 {
        setValue(getEnumMetadata().defaultValue);
@@ -47,9 +42,7 @@ int ContactEnumField::getCustomValue() const
 
 int ContactEnumField::getValue() const
 {
-       int value = 0;
-       contacts_record_get_int(getRecord(), getPropertyId(), &value);
-       return value;
+       return getValue(getRecord());
 }
 
 void ContactEnumField::setValue(int value)
@@ -62,7 +55,19 @@ bool ContactEnumField::hasCustomValue() const
        return getValue() == getEnumMetadata().customValue;
 }
 
+int ContactEnumField::getValue(contacts_record_h record) const
+{
+       int value = 0;
+       contacts_record_get_int(record, getPropertyId(), &value);
+       return value;
+}
+
 const ContactEnumMetadata &ContactEnumField::getEnumMetadata() const
 {
        return *(const ContactEnumMetadata *) ContactField::getMetadata().typeMetadata;
 }
+
+void ContactEnumField::onInitialize(contacts_record_h record)
+{
+       m_InitialValue = getValue(record);
+}
index c3d11e3ea6ed899779a8fda252aef5405fa4fce1..ac01830fae99aed81b59145328c7bbb19e9c5efb 100644 (file)
 
 using namespace Contacts::Model;
 
-ContactField::ContactField(contacts_record_h record,
-               const ContactFieldMetadata &metadata)
-       : m_Record(record), m_Metadata(metadata)
+ContactField::ContactField(const ContactFieldMetadata &metadata)
+       : m_Record(nullptr), m_Metadata(metadata)
 {
 }
 
-ContactField::ContactField(const ContactFieldMetadata &metadata)
-       : m_Record(nullptr), m_Metadata(metadata)
+void ContactField::initialize(contacts_record_h record)
 {
+       onInitialize(record);
+       m_Record = record;
 }
 
 template <typename FieldType>
@@ -102,11 +102,6 @@ unsigned ContactField::getPropertyId() const
        return m_Metadata.propId;
 }
 
-void ContactField::setRecord(contacts_record_h record)
-{
-       m_Record = record;
-}
-
 const ContactFieldMetadata &ContactField::getMetadata() const
 {
        return m_Metadata;
index d03a78530e48277b25a4f48b73a852c43403494e..5dabf8b875cceba361a47b4b451fea32e4af4a5e 100644 (file)
@@ -75,7 +75,9 @@ size_t ContactFieldContainer::getFieldCount() const
 ContactField &ContactFieldContainer::addField(contacts_record_h record,
                const ContactFieldMetadata &metadata)
 {
-       ContactFieldPtr field = ContactFieldFactory::createField(record, metadata);
+       ContactFieldPtr field = ContactFieldFactory::createField(metadata);
+       field->initialize(record);
+
        if (field->isRequired()) {
                if (field->isFilled()) {
                        onChildFilled(true);
index 887609038c523f2a739d391b4b8f84cc52fa1732..e2a8c7a0873c6972af968ad6d314f5cedc79d9d0 100644 (file)
 
 using namespace Contacts::Model;
 
-ContactFieldPtr ContactFieldFactory::createField(contacts_record_h record,
-               const ContactFieldMetadata &metadata)
+ContactFieldPtr ContactFieldFactory::createField(const ContactFieldMetadata &metadata)
 {
        ContactField *field = nullptr;
        switch(metadata.typeMetadata->type) {
                case TypeBool:
-                       field = new ContactBoolField(record, metadata); break;
+                       field = new ContactBoolField(metadata); break;
                case TypeEnum:
-                       field = new ContactEnumField(record, metadata); break;
+                       field = new ContactEnumField(metadata); break;
                case TypeText:
-                       field = new ContactTextField(record, metadata); break;
+                       field = new ContactTextField(metadata); break;
                case TypeDate:
-                       field = new ContactDateField(record, metadata); break;
+                       field = new ContactDateField(metadata); break;
                case TypeArray:
-                       field = new ContactArray(record, metadata); break;
+                       field = new ContactArray(metadata); break;
                case TypeObject:
                {
-                       record = getObjectRecord(record, metadata);
-
                        unsigned subType = metadata.typeMetadata->subType;
                        if (subType & ObjectTyped) {
-                               field = new ContactTypedObject(record, metadata);
+                               field = new ContactTypedObject(metadata);
                        } else if (subType & ObjectCompound) {
                                switch (metadata.id) {
                                        case FieldName:
-                                               field = new ContactName(record, metadata);
+                                               field = new ContactName(metadata);
                                                break;
                                        case FieldPhoneticName:
-                                               field = new ContactPhoneticName(record, metadata);
+                                               field = new ContactPhoneticName(metadata);
                                                break;
                                }
                        } else {
-                               field = new ContactObject(record, metadata);
+                               field = new ContactObject(metadata);
                        }
                }
                        break;
        }
 
-       if (field) {
-               field->initialize();
-       }
-
        return ContactFieldPtr(field);
 }
-
-contacts_record_h ContactFieldFactory::getObjectRecord(contacts_record_h record,
-               const ContactFieldMetadata &metadata)
-{
-       contacts_record_h childRecord = nullptr;
-       int err = contacts_record_get_child_record_at_p(record, metadata.propId, 0, &childRecord);
-
-       if (err == CONTACTS_ERROR_NO_DATA) {
-               const char *uri = ((const ContactObjectMetadata *) metadata.typeMetadata)->uri;
-               contacts_record_create(uri, &childRecord);
-               contacts_record_add_child_record(record, metadata.propId, childRecord);
-       }
-
-       return childRecord ? childRecord : record;
-}
index d7a3a4842bc2ea34a632dafa707a375ff6be0b30..64485e0ac8a7cec63e24e7650bc9ab296f5ed557 100644 (file)
 
 using namespace Contacts::Model;
 
-void ContactObject::initialize()
-{
-       for (auto &&field : getObjectMetadata().fields) {
-               addField(getRecord(), field);
-       }
-}
-
 ContactField *ContactObject::getFieldById(unsigned id) const
 {
        for (auto &&field : *this) {
@@ -49,3 +42,28 @@ const ContactObjectMetadata &ContactObject::getObjectMetadata() const
 {
        return *(const ContactObjectMetadata *) ContactField::getMetadata().typeMetadata;
 }
+
+void ContactObject::onInitialize(contacts_record_h record)
+{
+       for (auto &&field : getObjectMetadata().fields) {
+               addField(getChildRecord(record, field), field);
+       }
+}
+
+contacts_record_h ContactObject::getChildRecord(contacts_record_h record,
+               const ContactFieldMetadata &metadata)
+{
+       if (metadata.typeMetadata->type != TypeObject) {
+               return record;
+       }
+
+       contacts_record_h childRecord = nullptr;
+       int err = contacts_record_get_child_record_at_p(record, metadata.propId, 0, &childRecord);
+       if (err == CONTACTS_ERROR_NO_DATA) {
+               const char *uri = ((const ContactObjectMetadata *) metadata.typeMetadata)->uri;
+               contacts_record_create(uri, &childRecord);
+               contacts_record_add_child_record(record, metadata.propId, childRecord);
+       }
+
+       return childRecord;
+}
index 78ed8593162f7f384c86ce5378697afaf07915ee..9c853d97354a10479ee3787cf8c39cfadbd7676f 100644 (file)
 
 using namespace Contacts::Model;
 
-void ContactTextField::initialize()
-{
-       const char *value = getValue();
-       if (value) {
-               m_InitialValue = value;
-       }
-}
-
 void ContactTextField::reset()
 {
        setValue("");
@@ -51,9 +43,7 @@ bool ContactTextField::isChanged() const
 
 const char *ContactTextField::getValue() const
 {
-       char *value = nullptr;
-       contacts_record_get_str_p(getRecord(), getPropertyId(), &value);
-       return value;
+       return getValue(getRecord());
 }
 
 void ContactTextField::setValue(const char *value)
@@ -66,3 +56,18 @@ void ContactTextField::setValue(const char *value)
                onFilled(isFilled);
        }
 }
+
+const char *ContactTextField::getValue(contacts_record_h record) const
+{
+       char *value = nullptr;
+       contacts_record_get_str_p(record, getPropertyId(), &value);
+       return value;
+}
+
+void ContactTextField::onInitialize(contacts_record_h record)
+{
+       const char *value = getValue(record);
+       if (value) {
+               m_InitialValue = value;
+       }
+}
index 5e0e6e6e920d4995ae7f8e59a0b0f473b1f4e89c..c233e208e8b8627df90eaef34b3fe8e5b5bf4059 100644 (file)
 
 using namespace Contacts::Model;
 
-void ContactTypedObject::initialize()
-{
-       ContactObject::initialize();
-       m_TypeField = ContactFieldFactory::createField(getRecord(), getTypedObjectMetadata().typeField);
-       m_LabelField = ContactFieldFactory::createField(getRecord(), getTypedObjectMetadata().labelField);
-}
-
 void ContactTypedObject::reset()
 {
        ContactObject::reset();
@@ -68,3 +61,13 @@ const ContactTypedObjectMetadata &ContactTypedObject::getTypedObjectMetadata() c
 {
        return (const ContactTypedObjectMetadata &) getObjectMetadata();
 }
+
+void ContactTypedObject::onInitialize(contacts_record_h record)
+{
+       ContactObject::onInitialize(record);
+       m_TypeField = ContactFieldFactory::createField(getTypedObjectMetadata().typeField);
+       m_LabelField = ContactFieldFactory::createField(getTypedObjectMetadata().labelField);
+
+       m_TypeField->initialize(record);
+       m_LabelField->initialize(record);
+}