Implemented sending messages (and fixed a few bugs on the way)
This closes issue #3
This commit is contained in:
@ -21,6 +21,7 @@ import ch.dissem.bitmessage.entity.BitmessageAddress;
|
||||
import ch.dissem.bitmessage.entity.Plaintext;
|
||||
import ch.dissem.bitmessage.entity.valueobject.Label;
|
||||
import ch.dissem.bitmessage.ports.MessageRepository;
|
||||
import ch.dissem.bitmessage.utils.Strings;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -37,13 +38,41 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
|
||||
private InternalContext ctx;
|
||||
|
||||
@Override
|
||||
public List<String> getLabels() {
|
||||
List<String> result = new LinkedList<>();
|
||||
public List<Label> getLabels() {
|
||||
List<Label> result = new LinkedList<>();
|
||||
try {
|
||||
Statement stmt = getConnection().createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT label FROM Label ORDER BY ord");
|
||||
ResultSet rs = stmt.executeQuery("SELECT id, label, type, color FROM Label ORDER BY ord");
|
||||
while (rs.next()) {
|
||||
result.add(rs.getString("label"));
|
||||
result.add(getLabel(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Label getLabel(ResultSet rs) throws SQLException {
|
||||
String typeName = rs.getString("type");
|
||||
Label.Type type = null;
|
||||
if (typeName != null) {
|
||||
type = Label.Type.valueOf(typeName);
|
||||
}
|
||||
Label label = new Label(rs.getString("label"), type, rs.getInt("color"));
|
||||
label.setId(rs.getLong("id"));
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Label> getLabels(Label.Type... types) {
|
||||
List<Label> result = new LinkedList<>();
|
||||
try {
|
||||
Statement stmt = getConnection().createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT id, label, type, color FROM Label WHERE type IN (" + Strings.join(types) +
|
||||
") ORDER BY ord");
|
||||
while (rs.next()) {
|
||||
result.add(getLabel(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
@ -94,9 +123,9 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
|
||||
List<Label> result = new ArrayList<>();
|
||||
try {
|
||||
Statement stmt = getConnection().createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT label, color FROM Label WHERE id IN (SELECT label_id FROM Message_Label WHERE message_id=" + messageId + ")");
|
||||
ResultSet rs = stmt.executeQuery("SELECT id, label, type, color FROM Label WHERE id IN (SELECT label_id FROM Message_Label WHERE message_id=" + messageId + ")");
|
||||
while (rs.next()) {
|
||||
result.add(new Label(rs.getString("label"), rs.getInt("color")));
|
||||
result.add(getLabel(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
|
@ -1,21 +1,23 @@
|
||||
CREATE TABLE Message (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
sender VARCHAR(40) NOT NULL,
|
||||
recipient VARCHAR(40) NOT NULL,
|
||||
data BLOB NOT NULL,
|
||||
sent BIGINT,
|
||||
received BIGINT,
|
||||
status VARCHAR(20) NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
sender VARCHAR(40) NOT NULL,
|
||||
recipient VARCHAR(40) NOT NULL,
|
||||
data BLOB NOT NULL,
|
||||
sent BIGINT,
|
||||
received BIGINT,
|
||||
status VARCHAR(20) NOT NULL,
|
||||
|
||||
FOREIGN KEY (sender) REFERENCES Address (address),
|
||||
FOREIGN KEY (recipient) REFERENCES Address (address)
|
||||
);
|
||||
|
||||
CREATE TABLE Label (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
label VARCHAR(255) NOT NULL,
|
||||
color INT,
|
||||
ord BIGINT,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
label VARCHAR(255) NOT NULL,
|
||||
type VARCHAR(20),
|
||||
color INT NOT NULL DEFAULT X'FF000000',
|
||||
ord BIGINT,
|
||||
|
||||
CONSTRAINT UC_label UNIQUE (label),
|
||||
CONSTRAINT UC_order UNIQUE (ord)
|
||||
);
|
||||
@ -29,7 +31,8 @@ CREATE TABLE Message_Label (
|
||||
FOREIGN KEY (label_id) REFERENCES Label (id)
|
||||
);
|
||||
|
||||
INSERT INTO Label(label, ord) VALUES ('Inbox', 0);
|
||||
INSERT INTO Label(label, ord) VALUES ('Sent', 10);
|
||||
INSERT INTO Label(label, ord) VALUES ('Drafts', 20);
|
||||
INSERT INTO Label(label, ord) VALUES ('Trash', 100);
|
||||
INSERT INTO Label(label, type, color, ord) VALUES ('Inbox', 'INBOX', X'FF0000FF', 0);
|
||||
INSERT INTO Label(label, type, color, ord) VALUES ('Drafts', 'DRAFTS', X'FFFF9900', 10);
|
||||
INSERT INTO Label(label, type, color, ord) VALUES ('Sent', 'SENT', X'FFFFFF00', 20);
|
||||
INSERT INTO Label(label, type, ord) VALUES ('Unread', 'UNREAD', 90);
|
||||
INSERT INTO Label(label, type, ord) VALUES ('Trash', 'TRASH', 100);
|
||||
|
Reference in New Issue
Block a user