Search in sources :

Example 11 with DatabaseException

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

the class BulkRecordsSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    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);
    }
    try {
        return new JsonApiOutput(space.feature(Database.class, provider, request).bulk(payload));
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 12 with DatabaseException

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

the class DeleteRecordSpi 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);
    }
    try {
        Database db = space.feature(Database.class, provider, request);
        DatabaseObject oRecord = db.get(sEntity, record);
        if (oRecord == null) {
            return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Deleted, false));
        }
        oRecord.delete();
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Deleted, 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) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 13 with DatabaseException

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

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

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

the class DatabaseObjectImpl method getSetRelationship.

private Object getSetRelationship(String key, Object v) {
    if (!Document.class.isAssignableFrom(v.getClass())) {
        return null;
    }
    Document ref = (Document) v;
    Object one2ManyLink = ref.get(One2ManyLinkKey);
    if (one2ManyLink != null) {
        @SuppressWarnings({ "rawtypes", "unchecked" }) DatabaseObjectList list = new DatabaseObjectList(db, (List<Document>) one2ManyLink);
        try {
            set(key, list);
        } catch (DatabaseException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return list;
    } else {
        String entity = ref.getString(ObjectEntityKey);
        Object id = ref.get(ObjectIdKey);
        if (entity == null || id == null) {
            return null;
        }
        DatabaseObjectImpl refObject = new DatabaseObjectImpl(db, entity, id, true);
        refObject.persistent = true;
        try {
            set(key, refObject);
        } catch (DatabaseException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return refObject;
    }
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) Document(org.bson.Document) DatabaseException(com.bluenimble.platform.db.DatabaseException)

Aggregations

DatabaseException (com.bluenimble.platform.db.DatabaseException)19 JsonObject (com.bluenimble.platform.json.JsonObject)11 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)8 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)8 ApiSpace (com.bluenimble.platform.api.ApiSpace)8 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)8 Database (com.bluenimble.platform.db.Database)8 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)8 JsonArray (com.bluenimble.platform.json.JsonArray)3 IOException (java.io.IOException)3 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)2 OCommandOutputListener (com.orientechnologies.orient.core.command.OCommandOutputListener)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 Document (org.bson.Document)2 Field (com.bluenimble.platform.db.Database.Field)1 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)1 DeleteResult (com.mongodb.client.result.DeleteResult)1 ODatabaseExport (com.orientechnologies.orient.core.db.tool.ODatabaseExport)1 ODatabaseImport (com.orientechnologies.orient.core.db.tool.ODatabaseImport)1