[refactor] replaced JDBC implementation of logging with OrmLite

This commit is contained in:
North 2013-03-10 11:05:03 +02:00
parent d44e3f1190
commit 01eee3995c
6 changed files with 119 additions and 314 deletions

View file

@ -1,79 +1,52 @@
package mage.db;
import mage.db.model.Feedback;
import mage.db.model.Log;
import org.apache.log4j.Logger;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import java.io.File;
import java.sql.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import mage.db.model.Feedback;
import mage.db.model.Log;
/**
* @author noxx
* @author noxx, North
*/
public enum EntityManager implements Storage {
instance;
private static final Logger log = Logger.getLogger(EntityManager.class);
private static final String LOG_JDBC_URL = "jdbc:sqlite:db/mage.db";
private static final String FEEDBACK_JDBC_URL = "jdbc:sqlite:db/feedback.db";
private static final String MAGE_JDBC_URL = "jdbc:sqlite:db/mage.db";
private Dao<Log, Object> logDao;
private Dao<Feedback, Object> feedbackDao;
private static final String MAGE_JDBC_URL_FEEDBACK_DB = "jdbc:sqlite:db/feedback.db";
private static String QUERY_SAVE_LOG = "insert into logs values (?, ?, ?, ?, ?, ?, ?, ?)";
private static String QUERY_GET_ALL_LOGS = "select * from logs";
private static String QUERY_SAVE_FEEDBACK = "insert into feedbacks values (?, ?, ?, ?, ?, ?, ?, ?)";
private static String QUERY_GET_ALL_FEEDBACKS = "select * from feedbacks";
static {
private EntityManager() {
File file = new File("db");
if (!file.exists()) {
file.mkdirs();
}
try {
init();
initFeedbackDB();
} catch (Exception e) {
log.fatal(e);
e.printStackTrace();
ConnectionSource logConnectionSource = new JdbcConnectionSource(LOG_JDBC_URL);
TableUtils.createTableIfNotExists(logConnectionSource, Log.class);
logDao = DaoManager.createDao(logConnectionSource, Log.class);
ConnectionSource feedbackConnectionSource = new JdbcConnectionSource(FEEDBACK_JDBC_URL);
TableUtils.createTableIfNotExists(feedbackConnectionSource, Feedback.class);
feedbackDao = DaoManager.createDao(feedbackConnectionSource, Feedback.class);
} catch (SQLException ex) {
}
}
public static EntityManager getInstance() {
return instance;
}
/**
* Inserts log entry to DB.
*
* @param key
* @param date
* @param args
* @throws Exception
*/
@Override
public void insertLog(String key, java.util.Date date, String... args) throws SQLException {
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
try {
PreparedStatement prep = conn.prepareStatement(QUERY_SAVE_LOG);
prep.setString(1, key);
prep.setDate(2, new java.sql.Date(date.getTime()));
int index = 3;
for (String arg : args) {
if (index > 8) break;
prep.setString(index++, arg);
}
prep.execute();
} finally {
try {
if (conn != null) conn.close();
} catch (Exception e) {
// swallow
}
}
Log logEntity = new Log(key, date);
logEntity.setArguments(args);
logDao.create(logEntity);
}
/**
@ -83,256 +56,27 @@ public enum EntityManager implements Storage {
@Override
public List<Log> getAllLogs() {
List<Log> logs = new ArrayList<Log>();
try {
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
try {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(QUERY_GET_ALL_LOGS);
while (rs.next()) {
Log log = new Log(rs.getString(1), rs.getDate(2));
List<String> args = new ArrayList<String>();
for (int index = 0; index < 6; index++) {
String arg = rs.getString(3 + index);
if (arg == null) {
break;
}
args.add(arg);
}
log.setArguments(args);
logs.add(log);
}
rs.close();
} finally {
try {
if (conn != null) conn.close();
} catch (Exception e) {
// swallow
}
}
} catch (SQLException e) {
log.fatal("SQL Exception: ", e);
logs = logDao.queryForAll();
} catch (SQLException ex) {
}
return logs;
}
/**
* Inserts feedback entry to DB.
*
*
* @param username
* @param title
* @param type
* @param message
* @param email
* @param host
* @param created
* @throws SQLException
*/
@Override
public void insertFeedback(String username, String title, String type, String message, String email, String host, java.util.Date created) throws SQLException {
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL_FEEDBACK_DB);
try {
PreparedStatement prep = conn.prepareStatement(QUERY_SAVE_FEEDBACK);
prep.setString(1, username);
prep.setString(2, title);
prep.setString(3, type);
prep.setString(4, message);
prep.setString(5, email);
prep.setString(6, host);
prep.setDate(7, new java.sql.Date(created.getTime()));
prep.setString(8, "new");
prep.execute();
} finally {
try {
if (conn != null) conn.close();
} catch (Exception e) {
// swallow
}
}
Feedback feedback = new Feedback(username, title, type, message, email, host, created, "new");
feedbackDao.create(feedback);
}
/**
* Get all feedbacks
* @return
*/
@Override
public List<Feedback> getAllFeedbacks() {
List<Feedback> feedbacks = new ArrayList<Feedback>();
try {
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL_FEEDBACK_DB);
try {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(QUERY_GET_ALL_FEEDBACKS);
while (rs.next()) {
Feedback feedback = new Feedback();
feedback.setUsername(rs.getString(1));
feedback.setTitle(rs.getString(2));
feedback.setType(rs.getString(3));
feedback.setMessage(rs.getString(4));
feedback.setEmail(rs.getString(5));
feedback.setHost(rs.getString(6));
feedback.setCreatedDate(rs.getDate(7));
feedback.setStatus(rs.getString(8));
feedbacks.add(feedback);
feedbacks = feedbackDao.queryForAll();
} catch (SQLException ex) {
}
rs.close();
} finally {
try {
if (conn != null) conn.close();
} catch (Exception e) {
// swallow
}
}
} catch (SQLException e) {
log.fatal("SQL Exception: ", e);
}
return feedbacks;
}
/**
* Inits database. Creates tables if they don't exist.
*
* @throws Exception
*/
protected static void init() throws Exception {
Class.forName("org.sqlite.JDBC");
checkDBFolderExistance();
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
try {
Statement stat = conn.createStatement();
stat.executeUpdate("create table if not exists users (login, password, status);");
stat.executeUpdate("create table if not exists logs (key, created_dt, arg0, arg1, arg2, arg3, arg4, arg5);");
} finally {
try {
conn.close();
} catch (Exception e) {
// swallow
}
}
}
protected static void initFeedbackDB() throws Exception {
Class.forName("org.sqlite.JDBC");
checkDBFolderExistance();
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL_FEEDBACK_DB);
try {
Statement stat = conn.createStatement();
stat.executeUpdate("create table if not exists feedbacks (username, title, type, message, email, host, created_dt, status);");
} finally {
try {
conn.close();
} catch (Exception e) {
// swallow
}
}
}
/**
* Reinits database. Drops all tables and then creates them from scratch.
* BE CAREFUL! THIS METHOD WILL DESTROY ALL DATA.
*
* @throws Exception
*/
public static void reinit() throws Exception {
Class.forName("org.sqlite.JDBC");
checkDBFolderExistance();
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
try {
Statement stat = conn.createStatement();
stat.executeUpdate("drop table users;");
init();
} finally {
try {
conn.close();
} catch (Exception e) {
// swallow
}
}
}
/**
* Creates folders for Mage sqlite db.
*/
protected static void checkDBFolderExistance() {
File file = new File("db");
if (!file.exists()) {
file.mkdirs();
}
}
/**
* Tests DB with base operations.
*
* @throws Exception
*/
public void testDB() throws Exception {
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
try {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from users where login = 'testtest';");
if (rs.next()) {
checkTestUser(conn, stat);
} else {
log.debug("[DBTest] creating test user...");
PreparedStatement prep = conn.prepareStatement("insert into users values (?, ?, ?);");
prep.setString(1, "testtest");
prep.setString(2, "12345");
prep.setString(3, "disabled");
prep.execute();
log.debug("[DBTest] creating test user [OK]");
checkTestUser(conn, stat);
}
rs.close();
} finally {
try {
if (conn != null) conn.close();
} catch (Exception e) {
// swallow
}
}
}
/**
* Checks test user existence and its parameters.
*
* @param conn
* @param stat
* @throws Exception
*/
private void checkTestUser(Connection conn, Statement stat) throws Exception {
ResultSet rs = stat.executeQuery("select * from users where login = 'testtest';");
if (rs.next()) {
log.debug("[DBTest] checking test user [OK]");
if (rs.getString("login").equals("testtest") && rs.getString("password").equals("12345") && rs.getString("status").equals("disabled")) {
log.debug("[DBTest] checking test user parameters [OK]");
} else {
log.debug("[DBTest] checking test user parameters [ERROR]");
}
} else {
log.debug("[DBTest] checking test user [ERROR]");
log.debug("[DBTest] couldn't find test user");
}
}
public static void main(String[] args) throws Exception {
//EntityManager.getInstance().reinit();
EntityManager.getInstance().testDB();
}
}

