287 lines
7.4 KiB
C++
287 lines
7.4 KiB
C++
/*
|
|
File: Notification.cpp
|
|
Description: Implements the Notification class which represents system notifications in the Vehicle Service Management System.
|
|
Provides constructors, accessors, and mutators for notification details such as ID, recipient, title, message, and timestamp.
|
|
Author: Trenser
|
|
Date: 19-May-2026
|
|
*/
|
|
|
|
#include <sstream>
|
|
#include "Notification.h"
|
|
#include "StringHelper.h"
|
|
#include "Factory.h"
|
|
|
|
int Notification::m_uid = 0;
|
|
|
|
/*
|
|
Function: Notification
|
|
Description: Default constructor that initializes a new notification with a unique ID and null recipient.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- A new Notification object.
|
|
*/
|
|
Notification::Notification()
|
|
: m_id("NOT" + std::to_string(++m_uid)),
|
|
m_recipient(nullptr) {}
|
|
|
|
/*
|
|
Function: Notification
|
|
Description: Parameterized constructor that initializes a new notification with a unique ID and specified details.
|
|
Parameters:
|
|
- recipientUserId: ID of the recipient user.
|
|
- recipient: Pointer to the User object representing the recipient.
|
|
- title: Title of the notification.
|
|
- message: Message content of the notification.
|
|
- createdAt: Timestamp of when the notification was created.
|
|
Returns:
|
|
- A new Notification object.
|
|
*/
|
|
Notification::Notification(const std::string& recipientUserId, User* recipient, const std::string& title, const std::string& message, const util::Timestamp& createdAt)
|
|
: m_id("NOT" + std::to_string(++m_uid)),
|
|
m_recipientUserId(recipientUserId),
|
|
m_recipient(recipient),
|
|
m_title(title),
|
|
m_message(message),
|
|
m_createdAt(createdAt) {}
|
|
|
|
/*
|
|
Function: Notification (parameterized constructor with ID)
|
|
Description: Initializes a notification with an existing ID, recipient details,
|
|
title, message, and creation timestamp. Updates UID tracking based on ID.
|
|
Parameters:
|
|
- id: const std::string&, unique notification ID
|
|
- recipientUserId: const std::string&, ID of the recipient user
|
|
- title: const std::string&, notification title
|
|
- message: const std::string&, notification message
|
|
- createdAt: const util::Timestamp&, timestamp of creation
|
|
Returns:
|
|
- 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)
|
|
: m_id(id),
|
|
m_recipientUserId(recipientUserId),
|
|
m_recipient(nullptr),
|
|
m_title(title),
|
|
m_message(message),
|
|
m_createdAt(createdAt)
|
|
{
|
|
int idNumber = util::extractNumber(m_id);
|
|
if (idNumber > m_uid)
|
|
{
|
|
m_uid = idNumber;
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: getId
|
|
Description: Retrieves the unique ID of the notification.
|
|
Returns:
|
|
- const std::string& representing the notification ID.
|
|
*/
|
|
const std::string& Notification::getId() const
|
|
{
|
|
return m_id;
|
|
}
|
|
|
|
/*
|
|
Function: getRecipientUserId
|
|
Description: Retrieves the recipient user ID associated with the notification.
|
|
Returns:
|
|
- const std::string& representing the recipient user ID.
|
|
*/
|
|
const std::string& Notification::getRecipientUserId() const
|
|
{
|
|
return m_recipientUserId;
|
|
}
|
|
|
|
/*
|
|
Function: getRecipient
|
|
Description: Retrieves the pointer to the recipient user.
|
|
Returns:
|
|
- User* representing the recipient.
|
|
*/
|
|
User* Notification::getRecipient() const
|
|
{
|
|
return m_recipient;
|
|
}
|
|
|
|
/*
|
|
Function: getTitle
|
|
Description: Retrieves the title of the notification.
|
|
Returns:
|
|
- const std::string& representing the notification title.
|
|
*/
|
|
const std::string& Notification::getTitle() const
|
|
{
|
|
return m_title;
|
|
}
|
|
|
|
/*
|
|
Function: getMessage
|
|
Description: Retrieves the message content of the notification.
|
|
Returns:
|
|
- const std::string& representing the notification message.
|
|
*/
|
|
const std::string& Notification::getMessage() const
|
|
{
|
|
return m_message;
|
|
}
|
|
|
|
/*
|
|
Function: getCreatedAt
|
|
Description: Retrieves the timestamp of when the notification was created.
|
|
Returns:
|
|
- const util::Timestamp& representing the creation timestamp.
|
|
*/
|
|
const util::Timestamp& Notification::getCreatedAt() const
|
|
{
|
|
return m_createdAt;
|
|
}
|
|
|
|
/*
|
|
Function: setId
|
|
Description: Sets the unique ID of the notification.
|
|
Parameters:
|
|
- id: New notification ID string.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Notification::setId(const std::string& id)
|
|
{
|
|
m_id = id;
|
|
}
|
|
|
|
/*
|
|
Function: setRecipientUserId
|
|
Description: Sets the recipient user ID for the notification.
|
|
Parameters:
|
|
- recipientUserId: New recipient user ID string.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Notification::setRecipientUserId(const std::string& recipientUserId)
|
|
{
|
|
m_recipientUserId = recipientUserId;
|
|
}
|
|
|
|
/*
|
|
Function: setRecipient
|
|
Description: Sets the recipient user pointer for the notification.
|
|
Parameters:
|
|
- recipient: Pointer to the User object.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Notification::setRecipient(User* recipient)
|
|
{
|
|
m_recipient = recipient;
|
|
}
|
|
|
|
/*
|
|
Function: setTitle
|
|
Description: Sets the title of the notification.
|
|
Parameters:
|
|
- title: New notification title string.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Notification::setTitle(const std::string& title)
|
|
{
|
|
m_title = title;
|
|
}
|
|
|
|
/*
|
|
Function: setMessage
|
|
Description: Sets the message content of the notification.
|
|
Parameters:
|
|
- message: New notification message string.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Notification::setMessage(const std::string& message)
|
|
{
|
|
m_message = message;
|
|
}
|
|
|
|
/*
|
|
Function: setCreatedAt
|
|
Description: Sets the timestamp of when the notification was created.
|
|
Parameters:
|
|
- createdAt: New timestamp value.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Notification::setCreatedAt(const util::Timestamp& createdAt)
|
|
{
|
|
m_createdAt = createdAt;
|
|
}
|
|
|
|
/*
|
|
Function: serialize
|
|
Description: Serializes the notification into a CSV-formatted string.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- std::string: Serialized notification record
|
|
*/
|
|
std::string Notification::serialize() const
|
|
{
|
|
std::ostringstream serializedNotification;
|
|
serializedNotification << m_id << ','
|
|
<< m_recipientUserId << ','
|
|
<< m_title << ','
|
|
<< m_message << ','
|
|
<< m_createdAt.toString();
|
|
return serializedNotification.str();
|
|
}
|
|
|
|
/*
|
|
Function: deserialize
|
|
Description: Deserializes a CSV-formatted string into a Notification object.
|
|
Parameters:
|
|
- record: const std::string&, serialized notification record
|
|
Returns:
|
|
- Notification*: Pointer to the deserialized Notification object
|
|
Throws:
|
|
- std::runtime_error if timestamp parsing fails
|
|
*/
|
|
Notification* Notification::deserialize(const std::string& record)
|
|
{
|
|
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>(
|
|
id,
|
|
recipientUserId,
|
|
title,
|
|
message,
|
|
createdAtTimestamp
|
|
);
|
|
}
|
|
|
|
/*
|
|
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";
|
|
} |