Skip to content
This repository was archived by the owner on Jun 11, 2022. It is now read-only.
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*.iml
/webapp/target
/server/target
server/src/main/resources/database.trace.db
server/src/main/resources/database.h2.db
1,245 changes: 1,245 additions & 0 deletions sakila-min.sql

Large diffs are not rendered by default.

48,588 changes: 0 additions & 48,588 deletions sakila-script.sql

This file was deleted.

5 changes: 5 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>

<build>
Expand Down
34 changes: 27 additions & 7 deletions server/src/main/java/com/balinski/api_project/Main.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
package com.balinski.api_project;
import com.balinski.api_project.database.DatabaseProxy;

import com.balinski.api_project.database.DaoManager;

import com.balinski.api_project.database.dao.DaoException;
import com.balinski.api_project.database.model.Film;
import com.balinski.api_project.server.JettyServer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


public class Main {

public static void testDB() throws SQLException {
public static void main(String[] args) {
testDatabase();
// runApplication();
}

static void runApplication() {
try {
JettyServer.start(8080);
} catch (Exception e) {
System.err.println("A critical error occurred when trying to run the server.");
System.err.println("The application will be stopped.");
System.exit(-1);
}
}

public static void main(String[] args) throws Exception {
testDB();
JettyServer.start(8080);
static void testDatabase() {
try {
List<Film> list = new DaoManager().getFilmDao().getAvailableInLanguage("jaPANESE");
for(var actor : list)
System.out.println(actor.getTitle());
} catch (DaoException e) {
e.printStackTrace();
}
}
}
125 changes: 101 additions & 24 deletions server/src/main/java/com/balinski/api_project/database/DaoManager.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,115 @@
package com.balinski.api_project.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import com.balinski.api_project.database.dao.ActorDao;
import com.balinski.api_project.database.dao.DaoException;
import com.balinski.api_project.database.dao.FilmDao;
import com.balinski.api_project.database.dao.LanguageDao;
import com.balinski.api_project.util.FilePropertiesLoader;
import com.balinski.api_project.util.SqlExceptionPrinter;

import java.io.IOException;
import java.sql.*;
import java.util.*;

public class DaoManager {
Connection connection;
protected DataSourceWrapper dataSource;

public DaoManager() throws DaoException {
try {
initDataSource("server/src/main/resources/database.properties");
} catch (IOException e) {
throw new DaoException(e);
}
}

public ActorDao getActorDao() {
return new ActorDao(this, false);
}

public FilmDao getFilmDao() {
return new FilmDao(this, false);
}

public LanguageDao getLanguageDao() {
return new LanguageDao(this, false);
}

public Set<Properties> query(String sql, String[] selectedFields) {
try(Connection connection = DatabaseProxy.getDatabaseConnection()) {
public ActorDao getActorDao(boolean transaction) {
return new ActorDao(this, transaction);
}

public FilmDao getFilmDao(boolean transaction) {
return new FilmDao(this, transaction);
}

public LanguageDao getLanguageDao (boolean transaction) {
return new LanguageDao(this, transaction);
}

public List<Map<String, Object>> queryGetData(String sql) {
List<Map<String, Object>> data = null;

try(Connection connection = getConnection()) {
try(Statement statement = connection.createStatement()) {
try(ResultSet result = statement.executeQuery(sql)) {
Set<Properties> data = new HashSet<>();
Properties record = new Properties();

while(result.next()) {
for(var field : selectedFields) {
record.put(field, result.getString(field));
}
data.add(record);
record.clear();
}
try(ResultSet rs = statement.executeQuery(sql)) {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
data = new LinkedList<>();

while(rs.next()) {
Map<String, Object> row = new HashMap<>(columns);

for(int i = 1; i <= columns; i++)
row.put(md.getColumnName(i), rs.getObject(i));

return data;
data.add(row);
}
}
}
} catch (SQLException e) {
DatabaseProxy.handleSqlException(e, "An error occurred when trying to query the database");
SqlExceptionPrinter.print("An error occurred when trying to query the database", e);
} finally {
closeConnection();
}

return null;
return data;
}

public int queryModify(String sql, boolean transaction) {
int rowsAffected = 0;

try(Connection connection = getConnection()) {
if(transaction)
connection.setAutoCommit(false);

try(Statement statement = connection.createStatement()) {
rowsAffected = statement.executeUpdate(sql);
}

if(transaction) {
connection.setAutoCommit(true);
connection.commit();
}
} catch (SQLException e) {
SqlExceptionPrinter.print("An error occurred when trying to query the database", e);
} finally {
closeConnection();
}

return rowsAffected;
}

public Connection getConnection() {
return dataSource.getConnection();
}

protected void closeConnection() {
dataSource.closeConnection();
}

protected void initDataSource(String path) throws IOException {
Properties dbProps = FilePropertiesLoader.load(path);
this.dataSource = new DataSourceWrapper(dbProps);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.balinski.api_project.database;

import com.balinski.api_project.util.SqlExceptionPrinter;
import org.apache.commons.dbcp2.BasicDataSource;

import java.sql.*;
import java.util.*;
import java.util.stream.Stream;

public class DataSourceWrapper {
private BasicDataSource dataSource;
private Connection connection;
private String url;
private String username;
private String password;

public DataSourceWrapper(Properties props) {
try {
loadDatabaseProperties(props);
initDataSource();
} catch (Exception e) {
System.err.println("Cannot initialize database data source: " + e.getMessage());
}
}

public Connection getConnection() {
try {
this.connection = dataSource.getConnection();
} catch (SQLException e) {
SqlExceptionPrinter.print("Could not obtain an instance of connection from given data source.", e);
}

return connection;
}

public void closeConnection() {
if(connection == null)
return;

try {
this.connection.close();
} catch (SQLException e) {
SqlExceptionPrinter.print("Cannot close the connection.", e);
}
}

private void initDataSource() {
dataSource = new BasicDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setMinIdle(5);
dataSource.setMaxIdle(10);
dataSource.setMaxOpenPreparedStatements(100);
}

private void loadDatabaseProperties(Properties props) throws NullPointerException, ClassNotFoundException {
url = props.getProperty("url");
String driver = props.getProperty("driver");
username = props.getProperty("username");
password = props.getProperty("password");

if(Stream.of(url, driver, username, password).anyMatch(Objects::isNull)) {
throw new NullPointerException("One or more of properties (url, driver, username, password) is missing");
}

try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException("Driver class " + driver + " not found.", e);
}
}
}

This file was deleted.

Loading