com.hadoopz.cloud.JCloud.SDK.utils.TableStructureLoader Maven / Gradle / Ivy
The newest version!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadoopz.cloud.JCloud.SDK.utils;
import com.mycomm.IProtocol.beans.JDataTypes;
import com.mycomm.IProtocol.beans.TableFieldStructure;
import com.mycomm.IProtocol.log.UniversalLogHolder;
import com.mycomm.YesHttp.core.HttpMethod;
import com.mycomm.YesHttp.core.Request;
import com.mycomm.YesHttp.core.Response;
import com.mycomm.YesHttp.core.StringRequest;
import com.mycomm.YesHttp.core.TextBaseResponseListener;
import com.mycomm.YesHttp.core.YesHttpEngine;
import com.mycomm.YesHttp.core.YesHttpError;
import com.mycomm.itool.SystemUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author jw362j
*/
public class TableStructureLoader {
public List loadTable(String tableName, final String jcloudToken) {
final List structures = new ArrayList();
String url = EndpointBuilder.getFullUrlForLoadTable(tableName);
//final JSONObject ps = new JSONObject();
Request request = new StringRequest(HttpMethod.POST, url, new TextBaseResponseListener() {
@Override
public void responseMe(String msg) {
YesLog.yeslog.d("the requestjSON TextBaseResponseListener.responseMe:" + msg);
try {
YesLog.yeslog.d("the requestjSON TextBaseResponseListener.responseMe:" + msg);
JSONObject response = new JSONObject(msg);
if (!response.has("datas")) {
return;
}
JSONArray table = response.getJSONArray("datas");
for (int i = 0; i < table.length(); i++) {
JSONObject field = table.getJSONObject(i);
structures.add(JCloudTableFieldStructure(field));
}
} catch (JSONException ex) {
UniversalLogHolder.d(getClass().getSimpleName(), ex.getMessage());
}
}
}, new Response.ErrorListener() {
public void onErrorResponse(YesHttpError error) {
YesLog.yeslog.e("the requestjSON ErrorListener.onErrorResponse:" + error.getMessage());
}
}, YesLog.yeslog, Request.Protocol.HTTP) {
@Override
public void getHeaders(Map headers) {
headers.put("Authorization", jcloudToken);
}
@Override
public void getParams(Map ps) {
ps.put("jcloudToken", jcloudToken); //To change body of generated methods, choose Tools | Templates.
}
};
YesHttpEngine.getYesHttpEngine().send(request);
return structures;
}
private TableFieldStructure JCloudTableFieldStructure(JSONObject jSONObject) {
if (jSONObject == null) {
return null;
}
TableFieldStructure structure = new TableFieldStructure();
try {
if (jSONObject.has("attributeName")) {
structure.setAttributeName(jSONObject.getString("attributeName"));
}
if (jSONObject.has("attributeDesc")) {
structure.setAttributeDesc(jSONObject.getString("attributeDesc"));
}
if (jSONObject.has("attributeDataType")) {
structure.setAttributeDataType(JDataTypes.fromValue(jSONObject.getString("attributeDataType")));
}
if (jSONObject.has("isPrimaryKey")) {
String tmp = jSONObject.getString("isPrimaryKey");
if (SystemUtil.isTxtEmpty(tmp)) {
structure.setIsPrimaryKey(false);
} else {
tmp = tmp.trim().toLowerCase();
if ("1".equals(tmp) || "yes".equals(tmp) || "true".equals(tmp)) {
structure.setIsPrimaryKey(true);
}
}
}
if (jSONObject.has("nullable")) {
String tmp = jSONObject.getString("nullable");
if (SystemUtil.isTxtEmpty(tmp)) {
structure.setNullable(false);
} else {
tmp = tmp.trim().toLowerCase();
if ("1".equals(tmp) || "yes".equals(tmp) || "true".equals(tmp)) {
structure.setNullable(true);
}
}
}
if (jSONObject.has("isUnique")) {
String tmp = jSONObject.getString("isUnique");
if (SystemUtil.isTxtEmpty(tmp)) {
structure.setIsUnique(false);
} else {
tmp = tmp.trim().toLowerCase();
if ("1".equals(tmp) || "yes".equals(tmp) || "true".equals(tmp)) {
structure.setIsUnique(true);
}
}
}
if (jSONObject.has("isIndex")) {
String tmp = jSONObject.getString("isIndex");
if (SystemUtil.isTxtEmpty(tmp)) {
structure.setIsIndex(false);
} else {
tmp = tmp.trim().toLowerCase();
if ("1".equals(tmp) || "yes".equals(tmp) || "true".equals(tmp)) {
structure.setIsIndex(true);
}
}
}
if (jSONObject.has("searchable")) {
String tmp = jSONObject.getString("searchable");
if (SystemUtil.isTxtEmpty(tmp)) {
structure.setSearchable(false);
} else {
tmp = tmp.trim().toLowerCase();
if ("1".equals(tmp) || "yes".equals(tmp) || "true".equals(tmp)) {
structure.setSearchable(true);
}
}
}
if (jSONObject.has("defaultValue")) {
structure.setDefaultValue(jSONObject.getString("defaultValue"));
}
if (jSONObject.has("regexPattern")) {
structure.setRegexPattern(jSONObject.getString("regexPattern"));
}
} catch (JSONException jSONException) {
UniversalLogHolder.e("", "the JSONException error in TableFieldStructure");
}
return structure;
}
}