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));
}
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;
}
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;
}
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);
}
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);
}
Aggregations