Skip to content

Commit

Permalink
Start date cannot be greater than end. Refs #8244.
Browse files Browse the repository at this point in the history
  • Loading branch information
jawrainey committed Nov 12, 2013
1 parent 53982f2 commit b37a1ec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace MantidQt
bool validSession();
/// Open the login dialog if user not logged in.
void openLoginDialog(QWidget* window);
/// Creates a time_t value from an input date ("23/06/2003") for comparison.
time_t getTimevalue(const std::string& inputDate);

private:
/// Creates an algorithm with the name provided.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ namespace MantidQt
void populateInvestigationTypeBox();
/// Obtain the users' text input for each search field.
std::map<std::string, std::string> getSearchFields();
/// Checks if start date is greater than end date.
bool validateDates();

///////////////////////////////////////////////////////////////////////////////
// Methods for: "Search results"
Expand Down
21 changes: 21 additions & 0 deletions Code/Mantid/MantidQt/MantidWidgets/src/ICatHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "MantidQtMantidWidgets/ICatHelper.h"
#include "MantidQtAPI/AlgorithmDialog.h"
#include "MantidQtAPI/InterfaceManager.h"
#include "MantidKernel/DateAndTime.h"

#include <boost/algorithm/string/regex.hpp>
#include <QCoreApplication>

namespace MantidQt
Expand Down Expand Up @@ -193,6 +195,25 @@ namespace MantidQt
}
}

/**
* Creates a time_t value from an input date ("23/06/2003") for comparison.
* @param inputDate :: string containing the date.
* @return time_t value of date
*/
time_t ICatHelper::getTimevalue(const std::string& inputDate)
{
// Prevent any possible errors.
if(inputDate.empty()) return 0;
// A container to hold the segments of the date.
std::vector<std::string> dateSegments;
// Split input by "/" prior to rearranging the date
boost::algorithm::split_regex(dateSegments, inputDate, boost::regex("/"));
// Reorganise the date to be ISO format.
std::string isoDate = dateSegments.at(2) + "-" + dateSegments.at(1) + "-" + dateSegments.at(0) + " 0:00:00.000";
// Return the date as time_t value.
return Mantid::Kernel::DateAndTime(isoDate).to_time_t();
}

/**
* Obtain the algorithm documentation for the given property.
* @param properties :: A list of properties for a provided algorithm.
Expand Down
30 changes: 29 additions & 1 deletion Code/Mantid/MantidQt/MantidWidgets/src/ICatSearch2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,32 @@ namespace MantidQt
m_calendar->close();
}

/**
* Checks if start date is greater than end date.
* @returns true if start date is greater than end date.
*/
bool ICatSearch2::validateDates()
{
std::string startDateInput = m_icatUiForm.StartDate->text().toStdString();
std::string endDateInput = m_icatUiForm.EndDate->text().toStdString();

// Return false if the user has not input any dates. This prevents any null errors occurring.
if (startDateInput.size() <= 2 || endDateInput.size() <= 2) return false;

// If startDate > endDate we want to throw an error and inform the user (red star(*)).
if (m_icatHelper->getTimevalue(startDateInput) > m_icatHelper->getTimevalue(endDateInput))
{
m_icatUiForm.StartDate_err->setToolTip(QString::fromStdString("<span style=\"color: white;\">Start date cannot be greater than end date.</span>"));
m_icatUiForm.StartDate_err->show();
return true;
}
else
{
m_icatUiForm.StartDate_err->hide();
return false;
}
}

/**
* Show or hide advanced options if "Advanced Search" checked.
*/
Expand Down Expand Up @@ -541,8 +567,10 @@ namespace MantidQt
std::map<std::string, std::string> errors = m_icatHelper->validateProperties(inputFields);

// Has any errors occurred?
if (!errors.empty())
if (!errors.empty() || validateDates())
{
// Clear form to prevent previous search results showing if an error occurs.
clearSearchResultFrame();
showErrorLabels(errors);
m_icatUiForm.searchResultsLbl->setText("An error has occurred in the search form.");
// Stop here to prevent the search being carried out below.
Expand Down

0 comments on commit b37a1ec

Please sign in to comment.