Skip to content
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
47 changes: 47 additions & 0 deletions homework-g596-kupriyanov/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

<artifactId>homework-g596-kupriyanov</artifactId>

<properties>
<spring.boot.version>1.4.2.RELEASE</spring.boot.version>
</properties>

<dependencies>
<dependency>
<groupId>ru.mipt.java2016</groupId>
Expand All @@ -24,6 +28,49 @@
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-base</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>net.sourceforge.jeval</groupId>
<artifactId>jeval</artifactId>
<version>0.9.4</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package ru.mipt.java2016.homework.g596.kupriyanov.task4;

/**
* Created by Artem Kupriyanov on 17/12/2016.
*/

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import ru.mipt.java2016.homework.base.task1.ParsingException;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

@Repository
public class BillingDao {
private static final Logger LOG = LoggerFactory.getLogger(BillingDao.class);

@Autowired
private DataSource dataSource;

private JdbcTemplate jdbcTemplate;

@PostConstruct
public void postConstruct() {
jdbcTemplate = new JdbcTemplate(dataSource, false);
initSchema();
}

public void initSchema() {
LOG.trace("Initializing schema");
jdbcTemplate.execute("CREATE SCHEMA IF NOT EXISTS billing");

// ЗДЕСЬ ДОЛЖНА БЫТЬ ХОРОШАЯ РЕАЛИЗАЦИЯ БАЗЫ ДАННЫХ
// Users table
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.users " +
"(username VARCHAR PRIMARY KEY, password VARCHAR, enabled BOOLEAN)");
jdbcTemplate.update("INSERT INTO billing.users VALUES (\'username\', \'password\', TRUE)");

// Variable table
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.variables " +
"(username VARCHAR, variable VARCHAR, val DOUBLE)");

// Functions table
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.functions " +
"(username VARCHAR, function VARCHAR, arity INTEGER, body VARCHAR)");
}

private Boolean checkNull(String condition) {
LOG.trace("check " + condition);
String allSelect = "SELECT * FROM " + condition;
return jdbcTemplate.queryForObject(
allSelect,
new Object[]{},
new RowMapper<Boolean>() {
@Override
public Boolean mapRow(ResultSet rs, int rowNum) throws SQLException {
Boolean flag = false;
while (true) {
if (!rs.next()) {
break;
}
flag = true;
}
return flag;
}
}
);
}

public BillingUser loadUser(String username) throws EmptyResultDataAccessException {
LOG.trace("Querying for user " + username);
return jdbcTemplate.queryForObject(
"SELECT username, password, enabled FROM billing.users WHERE username = ?",
new Object[]{username},
new RowMapper<BillingUser>() {
@Override
public BillingUser mapRow(ResultSet rs, int rowNum) throws SQLException {
return new BillingUser(
rs.getString("username"),
rs.getString("password"),
rs.getBoolean("enabled")
);
}
}
);
}

public void putUser(String username, String password) {
try {
loadUser(username);
} catch (EmptyResultDataAccessException e) {
jdbcTemplate.execute("INSERT INTO billing.users VALUES (\'" + username + "\', \'" + password + "\', TRUE)");
}
}

public Double getVariable(String username, String variable) {
LOG.trace("Get variable " + username + " variable " + variable);
try {
return jdbcTemplate.queryForObject(
"select val from billing.variables " +
"where username = \'" + username + "\' and variable = \'" + variable + "\'",
new Double[]{},
new RowMapper<Double>() {
@Override
public Double mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Double(
rs.getString("val").toString()
);
}
}
);
} catch (EmptyResultDataAccessException e) {
return 0.0;
}
}

public void putVariable(String username, String variable, Double value) {
LOG.trace("Put variable " + variable + " for user " + username);
try {
deleteVariable(username, variable);
} catch (Exception e) {
e.printStackTrace();
} finally {
jdbcTemplate.execute("INSERT INTO billing.variables VALUES (\'"
+ username + "\', \'" + variable + "\', " + value.toString() + ")");
}
}

