@@ -59,6 +59,10 @@ private void logout(Connection conn) {
59
59
}
60
60
}
61
61
62
+ private String prepareStringInput (String input ) {
63
+ return input .trim ();
64
+ }
65
+
62
66
private void printAffiliations () {
63
67
String sql = "SELECT * FROM organizations" ;
64
68
Connection conn = login ();
@@ -80,6 +84,7 @@ private void printAffiliations() {
80
84
}
81
85
82
86
public String getProjectName (String projectIdentifier ) {
87
+ projectIdentifier = prepareStringInput (projectIdentifier );
83
88
String sql = "SELECT short_title from projects WHERE openbis_project_identifier = ?" ;
84
89
String res = "" ;
85
90
Connection conn = login ();
@@ -176,6 +181,7 @@ private Connection login() {
176
181
}
177
182
178
183
public void addOrChangeSecondaryNameForProject (int projectID , String secondaryName ) {
184
+ secondaryName = prepareStringInput (secondaryName );
179
185
logger .info (
180
186
"Adding/Updating secondary name of project with id " + projectID + " to " + secondaryName );
181
187
boolean saved = saveOldSecondaryNameForProjects (projectID );
@@ -333,6 +339,7 @@ public List<String> getPossibleSetOptionsForColumnsInTable(String table, String
333
339
}
334
340
335
341
public boolean isProjectInDB (String projectIdentifier ) {
342
+ projectIdentifier = prepareStringInput (projectIdentifier );
336
343
logger .info ("Looking for project " + projectIdentifier + " in the DB" );
337
344
String sql = "SELECT * from projects WHERE openbis_project_identifier = ?" ;
338
345
boolean res = false ;
@@ -356,6 +363,8 @@ public boolean isProjectInDB(String projectIdentifier) {
356
363
}
357
364
358
365
public int addProjectToDB (String projectIdentifier , String projectName ) {
366
+ projectIdentifier = prepareStringInput (projectIdentifier );
367
+ projectName = prepareStringInput (projectName );
359
368
if (!isProjectInDB (projectIdentifier )) {
360
369
logger .info ("Trying to add project " + projectIdentifier + " to the person DB" );
361
370
String sql = "INSERT INTO projects (openbis_project_identifier, short_title) VALUES(?, ?)" ;
@@ -384,6 +393,7 @@ public int addProjectToDB(String projectIdentifier, String projectName) {
384
393
}
385
394
386
395
public boolean hasPersonRoleInProject (int personID , int projectID , String role ) {
396
+ role = prepareStringInput (role );
387
397
logger .info ("Checking if person already has this role in the project." );
388
398
String sql =
389
399
"SELECT * from projects_persons WHERE person_id = ? AND project_id = ? and project_role = ?" ;
@@ -410,6 +420,7 @@ public boolean hasPersonRoleInProject(int personID, int projectID, String role)
410
420
}
411
421
412
422
public void addOrUpdatePersonToProject (int projectID , int personID , String role ) {
423
+ role = prepareStringInput (role );
413
424
if (!hasPersonRoleInProject (personID , projectID , role )) {
414
425
logger .info ("Trying to add person with role " + role + " to a project." );
415
426
if (!roleForProjectTaken (projectID , role )) {
@@ -455,6 +466,7 @@ public void addOrUpdatePersonToProject(int projectID, int personID, String role)
455
466
}
456
467
457
468
private boolean roleForProjectTaken (int projectID , String role ) {
469
+ role = prepareStringInput (role );
458
470
boolean res = false ;
459
471
String sql = "SELECT person_ID FROM projects_persons WHERE project_id = ? AND project_role = ?" ;
460
472
Connection conn = login ();
@@ -764,25 +776,6 @@ public boolean addNewPerson(Person person) {
764
776
return res ;
765
777
}
766
778
767
- // public List<String> getPersons() {
768
- // List<String> res = new ArrayList<String>();
769
- // String sql = "SELECT * FROM person";
770
- // Connection conn = login();
771
- // try (PreparedStatement statement = conn.prepareStatement(sql)) {
772
- // ResultSet rs = statement.executeQuery();
773
- // while (rs.next()) {
774
- // String first = Integer.toString(rs.getInt("first_name"));
775
- // String last = Integer.toString(rs.getInt("last_name"));
776
- // res.add(first + " " + last);
777
- // }
778
- // } catch (SQLException e) {
779
- // e.printStackTrace();
780
- // } finally {
781
- // endQuery(conn, statement);
782
- // }
783
- // return res;
784
- // }
785
-
786
779
public Map <String , Integer > getPersonMap () {
787
780
Map <String , Integer > res = new HashMap <String , Integer >();
788
781
String sql = "SELECT * FROM persons" ;
@@ -805,6 +798,20 @@ public Map<String, Integer> getPersonMap() {
805
798
return res ;
806
799
}
807
800
801
+ public Map <String , Integer > getColsMaxLengthsForTable (String table ) throws SQLException {
802
+ Connection conn = login ();
803
+ DatabaseMetaData md = conn .getMetaData ();
804
+ Map <String , Integer > res = new HashMap <>();
805
+ ResultSet rs = md .getColumns (null , null , table , null );
806
+ while (rs .next ()) {
807
+ String cName = rs .getString ("COLUMN_NAME" );
808
+ int length = rs .getInt ("COLUMN_SIZE" );
809
+ res .put (cName , new Integer (length ));
810
+ }
811
+ logout (conn );
812
+ return res ;
813
+ }
814
+
808
815
public void printTableNames () throws SQLException {
809
816
Connection conn = login ();
810
817
DatabaseMetaData md = conn .getMetaData ();
@@ -982,6 +989,7 @@ public Set<String> getInstituteNames() {
982
989
}
983
990
984
991
public Affiliation getOrganizationInfosFromInstitute (String institute ) {
992
+ institute = prepareStringInput (institute );
985
993
Affiliation res = null ;
986
994
String sql = "SELECT * FROM organizations WHERE institute LIKE ?" ;
987
995
Connection conn = login ();
@@ -1034,6 +1042,7 @@ public Affiliation getOrganizationInfosFromInstitute(String institute) {
1034
1042
}
1035
1043
1036
1044
public Affiliation getOrganizationInfosFromOrg (String organization ) {
1045
+ organization = prepareStringInput (organization );
1037
1046
Affiliation res = null , maybe = null ;
1038
1047
String sql = "SELECT * FROM organizations WHERE umbrella_organization LIKE ?" ;
1039
1048
Connection conn = login ();
@@ -1118,53 +1127,12 @@ public List<Affiliation> getAffiliationTable() {
1118
1127
return res ;
1119
1128
}
1120
1129
1121
-
1122
1130
public String getInvestigatorForProject (String projectIdentifier ) {
1131
+ projectIdentifier = prepareStringInput (projectIdentifier );
1123
1132
String details = getPersonDetailsForProject (projectIdentifier , "PI" );
1124
1133
return details .split ("\n " )[0 ].trim ();
1125
1134
}
1126
1135
1127
- // public Person getPersonForProject(String projectIdentifier, String role) {
1128
- // String sql =
1129
- // "SELECT * FROM persons LEFT JOIN projects_persons ON persons.id = projects_persons.person_id "
1130
- // + "LEFT JOIN projects ON projects_persons.project_id = projects.id WHERE "
1131
- // + "projects.openbis_project_identifier = ? AND projects_persons.project_role = ?";
1132
- // Person res = null;
1133
- //
1134
- // Connection conn = login();
1135
- // try (PreparedStatement statement = conn.prepareStatement(sql)) {
1136
- // statement.setString(1, projectIdentifier);
1137
- // statement.setString(2, role);
1138
- //
1139
- // ResultSet rs = statement.executeQuery();
1140
- //
1141
- //
1142
- // while (rs.next()) {
1143
- // String title = rs.getString("title");
1144
- // String zdvID = rs.getString("username");
1145
- // String first = rs.getString("first_name");
1146
- // String last = rs.getString("family_name");
1147
- // String email = rs.getString("email");
1148
- // String tel = rs.getString("phone");
1149
- // Affiliation affiliation = getAffiliationFromProjectIDAndRole(projectIdentifier, role);
1150
- // int instituteID = -1;// TODO fetch correct id
1151
- //
1152
- //
1153
- // res = new Person(zdvID, title, first, last, email, tel, instituteID, affiliation);
1154
- // }
1155
- // } catch (SQLException e) {
1156
- // LOG.error("Could not get person for project due to database error", e);
1157
- // } finally {
1158
- // logout(conn);
1159
- // }
1160
- //
1161
- // return res;
1162
- // }
1163
-
1164
- //
1165
- // int affiliationID = getAffiliationIDForPersonID(id);
1166
- // affiliationOfPerson = getAffiliationWithID(affiliationID);
1167
- //
1168
1136
/**
1169
1137
*
1170
1138
* @param personID
@@ -1197,6 +1165,8 @@ public int getAffiliationIDForPersonID(Integer personID) {
1197
1165
}
1198
1166
1199
1167
public String getPersonDetailsForProject (String projectIdentifier , String role ) {
1168
+ projectIdentifier = prepareStringInput (projectIdentifier );
1169
+ role = prepareStringInput (role );
1200
1170
String sql =
1201
1171
"SELECT projects_persons.*, projects.* FROM projects_persons, projects WHERE projects.openbis_project_identifier = ?"
1202
1172
+ " AND projects.id = projects_persons.project_id AND projects_persons.project_role = ?" ;
@@ -1377,6 +1347,7 @@ private void endQuery(Connection c, PreparedStatement p) {
1377
1347
}
1378
1348
1379
1349
public void setAffiliationVIP (int affi , int person , String role ) {
1350
+ role = prepareStringInput (role );
1380
1351
logger .info ("Trying to set/change affiliation-specific role " + role );
1381
1352
String sql = "UPDATE organizations SET " + role + "=? WHERE id = ?" ;
1382
1353
Connection conn = login ();
@@ -1396,6 +1367,8 @@ public void setAffiliationVIP(int affi, int person, String role) {
1396
1367
}
1397
1368
1398
1369
public List <Person > getPersonsByName (String one , String two ) {
1370
+ one = prepareStringInput (one );
1371
+ two = prepareStringInput (two );
1399
1372
List <Person > res = new ArrayList <Person >();
1400
1373
1401
1374
String sql = "SELECT * from persons where (first_name LIKE ? AND family_name LIKE ?) OR "
@@ -1599,6 +1572,7 @@ public Map<String, ProjectInfo> getProjectMap() {
1599
1572
}
1600
1573
1601
1574
public List <CollaboratorWithResponsibility > getCollaboratorsOfProject (String project ) {
1575
+ project = prepareStringInput (project );
1602
1576
List <CollaboratorWithResponsibility > res = new ArrayList <CollaboratorWithResponsibility >();
1603
1577
// for experiments
1604
1578
String sql =
@@ -1651,6 +1625,7 @@ public List<CollaboratorWithResponsibility> getCollaboratorsOfProject(String pro
1651
1625
}
1652
1626
1653
1627
public int addExperimentToDB (String openbisIdentifier ) {
1628
+ openbisIdentifier = prepareStringInput (openbisIdentifier );
1654
1629
int exists = isExpInDB (openbisIdentifier );
1655
1630
if (exists < 0 ) {
1656
1631
logger .info ("Trying to add experiment " + openbisIdentifier + " to the person DB" );
@@ -1676,14 +1651,15 @@ public int addExperimentToDB(String openbisIdentifier) {
1676
1651
return exists ;
1677
1652
}
1678
1653
1679
- private int isExpInDB (String id ) {
1680
- logger .info ("Looking for experiment " + id + " in the DB" );
1654
+ private int isExpInDB (String expID ) {
1655
+ expID = prepareStringInput (expID );
1656
+ logger .info ("Looking for experiment " + expID + " in the DB" );
1681
1657
String sql = "SELECT * from experiments WHERE openbis_experiment_identifier = ?" ;
1682
1658
int res = -1 ;
1683
1659
Connection conn = login ();
1684
1660
try {
1685
1661
PreparedStatement statement = conn .prepareStatement (sql );
1686
- statement .setString (1 , id );
1662
+ statement .setString (1 , expID );
1687
1663
ResultSet rs = statement .executeQuery ();
1688
1664
if (rs .next ()) {
1689
1665
logger .info ("project found!" );
@@ -1698,6 +1674,7 @@ private int isExpInDB(String id) {
1698
1674
}
1699
1675
1700
1676
public void addPersonToExperiment (int expID , int personID , String role ) {
1677
+ role = prepareStringInput (role );
1701
1678
if (expID == 0 || personID == 0 )
1702
1679
return ;
1703
1680
@@ -1746,6 +1723,7 @@ private boolean hasPersonRoleInExperiment(int personID, int expID, String role)
1746
1723
}
1747
1724
1748
1725
public int getProjectIDFromCode (String code ) {
1726
+ code = prepareStringInput (code );
1749
1727
int res = -1 ;
1750
1728
String sql = "SELECT id from projects WHERE openbis_project_identifier LIKE ?" ;
1751
1729
Connection conn = login ();
@@ -1766,6 +1744,7 @@ public int getProjectIDFromCode(String code) {
1766
1744
}
1767
1745
1768
1746
public void removePersonFromProject (int id , String role ) {
1747
+ role = prepareStringInput (role );
1769
1748
logger .info ("Trying to remove person with role " + role + " from project with id " + id );
1770
1749
String sql = "DELETE FROM projects_persons WHERE project_id = ? AND project_role = ?" ;
1771
1750
Connection conn = login ();
@@ -1802,6 +1781,7 @@ public void removePersonFromExperiment(int experimentID) {
1802
1781
}
1803
1782
1804
1783
public void addOrUpdatePersonToExperiment (int experimentID , int personID , String role ) {
1784
+ role = prepareStringInput (role );
1805
1785
if (!hasPersonRoleInExperiment (personID , experimentID , role )) {
1806
1786
logger .info ("Trying to add person with role " + role + " to an experiment." );
1807
1787
if (!roleForExperimentTaken (experimentID , role )) {
0 commit comments