io.cloudboost.CloudTable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JavaSDK Show documentation
Show all versions of JavaSDK Show documentation
An open source Java SDK for CloudBoost developing apps
package io.cloudboost;
import io.cloudboost.Column.DataType;
import io.cloudboost.beans.CBResponse;
import io.cloudboost.json.JSONArray;
import io.cloudboost.json.JSONException;
import io.cloudboost.json.JSONObject;
import io.cloudboost.util.CBParser;
/**
* An abstract wrapper for tables in CloudBoost, with methods to fetch table details, create
* tables, delete etc.
* @author cloudboost
*
*/
public class CloudTable{
protected JSONObject document;
public CloudTable(String tableName){
if(!PrivateMethod._tableValidation(tableName)){
try {
throw new CloudException("Invalid Table Name");
} catch (CloudException e) {
e.printStackTrace();
}
}
try{
this.document = new JSONObject();
this.document.put("name", tableName);
this.document.put("appId", CloudApp.getAppId());
this.document.put("_type", "table");
if(tableName.toLowerCase() == "user"){
this.document.put("type", "user");
this.document.put("maxCount", 1);
}else if(tableName.toLowerCase() == "role"){
this.document.put("type", "role");
this.document.put("maxCount", 1);
}else{
this.document.put("type", "custom");
this.document.put("maxCount", 9999);
}
this.document.put("columns", PrivateMethod._defaultColumns(this.document.getString("type")));
} catch (JSONException e2) {
e2.printStackTrace();
}
}
public String getType(){
try {
return this.document.getString("_type");
} catch (JSONException e) {
return null;
}
}
/***
*
* @param tableName
*/
public void setTableName(String tableName){
try {
this.document.put("name", tableName);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String getTableName(){
try {
return this.document.getString("name");
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
String getTableType(){
try {
return this.document.getString("type");
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
/**
*
* CloudTable Methods
* @throws CloudException
*
*/
/**
*
* Add Column
* @param column
* @param table
* @throws CloudException
*/
public void addColumn(Column column) throws CloudException{
if(!PrivateMethod._columnValidation(column, this)){
throw new CloudException("Invalid Column Found, Do Not Use Reserved Column Names");
}
try{
JSONArray columnList = new JSONArray( this.document.get("columns").toString());
columnList.put(column.document);
this.document.put("columns", columnList);
} catch (JSONException e2) {
e2.printStackTrace();
}
}
public void setColumn(Column column) throws CloudException{
if(!PrivateMethod._columnValidation(column, this)){
throw new CloudException("Invalid Column Found, Do Not Use Reserved Column Names");
}
try{
String name=column.getColumnName();
JSONArray columnList = new JSONArray( this.document.get("columns").toString());
for(int i=0; i