to allow reuse of initialization logic in update from database feature.
Change-Id: I2ea575c2992dc1c1f2963e22ddf094e4365f0f61
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
public:
using ContactFieldContainer::ContactFieldContainer;
- /**
- * @see ContactField::initialize()
- */
- virtual void initialize() override;
-
/**
* @see ContactField::isChanged()
*/
*/
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;
};
}
public:
using ContactField::ContactField;
- /**
- * @see ContactField::initialize()
- */
- virtual void initialize() override;
-
/**
* @see ContactInputField::reset()
*/
*/
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);
public:
using ContactField::ContactField;
- /**
- * @see ContactField::initialize()
- */
- virtual void initialize() override;
-
/**
* @see ContactField::reset()
*/
*/
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;
};
}
/**
* @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.
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.
*/
*/
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;
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);
};
}
}
public:
using ContactFieldContainer::ContactFieldContainer;
- /**
- * @see ContactField::initialize()
- */
- virtual void initialize() override;
-
/**
* @brief Get child field by id.
* @param[in] id Child field id
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);
};
}
}
public:
using ContactField::ContactField;
- /**
- * @see ContactField::initialize()
- */
- virtual void initialize() override;
-
/**
* @see ContactField::reset()
*/
*/
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;
};
public:
using ContactObject::ContactObject;
- /**
- * @see ContactField::initialize()
- */
- virtual void initialize() override;
-
/**
* @see ContactField::reset()
*/
*/
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;
};
err = contacts_db_get_record(uri, recordId, &record);
}
- setRecord(record);
- ContactObject::initialize();
-
+ ContactObject::initialize(record);
return err;
}
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) {
{
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();
+}
using namespace Contacts::Model;
-void ContactDateField::initialize()
-{
- tm value = getValue();
- m_InitialValue = mktime(&value);
-}
-
void ContactDateField::reset()
{
time_t now = time(nullptr);
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)
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 };
using namespace Contacts::Model;
-void ContactEnumField::initialize()
-{
- m_InitialValue = getValue();
-}
-
void ContactEnumField::reset()
{
setValue(getEnumMetadata().defaultValue);
int ContactEnumField::getValue() const
{
- int value = 0;
- contacts_record_get_int(getRecord(), getPropertyId(), &value);
- return value;
+ return getValue(getRecord());
}
void ContactEnumField::setValue(int value)
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);
+}
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>
return m_Metadata.propId;
}
-void ContactField::setRecord(contacts_record_h record)
-{
- m_Record = record;
-}
-
const ContactFieldMetadata &ContactField::getMetadata() const
{
return m_Metadata;
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);
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;
-}
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) {
{
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;
+}
using namespace Contacts::Model;
-void ContactTextField::initialize()
-{
- const char *value = getValue();
- if (value) {
- m_InitialValue = value;
- }
-}
-
void ContactTextField::reset()
{
setValue("");
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)
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;
+ }
+}
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();
{
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);
+}