overhauling constants, working on translations
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -8,10 +8,14 @@ import static de.srsoftware.tools.jdbc.Query.*;
|
||||
import static de.srsoftware.tools.jdbc.Query.Dialect.SQLITE;
|
||||
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
||||
import static de.srsoftware.umbrella.bookmarks.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.USER_ID;
|
||||
import static de.srsoftware.umbrella.core.Errors.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.TAGS;
|
||||
import static de.srsoftware.umbrella.core.constants.Module.BOOKMARK;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.TABLE_WITH_NAME;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.USER_WITH_ID;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
import static de.srsoftware.umbrella.tags.Constants.*;
|
||||
import static java.lang.System.Logger.Level.*;
|
||||
import static java.text.MessageFormat.format;
|
||||
@@ -21,6 +25,8 @@ import de.srsoftware.tools.Tuple;
|
||||
import de.srsoftware.tools.jdbc.Query;
|
||||
import de.srsoftware.umbrella.bookmarks.BookmarkDb;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.constants.Text;
|
||||
import de.srsoftware.umbrella.core.model.Translatable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -33,7 +39,6 @@ public class SqliteDb extends BaseDb implements TagDB{
|
||||
public SqliteDb(Connection tagDb, Connection bmDb) {
|
||||
super(tagDb);
|
||||
bookmarks = new de.srsoftware.umbrella.bookmarks.SqliteDb(bmDb);
|
||||
createTables();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -61,14 +66,14 @@ public class SqliteDb extends BaseDb implements TagDB{
|
||||
var pattern = Pattern.compile("/([^/]+)/(\\d+)/view");
|
||||
try {
|
||||
LocalDateTime earliestDate = LocalDateTime.of(2000,1,1,0,0);
|
||||
var rs = select(ALL).from(TABLE_URLS).leftJoin(HASH,TABLE_URL_COMMENTS,URL_HASH).leftJoin(COMMENT_HASH,TABLE_COMMENTS,HASH).where(TIMESTAMP,moreThan(0)).limit(1).exec(db);
|
||||
var rs = select(ALL).from(TABLE_URLS).leftJoin(HASH,TABLE_URL_COMMENTS,URL_HASH).leftJoin(COMMENT_HASH,TABLE_COMMENTS, HASH).where(TIMESTAMP,moreThan(0)).limit(1).exec(db);
|
||||
if (rs.next()) earliestDate = LocalDateTime.ofEpochSecond(rs.getLong(1),0,UTC);
|
||||
rs.close();
|
||||
|
||||
var sql = "CREATE TABLE IF NOT EXISTS tags_new (TAG)";
|
||||
db.prepareStatement(sql).execute();
|
||||
// IMPORT BOOKMARKS
|
||||
var commentedURLS = select(ALL).from(TABLE_URLS).leftJoin(HASH,TABLE_URL_COMMENTS,URL_HASH).leftJoin(COMMENT_HASH,TABLE_COMMENTS,HASH).exec(db);
|
||||
var commentedURLS = select(ALL).from(TABLE_URLS).leftJoin(HASH,TABLE_URL_COMMENTS,URL_HASH).leftJoin(COMMENT_HASH,TABLE_COMMENTS, HASH).exec(db);
|
||||
|
||||
while (commentedURLS.next()){
|
||||
var userId = commentedURLS.getLong(USER_ID);
|
||||
@@ -92,10 +97,10 @@ public class SqliteDb extends BaseDb implements TagDB{
|
||||
}
|
||||
|
||||
var urlTags = select(ALL).from(TABLE_TAGS).where(URL_HASH,equal(urlHash)).where(USER_ID,equal(userId)).exec(db);
|
||||
var insertQuery = insertInto(TABLE_TAGS_NEW,TAG,MODULE,ENTITY_ID,USER_ID).ignoreDuplicates(SQLITE);
|
||||
var insertQuery = insertInto(TABLE_TAGS_NEW,TAG, MODULE, ENTITY_ID, USER_ID).ignoreDuplicates(SQLITE);
|
||||
while (urlTags.next()){
|
||||
var tag = urlTags.getString(TAG);
|
||||
insertQuery.values(tag,BOOKMARK,bm.urlId(),userId);
|
||||
insertQuery.values(tag, BOOKMARK,bm.urlId(),userId);
|
||||
if (module != null && entityId != 0) insertQuery.values(tag,module,entityId,userId);
|
||||
}
|
||||
insertQuery.execute(db).close();
|
||||
@@ -103,7 +108,7 @@ public class SqliteDb extends BaseDb implements TagDB{
|
||||
}
|
||||
commentedURLS.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_UPDATE_TABLE,TABLE_URLS).causedBy(e);
|
||||
throw databaseException(FAILED_TO_UPDATE_OBJECT, OBJECT, Translatable.t(TABLE_WITH_NAME,NAME,TABLE_URLS)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +119,7 @@ public class SqliteDb extends BaseDb implements TagDB{
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,"Comments (legacy)").causedBy(e);
|
||||
throw failedToCreateTable("Comments (legacy)").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +130,7 @@ public class SqliteDb extends BaseDb implements TagDB{
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,"Tags (legacy)").causedBy(e);
|
||||
throw failedToCreateTable("Tags (legacy)").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +141,7 @@ public class SqliteDb extends BaseDb implements TagDB{
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,"URLs (legacy)").causedBy(e);
|
||||
throw failedToCreateTable("URLs (legacy)").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +152,7 @@ public class SqliteDb extends BaseDb implements TagDB{
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,"URL_comments (legacy)").causedBy(e);
|
||||
throw failedToCreateTable("URL_comments (legacy)").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,7 +171,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_TAGS_NEW).causedBy(e);
|
||||
throw failedToCreateTable(TABLE_TAGS_NEW).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +184,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY,table).causedBy(e);
|
||||
throw failedToDropObject(table).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +195,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_TAGS).causedBy(e);
|
||||
throw failedToCreateTable(TABLE_TAGS).causedBy(e);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -206,7 +211,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
.execute(db);
|
||||
return tag;
|
||||
} catch (SQLException e){
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY,tag);
|
||||
throw failedToDropObject(tag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,7 +222,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
.where(MODULE,iEqual(module)).where(ENTITY_ID,equal(entityId))
|
||||
.execute(db);
|
||||
} catch (SQLException e){
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY_OF_ENTITY,entityId,module).causedBy(e);
|
||||
throw failedToDropObject(Translatable.t("{module}.{id}", MODULE,module, ID,entityId)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,7 +246,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
rs.close();
|
||||
return result;
|
||||
} catch (SQLException e){
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,"uses",tag).causedBy(e);
|
||||
throw failedToDropObject(Translatable.t("uses of {tag}",TAG,tag)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,7 +266,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
rs.close();
|
||||
return tags;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LIST_ENTITIES,"tags").causedBy(e);
|
||||
throw databaseException(FAILED_TO_LIST_ENTITIES, TYPE,TAGS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,7 +284,8 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
rs.close();
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,"tags",userId).causedBy(e);
|
||||
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER, TYPE,t(Text.TAGS), OWNER, Translatable.t(USER_WITH_ID, ID,userId)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,14 +305,14 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
rs.close();
|
||||
return tags;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LIST_ENTITIES,"tags").causedBy(e);
|
||||
throw databaseException(FAILED_TO_LIST_ENTITIES, TYPE,t(Text.TAGS)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Collection<Long> userIds, String module, long entityId, Collection<String> tags) {
|
||||
try {
|
||||
var query = replaceInto(TABLE_TAGS,USER_ID,MODULE,ENTITY_ID,TAG);
|
||||
var query = replaceInto(TABLE_TAGS, USER_ID, MODULE, ENTITY_ID,TAG);
|
||||
for (var tag : tags) {
|
||||
if (userIds == null) { // tags not assigned to a user are available to all users
|
||||
query.values(null, module, entityId, tag);
|
||||
@@ -316,7 +322,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
}
|
||||
query.execute(db).close();
|
||||
} catch (SQLException e){
|
||||
throw databaseException(FAILED_TO_STORE_ENTITY,String.join(", ",tags)).causedBy(e);
|
||||
throw failedToStoreObject(String.join(", ",tags)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,7 +332,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
update(TABLE_TAGS).set(ENTITY_ID).where(MODULE,iEqual(module)).where(ENTITY_ID,equal(oldId)).prepare(db).apply(newId).close();
|
||||
LOG.log(DEBUG,"Updated tag @ {0}.{1} → {0}.{2}",module,oldId,newId);
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_UPDATE_ENTITY,format("{0}.{1} → {0}.{2}",module,oldId,newId)).causedBy(e);
|
||||
throw databaseException(FAILED_TO_UPDATE_OBJECT,format("{0}.{1} → {0}.{2}",module,oldId,newId)).causedBy(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
package de.srsoftware.umbrella.tags;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.unprocessable;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.USER_LIST;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.USES;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.tags.Constants.*;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
@@ -28,8 +28,8 @@ public class TagModule extends BaseHandler implements TagService {
|
||||
|
||||
public TagModule(Configuration config) {
|
||||
super();
|
||||
var tagDbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
var bmDbFile = config.get(de.srsoftware.umbrella.bookmarks.Constants.CONFIG_DATABASE).orElseThrow(() -> missingFieldException(de.srsoftware.umbrella.bookmarks.Constants.CONFIG_DATABASE));
|
||||
var tagDbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingField(CONFIG_DATABASE));
|
||||
var bmDbFile = config.get(de.srsoftware.umbrella.bookmarks.Constants.CONFIG_DATABASE).orElseThrow(() -> missingField(de.srsoftware.umbrella.bookmarks.Constants.CONFIG_DATABASE));
|
||||
tagDb = new SqliteDb(connect(tagDbFile),connect(bmDbFile));
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
@@ -90,9 +90,9 @@ public class TagModule extends BaseHandler implements TagService {
|
||||
var head = path.pop();
|
||||
long entityId = Long.parseLong(head);
|
||||
var json = json(ex);
|
||||
if (!(json.has(TAG) && json.get(TAG) instanceof String tag && !tag.isBlank())) throw missingFieldException(TAG);
|
||||
if (!(json.has(TAG) && json.get(TAG) instanceof String tag && !tag.isBlank())) throw missingField(TAG);
|
||||
List<Long> userList = null;
|
||||
if (!json.has(USER_LIST)) throw missingFieldException(USER_LIST);
|
||||
if (!json.has(USER_LIST)) throw missingField(USER_LIST);
|
||||
var ul = json.isNull(USER_LIST) ? null : json.get(USER_LIST);
|
||||
if (ul instanceof JSONArray arr){
|
||||
userList = arr.toList().stream()
|
||||
|
||||
Reference in New Issue
Block a user