use of org.structr.core.entity.Favoritable in project structr by structr.
the class FavoritesCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final Map<String, Object> data = webSocketData.getNodeData();
final String mode = (String) data.get("mode");
final String favoritableId = (String) data.get("id");
final Principal currentUser = webSocket.getCurrentUser();
if (mode == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Favorites Command: No mode given. Valid modes: add, remove").build(), true);
} else if (favoritableId == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Favorites Command: No favoritable id given").build(), true);
} else {
final App app = StructrApp.getInstance(webSocket.getSecurityContext());
try (final Tx tx = app.tx()) {
final Favoritable file = app.get(Favoritable.class, favoritableId);
if (file != null) {
final List<Favoritable> favorites = currentUser.getFavorites();
switch(mode) {
case "add":
{
favorites.add((Favoritable) file);
break;
}
case "remove":
{
favorites.remove((Favoritable) file);
break;
}
default:
getWebSocket().send(MessageBuilder.status().code(422).message("Favorites Command: Invalid mode '" + mode + "'. Valid modes: add, remove").build(), true);
return;
}
currentUser.setFavorites(favorites);
getWebSocket().send(MessageBuilder.finished().callback(callback).build(), true);
} else {
getWebSocket().send(MessageBuilder.status().code(422).message("Favorites Command: Favoritable with id '" + favoritableId + "'does not exist!").build(), true);
}
tx.success();
} catch (FrameworkException fex) {
getWebSocket().send(MessageBuilder.status().code(422).message("Favorites Command: Favoritable with id '" + favoritableId + "'does not exist!").build(), true);
}
}
}
Aggregations