|
|
|
|
@@ -9,9 +9,14 @@ import static de.srsoftware.umbrella.core.constants.Constants.TABLE_SETTINGS;
|
|
|
|
|
import static de.srsoftware.umbrella.core.constants.Field.*;
|
|
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
|
|
|
|
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
|
|
|
|
import static de.srsoftware.umbrella.message.Constants.*;
|
|
|
|
|
import static de.srsoftware.umbrella.message.model.Schedule.schedule;
|
|
|
|
|
import static java.text.MessageFormat.format;
|
|
|
|
|
import static java.time.ZoneOffset.UTC;
|
|
|
|
|
|
|
|
|
|
import de.srsoftware.tools.jdbc.Query;
|
|
|
|
|
import de.srsoftware.umbrella.core.BaseDb;
|
|
|
|
|
import de.srsoftware.umbrella.core.constants.Field;
|
|
|
|
|
import de.srsoftware.umbrella.core.constants.Text;
|
|
|
|
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
|
|
|
|
import de.srsoftware.umbrella.core.model.Envelope;
|
|
|
|
|
@@ -22,28 +27,73 @@ import de.srsoftware.umbrella.message.model.Instantly;
|
|
|
|
|
import de.srsoftware.umbrella.message.model.Settings;
|
|
|
|
|
import de.srsoftware.umbrella.message.model.Silent;
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.time.ZoneOffset;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
|
|
public class SqliteMessageDb implements MessageDb, MessageQueue<TranslatedMessage> {
|
|
|
|
|
public class SqliteMessageDb extends BaseDb implements MessageDb, MessageQueue<TranslatedMessage> {
|
|
|
|
|
private static final System.Logger LOG = System.getLogger(SqliteMessageDb.class.getSimpleName());
|
|
|
|
|
private final Connection db;
|
|
|
|
|
private static final String DB_VERSION = "message_db_version";
|
|
|
|
|
private static final int INITIAL_DB_VERSION = 1;
|
|
|
|
|
private static final String TABLE_SUBMISSIONS = "message_submission";
|
|
|
|
|
|
|
|
|
|
public SqliteMessageDb(Connection conn){
|
|
|
|
|
db = conn;
|
|
|
|
|
init();
|
|
|
|
|
super(conn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createMessageTables() {
|
|
|
|
|
var sql = """
|
|
|
|
|
CREATE TABLE IF NOT EXISTS {0} (
|
|
|
|
|
{1} INTEGER PRIMARY KEY,
|
|
|
|
|
{2} LONG NOT NULL,
|
|
|
|
|
{3} LONG NOT NULL,
|
|
|
|
|
{4} TEXT,
|
|
|
|
|
{5} TEXT
|
|
|
|
|
);
|
|
|
|
|
""";
|
|
|
|
|
sql = format(sql,TABLE_MESSAGES, ID, TIMESTAMP, SENDER_USER_ID, SUBJECT, BODY);
|
|
|
|
|
try {
|
|
|
|
|
db.prepareStatement(sql).execute();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw failedToCreateTable(TABLE_MESSAGES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sql = """
|
|
|
|
|
CREATE TABLE IF NOT EXISTS {0} (
|
|
|
|
|
{1} INTEGER NOT NULL,
|
|
|
|
|
{2} VARCHAR(255) NOT NULL,
|
|
|
|
|
{3} VARCHAR(255),
|
|
|
|
|
PRIMARY KEY ({1}, {2})
|
|
|
|
|
);
|
|
|
|
|
""";
|
|
|
|
|
sql = format(sql,TABLE_RECEIVERS, MESSAGE_ID, EMAIL, NAME);
|
|
|
|
|
try {
|
|
|
|
|
db.prepareStatement(sql).execute();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw failedToCreateTable(TABLE_MESSAGES);
|
|
|
|
|
}
|
|
|
|
|
sql = """
|
|
|
|
|
CREATE TABLE IF NOT EXISTS {0} (
|
|
|
|
|
{1} INTEGER NOT NULL,
|
|
|
|
|
{2} VARCHAR(255) NOT NULL,
|
|
|
|
|
{3} VARCHAR(100),
|
|
|
|
|
{4} BLOB NOT NULL,
|
|
|
|
|
PRIMARY KEY ({1}, {2})
|
|
|
|
|
);
|
|
|
|
|
""";
|
|
|
|
|
sql = format(sql,TABLE_ATTACHMENTS, MESSAGE_ID, NAME, MIME, DATA);
|
|
|
|
|
try {
|
|
|
|
|
db.prepareStatement(sql).execute();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw failedToCreateTable(TABLE_MESSAGES);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createSubmissionTable() {
|
|
|
|
|
var createTable = """
|
|
|
|
|
CREATE TABLE IF NOT EXISTS {0} ( {1} Integer PRIMARY KEY, {2} VARCHAR(255) NOT NULL);
|
|
|
|
|
CREATE TABLE IF NOT EXISTS {0} ( {1} INTEGER PRIMARY KEY, {2} VARCHAR(255) NOT NULL);
|
|
|
|
|
""";
|
|
|
|
|
try {
|
|
|
|
|
var stmt = db.prepareStatement(format(createTable,TABLE_SUBMISSIONS, USER_ID, VALUE));
|
|
|
|
|
@@ -54,37 +104,16 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} Integer PRIMARY KEY, {2} VARCHAR(255) NOT N
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int createSettingsTable() {
|
|
|
|
|
var createTable = """
|
|
|
|
|
CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255) NOT NULL);
|
|
|
|
|
""";
|
|
|
|
|
try {
|
|
|
|
|
var stmt = db.prepareStatement(format(createTable,TABLE_SETTINGS, KEY, VALUE));
|
|
|
|
|
stmt.execute();
|
|
|
|
|
stmt.close();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw failedToCreateTable(TABLE_SETTINGS).causedBy(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Integer version = null;
|
|
|
|
|
try {
|
|
|
|
|
var rs = select(VALUE).from(TABLE_SETTINGS).where(KEY, equal(DB_VERSION)).exec(db);
|
|
|
|
|
if (rs.next()) version = rs.getInt(VALUE);
|
|
|
|
|
rs.close();
|
|
|
|
|
if (version == null) {
|
|
|
|
|
version = INITIAL_DB_VERSION;
|
|
|
|
|
insertInto(TABLE_SETTINGS, KEY, VALUE).values(DB_VERSION,version).execute(db).close();
|
|
|
|
|
}
|
|
|
|
|
return version;
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw databaseException(FAILED_TO_UPDATE_OBJECT, OBJECT,DB_VERSION).causedBy(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int createTables() {
|
|
|
|
|
@Override
|
|
|
|
|
protected int createTables() {
|
|
|
|
|
int currentVersion = createSettingsTable();
|
|
|
|
|
switch (currentVersion){
|
|
|
|
|
case 0:
|
|
|
|
|
createSubmissionTable();
|
|
|
|
|
return createSettingsTable();
|
|
|
|
|
case 1:
|
|
|
|
|
createMessageTables();
|
|
|
|
|
}
|
|
|
|
|
return setCurrentVersion(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@@ -132,8 +161,28 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void push(Envelope<TranslatedMessage> message) {
|
|
|
|
|
throw new UmbrellaException(HTTP_SERVER_ERROR,"{class}.push(message) not implemented!","class",getClass().getSimpleName()); // TODO
|
|
|
|
|
public void push(Envelope<TranslatedMessage> envelope) {
|
|
|
|
|
var timestamp = envelope.time().toEpochSecond(UTC);
|
|
|
|
|
var message = envelope.message();
|
|
|
|
|
var sender = message.sender().id();
|
|
|
|
|
var subject = message.subject();
|
|
|
|
|
var body = message.body();
|
|
|
|
|
try {
|
|
|
|
|
var rs = insertInto(TABLE_MESSAGES, TIMESTAMP, SENDER_USER_ID, SUBJECT, BODY).values(timestamp, sender, subject, body).execute(db).getGeneratedKeys();
|
|
|
|
|
Long messageId = null;
|
|
|
|
|
if (rs.next()) messageId = rs.getLong(1);
|
|
|
|
|
rs.close();
|
|
|
|
|
if (messageId == null) throw failedToStoreObject(envelope);
|
|
|
|
|
|
|
|
|
|
for (var receiver : envelope.receivers()){
|
|
|
|
|
insertInto(TABLE_RECEIVERS,MESSAGE_ID,EMAIL,NAME).values(messageId,receiver.email(),receiver.name()).execute(db).close();
|
|
|
|
|
}
|
|
|
|
|
for (var attachment : envelope.message().attachments()){
|
|
|
|
|
insertInto(TABLE_ATTACHMENTS,MESSAGE_ID,NAME,MIME,DATA).values(messageId,attachment.name(),attachment.mime(),attachment.content()).execute(db).close();
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw failedToStoreObject(envelope).causedBy(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Settings toSettings(ResultSet rs) throws SQLException {
|
|
|
|
|
|