GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoDBInitCreateAuthorsPage.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
35 #include "CreateDataBaseHelper.h"
37 #include "vtkSQLQuery.h"
38 #include "QueryDataBaseHelper.h"
40 #include "GoDBAuthorRow.h"
41 #include <QFormLayout>
42 #include <QMessageBox>
43 #include <QVariant>
44 #include <QInputDialog>
45 #include <QDir>
46 #include <QPushButton>
47 
49  QWizardPage(iParent)
50 {
51  QFont tfont;
52 
53  tfont.setBold(false);
54  this->setFont(tfont);
56  setSubTitle( tr("Create the authors for the gofigure projects:") );
57 
58  QFormLayout *formLayout = new QFormLayout;
59 
60  lineLastName = new QLineEdit;
63  CreateButton = new QPushButton( tr("Create Author") );
64 
65  formLayout->addRow(tr("Enter the Author FirstName:"), lineFirstName);
66  formLayout->addRow(tr("Enter the Author MiddleName:"), lineMiddleName);
67  formLayout->addRow(tr("Enter the Author LastName:"), lineLastName);
68  formLayout->addWidget(CreateButton);
69 
70  QObject::connect( this->CreateButton, SIGNAL( clicked() ),
71  this, SLOT( CreateAuthor() ) );
72 
73  setLayout(formLayout);
74 }
75 
76 //-------------------------------------------------------------------------
77 
78 //-------------------------------------------------------------------------
80 {
81  this->OpenDBConnection();
82 
84  "AuthorID", "author").empty() )
85  {
86  QMessageBox msgBox;
87  msgBox.setText(
88  tr("Please create at least one author for your project.") );
89  msgBox.exec();
91  {
93  }
94  return false;
95  }
97  {
99  }
100  return true;
101 }
102 
103 //-------------------------------------------------------------------------
104 
105 //-------------------------------------------------------------------------
107 {
108  this->OpenDBConnection();
109  QMessageBox msgBox;
110  std::string LastNameValue = lineLastName->text().toStdString();
111  std::string FirstNameValue = lineFirstName->text().toStdString();
112  std::string MiddleNameValue = lineMiddleName->text().toStdString();
113 
114  if ( FirstNameValue.empty() || LastNameValue.empty() )
115  {
116  msgBox.setText(
117  tr("Please enter at least the lastname and the firstname of your author") );
118  msgBox.exec();
119  return;
120  }
121 
122  std::vector< FieldWithValue > Conditions;
123  FieldWithValue FirstName = { "FirstName", FirstNameValue, "=" };
124  FieldWithValue LastName = { "LastName", LastNameValue, "=" };
125  Conditions.push_back(FirstName);
126  Conditions.push_back(LastName);
127 
128  if ( FindOneID(this->m_DatabaseConnector, "author", "AuthorID", Conditions) != -1 && MiddleNameValue.empty() )
129  {
130  msgBox.setText(
131  tr("There is already an Author with the same lastname and firstname, please enter a middlename") );
132  msgBox.exec();
133  return;
134  }
135 
136  GoDBAuthorRow NewAuthor;
137  NewAuthor.SetField( "FirstName", lineFirstName->text().toStdString() );
138  NewAuthor.SetField( "LastName", lineLastName->text().toStdString() );
139  NewAuthor.SetField( "MiddleName", lineMiddleName->text().toStdString() );
140 
141  if ( NewAuthor.DoesThisAuthorAlreadyExists(this->m_DatabaseConnector) != -1 )
142  {
143  msgBox.setText(
144  tr("This author already exists") );
145  msgBox.exec();
146  return;
147  }
148  NewAuthor.SaveInDB(this->m_DatabaseConnector);
149 
150  msgBox.setText(
151  tr("Your author has been successfully created") );
152  msgBox.exec();
153  emit NewAuthorCreated();
154 
156  {
158  }
159 }
160 
161 //------------------------------------------------------------------------------
162 
163 //------------------------------------------------------------------------------
165 {
166  if ( this->m_User.empty() || this->m_Password.empty() )
167  {
168  this->SetDatabaseVariables( field("User").toString().toStdString(),
169  field("Password").toString().toStdString() );
170  }
171  if ( this->m_DatabaseConnector == 0 )
172  {
174  this->m_Server, this->m_User, this->m_Password, this->m_DBName);
175  }
176 }
177 
178 //-------------------------------------------------------------------------
179 
180 //-------------------------------------------------------------------------
182  std::string iUser, std::string iPassword)
183 {
184  this->m_User = iUser;
185  this->m_Password = iPassword;
186  this->m_Server = "localhost";
187  this->m_DBName = "gofiguredatabase";
188 }
int DoesThisAuthorAlreadyExists(vtkMySQLDatabase *DatabaseConnector)
check if the author already exists in the database, if yes, return the corresponding ID...
void setSubTitle(const QString &subTitle)
bool CloseDatabaseConnection(vtkMySQLDatabase *DatabaseConnector)
return true if the connection has been closed, false if the connection was already closed ...
vtkMySQLDatabase * OpenDatabaseConnection(std::string ServerName, std::string login, std::string Password, std::string DBName)
manages a map with keys matching fields of the gofiguredatabase Author table and values of the map ma...
Definition: GoDBAuthorRow.h:50
QString tr(const char *sourceText, const char *disambiguation, int n)
void setBold(bool enable)
void setLayout(QLayout *layout)
std::vector< std::string > ListAllValuesForOneColumn(vtkMySQLDatabase *DatabaseConnector, const std::string &ColumnName, const std::string &TableName, std::string OrderByColumnName)
SELECT ColumnName from TableName ORDER BY OrderbyColumnName.
QGoDBInitCreateAuthorsPage(QWidget *iparent=0)
void setText(const QString &text)
int SaveInDB(vtkMySQLDatabase *DatabaseConnector)
int FindOneID(vtkMySQLDatabase *DatabaseConnector, const std::string &TableName, const std::string &ColumnName, const std::string &field, const std::string &value)
SELECT ColumnName FROM TableName WHERE field = value.
void addWidget(QWidget *w)
void addRow(QWidget *label, QWidget *field)
QVariant field(const QString &name) const
void SetDatabaseVariables(std::string iUser, std::string iPassword)
void setFont(const QFont &)
void SetField(const std::string &key, const T &value)
convert the value into a string and assign it to the key in the map
Definition: GoDBRow.h:73
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)