Search in sources :

Example 21 with Database

use of com.bluenimble.platform.db.Database in project serverless by bluenimble.

the class DropEntitySpi 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);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    try {
        Database db = space.feature(Database.class, provider, request);
        db.drop(sEntity);
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Dropped, true));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 22 with Database

use of com.bluenimble.platform.db.Database in project serverless by bluenimble.

the class DatabaseApiRequestTrack method finish.

@Override
public void finish(JsonObject feedback) {
    final Date now = new Date();
    Json.getObject(track, Fields.Time.class.getSimpleName().toLowerCase()).set(Fields.Time.Finished, now);
    int code = Json.getInteger(feedback, ApiResponse.Error.Code, ApiResponse.OK.getCode());
    boolean isError = code >= ApiResponse.BAD_REQUEST.getCode();
    track.set(Fields.Status, isError ? 0 : 1);
    track.set(Fields.Feedback, feedback);
    JsonObject metrics = Json.getObject(track, Fields.Metrics.class.getSimpleName().toLowerCase());
    if (MX.isCurrentThreadCpuTimeSupported()) {
        metrics.set(Fields.Metrics.Cpu, MX.getCurrentThreadUserTime());
    }
    if (MX.isCurrentThreadCpuTimeSupported()) {
        metrics.set(Fields.Metrics.Memory, MX.getThreadAllocatedBytes(Thread.currentThread().getId()));
    }
    try {
        tracker.executor().execute(new Runnable() {

            @Override
            public void run() {
                // shrink
                track.shrink();
                JsonObject oTracking = api.getTracking();
                // put
                Database db = null;
                try {
                    db = api.space().feature(Database.class, Json.getString(oTracking, Spec.Feature, ApiSpace.Features.Default), tracker.context());
                    if (db == null) {
                        api.tracer().log(Tracer.Level.Error, "CantStore[{0}] due to database declared in the api is not defined at the space level", track.toString());
                        return;
                    }
                    // put
                    DatabaseObject entity = db.create(Json.getString(oTracking, Spec.Entity, DefaultEntity));
                    entity.load(track);
                    entity.save();
                } catch (DatabaseException e) {
                    api.tracer().log(Tracer.Level.Error, "CantStore[{0}] due to {1}", track.toString(), e.getMessage());
                    api.tracer().log(Tracer.Level.Error, Lang.BLANK, e);
                } finally {
                    if (db != null) {
                        db.recycle();
                    }
                }
            }
        });
    } catch (Exception ex) {
        api.tracer().log(Tracer.Level.Error, Lang.BLANK, ex);
    }
}
Also used : Database(com.bluenimble.platform.db.Database) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) Date(java.util.Date) DatabaseException(com.bluenimble.platform.db.DatabaseException)

Example 23 with Database

use of com.bluenimble.platform.db.Database 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 24 with Database

use of com.bluenimble.platform.db.Database in project serverless by bluenimble.

the class ChangePasswordSpi 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 {
        account = db.get(Json.getString(config, Config.UsersEntity, Defaults.Users), (String) consumer.get(ApiConsumer.Fields.Id));
        account.set(Json.getString(config, Config.PasswordProperty, Spec.Password), Lang.md5((String) request.get(Spec.Password)));
        account.save();
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    ApiOutput result = new JsonApiOutput(JsonObject.Blank);
    // call extend if any
    if (config.containsKey(Config.onFinish.class.getSimpleName())) {
        JsonObject oAccount = account.toJson(null);
        result = SecurityUtils.onFinish(api, consumer, request, Json.getObject(config, Config.onFinish.class.getSimpleName()), oAccount);
    }
    return result;
}
Also used : 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) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 25 with Database

use of com.bluenimble.platform.db.Database 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)

Aggregations

Database (com.bluenimble.platform.db.Database)41 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)37 JsonObject (com.bluenimble.platform.json.JsonObject)27 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)20 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)14 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)11 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)11 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)6 JsonArray (com.bluenimble.platform.json.JsonArray)5 ApiOutput (com.bluenimble.platform.api.ApiOutput)4 Date (java.util.Date)4 Config (com.bluenimble.platform.api.impls.im.LoginServiceSpi.Config)2 Messenger (com.bluenimble.platform.messaging.Messenger)2 JsonRecipient (com.bluenimble.platform.messaging.impls.JsonRecipient)2 JsonSender (com.bluenimble.platform.messaging.impls.JsonSender)2 Field (com.bluenimble.platform.db.Database.Field)1 File (java.io.File)1