Search in sources :

Example 11 with JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery in project serverless by bluenimble.

the class QueryEntitySpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String sEntity = (String) request.get(CommonSpec.Entity);
    String serializer = (String) request.get(CommonSpec.Serializer);
    String[] levels = Lang.split(serializer, Lang.COMMA);
    int allStopLevel = 2;
    if (levels != null && levels.length > 0) {
        try {
            allStopLevel = Integer.valueOf(levels[0]);
        } catch (Exception ex) {
        }
    }
    int minStopLevel = 3;
    if (levels != null && levels.length > 0) {
        try {
            minStopLevel = Integer.valueOf(levels[1]);
        } catch (Exception ex) {
        }
    }
    JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    payload.set(Database.Fields.Entity, sEntity);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    List<DatabaseObject> records = null;
    try {
        Database db = space.feature(Database.class, provider, request);
        records = db.find(null, new JsonQuery(payload), null);
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    JsonObject result = new JsonObject();
    JsonArray aRecords = new JsonArray();
    result.set(Output.Records, aRecords);
    if (records == null || records.isEmpty()) {
        return new JsonApiOutput(result);
    }
    for (int i = 0; i < records.size(); i++) {
        aRecords.add(records.get(i).toJson(new DefaultDatabaseObjectSerializer(allStopLevel, minStopLevel)));
    }
    return new JsonApiOutput(result);
}
Also used : JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonArray(com.bluenimble.platform.json.JsonArray) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) DefaultDatabaseObjectSerializer(com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 12 with JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery in project serverless by bluenimble.

the class ActivateServiceSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    JsonObject config = request.getService().getCustom();
    Database db = api.space().feature(Database.class, Json.getString(config, Config.Database, ApiSpace.Features.Default), request);
    DatabaseObject account = null;
    try {
        JsonObject query = Json.getObject(config, Config.Query);
        if (query == null) {
            query = new JsonObject();
            JsonObject where = new JsonObject();
            query.set(Query.Construct.where.name(), where);
            where.set(Json.getString(config, Config.ActivationCodeProperty, Defaults.ActivationCode), request.get(Spec.ActivationCode));
        }
        account = db.findOne(Json.getString(config, Config.UsersEntity, Defaults.Users), new JsonQuery(query));
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    if (account == null) {
        throw new ApiServiceExecutionException("account not found").status(ApiResponse.NOT_FOUND);
    }
    Date now = new Date();
    // remove activationCode
    account.remove(Json.getString(config, Config.ActivationCodeProperty, Defaults.ActivationCode));
    // update lastLogin
    try {
        account.set(Json.getString(config, Config.LastLoginProperty, Fields.LastLogin), now);
        account.save();
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    JsonObject oAccount = account.toJson(DefaultDatabaseObjectSerializer.Default);
    // create token
    String[] tokenAndExpiration = SecurityUtils.tokenAndExpiration(api, oAccount, now);
    oAccount.remove(Json.getString(config, Config.PasswordProperty, Spec.Password));
    oAccount.set(Defaults.Token, tokenAndExpiration[0]);
    oAccount.set(Defaults.ExpiresOn, tokenAndExpiration[1]);
    // call onFinish if any
    JsonObject onFinish = Json.getObject(config, Config.onFinish.class.getSimpleName());
    ApiOutput onFinishOutput = SecurityUtils.onFinish(api, consumer, request, onFinish, oAccount);
    if (onFinishOutput != null) {
        oAccount.set(Json.getString(onFinish, Config.onFinish.ResultProperty, Config.onFinish.class.getSimpleName()), onFinishOutput.data());
    }
    return new JsonApiOutput(oAccount);
}
Also used : JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) Config(com.bluenimble.platform.api.impls.im.LoginServiceSpi.Config) JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Date(java.util.Date) ApiOutput(com.bluenimble.platform.api.ApiOutput) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 13 with JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery in project serverless by bluenimble.

the class FindOne2One method main.

public static void main(String[] args) throws Exception {
    String query = "{ }";
    Database db = new DatabaseServer().get();
    List<DatabaseObject> drivers = db.find("Drivers", new JsonQuery(new JsonObject(query)), null);
    for (DatabaseObject driver : drivers) {
        System.out.println(driver.toJson(new DefaultDatabaseObjectSerializer(2, 2)));
    }
}
Also used : JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) DefaultDatabaseObjectSerializer(com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 14 with JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery in project serverless by bluenimble.

the class Find method main.

public static void main(String[] args) throws DatabaseException {
    Database db = new DatabaseServer().get();
    final JsonArray records = new JsonArray();
    JsonObject result = (JsonObject) new JsonObject().set("records", records);
    db.find("Cities", new JsonQuery(new JsonObject()), new Database.Visitor() {

        @Override
        public boolean onRecord(DatabaseObject dbo) {
            try {
                dbo.set("org", "Labs");
                dbo.save();
            } catch (Exception ex) {
                throw new RuntimeException(ex.getMessage(), ex);
            }
            records.add(dbo.toJson(null));
            return false;
        }

        @Override
        public boolean optimize() {
            return true;
        }
    });
    System.out.println(result);
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) Database(com.bluenimble.platform.db.Database) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException)

Example 15 with JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery in project serverless by bluenimble.

the class FindOne method main.

public static void main(String[] args) throws DatabaseException {
    Database db = new DatabaseServer().get();
    DatabaseObject city = db.findOne("Cities", new JsonQuery(new JsonObject()));
    System.out.println(city.toJson(new DefaultDatabaseObjectSerializer(2, 2)));
}
Also used : JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) DefaultDatabaseObjectSerializer(com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Aggregations

JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)20 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)16 JsonObject (com.bluenimble.platform.json.JsonObject)16 Database (com.bluenimble.platform.db.Database)14 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)10 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)4 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)4 CompiledQuery (com.bluenimble.platform.db.query.CompiledQuery)4 Query (com.bluenimble.platform.db.query.Query)4 QueryCompiler (com.bluenimble.platform.db.query.QueryCompiler)4 SqlQueryCompiler (com.bluenimble.platform.db.query.impls.SqlQueryCompiler)4 File (java.io.File)4 Date (java.util.Date)4 ApiOutput (com.bluenimble.platform.api.ApiOutput)3 JsonArray (com.bluenimble.platform.json.JsonArray)3 HashMap (java.util.HashMap)3 Config (com.bluenimble.platform.api.impls.im.LoginServiceSpi.Config)2 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)2 DatabaseException (com.bluenimble.platform.db.DatabaseException)2 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)1