View file

@ -15,7 +15,6 @@ public class EntityManagerTest {
private static DateFormat timeFormatter = SimpleDateFormat.getTimeInstance(SimpleDateFormat.FULL);
public static void main(String[] args) throws Exception {
EntityManager.instance.testDB();
List<Log> logs = EntityManager.instance.getAllLogs();
System.out.println("logs found: " + logs.size());
for (Log log : logs) {

View file

@ -10,7 +10,6 @@ import java.util.*;
public class Statistics {
public static void main(String[] args) throws Exception {
EntityManager.instance.testDB();
List<Log> logs = EntityManager.instance.getAllLogs();
System.out.println("logs found: " + logs.size());

View file

@ -1,28 +1,48 @@
package mage.db.model;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.util.Date;
/**
* Feedback entity.
*
* @author noxx
* @author noxx, North
*/
@DatabaseTable(tableName = "feedbacks")
public class Feedback {
@DatabaseField
private String username;
@DatabaseField
private String title;
@DatabaseField
private String type;
@DatabaseField
private String message;
@DatabaseField
private String email;
@DatabaseField
private String host;
@DatabaseField(columnName = "created_dt")
private Date createdDate;
@DatabaseField
private String status;
public Feedback() {
}
public Feedback(String username, String title, String type, String message, String email, String host, Date createdDate, String status) {
this.username = username;
this.title = title;
this.type = type;
this.message = message;
this.email = email;
this.host = host;
this.createdDate = createdDate;
this.status = status;
}
public String getUsername() {
return username;
}

View file

@ -1,18 +1,36 @@
package mage.db.model;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author noxx
* @author noxx, North
*/
@DatabaseTable(tableName = "logs")
public class Log {
@DatabaseField
private String key;
@DatabaseField(columnName = "created_dt")
private Date createdDate;
@DatabaseField
private String arg0;
@DatabaseField
private String arg1;
@DatabaseField
private String arg2;
@DatabaseField
private String arg3;
@DatabaseField
private String arg4;
@DatabaseField
private String arg5;
private List<String> arguments;
public Log() {
}
public Log(String key, Date createdDate) {
this.key = key;
@ -36,10 +54,46 @@ public class Log {
}
public List<String> getArguments() {
ArrayList<String> arguments = new ArrayList<String>();
if (arg0 != null) {
arguments.add(arg0);
}
if (arg1 != null) {
arguments.add(arg1);
}
if (arg2 != null) {
arguments.add(arg2);
}
if (arg3 != null) {
arguments.add(arg3);
}
if (arg4 != null) {
arguments.add(arg4);
}
if (arg5 != null) {
arguments.add(arg5);
}
return arguments;
}
public void setArguments(List<String> arguments) {
this.arguments = arguments;
public void setArguments(String... arguments) {
if (arguments.length > 0) {
arg0 = arguments[0];
}
if (arguments.length > 1) {
arg1 = arguments[1];
}
if (arguments.length > 2) {
arg2 = arguments[2];
}
if (arguments.length > 3) {
arg3 = arguments[3];
}
if (arguments.length > 4) {
arg4 = arguments[4];
}
if (arguments.length > 5) {
arg5 = arguments[5];
}
}
}

View file

@ -1,11 +0,0 @@
package mage.db.model;
/**
* @author noxx
*/
public class User {
private String login;
private String password;
}