243 lines
9.3 KiB
C++
Executable File
243 lines
9.3 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* ISM Handler Mounting Point Schedule
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Iuliana Ojog, SLSE, 2016-11-26
|
|
*/
|
|
|
|
#include "handler_main_schedule.h"
|
|
#include "object_main_schedule.h"
|
|
#include "main_handler.h"
|
|
#include "convert_db_to_dds.h"
|
|
#include "handler_main_structure_element.h"
|
|
#include "object_structure_element.h"
|
|
|
|
#include <InfrastructureManagement/ism_db.h>
|
|
#include <NGLLogger/NGLLoggerBootstrap.h>
|
|
|
|
using namespace InfrastructureManagement;
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------------------------------
|
|
HandlerMainSchedule::HandlerMainSchedule(MainHandler *mainHandler)
|
|
: Base(mainHandler, getLogger("HandlerMainSchedule"))
|
|
, Interface::Fts::MainScheduleModifyRequestHandler(mainHandler->dds()->fts())
|
|
, Interface::Fts::MainScheduleCopyRequestHandler(mainHandler->dds()->fts())
|
|
, Interface::Fts::MainScheduleRemoveRequestHandler(mainHandler->dds()->fts())
|
|
, Interface::Fts::MountingPointPlanModifyRequestHandler(mainHandler->dds()->fts())
|
|
, m_mainHandler(mainHandler)
|
|
{
|
|
connect(db().get(), &DatabaseInterface::notifyInsertMainSchedule, this, &HandlerMainSchedule::onDbInsertedSchedule);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------------------------------
|
|
HandlerMainSchedule::~HandlerMainSchedule()
|
|
{
|
|
qDeleteAll(m_objectMainScheduleMap);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------------------------------
|
|
bool HandlerMainSchedule::init()
|
|
{
|
|
bool bRet = true;
|
|
auto cbMps = [this, &bRet] (const Database::IsmMountingPointSchedule &data)
|
|
{
|
|
InfrastructureManagement::MainSchedule ddsData = ObjectMainSchedule::toDdsData(data);
|
|
ObjectMainSchedule *ifs(new ObjectMainSchedule(m_mainHandler));
|
|
Core::FunctionReplyObject ret = ifs->createObject(ddsData);
|
|
if (ret.error())
|
|
{
|
|
delete ifs;
|
|
ERROR_LOG(m_logger) << ret();
|
|
bRet = false;
|
|
return;
|
|
}
|
|
m_objectMainScheduleMap.insert(ifs->getId(), ifs);
|
|
DEBUG_LOG(m_logger) << "MainSchedule Id = " << ifs->getId();
|
|
};
|
|
|
|
// create Mounting Point Schedule objects
|
|
if (!db()->select(cbMps) || !bRet)
|
|
{
|
|
ERROR_LOG(m_logger) << "Error reading data from database!";
|
|
return false;
|
|
}
|
|
|
|
for (ObjectMainSchedule * ifs : m_objectMainScheduleMap)
|
|
{
|
|
ifs->loadMountingPointPlans();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
void HandlerMainSchedule::onMainScheduleModifyRequest(const rti::core::SampleIdentity &cookie,
|
|
const MainScheduleModifyRequest &request)
|
|
{
|
|
DEBUG_LOG(m_logger) << "onMainScheduleModifyRequest: " << request;
|
|
const Core::FunctionReplyObject ret = request.id() > 0 ? mainScheduleModify(request) : mainScheduleCreate(request);
|
|
if (ret.error())
|
|
WARNING_LOG(m_logger) << ret();
|
|
m_mainHandler->dds()->fts()->mainScheduleModifyReply(cookie, ret);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
void HandlerMainSchedule::onMainScheduleCopyRequest(const rti::core::SampleIdentity &cookie,
|
|
const MainScheduleCopyRequest &request)
|
|
{
|
|
DEBUG_LOG(m_logger) << "onMainScheduleCopyRequest: " << request;
|
|
Core::FunctionReplyObject ret;
|
|
if (request.id() > 0)
|
|
{
|
|
ObjectMainSchedule ifs(m_mainHandler);
|
|
ret = ifs.copyObject(request.id());
|
|
onDbInsertedSchedule(ifs.getId()); // because notification is not emitted in this case
|
|
}
|
|
if (ret.error())
|
|
WARNING_LOG(m_logger) << ret();
|
|
m_mainHandler->dds()->fts()->mainScheduleCopyReply(cookie, ret);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
// ---- .elements() are explicitely NOT changed with this method ----
|
|
static MainSchedule convert(const MainScheduleModifyRequest &request)
|
|
{
|
|
MainSchedule ddsData;
|
|
ddsData.id(request.id());
|
|
ddsData.name(request.name());
|
|
ddsData.statusId(request.statusId());
|
|
ddsData.timeCreated(request.timeCreated());
|
|
ddsData.timeStart(request.timeStart());
|
|
ddsData.timeEnd(request.timeEnd());
|
|
ddsData.exerciseId(request.exerciseId());
|
|
ddsData.description(request.description());
|
|
|
|
return ddsData;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------------------------------
|
|
// ---- .elements() are explicitely NOT changed with this method ----
|
|
Core::FunctionReplyObject HandlerMainSchedule::mainScheduleCreate(const InfrastructureManagement::MainScheduleModifyRequest &request)
|
|
{
|
|
ObjectMainSchedule *ifs(new ObjectMainSchedule(m_mainHandler));
|
|
Core::FunctionReplyObject ret = ifs->createObject(convert(request));
|
|
if (ret.error())
|
|
delete ifs;
|
|
else
|
|
{
|
|
ifs->loadMountingPointPlans();
|
|
m_objectMainScheduleMap.insert(ifs->getId(), ifs);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------------------------------
|
|
// ---- .elements() are explicitely NOT changed with this method ----
|
|
Core::FunctionReplyObject HandlerMainSchedule::mainScheduleModify(const InfrastructureManagement::MainScheduleModifyRequest &request)
|
|
{
|
|
ObjectMainSchedule *ifs = m_objectMainScheduleMap.value(request.id());
|
|
if (ifs == nullptr)
|
|
return Core::FunctionReplyObject(request.id(), tr_lts("dds", "MainSchedule with id %1 not found")).arg(request.id());
|
|
|
|
return ifs->updateObject(convert(request));;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------------------------------
|
|
void HandlerMainSchedule::onMainScheduleRemoveRequest(const rti::core::SampleIdentity &cookie,
|
|
const Core::RemoveElementRequest &request)
|
|
{
|
|
Core::FunctionReplyObject ret;
|
|
ObjectMainSchedule *ifs = m_objectMainScheduleMap.value(request.id());
|
|
if (ifs == nullptr)
|
|
{
|
|
ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "MainSchedule with id %1 not found")).arg(request.id());
|
|
m_mainHandler->dds()->fts()->mainScheduleRemoveReply(cookie, ret);
|
|
return;
|
|
}
|
|
|
|
if (m_mainHandler->dds()->topicLockMaster()->isLocked(ifs->getData(), m_mainHandler->exerciseId()))
|
|
{
|
|
ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "MainSchedule '%1' is used by another user!")).arg(ifs->getName());
|
|
m_mainHandler->dds()->fts()->mainScheduleRemoveReply(cookie, ret);
|
|
return;
|
|
}
|
|
|
|
ret = ifs->removeObject();
|
|
|
|
if (ret.error())
|
|
ERROR_LOG(m_logger) << ret();
|
|
else
|
|
{
|
|
delete ifs;
|
|
m_objectMainScheduleMap.remove(request.id());
|
|
}
|
|
|
|
m_mainHandler->dds()->fts()->mainScheduleRemoveReply(cookie, ret);
|
|
}
|
|
//--------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
void HandlerMainSchedule::onMountingPointPlanModifyRequest(const rti::core::SampleIdentity &cookie,
|
|
const InfrastructureManagement::MountingPointPlanModifyRequest &request)
|
|
{
|
|
DEBUG_LOG(m_logger) << "onMountingPointPlanModifyRequest: " << request;
|
|
const Core::FunctionReplyObject ret = mountingPointPlanModify(request);
|
|
if (ret.error())
|
|
WARNING_LOG(m_logger) << ret();
|
|
m_mainHandler->dds()->fts()->mountingPointPlanModifyReply(cookie, ret);
|
|
}
|
|
|
|
Core::FunctionReplyObject HandlerMainSchedule::mountingPointPlanModify(const InfrastructureManagement::MountingPointPlanModifyRequest &request)
|
|
{
|
|
ObjectMainSchedule *ifs = m_objectMainScheduleMap.value(request.mountingPointScheduleId());
|
|
if (ifs == nullptr)
|
|
return Core::FunctionReplyObject(request.mountingPointScheduleId(), tr_lts("dds", "MainSchedule with id %1 not found")).arg(request.mountingPointScheduleId());
|
|
|
|
return ifs->updateElements(request.mountingPointPlans());
|
|
}
|
|
|
|
//----------------------------------------------------------------------------------------------------------------------------
|
|
void HandlerMainSchedule::onDbInsertedSchedule(const QVariant &id_)
|
|
{
|
|
int id = id_.toInt();
|
|
NOTICE_LOG(m_logger) << "DB inserted Schedule with id: " << id;
|
|
if (!m_objectMainScheduleMap.contains(id))
|
|
initByDbId(id);
|
|
}
|
|
|
|
//----------------------------------------------------------------------------------------------------------------------------
|
|
bool HandlerMainSchedule::initByDbId(int dbId)
|
|
{
|
|
Database::IsmMountingPointSchedule msData;
|
|
DEBUG_LOG(m_logger) << "Reading Schedule from database!";
|
|
if (!db()->selectIsmMountingPointSchedule(dbId, msData))
|
|
{
|
|
ERROR_LOG(m_logger) << "Error reading schedule data from database: id not found " << dbId;
|
|
return false;
|
|
}
|
|
|
|
if (msData.id != dbId)
|
|
return false;
|
|
|
|
// create object
|
|
ObjectMainSchedule *ifs(new ObjectMainSchedule(m_mainHandler));
|
|
Core::FunctionReplyObject ret = ifs->createObject(Database::convert(msData));
|
|
if (ret.error())
|
|
{
|
|
delete ifs;
|
|
ERROR_LOG(m_logger) << ret();
|
|
return false;
|
|
}
|
|
m_objectMainScheduleMap.insert(ifs->getId(), ifs);
|
|
|
|
return true;
|
|
}
|
|
|
|
std::vector<MountingPointPlan> HandlerMainSchedule::getMountingPointPlans(int scheduleId) const
|
|
{
|
|
if (!getMainSchedule(scheduleId))
|
|
return {};
|
|
return getMainSchedule(scheduleId)->getData().elements();
|
|
}
|