Search in sources :

Example 16 with JsonArray

use of com.bluenimble.platform.json.JsonArray 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 17 with JsonArray

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

the class BuildUtils method generate.

public static JsonArray generate(File apiFolder) throws Exception {
    JsonArray dsList = new JsonArray();
    File dataSourcesFolder = new File(apiFolder, Resources + Lang.SLASH + DataSources);
    if (!dataSourcesFolder.exists() || !dataSourcesFolder.isDirectory()) {
        return null;
    }
    File[] dataSources = dataSourcesFolder.listFiles(new FileFilter() {

        @Override
        public boolean accept(File file) {
            return file.isDirectory();
        }
    });
    if (dataSources == null || dataSources.length == 0) {
        return null;
    }
    File javaSrc = new File(apiFolder, JavaSource);
    if (!javaSrc.exists()) {
        javaSrc.mkdirs();
    }
    Persistence persistence = new Persistence();
    // generate sources
    for (File dsf : dataSources) {
        DataSource ds = new DataSource(dsf.getName());
        dsList.add(ds.getName());
        persistence.addDataSource(ds);
        loadEntities(ds, dsf, dsf, javaSrc);
    }
    // compile sources
    File javaBin = new File(apiFolder, JavaBinary);
    if (!javaBin.exists()) {
        javaBin.mkdirs();
    }
    new SourceCompiler(javaSrc, javaBin).compile();
    // create META-INF/persistence.xml
    File metaInf = new File(new File(apiFolder, JavaBinary), MetaInf);
    if (!metaInf.exists()) {
        metaInf.mkdirs();
    }
    File pXml = new File(metaInf, PersistenceXml);
    FileWriter writer = null;
    try {
        writer = new FileWriter(pXml);
        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
        writer.write("<persistence xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
        writer.write("   xsi:schemaLocation=\"http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd\"\n");
        writer.write("   version=\"2.0\" xmlns=\"http://java.sun.com/xml/ns/persistence\">\n");
        // add persistence units
        for (DataSource ds : persistence.getDataSources()) {
            writer.write("\t<persistence-unit name=\"" + ds.getName() + "\" transaction-type=\"RESOURCE_LOCAL\">\n");
            writer.write("\t\t<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>\n");
            // add classes
            for (DSEntity entity : ds.getEntities()) {
                writer.write("\t\t<class>" + (entity.getPackage() == null ? Lang.BLANK : Lang.replace(entity.getPackage(), Lang.SLASH, Lang.DOT) + Lang.DOT) + entity.getName() + "</class>\n");
            }
            // add properties
            File properties = new File(dataSourcesFolder, ds.getName() + Lang.SLASH + Properties);
            if (properties.exists()) {
                JsonObject oProperties = Json.load(properties);
                if (!Json.isNullOrEmpty(oProperties)) {
                    writer.write("\t\t<properties>\n");
                    Iterator<String> keys = oProperties.keys();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        writer.write("\t\t\t<property name=\"" + key + "\" value=\"" + oProperties.get(key) + "\" />\n");
                    }
                    writer.write("\t\t</properties>\n");
                }
            }
            writer.write("\t</persistence-unit>\n");
        }
        writer.write("</persistence>");
    } finally {
        IOUtils.closeQuietly(writer);
    }
    // create jar file
    File apiLibs = new File(apiFolder, Lib);
    if (!apiLibs.exists()) {
        apiLibs.mkdirs();
    }
    ArchiveUtils.compress(javaBin, new File(apiLibs, DataSources + Lang.UUID(6) + JarExt), true, new ArchiveUtils.CompressVisitor() {

        @Override
        public boolean onAdd(File file) {
            if (file.getName().startsWith(Lang.DOT)) {
                return false;
            }
            return true;
        }
    });
    return dsList;
}
Also used : FileWriter(java.io.FileWriter) JsonObject(com.bluenimble.platform.json.JsonObject) JsonArray(com.bluenimble.platform.json.JsonArray) ArchiveUtils(com.bluenimble.platform.ArchiveUtils) FileFilter(java.io.FileFilter) File(java.io.File)

Example 18 with JsonArray

use of com.bluenimble.platform.json.JsonArray 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 19 with JsonArray

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

the class JsonCountHandler method execute.

@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 1) {
        throw new CommandExecutionException("json variable name required");
    }
    String var = args[0];
    @SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    Object o = vars.get(var);
    if (o == null) {
        throw new CommandExecutionException("variable '" + var + "' not found");
    }
    if (!(o instanceof JsonObject)) {
        throw new CommandExecutionException("variable '" + var + "' isn't a valid json object");
    }
    JsonObject json = (JsonObject) o;
    if (args.length < 2) {
        return new DefaultCommandResult(CommandResult.OK, json.count());
    }
    String prop = args[1];
    String[] path = Lang.split(prop, Lang.DOT);
    Object child = Json.find(json, path);
    if (child == null) {
        throw new CommandExecutionException(Lang.join(path, Lang.DOT) + " not found");
    }
    if (child instanceof JsonObject) {
        return new DefaultCommandResult(CommandResult.OK, ((JsonObject) child).count());
    } else if (child instanceof JsonArray) {
        return new DefaultCommandResult(CommandResult.OK, ((JsonArray) child).count());
    }
    return new DefaultCommandResult(CommandResult.OK, 0);
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map)

Example 20 with JsonArray

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

the class JsonSetHandler method execute.

@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 1) {
        throw new CommandExecutionException("json variable name required");
    }
    if (args.length < 2) {
        throw new CommandExecutionException("json property required");
    }
    String var = args[0];
    @SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    Object o = vars.get(var);
    if (o == null) {
        throw new CommandExecutionException("variable '" + var + "' not found");
    }
    if (!(o instanceof JsonObject)) {
        throw new CommandExecutionException("variable '" + var + "' isn't a valid json object");
    }
    JsonObject json = (JsonObject) o;
    String prop = args[1];
    // it's a delete
    int indexOfDot = prop.indexOf(Lang.DOT);
    String value = args[2];
    boolean isJson = false;
    boolean isVar = false;
    Object oValue = null;
    if (value.startsWith("j\\")) {
        value = value.substring(3);
        isJson = true;
    }
    if (value.startsWith("v\\")) {
        value = value.substring(3);
        isVar = true;
    }
    if (Lang.isNullOrEmpty(value)) {
        isJson = false;
    }
    oValue = value;
    if (isJson) {
        try {
            oValue = JsonParser.parse(value);
        } catch (Exception ex) {
            throw new CommandExecutionException(ex.getMessage(), ex);
        }
    }
    if (isVar) {
        oValue = vars.get(value);
    }
    if (indexOfDot <= 0) {
        json.set(prop, oValue);
    } else {
        String[] path = Lang.split(prop, Lang.DOT);
        prop = path[path.length - 1];
        path = Lang.moveRight(path, 1);
        Object child = Json.find(json, path);
        if (child == null) {
            throw new CommandExecutionException(Lang.join(path, Lang.DOT) + " not found");
        }
        if (child instanceof JsonObject) {
            ((JsonObject) child).set(prop, oValue);
        } else if (child instanceof JsonArray) {
            boolean append = false;
            int iProp = -1;
            if (prop.equals(Lang.UNDERSCORE)) {
                append = true;
            } else {
                try {
                    iProp = Integer.valueOf(prop);
                } catch (Exception ex) {
                // ignore
                }
            }
            JsonArray array = (JsonArray) child;
            if (append) {
                array.add(oValue);
            } else {
                if (iProp > -1 && array.count() > iProp) {
                    ((JsonArray) child).add(iProp, oValue);
                }
            }
        }
    }
    return new DefaultCommandResult(CommandResult.OK, json);
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonArray(com.bluenimble.platform.json.JsonArray) DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map)

Aggregations

JsonArray (com.bluenimble.platform.json.JsonArray)42 JsonObject (com.bluenimble.platform.json.JsonObject)37 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 Map (java.util.Map)8 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)5 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)5 Database (com.bluenimble.platform.db.Database)5 IOException (java.io.IOException)5 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)4 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)4 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)4 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)4 ApiService (com.bluenimble.platform.api.ApiService)3 DescribeOption (com.bluenimble.platform.api.DescribeOption)3 DatabaseException (com.bluenimble.platform.db.DatabaseException)3 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)3 KeyPair (com.bluenimble.platform.security.KeyPair)3 ArrayList (java.util.ArrayList)3 ApiOutput (com.bluenimble.platform.api.ApiOutput)2