From 0939fe85933c7acf34d73f9004e363b30cea9dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20St=C3=A4hr?= <software@mstaehr.net> Date: Sun, 17 Jun 2018 01:02:32 +0200 Subject: [PATCH] fix: fix group database creation fix: fix naming for Project active feat: implement getUserByField/getUserByEmail/getProjectById refactor: camelcase for adminPassword --- .../mysql/VereinfachtesResultSet.java | 15 +++- .../gf/core/management/Management.java | 18 ++++- .../gf/core/management/ManagementImpl.java | 73 +++++++++++++------ .../gf/core/management/project/Project.java | 44 ++++++----- .../src/scripts/dbschema/fltrail.sql | 14 ++-- 5 files changed, 116 insertions(+), 48 deletions(-) diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/database/mysql/VereinfachtesResultSet.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/database/mysql/VereinfachtesResultSet.java index e997c2bd..ff270f7b 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/database/mysql/VereinfachtesResultSet.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/database/mysql/VereinfachtesResultSet.java @@ -1,6 +1,10 @@ package unipotsdam.gf.core.database.mysql; -import java.sql.*; +import java.sql.Date; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Timestamp; public class VereinfachtesResultSet { @@ -71,6 +75,15 @@ public class VereinfachtesResultSet { throw new Error("error in VereinfachtesResultSet"); } + public Timestamp getTimestamp(String columnLabel) { + try { + return resultSet.getTimestamp(columnLabel); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + } + throw new Error("error in VereinfachtesResultSet"); + } + public String getString(String columnLabel) { try { diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/Management.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/Management.java index 43236f22..52f68523 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/Management.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/Management.java @@ -91,7 +91,23 @@ public interface Management { */ User getUserByToken(String token); - void createGroup(List<User> groupMembers, String projectId); + /** + * get user by its email address + * + * @param email + * @return + */ + User getUserByEmail(String email); + + /** + * get project by its id + * + * @param id + * @return + */ + Project getProjectById(String id); + + void createGroup(Group group, String projectId); void addGroupMember(User groupMember, int groupId); diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java index 451e7385..c7f56be3 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java @@ -12,6 +12,7 @@ import unipotsdam.gf.modules.assessment.controller.StudentIdentifier; import javax.annotation.ManagedBean; import javax.annotation.Resource; import javax.inject.Singleton; +import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -48,6 +49,7 @@ public class ManagementImpl implements Management { // TODO implmement UserProfile @Mar } + // TODO: naming convention discussion? all is named create, but group is named createGroup @Override public void create(Project project) { UUID uuid = UUID.randomUUID(); @@ -58,8 +60,8 @@ public class ManagementImpl implements Management { String mysqlRequest = "INSERT INTO projects (`id`, `password`, `activ`, `timecreated`, `author`, " + "`adminpassword`, `token`) values (?,?,?,?,?,?,?)"; - connect.issueInsertOrDeleteStatement(mysqlRequest, project.getId(), project.getPassword(), project.getActiv(), - project.getTimecreated(), project.getAuthor(), project.getAdminpassword(), token); + connect.issueInsertOrDeleteStatement(mysqlRequest, project.getId(), project.getPassword(), project.isActive(), + project.getTimecreated(), project.getAuthor(), project.getAdminPassword(), token); connect.close(); } @@ -141,6 +143,18 @@ public class ManagementImpl implements Management { return new User(name, password, email, rocketChatId, rocketChatAuthToken, isStudent); } + private Project getProjectFromResultSet(VereinfachtesResultSet vereinfachtesResultSet) { + String id = vereinfachtesResultSet.getString("id"); + String password = vereinfachtesResultSet.getString("password"); + boolean active = vereinfachtesResultSet.getBoolean("active"); + Timestamp timestamp = vereinfachtesResultSet.getTimestamp("timecreated"); + String author = vereinfachtesResultSet.getString("author"); + String adminPassword = vereinfachtesResultSet.getString("adminpassword"); + String token = vereinfachtesResultSet.getString("token"); + + return new Project(id, password, active, timestamp, author, adminPassword, token); + } + @Override public String getUserToken(User user) { MysqlConnect connect = new MysqlConnect(); @@ -160,11 +174,20 @@ public class ManagementImpl implements Management { @Override public User getUserByToken(String token) { + return getUserByField("token", token); + } + + @Override + public User getUserByEmail(String email) { + return getUserByField("email", email); + } + + private User getUserByField(String field, String value) { MysqlConnect connect = new MysqlConnect(); connect.connect(); - String mysqlRequest = "SELECT * FROM users where token = ?"; + String mysqlRequest = "SELECT * FROM users where " + field + " = ?"; VereinfachtesResultSet vereinfachtesResultSet = - connect.issueSelectStatement(mysqlRequest, token); + connect.issueSelectStatement(mysqlRequest, value); boolean next = vereinfachtesResultSet.next(); if (next) { User user = getUserFromResultSet(vereinfachtesResultSet); @@ -177,27 +200,35 @@ public class ManagementImpl implements Management { } @Override - public void createGroup(List<User> groupMembers, String projectId) { - - for (User groupMember : groupMembers) { - UUID uuid = UUID.randomUUID(); - String token = uuid.toString(); - - MysqlConnect connect = new MysqlConnect(); - connect.connect(); - String mysqlRequest = - "INSERT INTO projects (`projectId`) values (?)"; - VereinfachtesResultSet vereinfachtesResultSet = connect.issueSelectStatement(mysqlRequest, projectId); - vereinfachtesResultSet.next(); - int id = vereinfachtesResultSet.getInt("id"); - - String mysqlRequest2 = - "INSERT INTO groupuser (`userEmail`, `groupId`) values (?,?)"; - connect.issueInsertOrDeleteStatement(mysqlRequest2, groupMember.getEmail(), id); + public Project getProjectById(String id) { + MysqlConnect connect = new MysqlConnect(); + connect.connect(); + String mysqlRequest = "SELECT * FROM project where id = ?"; + VereinfachtesResultSet vereinfachtesResultSet = + connect.issueSelectStatement(mysqlRequest, id); + boolean next = vereinfachtesResultSet.next(); + if (next) { + Project project = getProjectFromResultSet(vereinfachtesResultSet); connect.close(); + return project; + } else { + connect.close(); + return null; } + } + @Override + public void createGroup(Group group, String projectId) { + MysqlConnect connect = new MysqlConnect(); + connect.connect(); + Project project = getProjectById(projectId); + + for (User groupMember : group.getMembers()) { + String mysqlRequest2 = "INSERT INTO groupuser (`userEmail`, `groupId`) values (?,?)"; + connect.issueInsertOrDeleteStatement(mysqlRequest2, groupMember.getEmail(), project.getId()); + } + connect.close(); } @Override diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/project/Project.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/project/Project.java index de780722..99e4d8a4 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/project/Project.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/project/Project.java @@ -1,7 +1,5 @@ package unipotsdam.gf.core.management.project; -import org.glassfish.grizzly.http.util.TimeStamp; - import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.ZoneId; @@ -13,25 +11,35 @@ public class Project { private String id; private String password; - private String activ; + private Boolean active; private Timestamp timecreated; private String author; - private String adminpassword; + private String adminPassword; private String token; public Project() { } - public Project( - String id, String password, String activ, String author, String adminpassword) { + public Project(String id, String password, Boolean active, String author, String adminPassword, String token) { this.id = id; this.password = password; - this.activ = activ; + this.active = active; this.author = author; - this.adminpassword = adminpassword; + this.adminPassword = adminPassword; + this.token = token; + + this.timecreated = Timestamp.valueOf(LocalDateTime.now(ZoneId.of("UTC"))); + } - Timestamp ts = Timestamp.valueOf(LocalDateTime.now(ZoneId.of("UTC"))); - this.timecreated = ts; + public Project(String id, String password, Boolean active, Timestamp timecreated, String author, + String adminPassword, String token) { + this.id = id; + this.password = password; + this.active = active; + this.timecreated = timecreated; + this.author = author; + this.adminPassword = adminPassword; + this.token = token; } public String getId() { @@ -50,12 +58,12 @@ public class Project { this.password = password; } - public String getActiv() { - return activ; + public Boolean isActive() { + return active; } - public void setActiv(String activ) { - this.activ = activ; + public void setActive(Boolean active) { + this.active = active; } public String getAuthor() { @@ -66,12 +74,12 @@ public class Project { this.author = author; } - public String getAdminpassword() { - return adminpassword; + public String getAdminPassword() { + return adminPassword; } - public void setAdminpassword(String adminpassword) { - this.adminpassword = adminpassword; + public void setAdminPassword(String adminPassword) { + this.adminPassword = adminPassword; } public String getToken() { diff --git a/gemeinsamforschen/src/scripts/dbschema/fltrail.sql b/gemeinsamforschen/src/scripts/dbschema/fltrail.sql index 4ceecabc..fd0f6758 100644 --- a/gemeinsamforschen/src/scripts/dbschema/fltrail.sql +++ b/gemeinsamforschen/src/scripts/dbschema/fltrail.sql @@ -2,13 +2,13 @@ CREATE DATABASE IF NOT EXISTS `fltrail` DEFAULT CHARACTER SET utf8 COLLATE utf8_ USE `fltrail`; CREATE TABLE if not exists `projects` ( - `id` varchar(400) NOT NULL, - `password` varchar(400) NOT NULL, - `activ` tinyint(1) NOT NULL, - `timecreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - `author` varchar(400) NOT NULL, - `adminpassword` varchar(400) NOT NULL, - `token` varchar(400) NOT NULL + `id` varchar(400) NOT NULL, + `password` varchar(400) NOT NULL, + `active` tinyint(1) NOT NULL, + `timecreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `author` varchar(400) NOT NULL, + `adminPassword` varchar(400) NOT NULL, + `token` varchar(400) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- GitLab