

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cproc.datacube.client.api.ClientBaseApi;
import cproc.datacube.client.api.ClientBaseApiService;
import cproc.datacube.client.entity.AppInfo;
import cproc.datacube.client.entity.ColumnInfo;
import cproc.datacube.client.entity.ReObject;
import cproc.datacube.client.entity.Row;
import cproc.datacube.client.entity.TableInfo;
import cproc.datacube.client.entity.UserInfo;

public class test_Client_BaseApi {

	public static void main(String[] args) throws Exception {
		UserInfo user = new UserInfo();
		user.setUserName("jiarry");
		user.setAccessId("AMLHCNJ5MTQYNZY4NTI3NDMXMW==");
		AppInfo app = new AppInfo();
		app.setAppId("7686708862");
		app.setUserInfo(user);

		ClientBaseApi baseApi = new ClientBaseApiService();

		// interface getAllTableInfo
		ReObject allTabInfo = baseApi.getAllTableInfo(app);
		for (Object ob : allTabInfo.getData()) {
			TableInfo tal = (TableInfo) ob;
			System.out.println(tal.getTableName());
		}

		// interface getTableInfo
		ReObject tabInfo = baseApi.getTableInfo(app, "DATA");
		for (Object ob : tabInfo.getData()) {
			TableInfo tal = (TableInfo) ob;
			for (ColumnInfo cl : tal.getColumns()) {
				System.out.println(cl.getColumnName());
			}
		}

		// interface getTableRows
		List<String> ids = new ArrayList<String>();
		ids.add("9223370608904971694http46");
		ids.add("9223370608904972546http46");
		List<String> cls = new ArrayList<String>();
		cls.add("PM1");
		cls.add("PM2");

		ReObject row1 = baseApi.getTableRows(app, "DATA", ids, cls);
		for (Object ob : row1.getData()) {
			Row row = (Row) ob;
			System.out.println(row.getColumn("PM1"));
		}

		// interface getTableRowsByRange
		String idsBegin = "9223370608904971694http46";
		String idsEnd = "9223370608904975408http46";
		List<String> clsCol = new ArrayList<String>();
		clsCol.add("PM1");
		clsCol.add("PM2");

		ReObject row2 = baseApi.getTableRowsByRange(app, "DATA", idsBegin,
				idsEnd, clsCol);
		for (Object ob : row2.getData()) {
			Row row3 = (Row) ob;
			System.out.println(row3.getColumn("PM2"));
		}

		// interface deleteTable
		ReObject delobj = baseApi.deleteTable(app, "TESTCREATTABLE1");
		System.out.println(delobj.getMsg());

		// interface createTable
		List<ColumnInfo> columnMap = new ArrayList<ColumnInfo>();
		columnMap.add(new ColumnInfo("testkey", "STRING", URLEncoder.encode(
				"����", "UTF-8")));
		columnMap.add(new ColumnInfo("testA", "INT", URLEncoder.encode("����",
				"UTF-8")));
		columnMap.add(new ColumnInfo("testB", "LONG", URLEncoder.encode("������",
				"UTF-8")));

		TableInfo cTableInfo = new TableInfo("testcreattable1", "testkey",
				columnMap, "tablescribe");
		ReObject cobj = baseApi.createTable(app, cTableInfo);
		System.out.println(cobj.getMsg());

		// interface addTableColumns
		List<ColumnInfo> addColums = new ArrayList<ColumnInfo>();
		addColums.add(new ColumnInfo("testC", "INT", URLEncoder.encode("����",
				"UTF-8")));
		addColums.add(new ColumnInfo("testD", "INT", URLEncoder.encode("����",
				"UTF-8")));
		addColums.add(new ColumnInfo("testE", "LONG", URLEncoder.encode("������",
				"UTF-8")));

		ReObject addColuOjb = baseApi.addTableColumns(app, "testcreattable1",
				addColums);
		System.out.println(addColuOjb.getMsg());

		// interface addTableRows
		List<Row> addRow = new ArrayList<Row>();
		for (int i = 1; i < 10; i++) {
			Map<String, String> values = new HashMap<String, String>();
			values.put("testkey", "AAA" + i);
			values.put("testA", "111111" + i);
			values.put("testB", "111111111111" + i);
			addRow.add(new Row(values));
		}
		ReObject addRowOjb = baseApi.addTableRows(app, "testcreattable1",
				addRow);
		System.out.println(addRowOjb.getMsg());

		// interface deleteTableRows
		List<String> delRows =new ArrayList<String>();
		delRows.add("AAA1");
		ReObject delRowOjb = baseApi.deleteTableRows(app, "testcreattable1",
				delRows);
		System.out.println(delRowOjb.getMsg());
		
	}
}
