Search in sources :

Example 16 with Database

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

the class FindNotLike method main.

public static void main(String[] args) throws Exception {
    String query = "{ where: { name: { op: nlike, value: m } } }";
    Database db = new DatabaseServer().get();
    List<DatabaseObject> employees = db.find("Employees", new JsonQuery(new JsonObject(query)), null);
    for (DatabaseObject employee : employees) {
        System.out.println(employee.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 17 with Database

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

the class Create method main.

public static void main(String[] args) throws DatabaseException {
    Database db = new DatabaseServer().get();
    DatabaseObject employee = db.create("Employees");
    JsonArray names = new JsonArray();
    names.add(new JsonObject().set("number", "4098776623").set("weight", 40));
    employee.set("name", "New-1");
    employee.set("age", 27);
    employee.set("active", true);
    employee.set("salary", 48.50);
    employee.set("names", names);
    employee.save();
    System.out.println(employee.toJson(null));
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 18 with Database

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

the class CreateOne2One method main.

public static void main(String[] args) throws DatabaseException {
    Database db = new DatabaseServer().get();
    // create driver
    DatabaseObject driver = db.create("Drivers");
    driver.set("name", "One2One-New-2");
    driver.set("info", new JsonObject().set("x", "40987").set("y", 76623));
    driver.set("salary", 48.50);
    // create car
    DatabaseObject car = db.create("Cars");
    car.set("model", "Honda");
    car.set("year", "2040");
    driver.set("car", car);
    driver.save();
    System.out.println(driver.toJson(null));
}
Also used : Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 19 with Database

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

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

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