working on wiki module: adding users+permissions, adding notes, adding tags
adding tag currently does not work due to the tag module not allowing for strings as entity ids
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package de.srsoftware.umbrella.wiki;
|
||||
|
||||
import de.srsoftware.tools.jdbc.Condition;
|
||||
import de.srsoftware.tools.jdbc.Query;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.model.Permission;
|
||||
import de.srsoftware.umbrella.core.model.WikiPage;
|
||||
@@ -9,13 +10,14 @@ import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
import static de.srsoftware.tools.jdbc.Query.*;
|
||||
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.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.ERROR_FAILED_CREATE_TABLE;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.notFound;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.EDIT;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.READ_ONLY;
|
||||
import static de.srsoftware.umbrella.wiki.Constants.*;
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
import static java.text.MessageFormat.format;
|
||||
@@ -101,17 +103,28 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
}
|
||||
}
|
||||
|
||||
private int wikiPermissionCode(Permission perm){
|
||||
return switch (perm){
|
||||
case READ_ONLY -> 1;
|
||||
case ASSIGNEE, EDIT, OWNER -> 2;
|
||||
};
|
||||
}
|
||||
|
||||
private Permission wikiPermission(int code){
|
||||
return switch (code){
|
||||
case 1 -> READ_ONLY;
|
||||
case 2,3 -> EDIT;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, Permission> loadMembers(WikiPage page) {
|
||||
try {
|
||||
var map = new HashMap<Long, Permission>();
|
||||
var rs = select(ALL).from(TABLE_PAGES_USERS).where(PAGE_ID,Condition.equal(page.id())).exec(db);
|
||||
while (rs.next()){
|
||||
var permission = switch (rs.getInt(PERMISSIONS)){
|
||||
case 1 -> Permission.READ_ONLY;
|
||||
case 2, 3 -> Permission.EDIT;
|
||||
default -> null;
|
||||
};
|
||||
var permission = wikiPermission(rs.getInt(PERMISSIONS));
|
||||
if (permission != null) map.put(rs.getLong(USER_ID),permission);
|
||||
}
|
||||
rs.close();
|
||||
@@ -124,7 +137,13 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
@Override
|
||||
public WikiPage save(WikiPage page) {
|
||||
try {
|
||||
insertInto(TABLE_PAGES,ID,VERSION,CONTENT).values(page.id(),page.version(),page.content()).execute(db).close();
|
||||
if (page.isDirty(CONTENT) || page.isDirty(ID)) insertInto(TABLE_PAGES,ID,VERSION,CONTENT).values(page.id(),page.version(),page.content()).execute(db).close();
|
||||
if (page.isDirty(MEMBERS)){
|
||||
Query.delete().from(TABLE_PAGES_USERS).where(PAGE_ID,Condition.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);
|
||||
for (var member : page.members().entrySet()) query.values(page.id(),member.getKey(),wikiPermissionCode(member.getValue().permission()));
|
||||
query.execute(db).close();
|
||||
}
|
||||
return page;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException("Failed to write wiki page \"{0}\" to database",page.id(),e);
|
||||
|
||||
@@ -116,6 +116,6 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
var member = page.members().get(user.id());
|
||||
if (member == null || member.permission() != EDIT) throw forbidden("You are not allowed to edit {0}!",id);
|
||||
var json = json(ex);
|
||||
return sendContent(ex,wikiDb.save(page.patch(json)));
|
||||
return sendContent(ex,wikiDb.save(page.patch(json, userService())));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user