overhauling constants, working on translations
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -4,9 +4,9 @@ package de.srsoftware.umbrella.contact;
|
||||
import static de.srsoftware.umbrella.contact.Constants.CONFIG_DATABASE;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.LIST;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingField;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
@@ -28,7 +28,7 @@ public class ContactModule extends BaseHandler implements ContactService {
|
||||
|
||||
public ContactModule(Configuration config) throws UmbrellaException {
|
||||
super();
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingField(CONFIG_DATABASE));
|
||||
contactDb = new SqliteDb(connect(dbFile));
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
|
||||
@@ -6,10 +6,12 @@ import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
||||
import static de.srsoftware.tools.jdbc.Query.insertInto;
|
||||
import static de.srsoftware.tools.jdbc.Query.select;
|
||||
import static de.srsoftware.umbrella.contact.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Errors.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.notFound;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.TYPE;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.jdbc.Query;
|
||||
@@ -45,7 +47,7 @@ public class SqliteDb extends BaseDb implements ContactDb{
|
||||
try {
|
||||
db.prepareStatement(sql).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(ERROR_FAILED_CREATE_TABLE,TABLE_CONTACTS).causedBy(e);
|
||||
throw failedToCreateTable(TABLE_CONTACTS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +57,7 @@ public class SqliteDb extends BaseDb implements ContactDb{
|
||||
try {
|
||||
db.prepareStatement(sql).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(ERROR_FAILED_CREATE_TABLE,TABLE_CONTACTS_USERS).causedBy(e);
|
||||
throw failedToCreateTable(TABLE_CONTACTS_USERS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +69,7 @@ public class SqliteDb extends BaseDb implements ContactDb{
|
||||
Query.delete().from(TABLE_CONTACTS_USERS).where(CONTACT_ID,equal(contact.id())).execute(db);
|
||||
db.setAutoCommit(true);
|
||||
} catch (SQLException e){
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY,"contact",contact.id()).causedBy(e);
|
||||
throw failedToDropObject(t(CONTACT_WITH_ID, ID,contact.id())).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +85,10 @@ public class SqliteDb extends BaseDb implements ContactDb{
|
||||
rs.close();
|
||||
return contacts;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,"contacts",userId).causedBy(e);
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,
|
||||
TYPE, t(CONTACTS),
|
||||
OWNER, t(USER_WITH_ID, ID,userId)
|
||||
).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,9 +100,12 @@ public class SqliteDb extends BaseDb implements ContactDb{
|
||||
if (rs.next()) contact = Contact.of(rs);
|
||||
rs.close();
|
||||
if (contact != null) return contact;
|
||||
throw notFound(FAILED_TO_LOAD_ENTITY_BY_ID, "contact", contactId);
|
||||
throw failedToLoadObject(t(CONTACT), contactId);
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,"contacts",userId).causedBy(e);
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,
|
||||
TYPE, t(CONTACT),
|
||||
OWNER, t(USER_WITH_ID, ID,userId)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,19 +113,19 @@ public class SqliteDb extends BaseDb implements ContactDb{
|
||||
public Contact save(Contact contact) {
|
||||
if (contact.id() == 0){ // new contact
|
||||
try {
|
||||
var rs = insertInto(TABLE_CONTACTS,DATA).values(contact.vcard()).execute(db).getGeneratedKeys();
|
||||
var rs = insertInto(TABLE_CONTACTS, DATA).values(contact.vcard()).execute(db).getGeneratedKeys();
|
||||
Long id = null;
|
||||
if (rs.next()) id = rs.getLong(1);
|
||||
rs.close();
|
||||
if (id != null) return new Contact(id,contact.vcard());
|
||||
throw databaseException(FAILED_TO_STORE_ENTITY,"vcard");
|
||||
throw failedToStoreObject(VCARD);
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_STORE_ENTITY,"vcard").causedBy(e);
|
||||
throw failedToStoreObject(VCARD).causedBy(e);
|
||||
}
|
||||
} else try { // update
|
||||
Query.update(TABLE_CONTACTS).set(DATA).where(ID,equal(contact.id())).prepare(db).apply(contact.vcard()).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_UPDATE_ENTITY,"vcard").causedBy(e);
|
||||
throw failedToStoreObject(VCARD).causedBy(e);
|
||||
}
|
||||
return contact;
|
||||
}
|
||||
@@ -125,9 +133,9 @@ public class SqliteDb extends BaseDb implements ContactDb{
|
||||
@Override
|
||||
public void setOwner(long userId, Contact contact) {
|
||||
try {
|
||||
Query.replaceInto(TABLE_CONTACTS_USERS,USER_ID,CONTACT_ID,ASSIGNED).values(userId,contact.id(),false).execute(db).close();
|
||||
Query.replaceInto(TABLE_CONTACTS_USERS, USER_ID,CONTACT_ID,ASSIGNED).values(userId,contact.id(),false).execute(db).close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_ASSIGN_CONTACT_TO_USER,contact.id(),userId).causedBy(e);
|
||||
throw databaseException(FAILED_TO_ASSIGN_A_TO_B,"a", t(CONTACT_WITH_ID, ID,contact.id()),"b", t(USER_WITH_ID, ID,userId)).causedBy(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user