Mantid
Loading...
Searching...
No Matches
CatalogManager.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
13
14#include <map>
15#include <memory>
16
17namespace Mantid::API {
28CatalogSession_sptr CatalogManagerImpl::login(const std::string &username, const std::string &password,
29 const std::string &endpoint, const std::string &facility) {
30 std::string className = Kernel::ConfigService::Instance().getFacility(facility).catalogInfo().catalogName();
31 auto catalog = CatalogFactory::Instance().create(className);
32 CatalogSession_sptr session = catalog->login(username, password, endpoint, facility);
33 // Creates a new catalog and adds it to the compositeCatalog and activeCatalog
34 // list.
35 m_activeCatalogs.emplace(session, catalog);
36 return session;
37}
38
46ICatalog_sptr CatalogManagerImpl::getCatalog(const std::string &sessionID) {
47 // Checks if a user is logged into the catalog. Inform the user if they are
48 // not.
49 if (m_activeCatalogs.empty())
50 throw std::runtime_error("You are not currently logged into a catalog.");
51
52 if (sessionID.empty()) {
53 auto composite = std::make_shared<CompositeCatalog>();
54 for (auto &activeCatalog : m_activeCatalogs) {
55 composite->add(activeCatalog.second);
56 }
57 return composite;
58 }
59
60 const auto found =
61 std::find_if(m_activeCatalogs.cbegin(), m_activeCatalogs.cend(),
62 [&sessionID](const auto &catalog) { return catalog.first->getSessionId() == sessionID; });
63 if (found == m_activeCatalogs.cend()) {
64 throw std::runtime_error("The session ID you have provided is invalid.");
65 }
66 return found->second;
67}
68
76void CatalogManagerImpl::destroyCatalog(const std::string &sessionID) {
77 if (sessionID.empty()) {
78 for (auto &activeCatalog : m_activeCatalogs) {
79 activeCatalog.second->logout();
80 }
81 m_activeCatalogs.clear();
82 }
83
84 for (auto iter = m_activeCatalogs.begin(); iter != m_activeCatalogs.end();) {
85 if (sessionID == iter->first->getSessionId()) {
86 iter->second->logout();
87 m_activeCatalogs.erase(iter);
88 return;
89 }
90 }
91}
92
97std::vector<CatalogSession_sptr> CatalogManagerImpl::getActiveSessions() {
98 std::vector<CatalogSession_sptr> sessions;
99 sessions.reserve(m_activeCatalogs.size());
100
101 std::transform(m_activeCatalogs.begin(), m_activeCatalogs.end(), std::back_inserter(sessions),
102 [](const auto &activeCatalog) { return activeCatalog.first; });
103 return sessions;
104}
105
108} // namespace Mantid::API
void destroyCatalog(const std::string &sessionID)
Destroy a specific catalog (if session provided), otherwise destroys all active catalogs.
CatalogSession_sptr login(const std::string &username, const std::string &password, const std::string &endpoint, const std::string &facility)
Creates a new catalog and session, and adds it to the activeCatalogs container.
std::map< CatalogSession_sptr, ICatalog_sptr > m_activeCatalogs
std::vector< CatalogSession_sptr > getActiveSessions()
Obtains a list of the current active catalog sessions.
ICatalog_sptr getCatalog(const std::string &sessionID)
Get a specific catalog using the sessionID.
size_t numberActiveSessions() const
Returns the number of active sessions.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< CatalogSession > CatalogSession_sptr
std::shared_ptr< ICatalog > ICatalog_sptr
Definition: ICatalog.h:55