Skip to content
Snippets Groups Projects
Commit ab53cf28 authored by Julian Dehne's avatar Julian Dehne
Browse files

feat: implemented Group and Project DB access and interface

parent 18eccbb0
No related branches found
No related tags found
No related merge requests found
Showing
with 1452 additions and 0 deletions
package unipotsdam.gf.core.database;
/**
* Created by dehne on 31.05.2018.
*/
public class GFDatabaseConfig {
public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
public static final String DB_URL = "jdbc:mysql://localhost";
// Database credentials
public static final String USER = "root2";
public static final String PASS = "voyager2";
public static final String DB_NAME = "fltrail";
}
package unipotsdam.gf.core.database.mysql;
import unipotsdam.gf.core.database.GFDatabaseConfig;
import java.sql.*;
import java.util.Date;
public class MysqlConnect {
public Connection conn = null;
private static String createConnectionString() {
String connString = "jdbc:mysql://" + "localhost" +
"/" + GFDatabaseConfig.DB_NAME +
"?user=" + GFDatabaseConfig.USER +
"&password=" + GFDatabaseConfig.PASS;
return String.format(connString, GFDatabaseConfig.DB_NAME);
}
public void connect() {
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.out.println(ex); //logger?
}
conn = DriverManager.getConnection(createConnectionString());
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
throw new Error("could not connect to mysql");
}
}
public void close() {
try {
if (conn != null) {
conn.close();
}
} catch (final SQLException e) {
throw new Error("could not close mysql");
}
}
private PreparedStatement addParameters(final String statement, final Object[] args) {
try {
final PreparedStatement ps = conn.prepareStatement(statement);
if (args != null) {
for (int i = 0; i < args.length; i++) {
final Object arg = args[i];
setParam(ps, arg, i + 1);
}
}
return ps;
} catch (SQLException ex) {
System.out.println(ex);
}
return null;
}
public VereinfachtesResultSet issueSelectStatement(final String statement, final Object... args) {
try {
PreparedStatement ps = addParameters(statement, args);
ResultSet queryResult = ps.executeQuery();
return new VereinfachtesResultSet(queryResult);
} catch (SQLException ex) {
System.out.println(ex);
}
return null;
}
public void otherStatements(final String statement) {
try {
this.conn.createStatement().execute(statement);
} catch (SQLException ex) {
System.out.println(ex);
}
}
public Integer issueUpdateStatement(final String statement, final Object... args) {
PreparedStatement ps = addParameters(statement, args);
try {
return ps.executeUpdate();
} catch (SQLException ex) {
System.out.println(ex);
}
return null;
}
public void issueInsertOrDeleteStatement(final String statement, final Object... args) {
PreparedStatement ps = addParameters(statement, args);
try {
ps.execute();
} catch (SQLException ex) {
System.out.println(ex);
}
}
private void setParam(final PreparedStatement ps, final Object arg, final int i) throws SQLException {
if (arg instanceof String) {
ps.setString(i, (String) arg);
} else if (arg instanceof Integer) {
ps.setInt(i, (Integer) arg);
} else if (arg instanceof Double) {
ps.setDouble(i, (Double) arg);
} else if (arg instanceof Boolean) {
ps.setBoolean(i, (Boolean) arg);
} else if (arg instanceof Float) {
ps.setFloat(i, (Float) arg);
} else if (arg instanceof Short) {
ps.setShort(i, (Short) arg);
} else if (arg instanceof Long) {
ps.setLong(i, (Long) arg);
} else if (arg instanceof Byte) {
ps.setByte(i, (Byte) arg);
} else if (arg instanceof Character) {
ps.setString(i, arg.toString());
} else if (arg instanceof Date) {
final java.sql.Date d = new java.sql.Date(((Date) arg).getTime());
ps.setDate(i, d);
} else if (arg == null) {
ps.setNull(i, Types.NULL);
} else {
ps.setString(i, arg.toString());
}
}
public Connection getConnection() {
return conn;
}
public void setConnection(Connection conn) {
this.conn = conn;
}
}
package unipotsdam.gf.core.management;
import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.core.management.user.UserInterests;
import unipotsdam.gf.core.management.user.UserProfile;
import unipotsdam.gf.modules.assessment.controller.StudentIdentifier;
/**
* Created by dehne on 31.05.2018.
* TODO in DAOs refaktorisieren, sobald es mehr wird
*/
public interface Management {
/**
* delete a User in the database
* @param identifier
*/
void delete(StudentIdentifier identifier);
/**
* create a User in the database
* @param user
* @param profile
*/
void create(User user, UserProfile profile);
/**
* create a Project in the database
* @param project
*/
void create(Project project);
/**
* Delete a Project in the database
* @param project
*/
void delete(Project project);
/**
* Add an entry in the M:N table linking users and projects
* @param user
* @param project
* @param interests
*/
void register(User user, Project project, UserInterests interests);
/**
* Check if a user exists in the DB
* @param user
* @return
*/
Boolean exists(User user);
/**
* Get all the users linked to a project
* @param project
* @return
*/
java.util.List<User> getUsers(Project project);
}
package unipotsdam.gf.core.management;
import unipotsdam.gf.core.database.mysql.MysqlConnect;
import unipotsdam.gf.core.database.mysql.VereinfachtesResultSet;
import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.core.management.user.UserInterests;
import unipotsdam.gf.core.management.user.UserProfile;
import unipotsdam.gf.modules.assessment.controller.StudentIdentifier;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Created by dehne on 31.05.2018.
*/
public class ManagementImpl implements Management {
@Override
public void delete(StudentIdentifier identifier) {
MysqlConnect connect = new MysqlConnect();
connect.connect();
String mysqlRequest = "DELETE FROM users where email = (?)";
connect.issueInsertOrDeleteStatement(mysqlRequest, identifier.getStudentId());
connect.close();
}
@Override
public void create(User user, UserProfile profile) {
UUID uuid = UUID.randomUUID();
String token = uuid.toString();
MysqlConnect connect = new MysqlConnect();
connect.connect();
String mysqlRequest = "INSERT INTO users (`name`, `password`, `email`, `token`) values (?,?,?,?)";
connect.issueInsertOrDeleteStatement(mysqlRequest, user.getName(), user.getPassword(), user.getEmail(), token);
connect.close();
// TODO implmement UserProfile @Mar
}
@Override
public void create(Project project) {
UUID uuid = UUID.randomUUID();
String token = uuid.toString();
MysqlConnect connect = new MysqlConnect();
connect.connect();
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.close();
}
@Override
public void delete(Project project) {
MysqlConnect connect = new MysqlConnect();
connect.connect();
String mysqlRequest = "DELETE FROM projects where id = (?)";
connect.issueInsertOrDeleteStatement(mysqlRequest, project.getId());
connect.close();
}
@Override
public void register(User user, Project project, UserInterests interests) {
MysqlConnect connect = new MysqlConnect();
connect.connect();
String mysqlRequest =
"INSERT INTO projectuser (`projectId`, `userId`) values (?,?)";
connect.issueInsertOrDeleteStatement(mysqlRequest, project.getId(), user.getId());
connect.close();
}
@Override
public Boolean exists(User user) {
Boolean result = false;
MysqlConnect connect = new MysqlConnect();
connect.connect();
String mysqlRequest = "SELECT * FROM users where email = (?)";
VereinfachtesResultSet vereinfachtesResultSet = connect.issueSelectStatement(mysqlRequest, user.getEmail());
result = vereinfachtesResultSet.next();
connect.close();
return result;
}
@Override
public List<User> getUsers(Project project) {
String query = "SELECT * FROM users u " +
" JOIN projectuser pu ON u.email=pu.userId" +
" JOIN projects p ON pu.projectId = p.id" +
" WHERE pu.projectId = ?";
ArrayList<User> result = new ArrayList<User>();
MysqlConnect connect = new MysqlConnect();
connect.connect();
VereinfachtesResultSet vereinfachtesResultSet = connect.issueSelectStatement(query, project.getId());
while (!vereinfachtesResultSet.isLast()) {
vereinfachtesResultSet.next();
String name = vereinfachtesResultSet.getString("name");
String password = vereinfachtesResultSet.getString("password");
String email = vereinfachtesResultSet.getString("email");
String token = vereinfachtesResultSet.getString("token");
User user = new User(name, password, email);
user.setToken(token);
result.add(user);
}
connect.close();
return result;
}
}
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;
/**
* Created by dehne on 31.05.2018.
*/
public class Project {
private String id;
private String password;
private String activ;
private Timestamp timecreated;
private String author;
private String adminpassword;
private String token;
public Project() {
}
public Project(
String id, String password, String activ, String author, String adminpassword) {
this.id = id;
this.password = password;
this.activ = activ;
this.author = author;
this.adminpassword = adminpassword;
Timestamp ts = Timestamp.valueOf(LocalDateTime.now(ZoneId.of("UTC")));
this.timecreated = ts;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getActiv() {
return activ;
}
public void setActiv(String activ) {
this.activ = activ;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getAdminpassword() {
return adminpassword;
}
public void setAdminpassword(String adminpassword) {
this.adminpassword = adminpassword;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public Timestamp getTimecreated() {
return timecreated;
}
}
\ No newline at end of file
package unipotsdam.gf.core.management.user;
/**
* Created by dehne on 31.05.2018.
*/
public class User {
public User() {
}
public User(String name, String password, String email) {
this.name = name;
this.password = password;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
// the email is the id!
public String getId() {
return this.email;
}
private String name;
private String password;
private String email;
private String token;
}
package unipotsdam.gf.core.management.user;
/**
* Created by dehne on 31.05.2018.
*/
// user data that is created on a project basis
public class UserInterests {
}
package unipotsdam.gf.core.management.user;
/**
* Created by dehne on 31.05.2018.
*/
// User data specific to Group Formation or Asessment
public class UserProfile {
}
package unipotsdam.gf.interfaces;
import java.io.File;
/**
* Created by Johannes Zeiße on 30.05.2018.
*/
......
......@@ -31,6 +31,12 @@ CREATE TABLE `users` (
`token` varchar(800) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE projectuser
(
projectId varchar(400) NOT NULL,
userId varchar(400) NOT NULL
); ENGINE=InnoDB DEFAULT CHARSET=utf8;
......
package unipotsdam.gf.interfaces;
import org.junit.Test;
import unipotsdam.gf.core.management.ManagementImpl;
import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.core.management.user.UserProfile;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import static org.junit.Assert.*;
/**
* Created by dehne on 01.06.2018.
*/
public class ManagementTest {
@Test
public void testDelete() throws Exception {
}
/**
* CREATE a user in the DB using the
* @throws Exception
*/
@Test
public void testExists() throws Exception {
ManagementImpl management = new ManagementImpl();
User user = new User("julian", "1234", "from1123123123@stuff.com");
assert !management.exists(user);
}
/**
* CREATE a user in the DB using the
* @throws Exception
*/
@Test
public void testCreate() throws Exception {
ManagementImpl management = new ManagementImpl();
User user = new User("julian", "1234", "from@stuff.com");
management.create(user, new UserProfile());
assert management.exists(user);
}
/**
* Test creating a user in the DB
* @throws Exception
*/
@Test
public void testCreate1() throws Exception {
ManagementImpl management = new ManagementImpl();
management.create(new Project("Gemainsam Forschen", "1235", "1", "me", "keins"));
}
@Test
public void testRegister() throws Exception {
ManagementImpl management = new ManagementImpl();
User user = new User("julian", "1234", "from@stuff.com");
management.create(user, new UserProfile());
assert management.exists(user);
Project project = new Project("Gemainsam Forschen", "1235", "1", "me", "keins");
management.create(project);
management.register(user, project, null);
}
@Test
public void testGetUsers() throws Exception {
ManagementImpl management = new ManagementImpl();
User user = new User("julian", "1234", "from@stuff.com");
management.create(user, new UserProfile());
assert management.exists(user);
Project project = new Project("Gemainsam Forschen", "1235", "1", "me", "keins");
management.create(project);
management.register(user, project, null);
User user2 = new User("julian2", "12345", "from2@stuff.com");
management.create(user2, new UserProfile());
assert management.exists(user2);
List<User> users = management.getUsers(project);
assert users != null;
assert !users.isEmpty();
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment