working on backend-side translations

This commit is contained in:
2025-12-15 22:07:26 +01:00
parent a275b5065d
commit c7b5b11f4c
9 changed files with 86 additions and 119 deletions

View File

@@ -5,6 +5,8 @@ import static de.srsoftware.tools.jdbc.Condition.*;
import static de.srsoftware.tools.jdbc.Query.*;
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
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.user.Constants.*;
import static de.srsoftware.umbrella.user.model.DbUser.ADMIN_PERMISSIONS;
import static java.lang.System.Logger.Level.*;
@@ -53,8 +55,7 @@ CREATE TABLE IF NOT EXISTS {0} (
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_LOGIN_SERVICES,e);
throw new RuntimeException(e);
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_LOGIN_SERVICES).causedBy(e);
}
createTable = """
@@ -67,8 +68,7 @@ CREATE TABLE IF NOT EXISTS {0} (
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_SERVICE_IDS_USERS,e);
throw new RuntimeException(e);
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_SERVICE_IDS_USERS).causedBy(e);
}
createTable = """
@@ -81,8 +81,7 @@ CREATE TABLE IF NOT EXISTS {0} (
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_TOKEN_USES,e);
throw new RuntimeException(e);
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_TOKEN_USES).causedBy(e);
}
}
@@ -110,8 +109,7 @@ CREATE TABLE IF NOT EXISTS {0} (
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,"Failed to create column {1} in {0}",TABLE_USERS, LANGUAGE,e);
throw new RuntimeException(e);
throw databaseException(FAILED_TO_ADD_COLUMN,LANGUAGE,TABLE_USERS).causedBy(e);
}
}
@@ -157,8 +155,7 @@ CREATE TABLE IF NOT EXISTS {0} (
try {
if (count<1) insertInto(TABLE_USERS,LOGIN,PASS,THEME,SETTINGS).values("admin", hasher.hash("admin",null),"default",null).execute(db);
} catch (SQLException e) {
LOG.log(ERROR,"Failed to create first user");
throw new RuntimeException(e);
throw databaseException(FAILED_TO_STORE_ENTITY,"first user");
}
createTable = """
@@ -172,8 +169,7 @@ CREATE TABLE IF NOT EXISTS {0} (
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_USERS,e);
throw new RuntimeException(e);
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_USERS).causedBy(e);
}
}
@@ -182,8 +178,7 @@ CREATE TABLE IF NOT EXISTS {0} (
try {
Query.delete().from(TABLE_USERS).where(ID,equal(userId)).execute(db);
} catch (SQLException e) {
LOG.log(WARNING,"Failed to delete user with id = {0}!",userId,e);
throw new UmbrellaException(500,"Failed to delete user with id = {0}!",userId).causedBy(e);
throw databaseException(FAILED_TO_DROP_ENTITY,"user "+userId).causedBy(e);
}
return userId;
}
@@ -194,8 +189,7 @@ CREATE TABLE IF NOT EXISTS {0} (
Query.delete().from(TABLE_LOGIN_SERVICES).where(NAME,equal(serviceName)).execute(db);
Query.delete().from(TABLE_SERVICE_IDS_USERS).where(SERVICE_ID,like(serviceName+":%")).execute(db);
} catch (SQLException e) {
LOG.log(WARNING,"Failed to delete login service {0}!",serviceName,e);
throw new UmbrellaException(500,"Failed to delete login service {0}!",serviceName).causedBy(e);
throw databaseException(FAILED_TO_DROP_ENTITY,serviceName).causedBy(e);
}
return serviceName;
}
@@ -220,7 +214,7 @@ CREATE TABLE IF NOT EXISTS {0} (
try {
return Query.delete().from(TABLE_TOKENS).where(TOKEN, equal(token)).execute(db);
} catch (SQLException e){
throw new UmbrellaException(500,"Failed to drop session token").causedBy(e);
throw databaseException(FAILED_TO_DROP_ENTITY,"session token").causedBy(e);
}
}
@@ -232,8 +226,7 @@ CREATE TABLE IF NOT EXISTS {0} (
LOG.log(DEBUG,"Extended session of user {0} until {1}",session.user().name(),then());
return session.extended(newExpiration);
} catch (SQLException e) {
LOG.log(WARNING,"Failed to extend session {0}",session.token());
throw new UmbrellaException(500,"Failed to extend session {0}",session.token()).causedBy(e);
throw databaseException(FAILED_TO_UPDATE_ENTITY,session.token()).causedBy(e);
}
}
@@ -252,8 +245,7 @@ CREATE TABLE IF NOT EXISTS {0} (
insertInto(TABLE_TOKENS, USER_ID, TOKEN, EXPIRATION).values(session.user().id(),session.token(),session.expiration().getEpochSecond()).execute(db);
return session;
} catch (SQLException e) {
LOG.log(WARNING,"Failed to request session for \"{0}\" from database",user.name());
throw new UmbrellaException(500,"Failed to request session for \"{0}\" from database",user.name()).causedBy(e);
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,"session",user.name()).causedBy(e);
}
}
@@ -267,7 +259,7 @@ CREATE TABLE IF NOT EXISTS {0} (
} catch (SQLException sqle){
LOG.log(WARNING,"Failed to load user_id for '{0}' @ '{1}'!",foreignUserId,loginService);
}
if (userId == null) throw new UmbrellaException(500,"Failed to load user_id for \"{0}\" @ \"{1}\"!",foreignUserId,loginService);
if (userId == null) throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,foreignUserId,loginService);
return userId;
}
@@ -286,8 +278,7 @@ CREATE TABLE IF NOT EXISTS {0} (
}
rs.close();
} catch (SQLException e) {
LOG.log(WARNING,"Failed to load user list from database!",e);
throw new UmbrellaException(500,"Failed to load user list from database!").causedBy(e);
throw databaseException(FAILED_TO_LIST_ENTITIES,USERS).causedBy(e);
}
return list;
}
@@ -301,8 +292,7 @@ CREATE TABLE IF NOT EXISTS {0} (
rs.close();
return list;
} catch (SQLException e) {
LOG.log(WARNING,"Failed to fetch {0} for user {1}",TABLE_SERVICE_IDS_USERS,userId,e);
throw new UmbrellaException(500,"Failed to fetch {0} for user {1}",TABLE_SERVICE_IDS_USERS,userId).causedBy(e);
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,"service ids","user "+userId).causedBy(e);
}
}
@@ -314,8 +304,7 @@ CREATE TABLE IF NOT EXISTS {0} (
while (rs.next()) list.add(toLoginService(rs));
rs.close();
} catch (SQLException e){
LOG.log(WARNING,"Failed to load login service list from database!",e);
throw new UmbrellaException(500,"Failed to load login service list from database!");
throw databaseException(FAILED_TO_LIST_ENTITIES,TABLE_LOGIN_SERVICES).causedBy(e);
}
return list;
}
@@ -328,7 +317,7 @@ CREATE TABLE IF NOT EXISTS {0} (
if (rs.next()) loginService = toLoginService(rs);
rs.close();
} catch (SQLException e) {
LOG.log(WARNING,"Failed to load login service \"{0}\"!",name);
throw databaseException(FAILED_TO_LOAD_ENTITY,"login service").causedBy(e);
}
if (loginService == null) throw new UmbrellaException(500,"Failed to load login service \"{0}\"!",name);
return loginService;
@@ -345,7 +334,7 @@ CREATE TABLE IF NOT EXISTS {0} (
} catch (SQLException e) {
LOG.log(WARNING,"Failed to load user for \"{0}\"!",email);
}
if (user == null) throw new UmbrellaException(500,"Failed to load user for \"{0}\"!",email);
if (user == null) throw databaseException(FAILED_TO_LOAD_ENTITY_BY_ID,USER,email);
return user;
}
@@ -360,7 +349,7 @@ CREATE TABLE IF NOT EXISTS {0} (
} catch (SQLException e) {
LOG.log(WARNING,"Failed to load user \"{0}\"!",id);
}
if (user == null) throw new UmbrellaException(500,"Failed to load user \"{0}\"!",id);
if (user == null) throw databaseException(FAILED_TO_LOAD_ENTITY_BY_ID,USER,id);
return user;
}
@@ -375,7 +364,7 @@ CREATE TABLE IF NOT EXISTS {0} (
} catch (SQLException e) {
LOG.log(WARNING,"Failed to load user for session {0}!",session.token());
}
if (user == null) throw new UmbrellaException(500,"Failed to load user for session {0}!",session.token());
if (user == null) throw databaseException(FAILED_TO_LOAD_ENTITY_BY_ID,USER,session.token());
return user;
}
@@ -401,7 +390,7 @@ CREATE TABLE IF NOT EXISTS {0} (
} catch (SQLException e) {
LOG.log(WARNING,"Failed to load user \"{0}\"!",key,e);
}
if (user == null) throw new UmbrellaException(HTTP_UNAUTHORIZED,"Failed to load user \"{0}\"!",key);
if (user == null) throw new UmbrellaException(HTTP_UNAUTHORIZED,FAILED_TO_LOAD_ENTITY_BY_ID,USER,key);
return user;
}
@@ -418,7 +407,7 @@ CREATE TABLE IF NOT EXISTS {0} (
LOG.log(WARNING,"Failed to request session ({0}) from database",token);
throw new UmbrellaException(500,"Failed to request session ({0}) from database",token);
}
if (session == null) throw new UmbrellaException(500,"Failed to request session ({0}) from database",token);
if (session == null) throw databaseException(FAILED_TO_LOAD_ENTITY_BY_ID,"session",token);
return session;
}
@@ -431,8 +420,7 @@ CREATE TABLE IF NOT EXISTS {0} (
try {
insertInto(TABLE_SERVICE_IDS_USERS, SERVICE_ID, USER_ID).values(assignment.loginService()+":"+assignment.foreingId(),assignment.userId()).execute(db).close();
} catch (SQLException e) {
LOG.log(WARNING,"Failed to insert assignment into table {0}!",TABLE_SERVICE_IDS_USERS,e);
throw new UmbrellaException(500,"Failed to insert assignment into table {0}!",TABLE_SERVICE_IDS_USERS).causedBy(e);
throw databaseException(FAILED_TO_STORE_ENTITY,"user assignment").causedBy(e);
}
return assignment;
}
@@ -446,9 +434,7 @@ CREATE TABLE IF NOT EXISTS {0} (
.execute(db)
.close();
} catch (SQLException e){
LOG.log(WARNING,"Failed to store login service data for {0}!",service.name(),e);
throw new UmbrellaException(500,"Failed to store login service for {0}!",service.name()).causedBy(e);
throw databaseException(FAILED_TO_STORE_ENTITY,"login service data").causedBy(e);
}
return service;
}
@@ -470,8 +456,7 @@ CREATE TABLE IF NOT EXISTS {0} (
.close();
}
} catch (SQLException e) {
LOG.log(WARNING,"Failed to store user data for {0}!",user.name(),e);
throw new UmbrellaException(500,"Failed to store user data for {0}!",user.name()).causedBy(e);
throw databaseException(FAILED_TO_STORE_ENTITY,"user data").causedBy(e);
}
return user;
}
@@ -488,7 +473,7 @@ CREATE TABLE IF NOT EXISTS {0} (
rs.close();
return users;
} catch (SQLException e){
throw new UmbrellaException("Failed to search for user by key = {0}",key);
throw databaseException(FAILED_TO_SEARCH_DB,USERS).causedBy(e);
}
}
@@ -539,8 +524,7 @@ CREATE TABLE IF NOT EXISTS {0} (
.execute(db);
return assignment;
} catch (SQLException e) {
LOG.log(WARNING,"Failed to drop foreign login assignment: {0} - {1}",assignment.loginService(),assignment.foreingId(),e);
throw new UmbrellaException(500,"Failed to drop foreign login assignment: {0} - {1}",assignment.loginService(),assignment.foreingId()).causedBy(e);
throw databaseException(FAILED_TO_DROP_ENTITY_OF_ENTITY,"foreign id",assignment.foreingId(),"service",assignment.loginService()).causedBy(e);
}
}
}