Merge branch 'feature/translation' into module/messagebus
This commit is contained in:
@@ -5,13 +5,15 @@ import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||
import static de.srsoftware.tools.jdbc.Condition.like;
|
||||
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.ModuleRegistry.*;
|
||||
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.Text.WIKI_PAGE;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.WIKI_PAGES;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.EDIT;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.READ_ONLY;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
import static de.srsoftware.umbrella.wiki.Constants.*;
|
||||
import static java.lang.System.Logger.Level.*;
|
||||
import static java.text.MessageFormat.format;
|
||||
@@ -85,7 +87,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,newPages).causedBy(e);
|
||||
throw failedToCreateTable(newPages).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +98,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_PAGES).causedBy(e);
|
||||
throw failedToCreateTable(TABLE_PAGES).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +109,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_PAGES_USERS).causedBy(e);
|
||||
throw failedToCreateTable(TABLE_PAGES_USERS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +119,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
delete().from(TABLE_PAGES).where(ID,equal(page.id())).where(VERSION,equal(page.version())).execute(db);
|
||||
return page;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY,"page version").causedBy(e);
|
||||
throw failedToDropObject("page version").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,12 +127,12 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
try {
|
||||
db.prepareStatement(format("DROP TABLE {0}",TABLE_PAGES)).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY,TABLE_PAGES).causedBy(e);
|
||||
throw failedToDropObject(TABLE_PAGES).causedBy(e);
|
||||
}
|
||||
try {
|
||||
db.prepareStatement(format("DROP TABLE {0}",TABLE_PAGES_USERS)).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY,TABLE_PAGES_USERS).causedBy(e);
|
||||
throw failedToDropObject(TABLE_PAGES_USERS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +163,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
rs.close();
|
||||
return map;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_SEARCH_DB,"wiki pages").causedBy(e);
|
||||
throw failedToSearchDb(t(WIKI_PAGES)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +177,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
rs.close();
|
||||
return count < 1;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CHECK_ENTITY_AVAILABLE,title).causedBy(e);
|
||||
throw databaseException(FAILED_TO_CHECK_ENTITY_AVAILABLE, OBJECT,title).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,7 +190,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
rs.close();
|
||||
return set;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LIST_ENTITIES,"wiki pages").causedBy(e);
|
||||
throw databaseException(FAILED_TO_LIST_ENTITIES, TYPE,WIKI_PAGES).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,7 +211,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
} catch (NumberFormatException ignored){
|
||||
// title is not an id, go on…
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITY_BY_ID,"wiki page",title).causedBy(e);
|
||||
throw failedToLoadObject("wiki page",title).causedBy(e);
|
||||
}
|
||||
if (page == null) try { // page was not loaded by ID
|
||||
var query = select(ALL).from(TABLE_PAGES).where(TITLE, equal(title));
|
||||
@@ -222,9 +224,9 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
if (rs.next()) page = WikiPage.of(rs);
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITY_BY_ID,"wiki page",title).causedBy(e);
|
||||
throw failedToLoadObject("wiki page",title).causedBy(e);
|
||||
}
|
||||
if (page == null) throw notFound(FAILED_TO_LOAD_ENTITY_BY_ID,"wiki page",title);
|
||||
if (page == null) throw notFound(FAILED_TO_LOAD_OBJECT_BY_ID, OBJECT,WIKI_PAGE, ID,title);
|
||||
try {
|
||||
var rs = select(VERSION).from(TABLE_PAGES).where(ID, equal(page.id())).sort(VERSION).exec(db);
|
||||
var versions = page.versions();
|
||||
@@ -232,7 +234,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
rs.close();
|
||||
return page;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITY_BY_ID,"wiki page",title).causedBy(e);
|
||||
throw failedToLoadObject("wiki page",title).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,7 +250,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
rs.close();
|
||||
return map;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITY_MEMBERS,"wiki page",page.title()).causedBy(e);
|
||||
throw failedToLoadMembers(t("wiki page {title}",TITLE,page.title())).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,12 +258,12 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
try {
|
||||
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}",newPages,TABLE_PAGES)).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_UPDATE_TABLE,format("{0} → {1}", newPages, TABLE_PAGES)).causedBy(e);
|
||||
throw databaseException(FAILED_TO_UPDATE_OBJECT, OBJECT,format("{0} → {1}", newPages, TABLE_PAGES)).causedBy(e);
|
||||
}
|
||||
try {
|
||||
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}",newUsers,TABLE_PAGES_USERS)).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_UPDATE_TABLE,format("{0} → {1}", newPages, TABLE_PAGES_USERS)).causedBy(e);
|
||||
throw databaseException(FAILED_TO_UPDATE_OBJECT, OBJECT,format("{0} → {1}", newPages, TABLE_PAGES_USERS)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,25 +271,25 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
public WikiPage save(WikiPage page) {
|
||||
try {
|
||||
if (page.isDirty(CONTENT) || page.isDirty(ID) || page.isDirty(TITLE)) {
|
||||
insertInto(TABLE_PAGES,ID,VERSION,TITLE,CONTENT).values(page.id(),page.version(),page.title(),page.content()).execute(db).close();
|
||||
insertInto(TABLE_PAGES, ID,VERSION,TITLE, CONTENT).values(page.id(),page.version(),page.title(),page.content()).execute(db).close();
|
||||
page.versions().add(page.version());
|
||||
}
|
||||
if (page.isDirty(MEMBERS)){
|
||||
Query.delete().from(TABLE_PAGES_USERS).where(PAGE_ID, equal(page.id())).where(USER_ID,Condition.notIn(page.members().keySet().toArray())).execute(db);
|
||||
var query = replaceInto(TABLE_PAGES_USERS,PAGE_ID,USER_ID,PERMISSIONS);
|
||||
var query = replaceInto(TABLE_PAGES_USERS,PAGE_ID, USER_ID,PERMISSIONS);
|
||||
for (var member : page.members().entrySet()) query.values(page.id(),member.getKey(),wikiPermissionCode(member.getValue().permission()));
|
||||
query.execute(db).close();
|
||||
}
|
||||
if (page.isDirty(GUEST_ALLOWED)){
|
||||
if (page.guestAllowed()) {
|
||||
insertInto(TABLE_PAGES_USERS,PAGE_ID,USER_ID,PERMISSIONS).values(page.id(),0, wikiPermissionCode(READ_ONLY)).execute(db).close();
|
||||
insertInto(TABLE_PAGES_USERS,PAGE_ID, USER_ID,PERMISSIONS).values(page.id(),0, wikiPermissionCode(READ_ONLY)).execute(db).close();
|
||||
} else {
|
||||
Query.delete().from(TABLE_PAGES_USERS).where(PAGE_ID,equal(page.id())).where(USER_ID,equal(0)).execute(db);
|
||||
}
|
||||
}
|
||||
return page;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_STORE_ENTITY,page.title()).causedBy(e);
|
||||
throw failedToStoreObject(page.title()).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,7 +312,7 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
pageMap.put(lastTitle = title,++id);
|
||||
LOG.log(DEBUG,"Transferring \"{0}\" to new table with id = {1}",title,id);
|
||||
}
|
||||
insertInto(newTable,ID,VERSION,TITLE,CONTENT).values(id,version,title,content).execute(db).close();
|
||||
insertInto(newTable, ID,VERSION,TITLE, CONTENT).values(id,version,title,content).execute(db).close();
|
||||
}
|
||||
|
||||
rs.close();
|
||||
@@ -325,11 +327,11 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
LOG.log(WARNING,"Found reference to non-existing page ({0}) in {1} – skipping transfer of permission",title,TABLE_PAGES_USERS);
|
||||
continue;
|
||||
}
|
||||
insertInto(newUsers,PAGE_ID,USER_ID,PERMISSIONS).values(id,userId,perm).execute(db).close();
|
||||
insertInto(newUsers,PAGE_ID, USER_ID,PERMISSIONS).values(id,userId,perm).execute(db).close();
|
||||
}
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_MOVE,TABLE_PAGES,TABLE_PAGES+"_new").causedBy(e);
|
||||
throw databaseException(FAILED_TO_MOVE,"old",TABLE_PAGES,"new",TABLE_PAGES+"_new").causedBy(e);
|
||||
}
|
||||
var notes = noteService();
|
||||
var tags = tagService();
|
||||
|
||||
@@ -2,11 +2,10 @@
|
||||
package de.srsoftware.umbrella.wiki;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.FULLTEXT;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
import static de.srsoftware.umbrella.core.Paths.*;
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.EDIT;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.READ_ONLY;
|
||||
@@ -31,7 +30,7 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
|
||||
public WikiModule(Configuration config) {
|
||||
super();
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingField(CONFIG_DATABASE));
|
||||
wikiDb = new SqliteDb(connect(dbFile));
|
||||
}
|
||||
|
||||
@@ -41,10 +40,10 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
if (id == null) return doDelete(path,ex);
|
||||
Integer version = null;
|
||||
if (VERSION.equals(path.pop())) version = Integer.parseInt(path.pop());
|
||||
if (version == null) throw missingFieldException("VERSION");
|
||||
if (version == null) throw missingField(VERSION);
|
||||
var page = loadMembers(wikiDb.load(id,version));
|
||||
var member = page.members().get(user.id());
|
||||
if (member == null || member.permission() != EDIT) throw forbidden("You are not allowed to delete \"{0}\"!",page.title());
|
||||
if (member == null || member.permission() != EDIT) throw forbidden("You are not allowed to delete \"{id}\"!", ID,page.title());
|
||||
return sendContent(ex,wikiDb.drop(page));
|
||||
}
|
||||
|
||||
@@ -132,7 +131,7 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
|
||||
private boolean postSearch(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var json = json(ex);
|
||||
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingFieldException(KEY);
|
||||
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingField(KEY);
|
||||
var keys = Arrays.asList(key.split(" "));
|
||||
var fulltext = json.has(FULLTEXT) && json.get(FULLTEXT) instanceof Boolean val && val;
|
||||
var pages = wikiDb.find(user.id(),keys,fulltext);
|
||||
@@ -152,13 +151,13 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
try {
|
||||
version = Integer.parseInt(path.pop());
|
||||
} catch (NumberFormatException e) {
|
||||
throw invalidFieldException(VERSION,"int number");
|
||||
throw invalidField(VERSION,"int number");
|
||||
}
|
||||
}
|
||||
var page = loadPage(id,version);
|
||||
if (page.guestAllowed()) return sendContent(ex,page);
|
||||
var permission = page.members().get(user.id());
|
||||
if (permission == null) throw forbidden("You are not allowed to access \"{0}\"!",id);
|
||||
if (permission == null) throw forbidden("You are not allowed to access \"{id}\"!", ID,id);
|
||||
return sendContent(ex, page);
|
||||
}
|
||||
|
||||
@@ -185,7 +184,7 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
}
|
||||
|
||||
private WikiPage loadPage(String id, Integer version){
|
||||
if (id == null) throw missingFieldException(PAGE_ID);
|
||||
if (id == null) throw missingField(PAGE_ID);
|
||||
return loadMembers(wikiDb.load(id, version));
|
||||
}
|
||||
|
||||
@@ -194,7 +193,7 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
var page = loadPage(id, null);
|
||||
var old = page.toMap();
|
||||
var member = page.members().get(user.id());
|
||||
if (member == null || member.permission() != EDIT) throw forbidden("You are not allowed to edit {0}!",id);
|
||||
if (member == null || member.permission() != EDIT) throw forbidden("You are not allowed to edit {0}!", ID,id);
|
||||
var json = json(ex);
|
||||
page = wikiDb.save(page.patch(json, userService()));
|
||||
messageBus().dispatch(new WikiEvent(user,page,old));
|
||||
|
||||
Reference in New Issue
Block a user