src/utilities/bcl/LocalBCL.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #ifndef UTILITIES_BCL_LOCALBCL_HPP | ||
| 7 | #define UTILITIES_BCL_LOCALBCL_HPP | ||
| 8 | |||
| 9 | #include "BCL.hpp" | ||
| 10 | #include "../core/Optional.hpp" | ||
| 11 | #include "../core/Path.hpp" | ||
| 12 | |||
| 13 | #include <string> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | struct sqlite3; | ||
| 17 | |||
| 18 | namespace openstudio { | ||
| 19 | |||
| 20 | /// \todo This class is currently a singleton implemented with a first use static | ||
| 21 | /// this may cause problems with threading in the future and should be moved | ||
| 22 | /// into some other singleton implementation. ONE OPTION would be to | ||
| 23 | /// instantiate the singleton at the time of QApplication construction in the Application singleton | ||
| 24 | /// this would avoid all known problems with threading. The Singleton<> template cannot be used because | ||
| 25 | /// of static initialize and destruction order problems caused by statics used in the QDatabase stuff | ||
| 26 | class UTILITIES_API LocalBCL : public BCL | ||
| 27 | { | ||
| 28 | public: | ||
| 29 | /** @name Constructor */ | ||
| 30 | //@{ | ||
| 31 | |||
| 32 | /// Static method to retrieve the current local BCL singleton | ||
| 33 | /// If a local BCL singleton does not exist, one will be created at the default library path stored in user preferences | ||
| 34 | static LocalBCL& instance(); | ||
| 35 | |||
| 36 | /// Static method to retrieve the local BCL singleton at the given library path | ||
| 37 | /// If a local BCL singleton does not exist, one will be created at the given library path | ||
| 38 | /// If a local BCL singleton does exist, but is not at the given path, | ||
| 39 | /// the current one will be closed and a new one will be created at the given library path | ||
| 40 | static LocalBCL& instance(const path& libraryPath); | ||
| 41 | |||
| 42 | //@} | ||
| 43 | /** @name Destructors */ | ||
| 44 | //@{ | ||
| 45 | |||
| 46 | /// Static method to close the current local BCL singleton if it exists | ||
| 47 | static void close(); | ||
| 48 | |||
| 49 | /// virtual destructor | ||
| 50 | virtual ~LocalBCL() override; | ||
| 51 | |||
| 52 | //@} | ||
| 53 | /** @name Inherited members */ | ||
| 54 | //@{ | ||
| 55 | |||
| 56 | /// Get the component by uid | ||
| 57 | virtual boost::optional<BCLComponent> getComponent(const std::string& uid, const std::string& versionId = "") const override; | ||
| 58 | |||
| 59 | /// Get the measure by uid | ||
| 60 | virtual boost::optional<BCLMeasure> getMeasure(const std::string& uid, const std::string& versionId = "") const override; | ||
| 61 | |||
| 62 | /// Get all components | ||
| 63 | std::vector<BCLComponent> components() const; | ||
| 64 | |||
| 65 | /// Get all measures | ||
| 66 | std::vector<BCLMeasure> measures() const; | ||
| 67 | |||
| 68 | /// Get all measure uids | ||
| 69 | std::vector<std::string> measureUids() const; | ||
| 70 | |||
| 71 | // TODO: make this take a vector of remote bcl filters | ||
| 72 | /// Perform a component search of the library | ||
| 73 | std::vector<BCLComponent> searchComponents(const std::string& searchTerm, const std::string& componentType) const; | ||
| 74 | // cppcheck-suppress functionStatic | ||
| 75 | std::vector<BCLComponent> searchComponents(const std::string& searchTerm, const unsigned componentTypeTID) const; | ||
| 76 | |||
| 77 | // TODO: make this take a vector of remote bcl filters | ||
| 78 | /// Perform a measure search of the library | ||
| 79 | virtual std::vector<BCLMeasure> searchMeasures(const std::string& searchTerm, const std::string& componentType) const; | ||
| 80 | virtual std::vector<BCLMeasure> searchMeasures(const std::string& searchTerm, const unsigned componentTypeTID) const; | ||
| 81 | |||
| 82 | //@} | ||
| 83 | /** @name Class members */ | ||
| 84 | //@{ | ||
| 85 | |||
| 86 | /// Add a component to the local library | ||
| 87 | bool addComponent(BCLComponent& component); | ||
| 88 | |||
| 89 | /// Remove a component from the local library and delete its directory | ||
| 90 | // cppcheck-suppress constParameter | ||
| 91 | bool removeComponent(BCLComponent& component); | ||
| 92 | |||
| 93 | /// Add a measure to the local library | ||
| 94 | // cppcheck-suppress constParameter | ||
| 95 | bool addMeasure(BCLMeasure& measure); | ||
| 96 | |||
| 97 | /// Remove a measure from the local library and delete its directory | ||
| 98 | // cppcheck-suppress constParameter | ||
| 99 | bool removeMeasure(BCLMeasure& measure); | ||
| 100 | |||
| 101 | // Removes all components with uid but NOT currentVersionId | ||
| 102 | size_t removeOutdatedLocalComponents(const std::string& uid, const std::string& currentVersionId); | ||
| 103 | |||
| 104 | // Removes all measures with uid but NOT currentVersionId | ||
| 105 | size_t removeOutdatedLocalMeasures(const std::string& uid, const std::string& currentVersionId); | ||
| 106 | |||
| 107 | /// Search for components with attributes matching those in searchTerms | ||
| 108 | std::vector<BCLComponent> componentAttributeSearch(const std::vector<std::pair<std::string, std::string>>& searchTerms) const; | ||
| 109 | |||
| 110 | /// Search for measures with attributes matching those in searchTerms | ||
| 111 | std::vector<BCLMeasure> measureAttributeSearch(const std::vector<std::pair<std::string, std::string>>& searchTerms) const; | ||
| 112 | |||
| 113 | /// Return production OAuth key | ||
| 114 | std::string prodAuthKey() const; | ||
| 115 | |||
| 116 | /// Set the production OAuth key, validates the auth key before saving | ||
| 117 | bool setProdAuthKey(const std::string& prodAuthKey); | ||
| 118 | |||
| 119 | /// Return development OAuth key | ||
| 120 | std::string devAuthKey() const; | ||
| 121 | |||
| 122 | /// Set the development OAuth key, validates the auth key before saving | ||
| 123 | bool setDevAuthKey(const std::string& devAuthKey); | ||
| 124 | |||
| 125 | /// Returns the path to the local BCL library | ||
| 126 | openstudio::path libraryPath() const; | ||
| 127 | |||
| 128 | // DLM: removed for openstudio 3 | ||
| 129 | /// Relocates the local BCL library, stores the library path in user preferences | ||
| 130 | //bool setLibraryPath(const openstudio::path& libraryPath); | ||
| 131 | |||
| 132 | /// returns the fully qualified path of the current database file | ||
| 133 | openstudio::path dbPath() const; | ||
| 134 | |||
| 135 | //@} | ||
| 136 | private: | ||
| 137 | /// private constructor | ||
| 138 | LocalBCL(const path& libraryPath); | ||
| 139 | |||
| 140 | /// Explicitly not copyable | ||
| 141 | LocalBCL(const LocalBCL& other) = delete; | ||
| 142 | |||
| 143 | bool initializeLocalDb(); | ||
| 144 | |||
| 145 | bool updateLocalDb(); | ||
| 146 | |||
| 147 | bool validateProdAuthKey(const std::string& authKey); | ||
| 148 | bool validateDevAuthKey(const std::string& authKey); | ||
| 149 | |||
| 150 | static std::string escape(const std::string& s); | ||
| 151 | |||
| 152 | std::set<std::pair<std::string, std::string>> attributeSearch(const std::vector<std::pair<std::string, std::string>>& searchTerms, | ||
| 153 | const std::string& componentType) const; | ||
| 154 | |||
| 155 | static std::string formatString(double d, unsigned prec = 15); | ||
| 156 | |||
| 157 | static std::shared_ptr<LocalBCL>& instanceInternal(); | ||
| 158 | |||
| 159 | bool closeConnection(); | ||
| 160 | |||
| 161 | openstudio::path m_libraryPath; | ||
| 162 | const openstudio::path m_dbName; | ||
| 163 | const std::string m_dbVersion; | ||
| 164 | bool m_connectionOpen; | ||
| 165 | |||
| 166 | std::string m_prodAuthKey; | ||
| 167 | std::string m_devAuthKey; | ||
| 168 | |||
| 169 | sqlite3* m_db; | ||
| 170 | openstudio::path m_sqliteFilePath; | ||
| 171 | std::string m_sqliteFilename; | ||
| 172 | |||
| 173 | // Helper function to retrieve a string | ||
| 174 | static std::string columnText(const unsigned char* column); | ||
| 175 | |||
| 176 | // Begins a transaction, checking for return code. If failed, does nothing but logs and returns false | ||
| 177 | bool beginTransaction(); | ||
| 178 | // Commit a transaction, checking for return code. If failed, calls rollbackTransaction | ||
| 179 | bool commitTransaction(); | ||
| 180 | // Rollback a transaction, checking for return code. If failed, throws. | ||
| 181 | bool rollbackTransaction(); | ||
| 182 | |||
| 183 | − | REGISTER_LOGGER("openstudio.bcl.LocalBCL"); | |
| 184 | }; | ||
| 185 | |||
| 186 | } // namespace openstudio | ||
| 187 | |||
| 188 | #endif // UTILITIES_BCL_LOCALBCL_HPP | ||
| 189 |