Implement Model Refactoring
<UserStory> 1951: Model Refactoring</UserStory> UserStory #1951 <Changes> 1. Replaced CSV-based serialization and deserialization in ComboPackage, JobCard, Service, and ServiceBooking models with fixed-size SerializedRecord structures for shared memory storage. 2. Implemented serialize() methods to convert objects into SerializedComboPackage, SerializedJobCard, SerializedService, and SerializedServiceBooking records. 3. Implemented deserialize() methods to reconstruct objects directly from SerializedRecord types instead of parsing CSV strings. 4. Updated model class interfaces to use SerializedRecord types, removing legacy CSV serialization APIs and header generation functions. 5. Added SerializedRecords.h dependencies and forward declarations for Serialized structures across affected models. </Changes> <Test> N/A </Test> <Review> Sreeja Reghukumar </Review>
This commit is contained in:
@@ -8,6 +8,7 @@ Date: 19-May-2026
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include "SerializedRecords.h"
|
||||||
#include "User.h"
|
#include "User.h"
|
||||||
#include "Notification.h"
|
#include "Notification.h"
|
||||||
#include "Enums.h"
|
#include "Enums.h"
|
||||||
@@ -28,7 +29,8 @@ Returns:
|
|||||||
User::User()
|
User::User()
|
||||||
: m_id("USR" + std::to_string(++m_uid)),
|
: m_id("USR" + std::to_string(++m_uid)),
|
||||||
m_type(util::UserType::CUSTOMER),
|
m_type(util::UserType::CUSTOMER),
|
||||||
m_status(util::State::ACTIVE) {}
|
m_status(util::State::ACTIVE) {
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: User
|
Function: User
|
||||||
@@ -51,7 +53,8 @@ User::User(const std::string& userName, const std::string& password, const std::
|
|||||||
m_phone(phone),
|
m_phone(phone),
|
||||||
m_email(email),
|
m_email(email),
|
||||||
m_type(role),
|
m_type(role),
|
||||||
m_status(util::State::ACTIVE) {}
|
m_status(util::State::ACTIVE) {
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: User (parameterized constructor with ID)
|
Function: User (parameterized constructor with ID)
|
||||||
@@ -324,68 +327,43 @@ void User::setState(util::State status)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Function: serialize
|
Function: serialize
|
||||||
Description: Serializes the user into a CSV-formatted string.
|
Description: Serializes the User object into a SerializedUser record.
|
||||||
Parameters:
|
Parameters:
|
||||||
- None
|
- None
|
||||||
Returns:
|
Returns:
|
||||||
- std::string: Serialized user record
|
- SerializedUser: Serialized representation of the user
|
||||||
*/
|
*/
|
||||||
std::string User::serialize() const
|
SerializedUser User::serialize() const
|
||||||
{
|
{
|
||||||
std::ostringstream serializedUser;
|
SerializedUser serialized = {};
|
||||||
serializedUser << m_id << ','
|
strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
|
||||||
<< m_userName << ','
|
strcpy_s(serialized.username, sizeof(serialized.username), m_userName.c_str());
|
||||||
<< m_password << ','
|
strcpy_s(serialized.password, sizeof(serialized.password), m_password.c_str());
|
||||||
<< m_name << ','
|
strcpy_s(serialized.name, sizeof(serialized.name), m_name.c_str());
|
||||||
<< m_phone << ','
|
strcpy_s(serialized.phone, sizeof(serialized.phone), m_phone.c_str());
|
||||||
<< m_email << ','
|
strcpy_s(serialized.email, sizeof(serialized.email), m_email.c_str());
|
||||||
<< util::getUserTypeString(m_type) << ','
|
serialized.userType = m_type;
|
||||||
<< util::getStateString(m_status);
|
serialized.status = m_status;
|
||||||
return serializedUser.str();
|
return serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: deserialize
|
Function: deserialize
|
||||||
Description: Deserializes a CSV-formatted string into a User object.
|
Description: Deserializes a SerializedUser record into a User object.
|
||||||
Parameters:
|
Parameters:
|
||||||
- record: const std::string&, serialized user record
|
- serializedUser: const SerializedUser&, serialized user record
|
||||||
Returns:
|
Returns:
|
||||||
- User*: Pointer to the deserialized User object
|
- User*: Pointer to the deserialized User object
|
||||||
*/
|
*/
|
||||||
User* User::deserialize(const std::string& record)
|
User* User::deserialize(const SerializedUser& serializedUser)
|
||||||
{
|
{
|
||||||
std::string id, name, username, phone, password, email;
|
return Factory::getObject<User>(
|
||||||
std::string userTypeString, stateString;
|
serializedUser.id,
|
||||||
std::istringstream serializedUser(record);
|
serializedUser.username,
|
||||||
getline(serializedUser, id, ',');
|
serializedUser.password,
|
||||||
getline(serializedUser, username, ',');
|
serializedUser.name,
|
||||||
getline(serializedUser, password, ',');
|
serializedUser.phone,
|
||||||
getline(serializedUser, name, ',');
|
serializedUser.email,
|
||||||
getline(serializedUser, phone, ',');
|
serializedUser.userType,
|
||||||
getline(serializedUser, email, ',');
|
serializedUser.status);
|
||||||
getline(serializedUser, userTypeString, ',');
|
|
||||||
getline(serializedUser, stateString);
|
|
||||||
util::UserType userType = util::getUserType(userTypeString);
|
|
||||||
util::State status = util::getState(stateString);
|
|
||||||
return Factory::getObject<User>(id,
|
|
||||||
username,
|
|
||||||
password,
|
|
||||||
name,
|
|
||||||
phone,
|
|
||||||
email,
|
|
||||||
userType,
|
|
||||||
status);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Function: getHeaders
|
|
||||||
Description: Retrieves the CSV headers for user serialization.
|
|
||||||
Parameters:
|
|
||||||
- None
|
|
||||||
Returns:
|
|
||||||
- std::string: Header string ("ID,Username,Password,Name,Phone,Email,UserType,UserStatus")
|
|
||||||
*/
|
|
||||||
std::string User::getHeaders()
|
|
||||||
{
|
|
||||||
return "ID,Username,Password,Name,Phone,Email,UserType,UserStatus";
|
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,7 @@ Date: 19-May-2026
|
|||||||
#include "Enums.h"
|
#include "Enums.h"
|
||||||
|
|
||||||
class Notification;
|
class Notification;
|
||||||
|
struct SerializedUser;
|
||||||
|
|
||||||
class User : public Observer
|
class User : public Observer
|
||||||
{
|
{
|
||||||
@@ -51,7 +52,6 @@ public:
|
|||||||
void addNotification(Notification* notification) override;
|
void addNotification(Notification* notification) override;
|
||||||
void setRole(util::UserType role);
|
void setRole(util::UserType role);
|
||||||
void setState(util::State status);
|
void setState(util::State status);
|
||||||
std::string serialize() const;
|
SerializedUser serialize() const;
|
||||||
static User* deserialize(const std::string&);
|
static User* deserialize(const SerializedUser& serializedUser);
|
||||||
static std::string getHeaders();
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user