Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 49 additions & 26 deletions src/openstudio_lib/OSDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
#include <QSettings>
#include <QtGlobal> // Workaround for #659

#include <algorithm>
#include <memory>

#if (defined(_WIN32) || defined(_WIN64))
Expand Down Expand Up @@ -1354,19 +1355,6 @@ void OSDocument::addStandardMeasures() {
enable();
}

boost::optional<BCLComponent> OSDocument::getLocalComponent(const std::string& uid) const {
boost::optional<BCLComponent> 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<BCLComponent> OSDocument::getLocalComponent(const std::string& uid, const std::string& versionId) const {
boost::optional<BCLComponent> result;
if (m_haveLocalBCL) {
Expand All @@ -1380,19 +1368,6 @@ boost::optional<BCLComponent> OSDocument::getLocalComponent(const std::string& u
return result;
}

boost::optional<BCLMeasure> OSDocument::getLocalMeasure(const std::string& uid) const {
boost::optional<BCLMeasure> 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<BCLMeasure> OSDocument::getLocalMeasure(const std::string& uid, const std::string& versionId) const {
boost::optional<BCLMeasure> result;
if (m_haveLocalBCL) {
Expand Down Expand Up @@ -1432,6 +1407,54 @@ std::vector<BCLMeasure> 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, &currentVersionId](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, &currentVersionId](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<BCLComponent> OSDocument::componentAttributeSearch(const std::vector<std::pair<std::string, std::string>>& pairs) const {
std::vector<BCLComponent> result;
if (m_haveLocalBCL) {
Expand Down
11 changes: 7 additions & 4 deletions src/openstudio_lib/OSDocument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,18 @@ class OPENSTUDIO_API OSDocument : public OSQObjectController
// returns false if the LocalBCL cannot be accessed
bool haveLocalBCL() const;

boost::optional<BCLComponent> getLocalComponent(const std::string& uid) const;
boost::optional<BCLComponent> getLocalComponent(const std::string& uid, const std::string& versionId) const;
boost::optional<BCLComponent> getLocalComponent(const std::string& uid, const std::string& versionId = "") const;

boost::optional<BCLMeasure> getLocalMeasure(const std::string& uid) const;
boost::optional<BCLMeasure> getLocalMeasure(const std::string& uid, const std::string& versionId) const;
boost::optional<BCLMeasure> getLocalMeasure(const std::string& uid, const std::string& versionId = "") const;

std::vector<BCLComponent> getLocalComponents() const;
std::vector<BCLMeasure> 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<BCLComponent> componentAttributeSearch(const std::vector<std::pair<std::string, std::string>>& pairs) const;

boost::optional<BCLMeasure> standardReportMeasure();
Expand Down
15 changes: 2 additions & 13 deletions src/shared_gui_components/BuildingComponentDialogCentralWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions src/shared_gui_components/MeasureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,13 +964,11 @@ void MeasureManager::checkForRemoteBCLUpdates() {
+ tr("Would you like update them?"));

QString detailedText;
std::vector<BCLMeasure> 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");
Expand All @@ -995,22 +993,17 @@ void MeasureManager::downloadBCLMeasures() {
std::vector<BCLSearchResult> updates = remoteBCL.measuresWithUpdates();

QString detailedText;
std::vector<BCLMeasure> 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());
Expand Down