use of com.bluenimble.platform.db.DatabaseException 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));
}
use of com.bluenimble.platform.db.DatabaseException 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())));
}
use of com.bluenimble.platform.db.DatabaseException 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));
}
use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class DescribeDatabaseSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String provider = (String) request.get(CommonSpec.Provider);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
JsonObject describe;
try {
describe = space.feature(Database.class, provider, request).describe();
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
return new JsonApiOutput(describe);
}
use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class OrientDatabase method schedule.
@Override
public void schedule(String event, Query query, String cron) throws DatabaseException {
if (query == null || Lang.isNullOrEmpty(query.entity()) || query.construct() == null) {
throw new DatabaseException("query to schedule should provide the entity name and the construct [insert, update or delete]");
}
try {
OScheduler sch = db.getMetadata().getScheduler();
OScheduledEvent oEvent = sch.getEvent(event);
if (oEvent != null) {
sch.removeEvent(event);
}
sch.scheduleEvent(new OScheduledEventBuilder().setName(event).setRule(cron).setFunction(newFunction(event, query)).build());
} catch (Exception ex) {
throw new DatabaseException(ex.getMessage(), ex);
}
}
Aggregations