This commit is contained in:
Paul Schaub 2018-09-16 12:41:10 +02:00
parent bb7fcee47a
commit 177d2c0cc9
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
9 changed files with 229 additions and 36 deletions

View file

@ -7,6 +7,9 @@ import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils; import com.j256.ormlite.table.TableUtils;
import de.vanitasvitae.fasel.db.AbstractDatabase; import de.vanitasvitae.fasel.db.AbstractDatabase;
import de.vanitasvitae.fasel.db.entity.Account; import de.vanitasvitae.fasel.db.entity.Account;
import de.vanitasvitae.fasel.db.entity.Contact;
import de.vanitasvitae.fasel.db.entity.XmppEntity;
import de.vanitasvitae.fasel.db.java.JavaAccountDatabase;
public class FaselDBTest { public class FaselDBTest {
@ -19,27 +22,15 @@ public class FaselDBTest {
} }
public FaselDBTest() throws SQLException { public FaselDBTest() throws SQLException {
database = new AbstractDatabase() { database = new JavaAccountDatabase();
@Override
public ConnectionSource getConnectionSource() throws SQLException {
ConnectionSource connectionSource =
new JdbcConnectionSource("jdbc:h2:~/.local/share/fasel/db/fasel");
return connectionSource;
}
};
TableUtils.createTableIfNotExists(database.getConnectionSource(), Account.class);
} }
public void insert() throws SQLException { public void insert() throws SQLException {
XmppEntity entity = new XmppEntity();
String username = "alice@wonderland.lit"; entity.setJid("hans@vader.sw");
String id = "1234"; Contact contact = new Contact(entity);
contact.setNickname("Hans Vader");
String password = "swordfish"; database.getContactsDao().create(contact);
Account account = new Account(id, username, password);
database.getAccountsDao().create(account);
} }
public void get() throws SQLException { public void get() throws SQLException {

View file

@ -2,17 +2,41 @@ package de.vanitasvitae.fasel.db;
import java.sql.SQLException; import java.sql.SQLException;
import javax.swing.plaf.TableUI;
import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager; import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import de.vanitasvitae.fasel.db.entity.Account; import de.vanitasvitae.fasel.db.entity.Account;
import de.vanitasvitae.fasel.db.entity.BaseMessage;
import de.vanitasvitae.fasel.db.entity.Contact; import de.vanitasvitae.fasel.db.entity.Contact;
import de.vanitasvitae.fasel.db.entity.TextMessage;
import de.vanitasvitae.fasel.db.entity.XmppEntity; import de.vanitasvitae.fasel.db.entity.XmppEntity;
public abstract class AbstractDatabase { public abstract class AbstractDatabase {
private final ConnectionSource connectionSource;
public abstract ConnectionSource getConnectionSource() throws SQLException; public abstract ConnectionSource getConnectionSource() throws SQLException;
public AbstractDatabase() throws SQLException {
connectionSource = getConnectionSource();
setupTables();
}
private void setupTables() throws SQLException {
createTableIfNotExists(Account.class);
createTableIfNotExists(XmppEntity.class);
createTableIfNotExists(Contact.class);
createTableIfNotExists(BaseMessage.class);
createTableIfNotExists(TextMessage.class);
}
private void createTableIfNotExists(Class<?> klass) throws SQLException {
TableUtils.createTableIfNotExists(connectionSource, klass);
}
public Dao<Account, String> getAccountsDao() throws SQLException { public Dao<Account, String> getAccountsDao() throws SQLException {
return DaoManager.createDao(getConnectionSource(), Account.class); return DaoManager.createDao(getConnectionSource(), Account.class);
} }

View file

@ -9,7 +9,8 @@ import de.vanitasvitae.fasel.db.entity.XmppEntity;
public class XmppEntityDaoImpl extends BaseDaoImpl<XmppEntity, String> implements XmppEntityDao { public class XmppEntityDaoImpl extends BaseDaoImpl<XmppEntity, String> implements XmppEntityDao {
protected XmppEntityDaoImpl(ConnectionSource connectionSource) throws SQLException { public XmppEntityDaoImpl(ConnectionSource connectionSource)
throws SQLException {
super(connectionSource, XmppEntity.class); super(connectionSource, XmppEntity.class);
} }
} }

View file

@ -8,32 +8,32 @@ import de.vanitasvitae.fasel.db.dao.impl.AccountDaoImpl;
@DatabaseTable(tableName = "accounts", daoClass = AccountDaoImpl.class) @DatabaseTable(tableName = "accounts", daoClass = AccountDaoImpl.class)
public class Account { public class Account {
private String TAG = "fasel_db"; @DatabaseField(generatedId = true, dataType = DataType.INTEGER)
private int accountId;
@DatabaseField(id = true, dataType = DataType.STRING) @DatabaseField(canBeNull = false, unique = true, dataType = DataType.STRING)
private String accountId;
@DatabaseField(canBeNull = false, dataType = DataType.STRING)
private String jid; private String jid;
@DatabaseField(canBeNull = false, dataType = DataType.STRING) @DatabaseField(canBeNull = false, dataType = DataType.STRING)
private String password; private String password;
/**
* Empty account for ORMLite.
*/
public Account() { public Account() {
} }
public Account(String accountId, String jid, String password) { public Account(String jid, String password) {
this.accountId = accountId;
this.jid = jid; this.jid = jid;
this.password = password; this.password = password;
} }
public String getAccountId() { public int getAccountId() {
return accountId; return accountId;
} }
public void setAccountId(String accountId) { public void setAccountId(int accountId) {
this.accountId = accountId; this.accountId = accountId;
} }

View file

@ -9,6 +9,9 @@ import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "messages") @DatabaseTable(tableName = "messages")
public class BaseMessage { public class BaseMessage {
/**
* Empty constructor for ORMLite.
*/
public BaseMessage() { public BaseMessage() {
} }
@ -22,9 +25,49 @@ public class BaseMessage {
@DatabaseField(columnName = "accountId", foreign = true) @DatabaseField(columnName = "accountId", foreign = true)
private Account account; private Account account;
@DatabaseField(columnName = "sendDate", dataType = DataType.DATE) @DatabaseField(columnName = "sentDate", dataType = DataType.DATE)
private Date sendDate; private Date sentDate;
@DatabaseField(columnName = "receiveDate", dataType = DataType.DATE) @DatabaseField(columnName = "receiveDate", dataType = DataType.DATE)
private Date receiveDate; private Date receiveDate;
public String getMessageId() {
return messageId;
}
public void setMessageId(String messageId) {
this.messageId = messageId;
}
public String getSenderJid() {
return senderJid;
}
public void setSenderJid(String senderJid) {
this.senderJid = senderJid;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public Date getSentDate() {
return sentDate;
}
public void setSentDate(Date sentDate) {
this.sentDate = sentDate;
}
public Date getReceiveDate() {
return receiveDate;
}
public void setReceiveDate(Date receiveDate) {
this.receiveDate = receiveDate;
}
} }

View file

@ -6,9 +6,39 @@ import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "contacts") @DatabaseTable(tableName = "contacts")
public class Contact extends XmppEntity { public class Contact extends XmppEntity {
@DatabaseField(id = true)
private String id;
@DatabaseField(foreign = true) @DatabaseField(foreign = true)
private XmppEntity baseEntity; private XmppEntity baseEntity;
@DatabaseField(columnName = "nick")
private String nickname;
// TODO: VCard as foreign key
/**
* Empty constructor for ORMLite.
*/
public Contact() {
}
public Contact(XmppEntity base) {
super();
this.setJid(base.getJid());
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public XmppEntity getBaseEntity() {
return baseEntity;
}
public void setBaseEntity(XmppEntity baseEntity) {
this.baseEntity = baseEntity;
}
} }

View file

@ -1,5 +1,7 @@
package de.vanitasvitae.fasel.db.entity; package de.vanitasvitae.fasel.db.entity;
import java.util.Date;
import com.j256.ormlite.field.DataType; import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable; import com.j256.ormlite.table.DatabaseTable;
@ -11,10 +13,79 @@ public class TextMessage extends BaseMessage {
} }
public TextMessage(BaseMessage baseMessage, String body) {
this.baseMessage = baseMessage;
}
@DatabaseField(canBeNull = false, foreign = true, foreignAutoRefresh = true) @DatabaseField(canBeNull = false, foreign = true, foreignAutoRefresh = true)
private BaseMessage baseMessage; private BaseMessage baseMessage;
@DatabaseField(columnName = "body", dataType = DataType.STRING) @DatabaseField(columnName = "body", dataType = DataType.STRING)
private String body; private String body;
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public BaseMessage getBaseMessage() {
return baseMessage;
}
public void setBaseMessage(BaseMessage baseMessage) {
this.baseMessage = baseMessage;
}
@Override
public String getMessageId() {
return baseMessage.getMessageId();
}
@Override
public void setMessageId(String messageId) {
baseMessage.setMessageId(messageId);
}
@Override
public String getSenderJid() {
return baseMessage.getSenderJid();
}
@Override
public void setSenderJid(String senderJid) {
baseMessage.setSenderJid(senderJid);
}
@Override
public Account getAccount() {
return baseMessage.getAccount();
}
@Override
public void setAccount(Account account) {
baseMessage.setAccount(account);
}
@Override
public Date getSentDate() {
return baseMessage.getSentDate();
}
@Override
public void setSentDate(Date sentDate) {
baseMessage.setSentDate(sentDate);
}
@Override
public Date getReceiveDate() {
return baseMessage.getReceiveDate();
}
@Override
public void setReceiveDate(Date receiveDate) {
baseMessage.setReceiveDate(receiveDate);
}
} }

View file

@ -1,5 +1,6 @@
package de.vanitasvitae.fasel.db.entity; package de.vanitasvitae.fasel.db.entity;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable; import com.j256.ormlite.table.DatabaseTable;
import de.vanitasvitae.fasel.db.dao.impl.XmppEntityDaoImpl; import de.vanitasvitae.fasel.db.dao.impl.XmppEntityDaoImpl;
@ -7,10 +8,21 @@ import de.vanitasvitae.fasel.db.dao.impl.XmppEntityDaoImpl;
@DatabaseTable(tableName = "entities", daoClass = XmppEntityDaoImpl.class) @DatabaseTable(tableName = "entities", daoClass = XmppEntityDaoImpl.class)
public class XmppEntity { public class XmppEntity {
@DatabaseField(id = true) public XmppEntity() {
private String id;
@DatabaseField
private String jid;
} }
@DatabaseField(generatedId = true, dataType = DataType.LONG)
private long id;
@DatabaseField(canBeNull = false, dataType = DataType.STRING)
private String jid;
public String getJid() {
return jid;
}
public void setJid(String jid) {
this.jid = jid;
}
}

View file

@ -0,0 +1,21 @@
package de.vanitasvitae.fasel.db.java;
import java.sql.SQLException;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import de.vanitasvitae.fasel.db.AbstractDatabase;
public class JavaAccountDatabase extends AbstractDatabase {
public JavaAccountDatabase() throws SQLException {
super();
}
@Override
public ConnectionSource getConnectionSource() throws SQLException {
ConnectionSource connectionSource =
new JdbcConnectionSource("jdbc:h2:~/.local/share/fasel/db/fasel");
return connectionSource;
}
}