org.entcore.common.mongodb.MongoDbResult.java Source code

Java tutorial

Introduction

Here is the source code for org.entcore.common.mongodb.MongoDbResult.java

Source

/* Copyright  "Open Digital Education", 2014
 *
 * This program is published by "Open Digital Education".
 * You must indicate the name of the software and the company in any production /contribution
 * using the software and indicate on the home page of the software industry in question,
 * "powered by Open Digital Education" with a reference to the website: https://opendigitaleducation.com/.
 *
 * This program is free software, licensed under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, version 3 of the License.
 *
 * You can redistribute this application and/or modify it since you respect the terms of the GNU Affero General Public License.
 * If you modify the source code and then use this modified source code in your creation, you must make available the source code of your modifications.
 *
 * You should have received a copy of the GNU Affero General Public License along with the software.
 * If not, please see : <http://www.gnu.org/licenses/>. Full compliance requires reading the terms of this license and following its directives.
    
 *
 */

package org.entcore.common.mongodb;

import fr.wseduc.webutils.Either;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

public class MongoDbResult {

    public static Either<String, JsonObject> validActionResult(Message<JsonObject> res) {
        if ("ok".equals(res.body().getString("status"))) {
            res.body().remove("status");
            return new Either.Right<>(res.body());
        } else {
            return new Either.Left<>(res.body().getString("message", ""));
        }
    }

    public static Either<String, JsonObject> validResult(Message<JsonObject> res) {
        if ("ok".equals(res.body().getString("status"))) {
            JsonObject r = res.body().getJsonObject("result", new JsonObject());
            return new Either.Right<>(r);
        } else {
            return new Either.Left<>(res.body().getString("message", ""));
        }
    }

    public static Either<String, JsonArray> validResults(Message<JsonObject> res) {
        if ("ok".equals(res.body().getString("status"))) {
            return new Either.Right<>(
                    res.body().getJsonArray("results", new fr.wseduc.webutils.collections.JsonArray()));
        } else {
            return new Either.Left<>(res.body().getString("message", ""));
        }
    }

    public static Handler<Message<JsonObject>> validActionResultHandler(
            final Handler<Either<String, JsonObject>> handler) {
        return new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> event) {
                handler.handle(validActionResult(event));
            }
        };
    }

    public static Handler<Message<JsonObject>> validResultHandler(
            final Handler<Either<String, JsonObject>> handler) {
        return new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> event) {
                handler.handle(validResult(event));
            }
        };
    }

    public static Handler<Message<JsonObject>> validResultsHandler(
            final Handler<Either<String, JsonArray>> handler) {
        return new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> event) {
                handler.handle(validResults(event));
            }
        };
    }

}