All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hadoopz.cloud.JCloud.SDK.utils.CreateTable 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.TableFieldStructure;
import com.mycomm.IProtocol.log.UniversalLogHolder;
import com.mycomm.YesHttp.core.JsonRequest;
import com.mycomm.YesHttp.core.Request;
import com.mycomm.YesHttp.core.Response;
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.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author jw362j
 */
public class CreateTable {

    public void createTable(String tableName, final List table, final String jcloudToken) {

        String url = EndpointBuilder.getFullUrlForCreateTable(tableName);
        final JSONObject ps = new JSONObject();

        Request request = new JsonRequest(url, new TextBaseResponseListener() {
            @Override
            public void responseMe(String msg) {
                YesLog.yeslog.d("the requestjSON TextBaseResponseListener.responseMe:" + msg);
            }
        }, 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);
            }

            public String JsonBodyBuilder() {
                if (ps.length() == 0) {
                    for (TableFieldStructure field : table) {
                        if (field == null) {
                            throw new IllegalArgumentException("atleast one item is null in CreateTable!");
                        }
                        if (SystemUtil.isTxtEmpty(field.getAttributeName())) {
                            throw new IllegalArgumentException("one of the AttributeName is null in CreateTable!");
                        }
                        if (field.getAttributeDataType() == null) {
                            throw new IllegalArgumentException("one of the AttributeDataType is null in CreateTable!" + field.getAttributeName());
                        }
                    }
                    try {
                        ps.put("tableEngine", "InnoDB");
                        ps.put("tableCharset", "utf8");

                        JSONArray values = new JSONArray();

                        for (TableFieldStructure field : table) {
                            if (field == null || SystemUtil.isTxtEmpty(field.getAttributeName()) || field.getAttributeDataType() == null) {
                                throw new IllegalArgumentException("found illegal item in CreateTable,one item is null or carring the invlid value!");
                            }
                            JSONObject object = new JSONObject();
                            object.put("attributeName", field.getAttributeName());

                            object.put("attributeDesc", field.getAttributeDesc());

                            object.put("attributeDataType", field.getAttributeDataType().getValue());
                            object.put("isPrimaryKey", field.isIsPrimaryKey() ? "1" : "0");
                            object.put("nullable", field.isNullable() ? "1" : "0");
                            object.put("isUnique", field.isIsUnique() ? "1" : "0");
                            object.put("isIndex", field.isIsIndex() ? "1" : "0");
                            object.put("searchable", field.isSearchable() ? "1" : "0");
                            object.put("defaultValue", field.getDefaultValue());
                            object.put("regexPattern", field.getRegexPattern());
                            object.put("needWebEditor", field.isNeedWebEditor() ? "1" : "0");
                            object.put("webLabel", field.getWebLabel());

                            object.put("isDate", field.isIsDate() ? "1" : "0");

                            object.put("placeholder1", field.getPlaceholder1());
                            object.put("placeholder2", field.getPlaceholder2());
                            object.put("placeholder3", field.getPlaceholder3());
                            object.put("placeholder4", field.getPlaceholder4());
                            object.put("placeholder5", field.getPlaceholder5());
                            object.put("placeholder6", field.getPlaceholder6());
                            object.put("placeholder7", field.getPlaceholder7());
                            object.put("placeholder8", field.getPlaceholder8());
                            object.put("placeholder9", field.getPlaceholder9());
                            object.put("placeholder10", field.getPlaceholder10());

                            object.put("placeholder11", field.isPlaceholder11() ? "1" : "0");
                            object.put("placeholder12", field.isPlaceholder12() ? "1" : "0");
                            object.put("placeholder13", field.isPlaceholder13() ? "1" : "0");
                            object.put("placeholder14", field.isPlaceholder14() ? "1" : "0");
                            object.put("placeholder15", field.isPlaceholder15() ? "1" : "0");
                            object.put("placeholder16", field.isPlaceholder16() ? "1" : "0");
                            object.put("placeholder17", field.isPlaceholder17() ? "1" : "0");
                            object.put("placeholder18", field.isPlaceholder18() ? "1" : "0");
                            object.put("placeholder19", field.isPlaceholder19() ? "1" : "0");
                            object.put("placeholder20", field.isPlaceholder20() ? "1" : "0");

                            object.put("placeholder21", field.getPlaceholder21());
                            object.put("placeholder22", field.getPlaceholder22());
                            object.put("placeholder23", field.getPlaceholder23());
                            object.put("placeholder24", field.getPlaceholder24());
                            object.put("placeholder25", field.getPlaceholder25());

                            values.put(object);
                        }
                        ps.put("values", values);

                    } catch (JSONException ex) {
                        UniversalLogHolder.d(getClass().getSimpleName(), ex.getMessage());
                    }

                }
                return ps.toString();
            }

        };
        YesHttpEngine.getYesHttpEngine().send(request);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy