Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed tables, models and services, requires database connection and tests #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 90 additions & 10 deletions src/main/java/com/github/perscholas/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.github.perscholas.utils.ConnectionBuilder;
import com.github.perscholas.utils.IOConsole;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.*;
import java.lang.StringBuilder;

/**
* Created by leon on 2/18/2020.
Expand All @@ -17,15 +17,16 @@ public enum DatabaseConnection implements DatabaseConnectionInterface {
private final ConnectionBuilder connectionBuilder;

DatabaseConnection(ConnectionBuilder connectionBuilder) {

this.connectionBuilder = connectionBuilder;
}

DatabaseConnection() {
this(new ConnectionBuilder()
.setUser("root")
.setPassword("")
.setPort(3306)
.setDatabaseVendor("mariadb")
.setPassword("Nikolaev@71")
.setPort(3307)
.setDatabaseVendor("mysql")
.setHost("127.0.0.1"));
}

Expand All @@ -34,24 +35,39 @@ public String getDatabaseName() {
return name().toLowerCase();
}

@Override
public String getParameters() {
return "?serverTimezone=UTC&useLegacyDatetimeCode=false";
}

@Override
public Connection getDatabaseConnection() {
return connectionBuilder
.setDatabaseName(getDatabaseName())
.setParameters(getParameters())
.build();
}

@Override
public Connection getDatabaseEngineConnection() {

return connectionBuilder.build();
}

@Override
public void create() {
String sqlStatement = null; // TODO - define statement
//String sqlStatement - create and define statement
String sqlStatement = new StringBuilder()
.append("CREATE DATABASE IF NOT EXISTS ")
.append(getDatabaseName())
.append(";")
.toString();
String info;
// execute statement
try {
// TODO - execute statement
getDatabaseEngineConnection()
.prepareStatement(sqlStatement)
.execute();
info = "Successfully executed statement `%s`.";
} catch (Exception sqlException) {
info = "Failed to executed statement `%s`.";
Expand All @@ -61,18 +77,82 @@ public void create() {

@Override
public void drop() {
//String sqlStatement - define statement
String sqlStatement = new StringBuilder()
.append("DROP DATABASE IF EXISTS ")
.append(getDatabaseName())
.append(";")
.toString();
String info;
try {
//execute statement
getDatabaseEngineConnection()
.prepareStatement(sqlStatement)
.execute();
info = "Successfully executed statement `%s`.";
} catch (Exception sqlException) {
info = "Failed to execute statement `%s`.";
}
console.println(info, sqlStatement);
}

@Override
public void use() {
//define statement
String sqlStatement = new StringBuilder()
.append("USE ")
.append(getDatabaseName())
.append(";")
.toString();
String info;
try {
//execute statement
getDatabaseEngineConnection()
.prepareStatement(sqlStatement)
.execute();
info = "Successfully executed statement `%s`.";
} catch (Exception sqlException) {
info = "Failed to execute statement `%s`.";
}
console.println(info, sqlStatement);
}

@Override
public void executeStatement(String sqlStatement) {
sqlStatement = sqlStatement.trim();
String info;
try {
getScrollableStatement().execute(sqlStatement);
info = "Successfully executed statement `%s`.";
} catch (Exception sqlException) {
info = "Failed to execute statement `%s`.";
}
console.println(info, sqlStatement);
}

//TO DO - create method
public Statement getScrollableStatement() {
int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
try {
return getDatabaseConnection().createStatement(resultSetType, resultSetConcurrency);
} catch (SQLException e) {
throw new Error(e);
}
}

@Override
public ResultSet executeQuery(String sqlQuery) {
return null;
public ResultSet executeQuery (String sqlQuery){
String info;
try {
sqlQuery = sqlQuery.trim();
ResultSet resultSet = getScrollableStatement().executeQuery(sqlQuery);
info = "Successfully executed query '%s'.";
console.println(info, sqlQuery);
return resultSet;
} catch (SQLException e) {
info = String.format("Failed to execute query '%s'.", sqlQuery);
throw new Error(info, e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
import java.sql.ResultSet;

public interface DatabaseConnectionInterface {

String getDatabaseName();

String getParameters();

//public interface
Connection getDatabaseConnection();
//public interface
Connection getDatabaseEngineConnection();
//methods
void drop();
void create();
void use();
void executeStatement(String sqlStatement);
//public interface
ResultSet executeQuery(String sqlQuery);
}
14 changes: 9 additions & 5 deletions src/main/java/com/github/perscholas/JdbcConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import com.github.perscholas.utils.DirectoryReference;
import com.github.perscholas.utils.FileReader;
import com.mysql.cj.jdbc.Driver;

import java.io.File;
import java.sql.DriverManager;


public class JdbcConfigurator {
static {
try {
// TODO - Attempt to register JDBC Driver
// register JDBC Driver
DriverManager.registerDriver(Driver.class.newInstance());
} catch (Exception e) {
throw new Error(e);
}
}

private static final DatabaseConnection dbc = DatabaseConnection.MANAGEMENT_SYSTEM;
//create instance of DatabaseConnection
private static final DatabaseConnection dbc = DatabaseConnection.UAT;

public static void initialize() {
dbc.drop();
Expand All @@ -24,14 +28,14 @@ public static void initialize() {
executeSqlFile("courses.populate-table.sql");
executeSqlFile("students.create-table.sql");
executeSqlFile("students.populate-table.sql");
executeSqlFile("studentcourses.create-table.sql");
}

private static void executeSqlFile(String fileName) {
File creationStatementFile = DirectoryReference.RESOURCE_DIRECTORY.getFileFromDirectory(fileName);
FileReader fileReader = new FileReader(creationStatementFile.getAbsolutePath());
String[] statements = fileReader.toString().split(";");
for (int i = 0; i < statements.length; i++) {
String statement = statements[i];
for (String statement : statements) {
dbc.executeStatement(statement);
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/github/perscholas/SchoolManagementSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.github.perscholas.dao.StudentDao;
import com.github.perscholas.model.CourseInterface;
import com.github.perscholas.service.CourseService;
import com.github.perscholas.service.StudentService;
import com.github.perscholas.utils.IOConsole;

import java.util.List;
import java.util.stream.Collectors;

public class SchoolManagementSystem implements Runnable {
private static final IOConsole console = new IOConsole();
Expand All @@ -15,7 +18,7 @@ public void run() {
do {
smsDashboardInput = getSchoolManagementSystemDashboardInput();
if ("login".equals(smsDashboardInput)) {
StudentDao studentService = null; // TODO - Instantiate `StudentDao` child
StudentDao studentService = new StudentService(); // Instantiate `StudentDao` child
String studentEmail = console.getStringInput("Enter your email:");
String studentPassword = console.getStringInput("Enter your password:");
Boolean isValidLogin = studentService.validateStudent(studentEmail, studentPassword);
Expand All @@ -26,7 +29,7 @@ public void run() {
studentService.registerStudentToCourse(studentEmail, courseId);
String studentCourseViewInput = getCourseViewInput();
if ("view".equals(studentCourseViewInput)) {
List<CourseInterface> courses = null; // TODO - Instantiate and populate `courses`;
List<CourseInterface> courses = studentService.getStudentCourses(studentEmail); // Instantiate and populate `courses`;
console.println(new StringBuilder()
.append("[ %s ] is registered to the following courses:")
.append("\n\t" + courses)
Expand Down Expand Up @@ -64,7 +67,11 @@ private String getStudentDashboardInput() {


private Integer getCourseRegistryInput() {
List<String> listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds`
//instantiate and populate `listOfCourseIds`
List<String> listOfCoursesIds = new CourseService().getAllCourses()
.stream()
.map(course -> course.getId().toString())
.collect(Collectors.toList());
return console.getIntegerInput(new StringBuilder()
.append("Welcome to the Course Registration Dashboard!")
.append("\nFrom here, you can select any of the following options:")
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/perscholas/dao/StudentDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.List;


/**
* @author leonhunter
* @created 02/12/2020 - 5:55 PM
Expand Down
81 changes: 79 additions & 2 deletions src/main/java/com/github/perscholas/model/Course.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,82 @@
package com.github.perscholas.model;

// TODO - Annotate and Implement respective interface and define behaviors
public class Course {
import javax.persistence.*;
import java.util.List;
import java.util.Objects;

// Annotate and Implement respective interface and define behaviors
@Entity
@TableGenerator(name = "course")

public class Course implements CourseInterface {

@Id
@GeneratedValue
@Column
private Integer id;

@Column
private String instructor;

@Column
private String name;


public Course() {
}

public Course(Integer id, String instructor, String name) {
this.id = id;
this.instructor = instructor;
this.name = name;
}


@Override
public Integer getId() {
return id;
}

@Override
public String getName() {
return name;
}

@Override
public String getInstructor() {
return instructor;
}

@Override
public void setId(Integer id) {
this.id = id;
}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public void setInstructor(String instructor) {
this.instructor = instructor;
}


@Override
public String toString() {
return "[id: " + id +
" name: " + name +
" Instructor: " + instructor + "]";
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Course)) return false;
Course course = (Course) obj;
return Objects.equals(id, course.id) &&
Objects.equals(name, course.name) &&
Objects.equals(instructor, course.instructor);
}
}
Loading