public void deleteVariable(String username, String variable) {
LOG.trace("Delete variable " + variable + "for user " + username);
jdbcTemplate.execute("DELETE FROM billing.variables WHERE username = \'"
+ username + "\' AND variable = \'" + variable + "\'");
}

public HashMap<String, String> getAllVariables(String username) throws ParsingException {
LOG.trace("List with all functions for user: " + username);
try {
return jdbcTemplate.queryForObject(
"SELECT username, variable, val FROM billing.variables WHERE username = ?",
new Object[]{username},
new RowMapper<HashMap<String, String>>() {
@Override
public HashMap<String, String> mapRow(ResultSet rs, int rowNum) throws SQLException {
HashMap<String, String> tmp = new HashMap<String, String>();
while (true) {
tmp.put(rs.getString("variable"), Double.toString(rs.getDouble("val")));
if (!rs.next()) {
break;
}
}
return tmp;
}
}
);
} catch (EmptyResultDataAccessException e) {
HashMap<String, String> tmp = new HashMap<String, String>();
return tmp;
}
}

public void putFunction(String username, String function, Integer arity, String body) {
LOG.trace("Put function " + function + " for user " + username);
try {
deleteFunction(username, function);
} catch (Exception e) {
e.printStackTrace();
} finally {
jdbcTemplate.execute("INSERT INTO billing.functions VALUES (\'"
+ username + "\', \'" + function + "\', " + arity.toString() + ", \'"
+ body + "\')");
}
}

public String getFunction(String username, String function) {
LOG.trace("Load function " + function + " for user " + username);
return jdbcTemplate.queryForObject(
"SELECT body FROM billing.functions WHERE username = \'"
+ username + "\' AND function = \'" + function + "\'",
new String[]{},
new RowMapper<String>() {
@Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString("body");
}
}
);
}

public void deleteFunction(String username, String function) {
LOG.info("Delete function " + function + " for user " + username);
jdbcTemplate.execute("DELETE FROM billing.functions WHERE username = \'" +
username + "\' AND function = \'" + function + "\'");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.mipt.java2016.homework.g596.kupriyanov.task4;

/**
* Created by Artem Kupriyanov on 17/12/2016.
*/

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class BillingDatabaseConfiguration {
@Bean
public DataSource billingDataSource(
@Value("${ru.mipt.java2016.homework.g000.lavrentyev.task4.jdbcUrl}") String jdbcUrl,
@Value("${ru.mipt.java2016.homework.g000.lavrentyev.task4.username:}") String username,
@Value("${ru.mipt.java2016.homework.g000.lavrentyev.task4.password:}") String password
) {
HikariConfig config = new HikariConfig();
config.setDriverClassName(org.h2.Driver.class.getName());
config.setJdbcUrl(jdbcUrl);
config.setUsername(username);
config.setPassword(password);
return new HikariDataSource(config);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ru.mipt.java2016.homework.g596.kupriyanov.task4;

/**
* Created by Artem Kupriyanov on 17/12/2016.
*/

public class BillingUser {
private final String username;
private final String password;
private final boolean enabled;

public BillingUser(String username, String password, boolean enabled) {
if (username == null) {
throw new IllegalArgumentException("Null username is not allowed");
}
if (password == null) {
throw new IllegalArgumentException("Null password is not allowed");
}
this.username = username;
this.password = password;
this.enabled = enabled;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public boolean isEnabled() {
return enabled;
}

@Override
public String toString() {
return "BillingUser{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", enabled=" + enabled +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

BillingUser that = (BillingUser) o;

if (enabled != that.enabled) {
return false;
}
if (!username.equals(that.username)) {
return false;
}
return password.equals(that.password);

}

@Override
public int hashCode() {
int result = username.hashCode();
result = 31 * result + password.hashCode();
result = 31 * result + (enabled ? 1 : 0);
return result;
}
}
Loading