Temp
This commit is contained in:
parent
bb7fcee47a
commit
177d2c0cc9
9 changed files with 229 additions and 36 deletions
|
@ -7,6 +7,9 @@ import com.j256.ormlite.support.ConnectionSource;
|
|||
import com.j256.ormlite.table.TableUtils;
|
||||
import de.vanitasvitae.fasel.db.AbstractDatabase;
|
||||
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 {
|
||||
|
||||
|
@ -19,27 +22,15 @@ public class FaselDBTest {
|
|||
}
|
||||
|
||||
public FaselDBTest() throws SQLException {
|
||||
database = new AbstractDatabase() {
|
||||
@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);
|
||||
database = new JavaAccountDatabase();
|
||||
}
|
||||
|
||||
public void insert() throws SQLException {
|
||||
|
||||
String username = "alice@wonderland.lit";
|
||||
String id = "1234";
|
||||
|
||||
String password = "swordfish";
|
||||
|
||||
Account account = new Account(id, username, password);
|
||||
|
||||
database.getAccountsDao().create(account);
|
||||
XmppEntity entity = new XmppEntity();
|
||||
entity.setJid("hans@vader.sw");
|
||||
Contact contact = new Contact(entity);
|
||||
contact.setNickname("Hans Vader");
|
||||
database.getContactsDao().create(contact);
|
||||
}
|
||||
|
||||
public void get() throws SQLException {
|
||||
|
|
|
@ -2,17 +2,41 @@ package de.vanitasvitae.fasel.db;
|
|||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.swing.plaf.TableUI;
|
||||
|
||||
import com.j256.ormlite.dao.Dao;
|
||||
import com.j256.ormlite.dao.DaoManager;
|
||||
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.BaseMessage;
|
||||
import de.vanitasvitae.fasel.db.entity.Contact;
|
||||
import de.vanitasvitae.fasel.db.entity.TextMessage;
|
||||
import de.vanitasvitae.fasel.db.entity.XmppEntity;
|
||||
|
||||
public abstract class AbstractDatabase {
|
||||
|
||||
private final ConnectionSource connectionSource;
|
||||
|
||||
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 {
|
||||
return DaoManager.createDao(getConnectionSource(), Account.class);
|
||||
}
|
||||
|
|
|
@ -9,7 +9,8 @@ import de.vanitasvitae.fasel.db.entity.XmppEntity;
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,32 +8,32 @@ import de.vanitasvitae.fasel.db.dao.impl.AccountDaoImpl;
|
|||
@DatabaseTable(tableName = "accounts", daoClass = AccountDaoImpl.class)
|
||||
public class Account {
|
||||
|
||||
private String TAG = "fasel_db";
|
||||
@DatabaseField(generatedId = true, dataType = DataType.INTEGER)
|
||||
private int accountId;
|
||||
|
||||
@DatabaseField(id = true, dataType = DataType.STRING)
|
||||
private String accountId;
|
||||
|
||||
@DatabaseField(canBeNull = false, dataType = DataType.STRING)
|
||||
@DatabaseField(canBeNull = false, unique = true, dataType = DataType.STRING)
|
||||
private String jid;
|
||||
|
||||
@DatabaseField(canBeNull = false, dataType = DataType.STRING)
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Empty account for ORMLite.
|
||||
*/
|
||||
public Account() {
|
||||
|
||||
}
|
||||
|
||||
public Account(String accountId, String jid, String password) {
|
||||
this.accountId = accountId;
|
||||
public Account(String jid, String password) {
|
||||
this.jid = jid;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getAccountId() {
|
||||
public int getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public void setAccountId(String accountId) {
|
||||
public void setAccountId(int accountId) {
|
||||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,9 @@ import com.j256.ormlite.table.DatabaseTable;
|
|||
@DatabaseTable(tableName = "messages")
|
||||
public class BaseMessage {
|
||||
|
||||
/**
|
||||
* Empty constructor for ORMLite.
|
||||
*/
|
||||
public BaseMessage() {
|
||||
|
||||
}
|
||||
|
@ -22,9 +25,49 @@ public class BaseMessage {
|
|||
@DatabaseField(columnName = "accountId", foreign = true)
|
||||
private Account account;
|
||||
|
||||
@DatabaseField(columnName = "sendDate", dataType = DataType.DATE)
|
||||
private Date sendDate;
|
||||
@DatabaseField(columnName = "sentDate", dataType = DataType.DATE)
|
||||
private Date sentDate;
|
||||
|
||||
@DatabaseField(columnName = "receiveDate", dataType = DataType.DATE)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,9 +6,39 @@ import com.j256.ormlite.table.DatabaseTable;
|
|||
@DatabaseTable(tableName = "contacts")
|
||||
public class Contact extends XmppEntity {
|
||||
|
||||
@DatabaseField(id = true)
|
||||
private String id;
|
||||
|
||||
@DatabaseField(foreign = true)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package de.vanitasvitae.fasel.db.entity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.j256.ormlite.field.DataType;
|
||||
import com.j256.ormlite.field.DatabaseField;
|
||||
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)
|
||||
private BaseMessage baseMessage;
|
||||
|
||||
@DatabaseField(columnName = "body", dataType = DataType.STRING)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package de.vanitasvitae.fasel.db.entity;
|
||||
|
||||
import com.j256.ormlite.field.DataType;
|
||||
import com.j256.ormlite.field.DatabaseField;
|
||||
import com.j256.ormlite.table.DatabaseTable;
|
||||
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)
|
||||
public class XmppEntity {
|
||||
|
||||
@DatabaseField(id = true)
|
||||
private String id;
|
||||
|
||||
@DatabaseField
|
||||
private String jid;
|
||||
public XmppEntity() {
|
||||
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue