/** * Handler ShootbackTargets (L400) * $Id$ * $HeadURL$ * $Author$ * Created by N. Thoden, NLE, 06.04.2020 */ #include "main_handler.h" #include "handler_target_area_assignment.h" #include "handler_shootback_target.h" #include "object_shootback_target.h" using namespace TargetManagement; //-------------------------------------------------------------------------- HandlerShootbackTarget::HandlerShootbackTarget(MainHandler *mainHandler) : QObject(mainHandler) , ShootbackTargetModifyRequestHandler(mainHandler->dds()->ftsTgt()) , ShootbackTargetRemoveRequestHandler(mainHandler->dds()->ftsTgt()) , m_mainHandler(mainHandler) , m_dtsTgt(m_mainHandler->dds()->dtsTgt()) , m_logger(getLogger("HandlerShootbackTarget")) { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } //-------------------------------------------------------------------------- HandlerShootbackTarget::~HandlerShootbackTarget() { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } //-------------------------------------------------------------------------- bool HandlerShootbackTarget::init() { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; // connect(m_dtcTab.get(), &TrainingAreaBase::Interface::Dtc::targetDataChanged, // this, &HandlerShootbackTarget::onTargetDataChanged); bool bRet = true; // create objects auto cb = [this, &bRet](const Database::TgtShootbackTarget & data) { DEBUG_LOG(m_logger) << "Reading from DB ShootbackTarget with id = " << data.id; ObjectShootbackTarget *ifs(new ObjectShootbackTarget(m_mainHandler)); Core::FunctionReplyObject ret = ifs->createObject(ObjectShootbackTarget::toDdsData(data)); if (ret.error()) { delete ifs; ERROR_LOG(m_logger) << ret(); bRet = false; return; } m_objectMap.insert(ifs->getId(), ifs); }; if (!m_mainHandler->db()->select(cb) || !bRet) { ERROR_LOG(m_logger) << "Error reading data from database!"; return false; } DEBUG_LOG(m_logger) << "m_objectMap.size() = " << m_objectMap.size(); return true; } ObjectShootbackTarget *HandlerShootbackTarget::dataForTargetParticipantId(int targetParticipantId) const { for (ObjectShootbackTarget *item : qAsConst(m_objectMap)) { if (!item) continue; if (item->getTargetParticipantId() == targetParticipantId) return item; } // for return nullptr; } int HandlerShootbackTarget::targetDataIdForTargetParticipantId(int targetParticipantId) const { ObjectShootbackTarget *target = dataForTargetParticipantId(targetParticipantId); if (!target) return -1; return target->getId(); } //-------------------------------------------------------------------------- void HandlerShootbackTarget::onShootbackTargetModifyRequest(const rti::core::SampleIdentity &cookie, const ShootbackTarget &request) { const auto ret = shootbackTargetModifyRequest(request); if (ret.error()) WARNING_LOG(m_logger) << ret(); else emit objectChanged(ret.id(), false); // For ShootbackController m_mainHandler->dds()->ftsTgt()->shootbackTargetModifyReply(cookie, ret); } //-------------------------------------------------------------------------- Core::FunctionReplyObject HandlerShootbackTarget::shootbackTargetModifyRequest(const ShootbackTarget &request) { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; // modify if (request.id() > 0) { ObjectShootbackTarget *ifs = m_objectMap.value(request.id()); if (!ifs) return Core::FunctionReplyObject(request.id(), tr_lts("dds", "ShootbackTarget with id %1 not found")).arg(request.id()); // since the attribute 'visibleParticipants' is *not* part of the db, // ignore request.visibleParticipants and use the value of the current memory: ShootbackTarget copy = request; copy.visibleParticipants(ifs->getData().visibleParticipants()); return ifs->updateObject(copy); } // create ObjectShootbackTarget *ifs(new ObjectShootbackTarget(m_mainHandler)); auto ret = ifs->createObject(request); if (ret.error()) { delete ifs; ifs = nullptr; } else m_objectMap.insert(ifs->getId(), ifs); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; return ret; } //-------------------------------------------------------------------------- void HandlerShootbackTarget::onShootbackTargetRemoveRequest(const rti::core::SampleIdentity &cookie, const Core::RemoveElementRequest &request) { TRACE_LOG(m_logger) << LOGFUNCTION << " request=" << request; Core::FunctionReplyObject ret; ObjectShootbackTarget *ifs = m_objectMap.value(request.id()); if (ifs == nullptr) { ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "ShootbackTarget with id %1 not found")).arg(request.id()); m_mainHandler->dds()->ftsTgt()->shootbackTargetRemoveReply(cookie, ret); return; } // // referenced by an assignment? // if (m_mainHandler->handlerTargetAreaAssignment() && // m_mainHandler->handlerTargetAreaAssignment()->isTargetDataIdUsedInAny(request.id())) // { // ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "ShootbackTarget with id %1 is used.")).arg(request.id()); // m_mainHandler->dds()->ftsTgt()->shootbackTargetRemoveReply(cookie, ret); // return; // } // if (m_mainHandler->dds()->topicLockMaster()->isLocked(ifs->getData(), m_mainHandler->exerciseId())) // { // ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "ShootbackTarget '%1' is used by another user!")).arg(ifs->getName()); // m_mainHandler->dds()->ftsTgt()->ShootbackTargetRemoveReply(cookie, ret); // return; // } ret = ifs->removeObject(); if (ret.error()) ERROR_LOG(m_logger) << ret(); else { // delete Object delete ifs; m_objectMap.remove(request.id()); } m_mainHandler->dds()->ftsTgt()->shootbackTargetRemoveReply(cookie, ret); // For ShootbackController if (!ret.error()) emit objectChanged(request.id(), true); return; }