-
Notifications
You must be signed in to change notification settings - Fork 122
/
CatalogManager.cpp
104 lines (93 loc) · 3.7 KB
/
CatalogManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "MantidAPI/CatalogFactory.h"
#include "MantidAPI/CatalogManager.h"
#include "MantidAPI/CompositeCatalog.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/FacilityInfo.h"
namespace Mantid
{
namespace API
{
CatalogManagerImpl::CatalogManagerImpl() : m_activeCatalogs() {}
CatalogManagerImpl::~CatalogManagerImpl(){}
/**
* Logs the user into the catalog if session details are valid.
* This is used here as we need to obtain the session for a specific catalog (e.g. the one created on login).
* @param username :: The login name of the user.
* @param password :: The password of the user.
* @param endpoint :: The endpoint url of the catalog to log in to.
* @param facility :: The facility of the catalog to log in to.
* @return The session created if login was successful.
*/
CatalogSession_sptr CatalogManagerImpl::login(const std::string& username,const std::string& password,
const std::string& endpoint,const std::string& facility)
{
std::string className = Kernel::ConfigService::Instance().getFacility(facility).catalogInfo().catalogName();
auto catalog = CatalogFactory::Instance().create(className);
CatalogSession_sptr session = catalog->login(username,password,endpoint,facility);
// Creates a new catalog and adds it to the compositeCatalog and activeCatalog list.
m_activeCatalogs.insert(std::make_pair(session,catalog));
return session;
}
/**
* Obtain a specific catalog using the sessionID, otherwise return all active catalogs.
* @param sessionID :: The session to search for in the active catalogs list.
* @return A specific catalog using the sessionID, otherwise returns all active catalogs
*/
ICatalog_sptr CatalogManagerImpl::getCatalog(const std::string &sessionID)
{
if(sessionID.empty())
{
auto composite = boost::make_shared<CompositeCatalog>();
for (auto item = m_activeCatalogs.begin(); item != m_activeCatalogs.end(); ++item)
{
composite->add(item->second);
}
return composite;
}
for(auto iter = m_activeCatalogs.begin(); iter != m_activeCatalogs.end(); ++iter)
{
if (sessionID == iter->first->getSessionId()) return iter->second;
}
// If we reached this point then the session is corrupt/invalid.
throw std::runtime_error("The session ID you have provided is invalid.");
}
/**
* Destroy and remove a specific catalog from the active catalogs list and the composite catalog.
* If sessionID is empty then all catalogs are removed from the active catalogs list.
* @param sessionID :: The session to search for in the active catalogs list.
*/
void CatalogManagerImpl::destroyCatalog(const std::string& sessionID)
{
if(sessionID.empty())
{
for(auto item = m_activeCatalogs.begin(); item != m_activeCatalogs.end(); ++item)
{
item->second->logout();
}
m_activeCatalogs.clear();
}
for(auto iter = m_activeCatalogs.begin(); iter != m_activeCatalogs.end();)
{
if (sessionID == iter->first->getSessionId())
{
iter->second->logout();
m_activeCatalogs.erase(iter);
return;
}
}
}
/**
* Obtains a list of the current active catalog sessions.
* @return A list of active catalog sessions.
*/
std::vector<CatalogSession_sptr> CatalogManagerImpl::getActiveSessions()
{
std::vector<CatalogSession_sptr> sessions;
for(auto item = m_activeCatalogs.begin(); item != m_activeCatalogs.end(); ++item)
{
sessions.push_back(item->first);
}
return sessions;
}
}
}