Search in sources :

Example 1 with Database

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

the class GetRecordSpi 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 record = (String) request.get(Spec.Record);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    DatabaseObject dbo = null;
    try {
        Database db = space.feature(Database.class, provider, request);
        dbo = db.get(sEntity, record);
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    if (dbo == null) {
        return null;
    }
    return new JsonApiOutput(dbo.toJson(null));
}
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) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 2 with Database

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

the class AddRecordSpi 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);
    final JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    DatabaseObject dbo;
    try {
        Database db = space.feature(Database.class, provider, request);
        dbo = db.create(sEntity);
        dbo.load(payload);
        dbo.save();
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(ApiOutput.Defaults.Id, dbo.getId()).set(ApiOutput.Defaults.Timestamp, Lang.toUTC(dbo.getTimestamp())));
}
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) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 3 with Database

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

the class CreateEntitySpi 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);
    }
    JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    JsonArray aFields = Json.getArray(payload, CommonSpec.Fields);
    if (aFields == null || aFields.isEmpty()) {
        throw new ApiServiceExecutionException("missing fields definition").status(ApiResponse.BAD_REQUEST);
    }
    Field[] fields = new Field[aFields.count()];
    for (int i = 0; i < aFields.count(); i++) {
        Object o = aFields.get(i);
        if (o == null || !(o instanceof JsonObject)) {
            continue;
        }
        final JsonObject oField = (JsonObject) o;
        fields[i] = new Field() {

            @Override
            public String name() {
                return Json.getString(oField, Spec.Field.Name);
            }

            @Override
            public boolean required() {
                return Json.getBoolean(oField, Spec.Field.Required, false);
            }

            @Override
            public Type type() {
                try {
                    return Type.valueOf(Json.getString(oField, Spec.Field.Type, Type.String.name()));
                } catch (Exception ex) {
                    return Type.String;
                }
            }

            @Override
            public boolean unique() {
                return Json.getBoolean(oField, Spec.Field.Unique, false);
            }
        };
    }
    try {
        Database db = space.feature(Database.class, provider, request);
        db.createEntity(sEntity, fields);
    } 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 : 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) Field(com.bluenimble.platform.db.Database.Field) 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) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 4 with Database

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

the class LoginServiceSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    JsonObject config = request.getService().getCustom();
    JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    Database db = api.space().feature(Database.class, Json.getString(config, Config.Database, ApiSpace.Features.Default), request);
    boolean encryptPassword = Json.getBoolean(config, Config.EncryptPassword, true);
    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.UserProperty, Fields.Email), payload.get(Spec.User));
            where.set(Json.getString(config, Config.PasswordProperty, Fields.Password), encryptPassword ? Crypto.md5(Json.getString(payload, Spec.Password), Encodings.UTF8) : Json.getString(payload, Spec.Password));
        }
        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.UNAUTHORIZED);
    }
    boolean active = true;
    boolean requiresActivation = Json.getBoolean(config, Config.RequiresActivation, false);
    if (requiresActivation && account.get(Json.getString(config, Config.ActivationCodeProperty, Defaults.ActivationCode)) != null) {
        active = false;
    }
    JsonObject oAccount = account.toJson(DefaultDatabaseObjectSerializer.Default);
    oAccount.remove(Json.getString(config, Config.PasswordProperty, Spec.Password));
    if (active) {
        Date now = new Date();
        // update lastLogin
        try {
            account.set(Json.getString(config, Config.LastLoginProperty, Fields.LastLogin), now);
            account.save();
        } catch (Exception ex) {
            throw new ApiServiceExecutionException(ex.getMessage(), ex);
        }
        // create token
        String[] tokenAndExpiration = SecurityUtils.tokenAndExpiration(api, oAccount, now);
        oAccount.set(Defaults.Token, tokenAndExpiration[0]);
        oAccount.set(Defaults.ExpiresOn, tokenAndExpiration[1]);
    }
    // call extend if any
    JsonObject onFinish = Json.getObject(config, Config.onFinish.class.getSimpleName());
    ApiOutput onFinishOutput = SecurityUtils.onFinish(api, consumer, request, onFinish, oAccount);
    oAccount.remove(Database.Fields.Id);
    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) 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 5 with Database

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

the class ResendActivationRequestSpi 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));
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    if (account == null) {
        throw new ApiServiceExecutionException("account not found").status(ApiResponse.NOT_FOUND);
    }
    String email = (String) account.get(Json.getString(config, Config.UserProperty, Fields.Email));
    if (Lang.isNullOrEmpty(email)) {
        throw new ApiServiceExecutionException("user email not found").status(ApiResponse.NOT_FOUND);
    }
    JsonObject oEmail = Json.getObject(config, Config.SignupEmail);
    String feature = Json.getString(oEmail, Email.Messenger);
    String template = Json.getString(oEmail, Email.Template);
    if (oEmail != null && !Lang.isNullOrEmpty(feature) && !Lang.isNullOrEmpty(template)) {
        String fromEmail = Json.getString(oEmail, Email.FromEmail);
        String fromName = Json.getString(oEmail, Email.FromName);
        String subject = Json.getString(oEmail, Email.Subject, "Welcome to " + api.getName());
        final Messenger messenger = api.space().feature(Messenger.class, feature, request);
        final JsonObject emailTemplateData = account.toJson(null);
        try {
            final String fEmail = email;
            api.space().executor().execute(new Callable<Void>() {

                @Override
                public Void call() {
                    try {
                        messenger.send(new JsonSender((JsonObject) new JsonObject().set(JsonActor.Spec.Id, fromEmail).set(JsonActor.Spec.Name, fromName)), new JsonRecipient[] { new JsonRecipient((JsonObject) new JsonObject().set(JsonActor.Spec.Id, fEmail)) }, subject, api.getResourcesManager().get(Lang.split(template, Lang.SLASH)), emailTemplateData);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex.getMessage(), ex);
                    }
                    return null;
                }
            }, CodeExecutor.Mode.Async);
        } catch (Exception ex) {
            throw new ApiServiceExecutionException(ex.getMessage(), ex);
        }
    }
    return new JsonApiOutput(JsonObject.Blank);
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) JsonRecipient(com.bluenimble.platform.messaging.impls.JsonRecipient) Messenger(com.bluenimble.platform.messaging.Messenger) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonSender(com.bluenimble.platform.messaging.impls.JsonSender) 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)

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