Mantid
Loading...
Searching...
No Matches
CatalogHelper.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 +
12
13#include <Poco/ActiveResult.h>
14#include <QCoreApplication>
15#include <QTime>
16#include <boost/algorithm/string/regex.hpp>
17
18namespace MantidQt {
19namespace MantidWidgets {
20
27const std::vector<std::string> CatalogHelper::getInstrumentList(const std::vector<std::string> &sessionIDs) {
28 auto catalogAlgorithm = createCatalogAlgorithm("CatalogListInstruments");
29 auto session = Mantid::API::CatalogManager::Instance().getActiveSessions();
30
31 // If ONE or ALL catalogs are selected to search through use no session
32 // (empty).
33 // This will invoke the compositeCatalog instead of specific catalogs for each
34 // session.
35 if (session.size() == sessionIDs.size()) {
36 executeAsynchronously(catalogAlgorithm);
37 return catalogAlgorithm->getProperty("InstrumentList");
38 } else {
39 // Use catalogs for the specified sessions.
40 for (const auto &sessionID : sessionIDs) {
41 catalogAlgorithm->setProperty("Session", sessionID);
42 executeAsynchronously(catalogAlgorithm);
43 }
44 // Return the vector containing the list of instruments available.
45 return catalogAlgorithm->getProperty("InstrumentList");
46 }
47}
48
55const std::vector<std::string> CatalogHelper::getInvestigationTypeList(const std::vector<std::string> &sessionIDs) {
56 auto catalogAlgorithm = createCatalogAlgorithm("CatalogListInvestigationTypes");
57 auto session = Mantid::API::CatalogManager::Instance().getActiveSessions();
58
59 if (session.size() == sessionIDs.size()) {
60 executeAsynchronously(catalogAlgorithm);
61 return catalogAlgorithm->getProperty("InvestigationTypes");
62 } else {
63 for (const auto &sessionID : sessionIDs) {
64 catalogAlgorithm->setProperty("Session", sessionID);
65 executeAsynchronously(catalogAlgorithm);
66 }
67 return catalogAlgorithm->getProperty("InvestigationTypes");
68 }
69}
70
81void CatalogHelper::executeSearch(const std::map<std::string, std::string> &userInputFields, const int &offset,
82 const int &limit, const std::vector<std::string> &sessionIDs) {
83 auto catalogAlgorithm = createCatalogAlgorithm("CatalogSearch");
84 // Set the properties to limit the number of results returned for paging
85 // purposes.
86 catalogAlgorithm->setProperty("Limit", limit);
87 catalogAlgorithm->setProperty("Offset", offset);
88 // Set the "search" properties to their related input fields.
89 setSearchProperties(catalogAlgorithm, userInputFields);
90
91 auto session = Mantid::API::CatalogManager::Instance().getActiveSessions();
92 if (session.size() == sessionIDs.size()) {
93 executeAsynchronously(catalogAlgorithm);
94 } else {
95 for (const auto &sessionID : sessionIDs) {
96 catalogAlgorithm->setProperty("Session", sessionID);
97 executeAsynchronously(catalogAlgorithm);
98 }
99 }
100}
101
110int64_t CatalogHelper::getNumberOfSearchResults(const std::map<std::string, std::string> &userInputFields,
111 const std::vector<std::string> &sessionIDs) {
112 auto catalogAlgorithm = createCatalogAlgorithm("CatalogSearch");
113 // Set the property to only perform a count search.
114 catalogAlgorithm->setProperty("CountOnly", true);
115 // Set the "search" properties to their related input fields.
116 setSearchProperties(catalogAlgorithm, userInputFields);
117
118 auto session = Mantid::API::CatalogManager::Instance().getActiveSessions();
119 if (session.size() == sessionIDs.size()) {
120 executeAsynchronously(catalogAlgorithm);
121 return catalogAlgorithm->getProperty("NumberOfSearchResults");
122 } else {
123 for (const auto &sessionID : sessionIDs) {
124 catalogAlgorithm->setProperty("Session", sessionID);
125 executeAsynchronously(catalogAlgorithm);
126 }
127 return catalogAlgorithm->getProperty("NumberOfSearchResults");
128 }
129}
130
137void CatalogHelper::executeGetDataFiles(const std::string &investigationId, const std::string &sessionID) {
138 auto catalogAlgorithm = createCatalogAlgorithm("CatalogGetDataFiles");
139
140 catalogAlgorithm->setProperty("InvestigationId", investigationId);
141 catalogAlgorithm->setPropertyValue("OutputWorkspace", "__dataFileResults");
142 catalogAlgorithm->setProperty("Session", sessionID);
143
144 executeAsynchronously(catalogAlgorithm);
145}
146
156const std::vector<std::string>
157CatalogHelper::downloadDataFiles(const std::vector<std::pair<int64_t, std::string>> &userSelectedFiles,
158 const std::string &downloadPath, const std::string &sessionID) {
159 auto catalogAlgorithm = createCatalogAlgorithm("CatalogDownloadDataFiles");
160
161 // Prepare for the ugly!
162
163 // These two vectors are required by the "CatalogDownloadDataFiles" algorithm.
164 std::vector<int64_t> fileIDs;
165 fileIDs.reserve(userSelectedFiles.size());
166 std::vector<std::string> fileNames;
167 fileNames.reserve(userSelectedFiles.size());
168
169 // For each pair in userSelectedFiles we want to add them to their related
170 // vector to pass to the algorithm.
171 for (const auto &userSelectedFile : userSelectedFiles) {
172 fileIDs.emplace_back(userSelectedFile.first);
173 fileNames.emplace_back(userSelectedFile.second);
174 }
175
176 // End of the ugly!
177
178 // The file IDs and file names of the data file(s) the user wants to download.
179 catalogAlgorithm->setProperty("FileIds", fileIDs);
180 catalogAlgorithm->setProperty("FileNames", fileNames);
181 catalogAlgorithm->setProperty("DownloadPath", downloadPath);
182 catalogAlgorithm->setProperty("Session", sessionID);
183
184 executeAsynchronously(catalogAlgorithm);
185 // Return a vector containing the file paths to the files to download.
186 return (catalogAlgorithm->getProperty("FileLocations"));
187}
188
196const std::map<std::string, std::string>
197CatalogHelper::validateProperties(const std::map<std::string, std::string> &inputFields) {
198 auto catalogAlgorithm = createCatalogAlgorithm("CatalogSearch");
199
200 // Holds the name of the marker to update if an error is found, and the
201 // related error message to use.
202 // E.g. key => "StartDate_err", value => "The start date for..."
203 std::map<std::string, std::string> errors;
204
205 // Validate all input elements in the map.
206 for (const auto &inputField : inputFields) {
207 try {
208 catalogAlgorithm->setProperty(inputField.first, inputField.second);
209 } catch (std::invalid_argument &) {
210 std::string documentation = propertyDocumentation(catalogAlgorithm->getProperties(), inputField.first);
211
212 // Add the input name + "_err" (to indicate the error marker in the GUI,
213 // rather than the input field) as the key, and the related error as the
214 // value.
215 errors.emplace(inputField.first + "_err", documentation);
216 }
217 }
218 // catch invalid date formats
219 std::string dateField = "StartDate";
220 try {
221
222 getTimevalue(catalogAlgorithm->getProperty(dateField));
223 dateField = "EndDate";
224
225 getTimevalue(catalogAlgorithm->getProperty(dateField));
226 } catch (std::invalid_argument &) {
227 std::string documentation = propertyDocumentation(catalogAlgorithm->getProperties(), dateField);
228 errors.emplace(dateField + "_err", documentation);
229 }
230 return errors;
231}
232
238time_t CatalogHelper::getTimevalue(const std::string &inputDate) {
239 // Prevent any possible errors.
240 if (inputDate.empty())
241 return 0;
242 // A container to hold the segments of the date.
243 std::vector<std::string> dateSegments;
244 // Split input by "/" prior to rearranging the date
245 boost::algorithm::split_regex(dateSegments, inputDate, boost::regex("/"));
246 // Reorganise the date to be ISO format.
247 std::string isoDate = dateSegments.at(2) + "-" + dateSegments.at(1) + "-" + dateSegments.at(0) + " 00:00:00.000";
248 // Return the date as time_t value.
249
250 return Mantid::Types::Core::DateAndTime(isoDate).to_time_t();
251}
252
258 API::InterfaceManager interfaceMgr;
259 auto dlg = interfaceMgr.createDialogFromName("CatalogLogin");
260 dlg->setModal(false);
261 dlg->show();
262 dlg->raise();
263 dlg->activateWindow();
264}
265
271 API::InterfaceManager interfaceMgr;
272 auto dlg = interfaceMgr.createDialogFromName("CatalogPublish");
273 dlg->setModal(false);
274 dlg->show();
275 dlg->raise();
276 dlg->activateWindow();
277}
278
285const std::string CatalogHelper::propertyDocumentation(const std::vector<Mantid::Kernel::Property *> &properties,
286 const std::string &name) {
287 for (auto property : properties) {
288 if (property->name() == name) {
289 return property->documentation();
290 }
291 }
292 return "";
293}
294
301 // If there is an exception we want it to be thrown.
302 return Mantid::API::AlgorithmManager::Instance().create(algName);
303}
304
310 Poco::ActiveResult<bool> result(algorithm->executeAsync());
311 while (!result.available()) {
312 QCoreApplication::processEvents();
313 }
314}
315
323 const std::map<std::string, std::string> &userInputFields) {
324 // This will be the workspace where the content of the search result is output
325 // to.
326 catalogAlgorithm->setProperty("OutputWorkspace", "__searchResults");
327
328 // Iterate over the provided map of user input fields. For each field that
329 // isn't empty (e.g. a value was input by the user)
330 // then we will set the algorithm property with the key and value of that
331 // specific value.
332 for (const auto &userInputField : userInputFields) {
333 std::string value = userInputField.second;
334 // If the user has input any search terms.
335 if (!value.empty()) {
336 // Set the property that the search algorithm uses to: (key => FieldName,
337 // value => FieldValue) (e.g., (Keywords, bob))
338 catalogAlgorithm->setProperty(userInputField.first, value);
339 }
340 }
341}
342
343} // namespace MantidWidgets
344} // namespace MantidQt
double value
The value of the point.
Definition: FitMW.cpp:51
This class is responsible for creating the correct dialog for an algorithm.
AlgorithmDialog * createDialogFromName(const QString &algorithmName, const int version=-1, QWidget *parent=nullptr, bool forScript=false, const QHash< QString, QString > &presetValues=(QHash< QString, QString >()), const QString &optionalMsg=QString(), const QStringList &enabled=QStringList(), const QStringList &disabled=QStringList())
Create an algorithm dialog for a given name and version.
const std::vector< std::string > getInvestigationTypeList(const std::vector< std::string > &sessionIDs)
Obtain the list of instruments that are available.
void showLoginDialog()
Executes the catalog login algorithm dialog, returning immediately.
Mantid::API::IAlgorithm_sptr createCatalogAlgorithm(const std::string &algName)
Creates an algorithm with the name provided.
void showPublishDialog()
Create a publishing dialog.
int64_t getNumberOfSearchResults(const std::map< std::string, std::string > &userInputFields, const std::vector< std::string > &sessionIDs)
Obtain the number of search results to be returned by the query of the user.
const std::string propertyDocumentation(const std::vector< Mantid::Kernel::Property * > &properties, const std::string &name)
Obtain the documentation for a given name from the given algorithm properties.
const std::vector< std::string > downloadDataFiles(const std::vector< std::pair< int64_t, std::string > > &userSelectedFiles, const std::string &downloadPath, const std::string &sessionID)
Download dataFile (via HTTP or copy if access to archive) and return the path to it.
void setSearchProperties(const Mantid::API::IAlgorithm_sptr &catalogAlgorithm, const std::map< std::string, std::string > &userInputFields)
Set the "search" properties to their related input fields.
void executeAsynchronously(const Mantid::API::IAlgorithm_sptr &algorithm)
Execute the given algorithm asynchronously.
const std::vector< std::string > getInstrumentList(const std::vector< std::string > &sessionIDs)
Obtain the list of instruments that are available for the given session information.
void executeSearch(const std::map< std::string, std::string > &userInputs, const int &offset, const int &limit, const std::vector< std::string > &sessionIDs)
Run the search algorithm with the given user input.
time_t getTimevalue(const std::string &inputDate)
Creates a time_t value from an input date ("23/06/2003") for comparison.
const std::map< std::string, std::string > validateProperties(const std::map< std::string, std::string > &inputFields)
Validate each input field against the related algorithm property.
void executeGetDataFiles(const std::string &investigationId, const std::string &sessionID)
Search for all related dataFiles for the specified investigation.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm