Search in sources :

Example 96 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class DatabaseObjectImpl method describe.

private JsonObject describe() {
    JsonObject o = new JsonObject();
    o.set("entity", entity);
    o.set("persistent", persistent);
    o.set("partial", partial);
    o.set("document", document.hashCode());
    o.set("anyUpdates", update == null ? false : !update.isEmpty());
    JsonObject data = new JsonObject();
    o.set("data", data);
    Iterator<String> keys = keys();
    if (keys == null) {
        return o;
    }
    while (keys.hasNext()) {
        String key = keys.next();
        Object value = document.get(key);
        if (value instanceof DatabaseObjectImpl) {
            data.set(key, ((DatabaseObjectImpl) value).describe());
        } else {
            if (key.equals(ObjectIdKey)) {
                data.set(key, value.getClass().getSimpleName() + "(" + value.toString() + ")");
            } else {
                data.set(key, value.getClass().getSimpleName());
            }
        }
    }
    return o;
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject)

Example 97 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class DatabaseObjectImpl method get.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Object get(String key) {
    if (Database.Fields.Id.equals(key)) {
        return getId();
    }
    if (partial) {
        refresh();
    }
    Object v = document.get(key);
    if (v == null) {
        return null;
    }
    Object refValue = getSetRelationship(key, v);
    if (refValue != null) {
        return refValue;
    }
    if (Map.class.isAssignableFrom(v.getClass()) && !(v instanceof JsonObject)) {
        return new JsonObject((Map) v, true);
    } else if (List.class.isAssignableFrom(v.getClass())) {
        List<Object> objects = (List<Object>) v;
        if (objects.isEmpty()) {
            return null;
        }
        return new JsonArray(objects);
    }
    return v;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 98 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class DatabaseObjectImpl method load.

@Override
public void load(JsonObject data) throws DatabaseException {
    if (data == null) {
        return;
    }
    data.shrink();
    if (Json.isNullOrEmpty(data)) {
        return;
    }
    Iterator<String> keys = data.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        Object value = data.get(key);
        if (value == null || value.equals(Null) || Undefined.class.equals(value.getClass())) {
            document.remove(key);
            continue;
        }
        set(key, data.get(key));
    }
}
Also used : Undefined(jdk.nashorn.internal.runtime.Undefined) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject)

Example 99 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class WorkspaceCommand method execute.

@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    String workFolder = (String) tool.currentContext().get(ToolContext.CommandLine);
    try {
        if (Lang.isNullOrEmpty(workFolder)) {
            if (BlueNimble.Workspace == null) {
                tool.printer().warning("No workspace found!\nUse command 'ws [Your Workspace Foler Path]'.\nExample: ws /applications/bluenimble");
            } else {
                tool.printer().content("Workspace", BlueNimble.Workspace.getAbsolutePath());
                String currentApi = Json.getString(BlueNimble.Config, CliSpec.Config.CurrentApi);
                JsonObject apis = Json.getObject(BlueNimble.Config, CliSpec.Config.Apis);
                if (apis != null && !apis.isEmpty()) {
                    StringBuilder sApis = new StringBuilder();
                    Iterator<String> keys = apis.keys();
                    while (keys.hasNext()) {
                        String api = keys.next();
                        String prefix = "    ";
                        if (api.equals(currentApi)) {
                            prefix = "(C) ";
                        }
                        sApis.append(prefix + api).append(Lang.ENDLN);
                    }
                    tool.printer().content("Apis", sApis.toString());
                    sApis.setLength(0);
                } else {
                    tool.printer().content("Apis", "No apis found in this workspace");
                }
            }
            return null;
        }
    } catch (Exception ex) {
        throw new CommandExecutionException(ex.getMessage(), ex);
    }
    File workspace = new File(workFolder);
    if (!workspace.exists() || !workspace.isDirectory()) {
        throw new CommandExecutionException(workFolder + " is not a valid folder");
    }
    try {
        BlueNimble.workspace(workspace);
    } catch (Exception ex) {
        throw new CommandExecutionException(ex.getMessage(), ex);
    }
    return new DefaultCommandResult(CommandResult.OK, "workspace sat to '" + workspace.getAbsolutePath() + "'");
}
Also used : DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) File(java.io.File) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 100 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class CreateApiHandler method execute.

@SuppressWarnings("unchecked")
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 1) {
        throw new CommandExecutionException("api namespace required. ex. create api myapi");
    }
    String namespace = args[0];
    if (!Pattern.matches("^[a-zA-Z0-9_-]*$", namespace)) {
        throw new CommandExecutionException("api namespace must contain only numbers, letters, '_' and '-'");
    }
    String sApiFolder = namespace;
    if (args.length > 1) {
        sApiFolder = args[1];
    }
    File apiFolder = new File(BlueNimble.Workspace, sApiFolder);
    if (apiFolder.exists()) {
        try {
            tool.printer().info(sApiFolder + " already exists");
            String exists = Json.getString(Json.getObject(BlueNimble.Config, CliSpec.Config.Apis), namespace);
            if (!Lang.isNullOrEmpty(exists)) {
                return null;
            }
            tool.printer().info("binding to the current workspace");
            String ns = BlueNimble.loadApi(apiFolder);
            if (ns == null) {
                tool.printer().warning("unable to bind current api. Please check if the api.json exists and the namespace is there.");
            }
            Json.getObject(BlueNimble.Config, CliSpec.Config.Apis).set(ns, sApiFolder);
            BlueNimble.Config.set(CliSpec.Config.CurrentApi, sApiFolder);
        } catch (Exception ex) {
            throw new CommandExecutionException(ex.getMessage(), ex);
        }
    }
    Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    String template = (String) vars.get(BlueNimble.DefaultVars.TemplateApi);
    if (Lang.isNullOrEmpty(template)) {
        template = DefaultTemplate;
    }
    File fTemplate = new File(new File(BlueNimble.Home, Templates.class.getSimpleName().toLowerCase() + Lang.SLASH + Templates.Apis), template);
    if (!fTemplate.exists()) {
        throw new CommandExecutionException("api template '" + template + "' not installed");
    }
    apiFolder.mkdir();
    try {
        FileUtils.copy(fTemplate, apiFolder, false);
    } catch (Exception ex) {
        throw new CommandExecutionException(ex.getMessage(), ex);
    }
    String specLang = (String) vars.get(BlueNimble.DefaultVars.SpecLanguage);
    if (Lang.isNullOrEmpty(specLang)) {
        specLang = BlueNimble.SpecLangs.Json;
    }
    CodeGenUtils.writeAll(apiFolder, (JsonObject) new JsonObject().set(Namespace, namespace), specLang);
    BlueNimble.Config.set(CliSpec.Config.CurrentApi, namespace);
    JsonObject oApis = Json.getObject(BlueNimble.Config, CliSpec.Config.Apis);
    if (oApis == null) {
        oApis = new JsonObject();
        BlueNimble.Config.set(CliSpec.Config.Apis, oApis);
    }
    oApis.set(namespace, sApiFolder);
    boolean secure = false;
    String sSecure = (String) vars.get(BlueNimble.DefaultVars.ApiSecurityEnabled);
    if (Lang.isNullOrEmpty(sSecure)) {
        secure = false;
    } else {
        secure = Lang.TrueValues.contains(sSecure.trim().toLowerCase());
    }
    try {
        JsonObject apiSpec = SpecUtils.read(apiFolder);
        JsonObject codeGen = Json.getObject(apiSpec, "_codegen_");
        if (codeGen != null) {
            secure = Json.getBoolean(codeGen, "secure", true);
            apiSpec.remove("_codegen_");
            SpecUtils.write(apiFolder, apiSpec);
        }
        BlueNimble.saveConfig();
        tool.printer().content("Api '" + namespace + "' created! path: $ws/ " + sApiFolder, BlueNimble.SpecLangs.Json.equals(specLang) ? apiSpec.toString(2) : new YamlStringPrinter(2).print(apiSpec).toString());
    } catch (Exception e) {
        throw new CommandExecutionException(e.getMessage(), e);
    }
    if (secure) {
        SecureApiHandler.execute(tool, new String[] { namespace, "token+signature", Lang.STAR });
    }
    return new DefaultCommandResult(CommandResult.OK, null);
}
Also used : DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) Templates(com.bluenimble.platform.icli.mgm.CliSpec.Templates) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) YamlStringPrinter(com.bluenimble.platform.json.printers.YamlStringPrinter) File(java.io.File) Map(java.util.Map) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Aggregations

JsonObject (com.bluenimble.platform.json.JsonObject)230 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)40 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)37 JsonArray (com.bluenimble.platform.json.JsonArray)37 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)34 Database (com.bluenimble.platform.db.Database)29 ApiSpace (com.bluenimble.platform.api.ApiSpace)26 File (java.io.File)25 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)23 Map (java.util.Map)22 IOException (java.io.IOException)20 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)17 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)16 InputStream (java.io.InputStream)14 Date (java.util.Date)14 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)13 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)12 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)11 HashMap (java.util.HashMap)11 DatabaseException (com.bluenimble.platform.db.DatabaseException)9