implemented update of shares table (new permission codes)
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -111,7 +111,7 @@ public class Poll implements Mappable {
|
|||||||
private HashMap<Integer,Map<Integer,Integer>> selections = new HashMap<>();
|
private HashMap<Integer,Map<Integer,Integer>> selections = new HashMap<>();
|
||||||
public void count(ResultSet rs) throws SQLException {
|
public void count(ResultSet rs) throws SQLException {
|
||||||
var optionId = rs.getInt(OPTION_ID);
|
var optionId = rs.getInt(OPTION_ID);
|
||||||
var userId = rs.getObject(USER_ID);
|
var userId = rs.getObject(USER);
|
||||||
var weight = rs.getInt(WEIGHT);
|
var weight = rs.getInt(WEIGHT);
|
||||||
var optionStats = selections.computeIfAbsent(optionId, k -> new HashMap<>());
|
var optionStats = selections.computeIfAbsent(optionId, k -> new HashMap<>());
|
||||||
optionStats.compute(weight, (k, sum) -> sum == null ? 1 : sum + 1);
|
optionStats.compute(weight, (k, sum) -> sum == null ? 1 : sum + 1);
|
||||||
|
|||||||
@@ -97,7 +97,6 @@ public class PollModule extends BaseHandler implements PollService {
|
|||||||
var poll = pollDb.loadPoll(pollId);
|
var poll = pollDb.loadPoll(pollId);
|
||||||
var permitted = !poll.isPrivate() || poll.owner().equals(user);
|
var permitted = !poll.isPrivate() || poll.owner().equals(user);
|
||||||
if (!permitted && poll.permissions().get(user) == null) throw forbidden(Text.NOT_ALLOWED_TO_EDIT, Field.OBJECT,Text.POLL);
|
if (!permitted && poll.permissions().get(user) == null) throw forbidden(Text.NOT_ALLOWED_TO_EDIT, Field.OBJECT,Text.POLL);
|
||||||
var eval = pollDb.loadEvaluation(pollId);
|
|
||||||
return sendContent(ex,poll);
|
return sendContent(ex,poll);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,11 @@ import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
|||||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||||
import static de.srsoftware.umbrella.core.constants.Field.DESCRIPTION;
|
import static de.srsoftware.umbrella.core.constants.Field.DESCRIPTION;
|
||||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||||
|
import static de.srsoftware.umbrella.core.model.Permission.READ_ONLY;
|
||||||
import static de.srsoftware.umbrella.poll.Constants.*;
|
import static de.srsoftware.umbrella.poll.Constants.*;
|
||||||
import static java.text.MessageFormat.format;
|
import static java.text.MessageFormat.format;
|
||||||
|
|
||||||
|
import de.srsoftware.tools.jdbc.Query;
|
||||||
import de.srsoftware.umbrella.core.BaseDb;
|
import de.srsoftware.umbrella.core.BaseDb;
|
||||||
import de.srsoftware.umbrella.core.constants.Field;
|
import de.srsoftware.umbrella.core.constants.Field;
|
||||||
import de.srsoftware.umbrella.core.constants.Text;
|
import de.srsoftware.umbrella.core.constants.Text;
|
||||||
@@ -41,8 +43,10 @@ public class SqliteDb extends BaseDb implements PollDb {
|
|||||||
createSelectionsTable();
|
createSelectionsTable();
|
||||||
case 1:
|
case 1:
|
||||||
createSharesTable();
|
createSharesTable();
|
||||||
|
case 2:
|
||||||
|
updateSharesTable();
|
||||||
}
|
}
|
||||||
return setCurrentVersion(2);
|
return setCurrentVersion(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createOptionsTable() {
|
private void createOptionsTable() {
|
||||||
@@ -296,4 +300,49 @@ public class SqliteDb extends BaseDb implements PollDb {
|
|||||||
return option;
|
return option;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateSharesTable() {
|
||||||
|
try {
|
||||||
|
var newPermissions = new HashMap<String, Map<Long, Permission>>(); // Map from PollId → (Map from UserId → Permission)
|
||||||
|
var rs = select(ALL).from(TABLE_SHARES).exec(db);
|
||||||
|
while (rs.next()) {
|
||||||
|
var pollID = rs.getString(POLL_ID);
|
||||||
|
var userId = rs.getLong(USER_ID);
|
||||||
|
var perm = rs.getInt(PERMISSION);
|
||||||
|
var userMap = newPermissions.computeIfAbsent(pollID, k -> new HashMap<>());
|
||||||
|
switch (perm) {
|
||||||
|
case 0:
|
||||||
|
userMap.put(userId, READ_ONLY);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
userMap.put(userId, Permission.EDIT);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
userMap.put(userId, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
for (var pollEntry : newPermissions.entrySet()){
|
||||||
|
var pollId = pollEntry.getKey();
|
||||||
|
for (var userEntry : pollEntry.getValue().entrySet()){
|
||||||
|
var userId = userEntry.getKey();
|
||||||
|
var permission = userEntry.getValue();
|
||||||
|
if (permission == null) {
|
||||||
|
Query.delete().from(TABLE_SHARES)
|
||||||
|
.where(USER_ID, equal(userId))
|
||||||
|
.where(POLL_ID, equal(pollId))
|
||||||
|
.execute(db);
|
||||||
|
} else {
|
||||||
|
Query.update(TABLE_SHARES)
|
||||||
|
.where(USER_ID, equal(userId))
|
||||||
|
.where(POLL_ID, equal(pollId))
|
||||||
|
.set(PERMISSION).prepare(db)
|
||||||
|
.apply(permission.code()).close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException ex){
|
||||||
|
throw databaseException("Failed to update permissions in shares table!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user