Implement Notification Model Refactoring

<UserStory> 1953: Model Refactoring </UserStory>

UserStory #1953

<Changes>
1. Replaced CSV-based Notification serialization and deserialization with SerializedNotification record-based serialization for shared memory storage.
2. Implemented Notification::serialize() to convert Notification objects into fixed-size SerializedNotification structures.
3. Implemented Notification::deserialize() to reconstruct Notification objects directly from SerializedNotification records.
4. Added Notification state persistence by introducing util::State support in constructors, serialization, and deserialization flows.
5. Updated Notification class interfaces to use SerializedNotification types instead of std::string serialization APIs.
6. Removed legacy CSV serialization support, including CSV parsing logic and header generation functionality.
7. Added SerializedNotification dependencies through SerializedRecords.h inclusion and forward declaration support.
8. Initialized Notification objects with ACTIVE state by default and added state getter/setter APIs.
</Changes>

<Test>
N/A
</Test>

<Review>
Sreeja Reghukumar, please review
</Review>
This commit is contained in:
2026-06-12 17:36:15 +05:30
parent 1a4821ea8a
commit 5f4ee72ffe
2 changed files with 62 additions and 59 deletions
@@ -7,6 +7,7 @@ Date: 19-May-2026
*/ */
#include <sstream> #include <sstream>
#include "SerializedRecords.h"
#include "Notification.h" #include "Notification.h"
#include "StringHelper.h" #include "StringHelper.h"
#include "Factory.h" #include "Factory.h"
@@ -23,7 +24,8 @@ Returns:
*/ */
Notification::Notification() Notification::Notification()
: m_id("NOT" + std::to_string(++m_uid)), : m_id("NOT" + std::to_string(++m_uid)),
m_recipient(nullptr) {} m_recipient(nullptr), m_state(util::State::ACTIVE) {
}
/* /*
Function: Notification Function: Notification
@@ -43,7 +45,9 @@ Notification::Notification(const std::string& recipientUserId, User* recipient,
m_recipient(recipient), m_recipient(recipient),
m_title(title), m_title(title),
m_message(message), m_message(message),
m_createdAt(createdAt) {} m_state(util::State::ACTIVE),
m_createdAt(createdAt) {
}
/* /*
Function: Notification (parameterized constructor with ID) Function: Notification (parameterized constructor with ID)
@@ -58,13 +62,14 @@ Parameters:
Returns: Returns:
- A new Notification object - A new Notification object
*/ */
Notification::Notification(const std::string& id, const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt) Notification::Notification(const std::string& id, const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt, const util::State& state)
: m_id(id), : m_id(id),
m_recipientUserId(recipientUserId), m_recipientUserId(recipientUserId),
m_recipient(nullptr), m_recipient(nullptr),
m_title(title), m_title(title),
m_message(message), m_message(message),
m_createdAt(createdAt) m_createdAt(createdAt),
m_state(state)
{ {
int idNumber = util::extractNumber(m_id); int idNumber = util::extractNumber(m_id);
if (idNumber > m_uid) if (idNumber > m_uid)
@@ -139,6 +144,17 @@ const util::Timestamp& Notification::getCreatedAt() const
return m_createdAt; return m_createdAt;
} }
/*
Function: getState
Description: Retrieves the Notification state
Returns:
- const util::Timestamp& representing the creation timestamp.
*/
util::State Notification::getState() const
{
return m_state;
}
/* /*
Function: setId Function: setId
Description: Sets the unique ID of the notification. Description: Sets the unique ID of the notification.
@@ -217,71 +233,54 @@ void Notification::setCreatedAt(const util::Timestamp& createdAt)
m_createdAt = createdAt; m_createdAt = createdAt;
} }
/*
Function: setState
Description: Sets the Notification state.
Parameters:
- state: Notification state value.
Returns:
- void
*/
void Notification::setState(util::State state)
{
m_state = state;
}
/* /*
Function: serialize Function: serialize
Description: Serializes the notification into a CSV-formatted string. Description: Serializes the Notification object into a SerializedNotification record.
Parameters: Parameters:
- None - None
Returns: Returns:
- std::string: Serialized notification record - SerializedNotification: Serialized representation of the notification
*/ */
std::string Notification::serialize() const SerializedNotification Notification::serialize() const
{ {
std::ostringstream serializedNotification; SerializedNotification serialized = {};
serializedNotification << m_id << ',' strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
<< m_recipientUserId << ',' strcpy_s(serialized.recipientUserId, sizeof(serialized.recipientUserId), m_recipientUserId.c_str());
<< m_title << ',' strcpy_s(serialized.title, sizeof(serialized.title), m_title.c_str());
<< m_message << ',' strcpy_s(serialized.message, sizeof(serialized.message), m_message.c_str());
<< m_createdAt.toString(); serialized.createdAt = m_createdAt;
return serializedNotification.str(); serialized.state = m_state;
return serialized;
} }
/* /*
Function: deserialize Function: deserialize
Description: Deserializes a CSV-formatted string into a Notification object. Description: Deserializes a SerializedNotification record into a Notification object.
Parameters: Parameters:
- record: const std::string&, serialized notification record - serializedNotification: const SerializedNotification&, serialized notification record
Returns: Returns:
- Notification*: Pointer to the deserialized Notification object - Notification*: Pointer to the deserialized Notification object
Throws:
- std::runtime_error if timestamp parsing fails
*/ */
Notification* Notification::deserialize(const std::string& record) Notification* Notification::deserialize(const SerializedNotification& serializedNotification)
{ {
std::string id, recipientUserId, title, message, createdAtTimestampString;
std::istringstream serializedNotification(record);
getline(serializedNotification, id, ',');
getline(serializedNotification, recipientUserId, ',');
getline(serializedNotification, title, ',');
getline(serializedNotification, message, ',');
getline(serializedNotification, createdAtTimestampString, ',');
util::Timestamp createdAtTimestamp;
try
{
createdAtTimestamp = util::Timestamp::fromString(createdAtTimestampString);
}
catch (...)
{
throw std::runtime_error("Invalid createdAt timestamp");
}
return Factory::getObject<Notification>( return Factory::getObject<Notification>(
id, serializedNotification.id,
recipientUserId, serializedNotification.recipientUserId,
title, serializedNotification.title,
message, serializedNotification.message,
createdAtTimestamp serializedNotification.createdAt,
); serializedNotification.state);
}
/*
Function: getHeaders
Description: Retrieves the CSV headers for notification serialization.
Parameters:
- None
Returns:
- std::string: Header string ("ID,RecipientID,Title,Message,Timestamp")
*/
std::string Notification::getHeaders()
{
return "ID,RecipientID,Title,Message,Timestamp";
} }
@@ -9,8 +9,10 @@ Date: 19-May-2026
#pragma once #pragma once
#include <string> #include <string>
#include "Timestamp.h" #include "Timestamp.h"
#include "Enums.h"
class User; class User;
struct SerializedNotification;
class Notification class Notification
{ {
@@ -22,10 +24,11 @@ private:
std::string m_title; std::string m_title;
std::string m_message; std::string m_message;
util::Timestamp m_createdAt; util::Timestamp m_createdAt;
util::State m_state;
public: public:
Notification(); Notification();
Notification(const std::string& recipientUserId, User* recipient, const std::string& title, const std::string& message, const util::Timestamp& createdAt); Notification(const std::string& recipientUserId, User* recipient, const std::string& title, const std::string& message, const util::Timestamp& createdAt);
Notification(const std::string& id, const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt); Notification(const std::string& id, const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt, const util::State& state);
const std::string& getId() const; const std::string& getId() const;
const std::string& getRecipientUserId() const; const std::string& getRecipientUserId() const;
User* getRecipient() const; User* getRecipient() const;
@@ -38,7 +41,8 @@ public:
void setTitle(const std::string& title); void setTitle(const std::string& title);
void setMessage(const std::string& message); void setMessage(const std::string& message);
void setCreatedAt(const util::Timestamp& createdAt); void setCreatedAt(const util::Timestamp& createdAt);
std::string serialize() const; util::State getState() const;
static Notification* deserialize(const std::string&); void setState(util::State state);
static std::string getHeaders(); SerializedNotification serialize() const;
static Notification* deserialize(const SerializedNotification&);
}; };