bool ProcedureController::UpdateProcedure(const Procedure &procedure) { QSqlQuery procedureUpdate = GetDb().CreateQuery(); procedureUpdate.prepare("UPDATE \"Procedure\" " "SET \"Description\"=:description " "WHERE \"ProcedureId\"=:procedureId;"); procedureUpdate.bindValue(":procedureId", procedure.GetProcedureId()); procedureUpdate.bindValue(":description", procedure.GetDescription()); if(!procedureUpdate.exec()) { return false; } if(!procedure.IsTemplateProcedure()) { QSqlQuery projectProcedureUpdate = GetDb().CreateQuery(); projectProcedureUpdate.prepare("UPDATE \"ProjectProcedure\" " "SET \"IsDone\"=:isDone " "WHERE \"ProjectProcedureId\"=:projectProcedureId;"); projectProcedureUpdate.bindValue(":projectProcedureId", procedure.GetProjectProcedureId()); projectProcedureUpdate.bindValue(":isDone", procedure.GetDoneState() ? 1 : 0); if(!projectProcedureUpdate.exec()) { return false; } emit sigProcedureModified(procedure); } // Update linked project procedures from template. if(procedure.IsPureTemplateProcedure()) { qint32 templateProcedureId = procedure.GetProcedureId(); if(m_proceduresFromTemplate.count(templateProcedureId) != 0) { std::set<qint32> &procedureSet = m_proceduresFromTemplate.at(templateProcedureId); for(auto it = procedureSet.begin(); it != procedureSet.end(); it++) { qint32 projectProcedureId = *it; ProcedureCacheKey key(ProcedureType::PT_PROJECT, projectProcedureId); std::shared_ptr<Procedure> projectProcedure = m_cache.Lookup(key); if(!projectProcedure) { continue; } projectProcedure->SetDescription(procedure.GetDescription()); emit sigProcedureModified(*projectProcedure); } } emit sigProcedureModified(procedure); } return true; }
bool ProcedureController::RemoveProcedure(const ProcedureCacheKey &id, const Procedure &procedure) { if(procedure.IsTemplateProcedure() && !procedure.IsPureTemplateProcedure()) { qint32 procedureId = procedure.GetProcedureId(); if(m_proceduresFromTemplate.count(procedureId) != 0) { std::set<qint32> &procedureSet = m_proceduresFromTemplate.at(procedureId); procedureSet.erase(procedure.GetProjectProcedureId()); if(procedureSet.size() == 0) { m_proceduresFromTemplate.erase(procedureId); // procedureSet is now invalid. } } //else //{ // // Should never happen. //} } return m_cache.Remove(id); }
ProcedureController::ProcedureCacheKey ProcedureController::GetCacheKey(const Procedure &procedure) { if(!procedure.IsPureTemplateProcedure()) { return ProcedureCacheKey(ProcedureType::PT_PROJECT, procedure.GetProjectProcedureId()); } else { return ProcedureCacheKey(ProcedureType::PT_PROJECT_TYPE, procedure.GetProcedureId()); } }
bool ProcedureController::RemoveProcedure(const Procedure &procedure) { if(!procedure.IsTemplateProcedure()) { QSqlQuery procedureProjectDelete = GetDb().CreateQuery(); procedureProjectDelete.prepare("DELETE FROM \"ProjectProcedure\" " "WHERE \"ProjectProcedureId\"=:projectProcedureId;"); procedureProjectDelete.bindValue(":projectProcedureId", procedure.GetProjectProcedureId()); if(!procedureProjectDelete.exec()) { return false; } } QSqlQuery procedureDelete = GetDb().CreateQuery(); procedureDelete.prepare("DELETE FROM \"Procedure\" " "WHERE \"ProcedureId\"=:procedureId;"); procedureDelete.bindValue(":procedureId", procedure.GetProcedureId()); if(!procedureDelete.exec()) { return false; } ProcedureCacheKey key = GetCacheKey(procedure); auto removedProcedure = m_cache.Lookup(key); if(removedProcedure) { m_cache.Remove(key); emit sigProcedureRemoved(*removedProcedure); if(procedure.IsPureTemplateProcedure()) { RemoveProjectProceduresOfTemplate(procedure.GetProcedureId()); } return true; } return false; }