diff --git a/src/openstudio_lib/OSDocument.cpp b/src/openstudio_lib/OSDocument.cpp index c832b1965..1e62aa786 100644 --- a/src/openstudio_lib/OSDocument.cpp +++ b/src/openstudio_lib/OSDocument.cpp @@ -128,6 +128,7 @@ #include #include // Workaround for #659 +#include #include #if (defined(_WIN32) || defined(_WIN64)) @@ -1354,19 +1355,6 @@ void OSDocument::addStandardMeasures() { enable(); } -boost::optional OSDocument::getLocalComponent(const std::string& uid) const { - boost::optional result; - if (m_haveLocalBCL) { - try { - result = LocalBCL::instance().getComponent(uid); - } catch (const std::exception& e) { - LOG(Error, "Cannot access local BCL: " << e.what()); - m_haveLocalBCL = false; - } - } - return result; -} - boost::optional OSDocument::getLocalComponent(const std::string& uid, const std::string& versionId) const { boost::optional result; if (m_haveLocalBCL) { @@ -1380,19 +1368,6 @@ boost::optional OSDocument::getLocalComponent(const std::string& u return result; } -boost::optional OSDocument::getLocalMeasure(const std::string& uid) const { - boost::optional result; - if (m_haveLocalBCL) { - try { - result = LocalBCL::instance().getMeasure(uid); - } catch (const std::exception& e) { - LOG(Error, "Cannot access local BCL: " << e.what()); - m_haveLocalBCL = false; - } - } - return result; -} - boost::optional OSDocument::getLocalMeasure(const std::string& uid, const std::string& versionId) const { boost::optional result; if (m_haveLocalBCL) { @@ -1432,6 +1407,54 @@ std::vector OSDocument::getLocalMeasures() const { return result; } +size_t OSDocument::removeOutdatedLocalComponents(const std::string& uid, const std::string& currentVersionId) const { + // TODO: when https://github.com/NREL/OpenStudio/pull/5127 is merged, we can just call it + // size_t result = 0; + // if (m_haveLocalBCL) { + // try { + // result = LocalBCL::instance().removeOutdatedLocalComponents(uid, currentVersionId); + // } catch (const std::exception& e) { + // LOG(Error, "Cannot access local BCL: " << e.what()); + // m_haveLocalBCL = false; + // } + // } + // return result; + + auto components = getLocalComponents(); + if (components.empty()) { + return {}; + } + + // Not empty, we do have a localbcl + components.erase(std::remove_if(components.begin(), components.end(), + [&uid, ¤tVersionId](const auto& component) { + return (component.uid() != uid) || (component.versionId() == currentVersionId); + }), + components.end()); + for (auto& component : components) { + LocalBCL::instance().removeComponent(component); + } + return components.size(); +} + +size_t OSDocument::removeOutdatedLocalMeasures(const std::string& uid, const std::string& currentVersionId) const { + // TODO: when https://github.com/NREL/OpenStudio/pull/5127 is merged, we can just call it + auto measures = getLocalMeasures(); + if (measures.empty()) { + return {}; + } + + // Not empty, we do have a localbcl + measures.erase( + std::remove_if(measures.begin(), measures.end(), + [&uid, ¤tVersionId](const auto& measure) { return (measure.uid() != uid) || (measure.versionId() == currentVersionId); }), + measures.end()); + for (auto& measure : measures) { + LocalBCL::instance().removeMeasure(measure); + } + return measures.size(); +} + std::vector OSDocument::componentAttributeSearch(const std::vector>& pairs) const { std::vector result; if (m_haveLocalBCL) { diff --git a/src/openstudio_lib/OSDocument.hpp b/src/openstudio_lib/OSDocument.hpp index c86413d7a..ef402c189 100644 --- a/src/openstudio_lib/OSDocument.hpp +++ b/src/openstudio_lib/OSDocument.hpp @@ -124,15 +124,18 @@ class OPENSTUDIO_API OSDocument : public OSQObjectController // returns false if the LocalBCL cannot be accessed bool haveLocalBCL() const; - boost::optional getLocalComponent(const std::string& uid) const; - boost::optional getLocalComponent(const std::string& uid, const std::string& versionId) const; + boost::optional getLocalComponent(const std::string& uid, const std::string& versionId = "") const; - boost::optional getLocalMeasure(const std::string& uid) const; - boost::optional getLocalMeasure(const std::string& uid, const std::string& versionId) const; + boost::optional getLocalMeasure(const std::string& uid, const std::string& versionId = "") const; std::vector getLocalComponents() const; std::vector getLocalMeasures() const; + // Removes all components with uid but NOT currentVersionId + size_t removeOutdatedLocalComponents(const std::string& uid, const std::string& currentVersionId) const; + // Removes all measures with uid but NOT currentVersionId + size_t removeOutdatedLocalMeasures(const std::string& uid, const std::string& currentVersionId) const; + std::vector componentAttributeSearch(const std::vector>& pairs) const; boost::optional standardReportMeasure(); diff --git a/src/shared_gui_components/BuildingComponentDialogCentralWidget.cpp b/src/shared_gui_components/BuildingComponentDialogCentralWidget.cpp index 287bf9065..1001e3413 100644 --- a/src/shared_gui_components/BuildingComponentDialogCentralWidget.cpp +++ b/src/shared_gui_components/BuildingComponentDialogCentralWidget.cpp @@ -288,12 +288,7 @@ void BuildingComponentDialogCentralWidget::componentDownloadComplete(const std:: if (component) { // good - // remove old components - for (auto& oldComponent : OSAppBase::instance()->currentDocument()->getLocalComponents()) { - if ((oldComponent.uid() == component->uid()) && (oldComponent.versionId() != component->versionId())) { - LocalBCL::instance().removeComponent(oldComponent); - } - } + OSAppBase::instance()->currentDocument()->removeOutdatedLocalComponents(component->uid(), component->versionId()); } else { // error downloading component downloadFailed(uid); @@ -308,13 +303,7 @@ void BuildingComponentDialogCentralWidget::measureDownloadComplete(const std::st if (measure) { // good - // remove old measures - for (auto& oldMeasure : OSAppBase::instance()->currentDocument()->getLocalMeasures()) { - if ((oldMeasure.uid() == measure->uid()) && (oldMeasure.versionId() != measure->versionId())) { - LocalBCL::instance().removeMeasure(oldMeasure); - } - } - + OSAppBase::instance()->currentDocument()->removeOutdatedLocalMeasures(measure->uid(), measure->versionId()); } else { // error downloading measure downloadFailed(uid); diff --git a/src/shared_gui_components/MeasureManager.cpp b/src/shared_gui_components/MeasureManager.cpp index 1124e283d..37d9b3c96 100644 --- a/src/shared_gui_components/MeasureManager.cpp +++ b/src/shared_gui_components/MeasureManager.cpp @@ -964,13 +964,11 @@ void MeasureManager::checkForRemoteBCLUpdates() { + tr("Would you like update them?")); QString detailedText; - std::vector oldMeasures; for (const BCLSearchResult& update : updates) { detailedText += toQString("* name: " + update.name() + "\n"); detailedText += toQString(" - uid: " + update.uid() + "\n"); auto current = m_bclMeasures.find(toUUID(update.uid())); if (current != m_bclMeasures.end()) { - oldMeasures.push_back(current->second); detailedText += toQString(" - old versionId: " + current->second.versionId() + "\n"); } detailedText += toQString(" - new versionId: " + update.versionId() + "\n\n"); @@ -995,22 +993,17 @@ void MeasureManager::downloadBCLMeasures() { std::vector updates = remoteBCL.measuresWithUpdates(); QString detailedText; - std::vector oldMeasures; for (const BCLSearchResult& update : updates) { detailedText += toQString("* name: " + update.name() + "\n"); detailedText += toQString(" - uid: " + update.uid() + "\n"); auto current = m_bclMeasures.find(toUUID(update.uid())); if (current != m_bclMeasures.end()) { - oldMeasures.push_back(current->second); detailedText += toQString(" - old versionId: " + current->second.versionId() + "\n"); } detailedText += toQString(" - new versionId: " + update.versionId() + "\n\n"); } remoteBCL.updateMeasures(); - for (auto& oldMeasure : oldMeasures) { - LocalBCL::instance().removeMeasure(oldMeasure); - } updateMeasuresLists(false); QMessageBox msg(m_app->mainWidget());