Search in sources :

Example 1 with Favoritable

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);
        }
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) List(java.util.List) Principal(org.structr.core.entity.Principal) Favoritable(org.structr.core.entity.Favoritable)

Aggregations

List (java.util.List)1 FrameworkException (org.structr.common.error.FrameworkException)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 Favoritable (org.structr.core.entity.Favoritable)1 Principal (org.structr.core.entity.Principal)1 Tx (org.structr.core.graph.Tx)1