Commit uncommitted changes
This commit is contained in:
parent
177d2c0cc9
commit
4b11b9efbb
29 changed files with 582 additions and 331 deletions
|
@ -28,6 +28,12 @@ dependencies {
|
|||
implementation 'com.j256.ormlite:ormlite-core:5.1'
|
||||
implementation 'com.j256.ormlite:ormlite-jdbc:5.1'
|
||||
implementation 'com.h2database:h2:1.4.197'
|
||||
|
||||
// Lombok
|
||||
compile 'org.projectlombok:lombok:1.18.6'
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.6'
|
||||
|
||||
testCompile 'junit:junit:4.12'
|
||||
}
|
||||
|
||||
jfx {
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package de.vanitasvitae.fasel;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
import de.vanitasvitae.fasel.ui.container.LoginController;
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
|
@ -10,9 +13,21 @@ import javafx.stage.Stage;
|
|||
public class FaselApplication extends Application {
|
||||
|
||||
private Stage primaryStage;
|
||||
private static FaselApplication INSTANCE;
|
||||
|
||||
private FaselApplication() throws SQLException {
|
||||
}
|
||||
|
||||
public static FaselApplication getInstance() throws SQLException {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new FaselApplication();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception{
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
INSTANCE = this;
|
||||
this.primaryStage = primaryStage;
|
||||
FXMLLoader loader = new FXMLLoader();
|
||||
loader.setLocation(getClass().getResource("/fxml/container/login_container.fxml"));
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
package de.vanitasvitae.fasel;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import com.j256.ormlite.jdbc.JdbcConnectionSource;
|
||||
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 {
|
||||
|
||||
private AbstractDatabase database;
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
FaselDBTest db = new FaselDBTest();
|
||||
db.insert();
|
||||
db.get();
|
||||
}
|
||||
|
||||
public FaselDBTest() throws SQLException {
|
||||
database = new JavaAccountDatabase();
|
||||
}
|
||||
|
||||
public void insert() throws SQLException {
|
||||
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 {
|
||||
for (Account account : database.getAccountsDao().queryForAll()) {
|
||||
System.out.print(account.getAccountId() + " " + account.getJid() + " " + account.getPassword());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,51 +1,40 @@
|
|||
package de.vanitasvitae.fasel.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
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();
|
||||
protected AbstractDatabase() throws SQLException {
|
||||
connectionSource = DatabaseService.getConnectionSource(getDatabaseName());
|
||||
setupTables();
|
||||
}
|
||||
|
||||
public ConnectionSource getConnectionSource() {
|
||||
return connectionSource;
|
||||
}
|
||||
|
||||
protected abstract List<Class<?>> getTables();
|
||||
|
||||
protected abstract String getDatabaseName();
|
||||
|
||||
private void setupTables() throws SQLException {
|
||||
createTableIfNotExists(Account.class);
|
||||
createTableIfNotExists(XmppEntity.class);
|
||||
createTableIfNotExists(Contact.class);
|
||||
createTableIfNotExists(BaseMessage.class);
|
||||
createTableIfNotExists(TextMessage.class);
|
||||
List<Class<?>> tableClasses = getTables();
|
||||
if (tableClasses == null) {
|
||||
throw new IllegalStateException("getTables cannot return null!");
|
||||
}
|
||||
|
||||
for (Class<?> clazz : tableClasses) {
|
||||
createTableIfNotExists(clazz);
|
||||
}
|
||||
}
|
||||
|
||||
private void createTableIfNotExists(Class<?> klass) throws SQLException {
|
||||
TableUtils.createTableIfNotExists(connectionSource, klass);
|
||||
}
|
||||
|
||||
public Dao<Account, String> getAccountsDao() throws SQLException {
|
||||
return DaoManager.createDao(getConnectionSource(), Account.class);
|
||||
}
|
||||
|
||||
public Dao<XmppEntity, String> getXmppEntityDao() throws SQLException {
|
||||
return DaoManager.createDao(getConnectionSource(), XmppEntity.class);
|
||||
}
|
||||
|
||||
public Dao<Contact, String> getContactsDao() throws SQLException {
|
||||
return DaoManager.createDao(getConnectionSource(), Contact.class);
|
||||
private void createTableIfNotExists(Class<?> clazz) throws SQLException {
|
||||
TableUtils.createTableIfNotExists(connectionSource, clazz);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package de.vanitasvitae.fasel.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
|
||||
public class DatabaseService {
|
||||
|
||||
private static DatabaseCreator CREATOR;
|
||||
|
||||
public interface DatabaseCreator {
|
||||
ConnectionSource getConnectionSource(String databaseName) throws SQLException;
|
||||
}
|
||||
|
||||
public static ConnectionSource getConnectionSource(String databaseName) throws SQLException {
|
||||
if (CREATOR == null) {
|
||||
throw new IllegalStateException("No DatabaseCreator set in DatabaseService.");
|
||||
}
|
||||
|
||||
return CREATOR.getConnectionSource(databaseName);
|
||||
}
|
||||
|
||||
public static void setDatabaseCreator(DatabaseCreator creator) {
|
||||
CREATOR = creator;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package de.vanitasvitae.fasel.db;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import com.j256.ormlite.jdbc.JdbcConnectionSource;
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
|
||||
public class H2DatabaseCreator implements DatabaseService.DatabaseCreator {
|
||||
|
||||
private final File directory;
|
||||
|
||||
public H2DatabaseCreator(File directory) {
|
||||
if (directory == null) {
|
||||
throw new IllegalArgumentException("Directory cannot be null!");
|
||||
}
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConnectionSource getConnectionSource(String databaseName) throws SQLException {
|
||||
return new JdbcConnectionSource(getH2DatabaseURL(directory, databaseName));
|
||||
}
|
||||
|
||||
private static String getH2DatabaseURL(File directory, String databaseName) {
|
||||
return "jdbc:h2:" + new File(directory, databaseName).getAbsolutePath();
|
||||
}
|
||||
}
|
|
@ -3,6 +3,6 @@ package de.vanitasvitae.fasel.db.dao;
|
|||
import com.j256.ormlite.dao.Dao;
|
||||
import de.vanitasvitae.fasel.db.entity.Account;
|
||||
|
||||
public interface AccountDao extends Dao<Account, String> {
|
||||
public interface AccountDao extends Dao<Account, Long> {
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package de.vanitasvitae.fasel.db.dao;
|
||||
|
||||
import com.j256.ormlite.dao.Dao;
|
||||
import de.vanitasvitae.fasel.db.entity.Avatar;
|
||||
|
||||
public interface AvatarDao extends Dao<Avatar, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package de.vanitasvitae.fasel.db.dao;
|
||||
|
||||
import com.j256.ormlite.dao.Dao;
|
||||
import de.vanitasvitae.fasel.db.entity.Contact;
|
||||
|
||||
public interface ContactDao extends Dao<Contact, Long> {
|
||||
|
||||
}
|
|
@ -3,6 +3,6 @@ package de.vanitasvitae.fasel.db.dao;
|
|||
import com.j256.ormlite.dao.Dao;
|
||||
import de.vanitasvitae.fasel.db.entity.XmppEntity;
|
||||
|
||||
public interface XmppEntityDao extends Dao<XmppEntity, String> {
|
||||
public interface XmppEntityDao extends Dao<XmppEntity, Long> {
|
||||
|
||||
}
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
package de.vanitasvitae.fasel.db.dao.impl;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import com.j256.ormlite.dao.BaseDaoImpl;
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
import de.vanitasvitae.fasel.db.dao.AccountDao;
|
||||
import de.vanitasvitae.fasel.db.entity.Account;
|
||||
|
||||
public class AccountDaoImpl extends BaseDaoImpl<Account, String> implements AccountDao {
|
||||
public class AccountDaoImpl extends BaseDaoImpl<Account, Long> implements AccountDao {
|
||||
|
||||
public AccountDaoImpl(ConnectionSource connectionSource)
|
||||
throws SQLException {
|
||||
super(connectionSource, Account.class);
|
||||
}
|
||||
|
||||
public List<Account> getAccounts() throws SQLException {
|
||||
return queryForAll();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package de.vanitasvitae.fasel.db.dao.impl;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import com.j256.ormlite.dao.BaseDaoImpl;
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
import de.vanitasvitae.fasel.db.dao.AvatarDao;
|
||||
import de.vanitasvitae.fasel.db.entity.Avatar;
|
||||
import de.vanitasvitae.fasel.db.entity.XmppEntity;
|
||||
|
||||
public class AvatarDaoImpl extends BaseDaoImpl<Avatar, Long> implements AvatarDao {
|
||||
|
||||
public AvatarDaoImpl(ConnectionSource connectionSource)
|
||||
throws SQLException {
|
||||
super(connectionSource, Avatar.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Test
|
||||
* @param entity
|
||||
* @return
|
||||
* @throws SQLException
|
||||
*/
|
||||
public Avatar getAvatarForEntity(XmppEntity entity) throws SQLException {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("XmppEntity cannot be null!");
|
||||
}
|
||||
|
||||
List<Avatar> avatars = queryForEq("entity", entity.getEntityID());
|
||||
if (avatars.isEmpty()) {
|
||||
return null;
|
||||
} else if (avatars.size() == 1) {
|
||||
return avatars.get(0);
|
||||
} else {
|
||||
throw new SQLException("There are more than one Avatar for entity " + entity.toString());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package de.vanitasvitae.fasel.db.dao.impl;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import com.j256.ormlite.dao.BaseDaoImpl;
|
||||
import com.j256.ormlite.dao.DaoManager;
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
import de.vanitasvitae.fasel.db.dao.ContactDao;
|
||||
import de.vanitasvitae.fasel.db.entity.Account;
|
||||
import de.vanitasvitae.fasel.db.entity.Contact;
|
||||
import de.vanitasvitae.fasel.db.entity.XmppEntity;
|
||||
|
||||
public class ContactDaoImpl extends BaseDaoImpl<Contact, Long> implements ContactDao {
|
||||
|
||||
public ContactDaoImpl(ConnectionSource connectionSource)
|
||||
throws SQLException {
|
||||
super(connectionSource, Contact.class);
|
||||
}
|
||||
|
||||
// TODO: Test
|
||||
public List<Contact> getContactsOf(Account account) throws SQLException {
|
||||
return queryForEq(Contact.ACCOUNT, account.getAccountId());
|
||||
}
|
||||
|
||||
public List<Contact> getContactsByJid(String jid) throws SQLException {
|
||||
XmppEntityDaoImpl entityDao = DaoManager.createDao(getConnectionSource(), XmppEntity.class);
|
||||
List<Contact> contacts = queryBuilder()
|
||||
.join(Contact.ENTITY, XmppEntity.ENTITY_ID, entityDao.queryBuilder())
|
||||
.where().eq(XmppEntity.JID, jid).query();
|
||||
return contacts;
|
||||
}
|
||||
|
||||
public List<Contact> getAllContacts() throws SQLException {
|
||||
return queryForAll();
|
||||
}
|
||||
}
|
|
@ -1,16 +1,21 @@
|
|||
package de.vanitasvitae.fasel.db.dao.impl;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import com.j256.ormlite.dao.BaseDaoImpl;
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
import de.vanitasvitae.fasel.db.dao.XmppEntityDao;
|
||||
import de.vanitasvitae.fasel.db.entity.XmppEntity;
|
||||
|
||||
public class XmppEntityDaoImpl extends BaseDaoImpl<XmppEntity, String> implements XmppEntityDao {
|
||||
public class XmppEntityDaoImpl extends BaseDaoImpl<XmppEntity, Long> implements XmppEntityDao {
|
||||
|
||||
public XmppEntityDaoImpl(ConnectionSource connectionSource)
|
||||
throws SQLException {
|
||||
super(connectionSource, XmppEntity.class);
|
||||
}
|
||||
|
||||
public List<XmppEntity> getXmppEntityByJid(String jid) throws SQLException {
|
||||
return queryForEq(XmppEntity.JID, jid);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package de.vanitasvitae.fasel.db.databases;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.j256.ormlite.dao.DaoManager;
|
||||
import de.vanitasvitae.fasel.db.AbstractDatabase;
|
||||
import de.vanitasvitae.fasel.db.dao.impl.AccountDaoImpl;
|
||||
import de.vanitasvitae.fasel.db.dao.impl.ContactDaoImpl;
|
||||
import de.vanitasvitae.fasel.db.dao.impl.XmppEntityDaoImpl;
|
||||
import de.vanitasvitae.fasel.db.entity.Account;
|
||||
import de.vanitasvitae.fasel.db.entity.Contact;
|
||||
import de.vanitasvitae.fasel.db.entity.XmppEntity;
|
||||
|
||||
public class AccountDatabase extends AbstractDatabase {
|
||||
|
||||
public AccountDatabase() throws SQLException {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Class<?>> getTables() {
|
||||
return Arrays.asList(
|
||||
Account.class,
|
||||
XmppEntity.class,
|
||||
Contact.class
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseName() {
|
||||
return "accounts";
|
||||
}
|
||||
|
||||
public AccountDaoImpl getAccountDao() throws SQLException {
|
||||
return DaoManager.createDao(getConnectionSource(), Account.class);
|
||||
}
|
||||
|
||||
public XmppEntityDaoImpl getXmppEntityDao() throws SQLException {
|
||||
return DaoManager.createDao(getConnectionSource(), XmppEntity.class);
|
||||
}
|
||||
|
||||
public ContactDaoImpl getContactDao() throws SQLException {
|
||||
return DaoManager.createDao(getConnectionSource(), Contact.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package de.vanitasvitae.fasel.db.databases;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.j256.ormlite.dao.DaoManager;
|
||||
import de.vanitasvitae.fasel.db.AbstractDatabase;
|
||||
import de.vanitasvitae.fasel.db.dao.impl.AvatarDaoImpl;
|
||||
import de.vanitasvitae.fasel.db.entity.Avatar;
|
||||
|
||||
public class AvatarDatabase extends AbstractDatabase {
|
||||
|
||||
public AvatarDatabase() throws SQLException {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Class<?>> getTables() {
|
||||
return Arrays.asList(Avatar.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseName() {
|
||||
return "avatars";
|
||||
}
|
||||
|
||||
public AvatarDaoImpl getAvatarDao() throws SQLException {
|
||||
return DaoManager.createDao(getConnectionSource(), Avatar.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package de.vanitasvitae.fasel.db.databases;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import de.vanitasvitae.fasel.db.AbstractDatabase;
|
||||
|
||||
public class OmemoDatabase extends AbstractDatabase {
|
||||
|
||||
public OmemoDatabase() throws SQLException {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Class<?>> getTables() {
|
||||
return Arrays.asList();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseName() {
|
||||
return "omemo";
|
||||
}
|
||||
}
|
|
@ -4,19 +4,50 @@ 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.AccountDaoImpl;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@DatabaseTable(tableName = "accounts", daoClass = AccountDaoImpl.class)
|
||||
public class Account {
|
||||
|
||||
@DatabaseField(generatedId = true, dataType = DataType.INTEGER)
|
||||
private int accountId;
|
||||
public static final String ACCOUNT_ID = "accountID";
|
||||
public static final String SERVICE_ADDRESS = "serviceAddress";
|
||||
public static final String USERNAME = "username";
|
||||
public static final String PASSWORD = "password";
|
||||
public static final String ENTITY = "entity";
|
||||
public static final String ENABLED = "enabled";
|
||||
|
||||
@DatabaseField(canBeNull = false, unique = true, dataType = DataType.STRING)
|
||||
private String jid;
|
||||
@Getter
|
||||
@DatabaseField(generatedId = true, columnName = ACCOUNT_ID, dataType = DataType.LONG)
|
||||
private long accountId;
|
||||
|
||||
@DatabaseField(canBeNull = false, dataType = DataType.STRING)
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(canBeNull = false, columnName = SERVICE_ADDRESS, uniqueCombo = true, dataType = DataType.STRING)
|
||||
private String serviceAddress;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(canBeNull = false, columnName = USERNAME, uniqueCombo = true, dataType = DataType.STRING)
|
||||
private String username;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(columnName = PASSWORD, dataType = DataType.STRING)
|
||||
private String password;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(foreign = true, columnName = ENTITY, canBeNull = false, foreignAutoCreate = true, foreignAutoRefresh = true)
|
||||
private XmppEntity baseEntity;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(canBeNull = false, columnName = ENABLED, defaultValue = "false", dataType = DataType.BOOLEAN)
|
||||
private boolean enabled;
|
||||
|
||||
/**
|
||||
* Empty account for ORMLite.
|
||||
*/
|
||||
|
@ -24,32 +55,15 @@ public class Account {
|
|||
|
||||
}
|
||||
|
||||
public Account(String jid, String password) {
|
||||
this.jid = jid;
|
||||
public Account(String username, String service, String password) {
|
||||
this.username = username;
|
||||
this.serviceAddress = service;
|
||||
this.password = password;
|
||||
this.baseEntity = new XmppEntity(username + "@" + service);
|
||||
}
|
||||
|
||||
public int getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public void setAccountId(int accountId) {
|
||||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
public String getJid() {
|
||||
return jid;
|
||||
}
|
||||
|
||||
public void setJid(String jid) {
|
||||
this.jid = jid;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Account[" + accountId + ", " + serviceAddress + ", " + username + ", " + password + ", " + baseEntity + ", " + enabled + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
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 lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@DatabaseTable(tableName = "Avatars")
|
||||
public class Avatar {
|
||||
|
||||
public static final String AVATAR_ID = "avatarID";
|
||||
public static final String ENTITY = "entity";
|
||||
public static final String IMAGE = "image";
|
||||
public static final String HASH = "hash";
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(generatedId = true, columnName = AVATAR_ID, dataType = DataType.LONG)
|
||||
private Long avatarID;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(foreign = true, columnName = ENTITY, unique = true)
|
||||
private XmppEntity entity;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(columnName = IMAGE, dataType = DataType.BYTE_ARRAY)
|
||||
private byte[] image;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(columnName = HASH, dataType = DataType.STRING)
|
||||
private String hash;
|
||||
|
||||
public Avatar() {
|
||||
|
||||
}
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
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;
|
||||
|
||||
@DatabaseTable(tableName = "messages")
|
||||
public class BaseMessage {
|
||||
|
||||
/**
|
||||
* Empty constructor for ORMLite.
|
||||
*/
|
||||
public BaseMessage() {
|
||||
|
||||
}
|
||||
|
||||
@DatabaseField(id = true, columnName = "messageId")
|
||||
private String messageId;
|
||||
|
||||
@DatabaseField(columnName = "senderJid", dataType = DataType.STRING)
|
||||
private String senderJid;
|
||||
|
||||
@DatabaseField(columnName = "accountId", foreign = true)
|
||||
private Account account;
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package de.vanitasvitae.fasel.db.entity;
|
||||
|
||||
import com.j256.ormlite.field.DatabaseField;
|
||||
import com.j256.ormlite.table.DatabaseTable;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@DatabaseTable(tableName = "chats")
|
||||
public class Chat {
|
||||
|
||||
public static final String CHAT_ID = "chatID";
|
||||
public static final String CONTACT = "contact";
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(generatedId = true, columnName = CHAT_ID)
|
||||
private long chatID;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(foreign = true, columnName = CONTACT, unique = true)
|
||||
private Contact contact;
|
||||
}
|
|
@ -1,44 +1,51 @@
|
|||
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.ContactDaoImpl;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@DatabaseTable(tableName = "contacts")
|
||||
public class Contact extends XmppEntity {
|
||||
@EqualsAndHashCode
|
||||
@DatabaseTable(tableName = "contacts", daoClass = ContactDaoImpl.class)
|
||||
public class Contact {
|
||||
|
||||
@DatabaseField(foreign = true)
|
||||
private XmppEntity baseEntity;
|
||||
public static final String CONTACT_ID = "contactID";
|
||||
public static final String ACCOUNT = "account";
|
||||
public static final String ENTITY = "entity";
|
||||
public static final String NICKNAME = "nickname";
|
||||
|
||||
@DatabaseField(columnName = "nick")
|
||||
@Getter
|
||||
@DatabaseField(generatedId = true, columnName = CONTACT_ID, dataType = DataType.LONG)
|
||||
private long contactID;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(foreign = true, columnName = ACCOUNT)
|
||||
private Account account;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(foreign = true, columnName = ENTITY, foreignAutoRefresh = true, foreignAutoCreate = true)
|
||||
private XmppEntity entity;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(dataType = DataType.STRING, columnName = NICKNAME)
|
||||
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) {
|
||||
public Contact(String jid, String nickname) {
|
||||
this.entity = new XmppEntity(jid);
|
||||
this.nickname = nickname;
|
||||
}
|
||||
|
||||
public XmppEntity getBaseEntity() {
|
||||
return baseEntity;
|
||||
}
|
||||
|
||||
public void setBaseEntity(XmppEntity baseEntity) {
|
||||
this.baseEntity = baseEntity;
|
||||
public String toString() {
|
||||
return "Contact[" + contactID + ", " + account + ", " + entity + ", " + nickname + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,91 +0,0 @@
|
|||
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;
|
||||
|
||||
@DatabaseTable(tableName = "textmessages")
|
||||
public class TextMessage extends BaseMessage {
|
||||
|
||||
public TextMessage() {
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -4,25 +4,36 @@ 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;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@DatabaseTable(tableName = "entities", daoClass = XmppEntityDaoImpl.class)
|
||||
public class XmppEntity {
|
||||
|
||||
public static final String ENTITY_ID = "entityID";
|
||||
public static final String JID = "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) {
|
||||
public XmppEntity(String jid) {
|
||||
this.jid = jid;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@DatabaseField(generatedId = true, columnName = ENTITY_ID, dataType = DataType.LONG)
|
||||
private long entityID;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@DatabaseField(columnName = JID, dataType = DataType.STRING)
|
||||
private String jid;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "XmppEntity[" + entityID + ", " + jid + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,22 +1,41 @@
|
|||
package de.vanitasvitae.fasel.xmpp;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
|
||||
import de.vanitasvitae.fasel.db.entity.Account;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
public class XmppConnectionService {
|
||||
public final class XmppConnectionService {
|
||||
|
||||
private final XMPPTCPConnection connection;
|
||||
|
||||
public XmppConnectionService(String username, String serverAddress, String password)
|
||||
private static final Map<Account, XmppConnectionService> CONNECTIONS = new WeakHashMap<>();
|
||||
|
||||
public static XmppConnectionService connectionForAccount(Account account) throws XmppStringprepException {
|
||||
XmppConnectionService connection = CONNECTIONS.get(account);
|
||||
if (connection == null) {
|
||||
connection = new XmppConnectionService(account.getUsername(), account.getServiceAddress(), account.getPassword());
|
||||
CONNECTIONS.put(account, connection);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
private XmppConnectionService(String username, String serverAddress, String password)
|
||||
throws XmppStringprepException {
|
||||
connection = new XMPPTCPConnection(username, password, serverAddress);
|
||||
}
|
||||
|
||||
public XMPPConnection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void login() throws InterruptedException, IOException, SmackException, XMPPException {
|
||||
connection.connect();
|
||||
connection.login();
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package de.vanitasvitae.fasel;
|
||||
|
||||
public class ContactsTest {
|
||||
|
||||
|
||||
}
|
21
fasel-jfx/src/test/java/de/vanitasvitae/fasel/TestUtils.java
Normal file
21
fasel-jfx/src/test/java/de/vanitasvitae/fasel/TestUtils.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package de.vanitasvitae.fasel;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Random;
|
||||
|
||||
public class TestUtils {
|
||||
|
||||
private static final Random RAND = new Random();
|
||||
|
||||
public static File getTempDir(String dirPrefix) {
|
||||
return new File(System.getProperty("java.io.tmpdir"), randomFilename(dirPrefix, ""));
|
||||
}
|
||||
|
||||
public static File getTempFile(String prefix, String suffix) {
|
||||
return new File(System.getProperty("java.io.tmpdir"), randomFilename(prefix, suffix));
|
||||
}
|
||||
|
||||
public static String randomFilename(String prefix, String suffix) {
|
||||
return prefix + RAND.nextInt() + suffix;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package de.vanitasvitae.fasel.database;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import de.vanitasvitae.fasel.TestUtils;
|
||||
import de.vanitasvitae.fasel.db.DatabaseService;
|
||||
import de.vanitasvitae.fasel.db.H2DatabaseCreator;
|
||||
import de.vanitasvitae.fasel.db.databases.AccountDatabase;
|
||||
import de.vanitasvitae.fasel.db.entity.Account;
|
||||
import de.vanitasvitae.fasel.db.entity.Contact;
|
||||
import de.vanitasvitae.fasel.db.entity.XmppEntity;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AccountDatabaseTest {
|
||||
|
||||
private final AccountDatabase database;
|
||||
|
||||
public AccountDatabaseTest() throws SQLException {
|
||||
File file = TestUtils.getTempDir("accounttest_");
|
||||
DatabaseService.setDatabaseCreator(new H2DatabaseCreator(file));
|
||||
database = new AccountDatabase();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void populate() throws SQLException {
|
||||
Account account = new Account("alice", "wonderland.lit","secret");
|
||||
database.getAccountDao().createIfNotExists(account);
|
||||
|
||||
assertEquals(account, database.getAccountDao().getAccounts().get(0));
|
||||
|
||||
Contact contact = new Contact("bob@builder.tv", "Bob the Builder");
|
||||
contact.setAccount(account);
|
||||
database.getContactDao().createIfNotExists(contact);
|
||||
|
||||
contact = new Contact("buggy@builder.tv", "Buggy");
|
||||
contact.setAccount(account);
|
||||
database.getContactDao().createIfNotExists(contact);
|
||||
|
||||
contact = new Contact("buggy@builder.tv", "Buggy");
|
||||
contact.setAccount(account);
|
||||
database.getContactDao().createIfNotExists(contact);
|
||||
|
||||
database.getXmppEntityDao().queryForAll().forEach(System.out::println);
|
||||
assertEquals(2, database.getContactDao().getContactsOf(account).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryContactTest() throws SQLException {
|
||||
Account account = database.getAccountDao().getAccounts().get(0);
|
||||
List<Contact> contacts = database.getContactDao().getAllContacts();
|
||||
contacts.forEach(contact -> System.out.println(contact.toString()));
|
||||
assertEquals(2, database.getContactDao().getAllContacts().size());
|
||||
assertEquals(2, database.getContactDao().getContactsOf(account).size());
|
||||
assertEquals(1, database.getContactDao().getContactsByJid("bob@builder.tv").size());
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeConnection() throws IOException {
|
||||
database.getConnectionSource().close();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue