/* This file is part of the KDE project * SPDX-FileCopyrightText: 2004-2007 Dag Andersen * SPDX-FileCopyrightText: 2011 Dag Andersen * SPDX-FileCopyrightText: 2016 Dag Andersen * SPDX-FileCopyrightText: 2019 Dag Andersen * * SPDX-License-Identifier: LGPL-2.0-or-later */ // clazy:excludeall=qstring-arg #include "InsertProjectCmd.h" #include "kptcommand.h" #include "AddResourceCmd.h" #include "AddParentGroupCmd.h" #include "RemoveParentGroupCmd.h" #include "RemoveResourceCmd.h" // #include "kptaccount.h" // #include "kptappointment.h" // #include "kptproject.h" // #include "kpttask.h" // #include "kptcalendar.h" // #include "kptrelation.h" // #include "kptresource.h" // #include "kptdocuments.h" // #include "kptlocale.h" #include "kptdebug.h" #include using namespace KPlato; //---------------- InsertProjectCmd::InsertProjectCmd(Project &fromProject, Node *parent, Node *after, const KUndo2MagicString& name) : MacroCommand(name) , m_fromProject(&fromProject) , m_project(static_cast(parent->projectNode())) , m_parent(parent) , m_after(after) { } void InsertProjectCmd::execute() { if (m_fromProject) { createCommands(*m_fromProject); m_fromProject = nullptr; } // debugPlanInsertProject<<"before execute:"<resourceGroups()<resourceList(); MacroCommand::execute(); // debugPlanInsertProject<<"after execute:"<resourceGroups()<resourceList(); } void InsertProjectCmd::unexecute() { QApplication::setOverrideCursor(Qt::WaitCursor); MacroCommand::unexecute(); QApplication::restoreOverrideCursor(); } ResourceGroup *findGroup(const QString &id, const Project *project, const QList &newGroups) { auto group = project->findResourceGroup(id); if (group) { return group; } for (const auto g : newGroups) { if (g->id() == id) { return g; } } return nullptr; } void InsertProjectCmd::createCommands(Project &fromProject) { Q_ASSERT(&fromProject != m_project); if (m_project->defaultCalendar()) { fromProject.setDefaultCalendar(nullptr); // or else m_project default calendar may be overwritten } QString defaultAccount; if (! m_project->accounts().defaultAccount() && fromProject.accounts().defaultAccount()) { defaultAccount = fromProject.accounts().defaultAccount()->name(); } QHash startupaccountmap; QHash shutdownaccountmap; QHash runningaccountmap; QHash nodecalendarmap; // remove unhandled info in tasks and get accounts and calendars const auto nodes = fromProject.allNodes(); for (Node *n : nodes) { if (n->type() == Node::Type_Task) { Task *t = static_cast(n); t->workPackage().clear(); while (! t->workPackageLog().isEmpty()) { WorkPackage *wp = t->workPackageLog().at(0); t->removeWorkPackage(wp); delete wp; } } while (auto schedule = n->schedules().values().value(0)) { n->takeSchedule(schedule); if (schedule->resource()) { schedule->resource()->takeSchedule(schedule->resource()->findSchedule(schedule->id())); } delete schedule; } if (n->startupAccount()) { startupaccountmap.insert(n, n->startupAccount()->name()); n->setStartupAccount(nullptr); } if (n->shutdownAccount()) { shutdownaccountmap.insert(n, n->shutdownAccount()->name()); n->setShutdownAccount(nullptr); } if (n->runningAccount()) { runningaccountmap.insert(n, n->runningAccount()->name()); n->setRunningAccount(nullptr); } if (n->estimate()->calendar()) { nodecalendarmap.insert(n, n->estimate()->calendar()->id()); n->estimate()->setCalendar(nullptr); } } // get resources pointing to calendars, accounts and parentgroups QHash resourceaccountmap; QHash resourcecalendarmap; QHash > resourceparentgroupcmds; const auto resources = fromProject.resourceList(); for (Resource *r : resources) { if (r->account()) { resourceaccountmap.insert(r, r->account()->name()); r->setAccount(nullptr); } if (r->calendar()) { resourcecalendarmap.insert(r, r->calendar()->id()); r->setCalendar(nullptr); } if (!m_project->findResource(r->id())) { // Need to parentgroups to m_project groups // to avoid dangling pointers const auto groups = r->parentGroups(); if (!groups.isEmpty()) { QList cmds; for (const auto g : groups) { auto group = m_project->findResourceGroup(g->id()); if (group) { r->removeParentGroup(g); cmds << new AddParentGroupCmd(r, group); } } if (!cmds.isEmpty()) { resourceparentgroupcmds.insert(r, cmds); } } } } // create add account commands and keep track of used and unused accounts QList unusedAccounts; QMap accountsmap; const auto accounts = m_project->accounts().allAccounts(); for (Account *a : accounts) { accountsmap.insert(a->name(), a); } const auto accounts2 = fromProject.accounts().accountList(); for (Account *a : accounts2) { addAccounts(a, nullptr, unusedAccounts, accountsmap); } // create add calendar commands and keep track of used and unused calendars QList unusedCalendars; QMap calendarsmap; const auto calendars = m_project->allCalendars(); for (Calendar *c : calendars) { calendarsmap.insert(c->id(), c); } const auto calendars2 = fromProject.calendars(); for (Calendar *c : calendars2) { addCalendars(c, nullptr, unusedCalendars, calendarsmap); } // get all requests from fromProject before resources are merged QMultiHash > rreqs; const auto nodes2 = fromProject.allNodes(); for (Node *n : nodes2 ) { if (n->type() != (int)Node::Type_Task || n->requests().isEmpty()) { continue; } while (ResourceRequest *rr = n->requests().resourceRequests(false).value(0)) { debugPlanInsertProject<<"Get resource request:"<(rr, rr->resource())); // all resource requests shall be reinserted rr->unregisterRequest(); n->requests().removeResourceRequest(rr); } } debugPlanInsertProject<<"fromProject resource requests:"< allResources; QList newResources; // resource in fromProject that does not exist in toProject QHash existingResources; // hash[toProject resource, fromProject resource] QHash existingResourceParentGroups; // existing resource with fromProject parentgroup ids QHash newResourceParentGroups; // new resource with parent group ids const auto resources2 = fromProject.resourceList(); for (Resource *fromResource : resources2) { Resource *toResource = m_project->resource(fromResource->id()); if (toResource == nullptr) { const auto groups = fromResource->parentGroups(); for (auto g : groups) { newResourceParentGroups[fromResource].append(g->id()); } fromResource->setParentGroups(QList()); newResources << fromResource; allResources << fromResource; fromProject.removeResourceFromProject(fromResource); } else { const auto groups = fromResource->parentGroups(); for (auto g : groups) { existingResourceParentGroups[toResource].append(g->id()); } existingResources[ toResource ] = fromResource; allResources << toResource; } } for (auto r1 : std::as_const(allResources)) { for (auto r2 : std::as_const(allResources)) { if (r1 != r2 && r1->name() == r2->name()) { warnPlanInsertProject<<"Two resources with same name!"< allGroups; QHash existingGroups; // QHash QList newGroups; // all new groups sorted by level: top1, child1..childn, top2, child1..childn ... QHash newGroupsWithParent; // QHash const auto groups = fromProject.allResourceGroups(true /*sorted*/); debugPlanInsertProject<<"fromProject resource groups:"<findResourceGroup(g->id()); if (gr) { existingGroups[gr] = g; } else { gr = g; newGroups << gr; if (gr->parentGroup()) { // try to map to toProject in case group exists there auto parent = m_project->findResourceGroup(gr->parentGroup()->id()); if (!parent) { parent = gr->parentGroup(); } newGroupsWithParent[gr] = parent; parent->removeChildGroup(gr); } fromProject.removeResourceGroupFromProject(gr); } allGroups << gr; } debugPlanInsertProject<<"New resource groups:"<resources(); addCommand(new AddResourceGroupCmd(m_project, parent, g, kundo2_noi18n(QStringLiteral("ResourceGroup")))); } // Add new resources for (Resource *r : std::as_const(newResources)) { addCommand(new AddResourceCmd(m_project, r, kundo2_noi18n(QStringLiteral("Resource")))); // add any moved parent groups auto cmds = resourceparentgroupcmds.take(r); while (!cmds.isEmpty()) { addCommand(cmds.takeFirst()); } debugPlanInsertProject<<"AddResourceCmd:"<name()<parentGroups(); // Update resource parent groups const auto groupIds = newResourceParentGroups.value(r); for (const auto &id : groupIds) { auto group = findGroup(id, m_project, newGroups); if (group) { addCommand(new AddParentGroupCmd(r, group)); } } } // Add the fromProjects parentgroups to existing resources {QHash::const_iterator it = existingResourceParentGroups.constBegin(); QHash::const_iterator end = existingResourceParentGroups.constEnd(); for (; it != end; ++it) { for (const auto &id : it.value()) { auto group = findGroup(id, m_project, newGroups); if (group) { addCommand(new AddParentGroupCmd(it.key(), group)); } else { warnPlanInsertProject<<"Failed to find parent group for resource:"<::const_iterator it = resourceaccountmap.constBegin(); QHash::const_iterator end = resourceaccountmap.constEnd(); for (; it != end; ++it) { Resource *r = it.key(); if (newResources.contains(r)) { Q_ASSERT(allResources.contains(r)); addCommand(new ResourceModifyAccountCmd(*r, nullptr, accountsmap.value(it.value()))); } }} // Update resource calendar {QHash::const_iterator it = resourcecalendarmap.constBegin(); QHash::const_iterator end = resourcecalendarmap.constEnd(); for (; it != end; ++it) { Resource *r = it.key(); if (newResources.contains(r)) { Q_ASSERT(allResources.contains(r)); addCommand(new ModifyResourceCalendarCmd(r, calendarsmap.value(it.value()))); } }} debugPlanInsertProject<<"Requests: clean up requests to resources already in m_project"; debugPlanInsertProject<<"All groups:"<setId(0); rr->setCollection(nullptr); Resource *newRes = i.value().second; debugPlanInsertProject<<"Resource exists:"<id()<<(void*)newRes<<':'<resource(newRes->id()); if (Resource *nr = existingResources.key(newRes)) { newRes = nr; debugPlanInsertProject<<"Resource existed:"<wbsCode()<name()<<"resource:"<requiredResources().isEmpty()) { // the resource request may have required resources that needs mapping QList required; const auto resources = rr->requiredResources(); for (Resource *r : resources) { if (newResources.contains(r)) { required << r; debugPlanInsertProject<<"Request: required (new)"<name(); continue; } Resource *r2 = existingResources.key(r); Q_ASSERT(allResources.contains(r2)); if (r2) { debugPlanInsertProject<<"Request: required (existing)"<name(); required << r2; } } rr->setRequiredResources(required); } if (!rr->alternativeRequests().isEmpty()) { const auto alts = rr->alternativeRequests(); for (auto alt : alts) { auto resource = m_project->findResource(alt->resource()->id()); if (resource) { debugPlanInsertProject<<"swap alternative"<resource()<<"->"<setResource(resource); } } } Q_ASSERT(allResources.contains(newRes)); // all resource requests shall be reinserted rr->setResource(newRes); addCommand(new AddResourceRequestCmd(&n->requests(), rr)); } // Add nodes (ids are unique, no need to check) Node *node_after = m_after; for (int i = 0; i < fromProject.numChildren(); ++i) { Node *n = fromProject.childNode(i); Q_ASSERT(n); Q_ASSERT(n->schedules().isEmpty()); n->setParentNode(nullptr); if (node_after) { addCommand(new TaskAddCmd(m_project, n, node_after, kundo2_noi18n(QStringLiteral("Task")))); node_after = n; } else { addCommand(new SubtaskAddCmd(m_project, n, m_parent, kundo2_noi18n(QStringLiteral("Subtask")))); } addChildNodes(n); } // Dependencies: const auto nodes3 = fromProject.allNodes(); for (Node *n : nodes3) { while (n->numDependChildNodes() > 0) { Relation *r = n->dependChildNodes().at(0); n->takeDependChildNode(r); r->child()->takeDependParentNode(r); addCommand(new AddRelationCmd(*m_project, r)); } } // node calendar {QHash::const_iterator it = nodecalendarmap.constBegin(); QHash::const_iterator end = nodecalendarmap.constEnd(); for (; it != end; ++it) { addCommand(new ModifyEstimateCalendarCmd(*(it.key()), nullptr, calendarsmap.value(it.value()))); }} // node startup account {QHash::const_iterator it = startupaccountmap.constBegin(); QHash::const_iterator end = startupaccountmap.constEnd(); for (; it != end; ++it) { addCommand(new NodeModifyStartupAccountCmd(*(it.key()), nullptr, accountsmap.value(it.value()))); }} // node shutdown account {QHash::const_iterator it = shutdownaccountmap.constBegin(); QHash::const_iterator end = shutdownaccountmap.constEnd(); for (; it != end; ++it) { addCommand(new NodeModifyShutdownAccountCmd(*(it.key()), nullptr, accountsmap.value(it.value()))); }} // node running account {QHash::const_iterator it = runningaccountmap.constBegin(); QHash::const_iterator end = runningaccountmap.constEnd(); for (; it != end; ++it) { addCommand(new NodeModifyRunningAccountCmd(*(it.key()), nullptr, accountsmap.value(it.value()))); }} if (! defaultAccount.isEmpty()) { Account *a = accountsmap.value(defaultAccount); if (a && a->list()) { addCommand(new ModifyDefaultAccountCmd(m_project->accounts(), nullptr, a)); } } debugPlanInsertProject<<"Cleanup unused stuff from inserted project:"<<&fromProject; // Cleanup // Remove nodes from fromProject so they are not deleted while (Node *ch = fromProject.childNode(0)) { fromProject.takeChildNode(ch); } const auto nodes4 = fromProject.allNodes(); for (Node *n : nodes4) { fromProject.removeId(n->id()); } // Remove calendars from fromProject while (fromProject.calendarCount() > 0) { fromProject.takeCalendar(fromProject.calendarAt(0)); } qDeleteAll(unusedCalendars); // Remove accounts from fromProject while (fromProject.accounts().accountCount() > 0) { fromProject.accounts().take(fromProject.accounts().accountAt(0)); } qDeleteAll(unusedAccounts); for (Resource *r = fromProject.resourceList().value(0); r; r = fromProject.resourceList().value(0)) { debugPlanInsertProject<<"remove all resources from fromProject:"< 0) { ResourceGroup *g = fromProject.resourceGroupAt(0); debugPlanInsertProject<<"Take used group:"<numChildGroups() > 0) { g->removeChildGroup(g->childGroupAt(0)); } } qDeleteAll(existingGroups); // deletes unused resource groups } void InsertProjectCmd::addCalendars(Calendar *calendar, Calendar *parent, QList &unused, QMap &calendarsmap) { Calendar *par = nullptr; if (parent) { par = calendarsmap.value(parent->id()); } if (par == nullptr) { par = parent; } Calendar *cal = calendarsmap.value(calendar->id()); if (cal == nullptr) { calendar->project()->takeCalendar(calendar); calendarsmap.insert(calendar->id(), calendar); addCommand(new CalendarAddCmd(m_project, calendar, -1, par)); } else { unused << calendar; } const auto calendars = calendar->calendars(); for (Calendar *c : calendars) { addCalendars(c, calendar, unused, calendarsmap); } } void InsertProjectCmd::addAccounts(Account *account, Account *parent, QList &unused, QMap &accountsmap) { Account *par = nullptr; if (parent) { par = accountsmap.value(parent->name()); } if (par == nullptr) { par = parent; } Account *acc = accountsmap.value(account->name()); if (acc == nullptr) { accountsmap.insert(account->name(), account); addCommand(new AddAccountCmd(*m_project, account, par, -1, kundo2_noi18n("Add account %1", account->name()))); } else { unused << account; } while (! account->accountList().isEmpty()) { Account *a = account->accountList().first(); account->list()->take(a); addAccounts(a, account, unused, accountsmap); } } void InsertProjectCmd::addChildNodes(Node *node) { // schedules not handled while (Schedule *s = node->schedules().values().value(0)) { node->takeSchedule(s); // schedules not handled delete s; } const auto nodes = node->childNodeIterator(); for (Node *n : nodes ) { n->setParentNode(nullptr); addCommand(new SubtaskAddCmd(m_project, n, node)); addChildNodes(n); } // Remove child nodes so they are not added twice while (Node *ch = node->childNode(0)) { node->takeChildNode(ch); } }