Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.hadoopz.cloud.JCloud.SDK.utils.JCloudContext Maven / Gradle / Ivy
/*
* 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.hadoopz.cloud.JCloud.SDK.JCloudAPI;
import com.hadoopz.cloud.JCloud.SDK.beans.APIError;
import com.hadoopz.cloud.JCloud.SDK.beans.DeveloperContext;
import com.hadoopz.cloud.JCloud.SDK.beans.SDKInitCallBack;
import com.hadoopz.cloud.JCloud.SDK.core.JCloudTokenListener;
import com.mycomm.IProtocol.beans.JDataTypes;
import com.mycomm.IProtocol.beans.QueryCondition;
import com.mycomm.IProtocol.beans.TableFieldStructure;
import com.mycomm.IProtocol.log.UniversalLogHolder;
import com.mycomm.IProtocol.sql.Clz2Sql.AnnotationParser;
import com.mycomm.IProtocol.sql.Clz2Sql.MySqlColumTypeDetector;
import com.mycomm.itool.SystemUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mycomm.IProtocol.sql.SqlRelationship;
import com.mycomm.IProtocol.sql.annotation.MyColumn;
import com.mycomm.IProtocol.sql.annotation.MyTable;
import com.mycomm.IProtocol.utils.Clazz2TableFields;
import com.mycomm.IProtocol.utils.RemoteTable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author jw362j
*/
public class JCloudContext implements JCloudAPI, JCloudTokenListener {
private static JCloudAPI cloudAPI;
private static DeveloperContext developerContext;
private String JCloudToken;
private SDKInitCallBack initCallBack;
public static Map> allTables = new HashMap>();
public static JCloudAPI getAPIInstance(DeveloperContext context, SDKInitCallBack callBack) {
if (cloudAPI == null) {
cloudAPI = new JCloudContext(context, callBack);
}
return cloudAPI;
}
public static JCloudAPI getAPIInstance(String serverHost, String jcloudToken) {
if (SystemUtil.isTxtEmpty(jcloudToken) || SystemUtil.isTxtEmpty(serverHost)) {
throw new IllegalArgumentException("serverHost or jcloudToken is null");
}
if (cloudAPI == null) {
EndpointBuilder.buildBaseEndpointForDeveloper(serverHost, jcloudToken);
cloudAPI = new JCloudContext(jcloudToken);
}
return cloudAPI;
}
private JCloudContext(String jcloudToken) {
JCloudToken = jcloudToken;
}
private JCloudContext(DeveloperContext context, SDKInitCallBack callBack) {
if (context == null) {
throw new IllegalArgumentException("DeveloperContext is null");
}
if (SystemUtil.isTxtEmpty(context.getDeveloperId()) || SystemUtil.isTxtEmpty(context.getPassword()) || SystemUtil.isTxtEmpty(context.getServerHostURL())) {
throw new IllegalArgumentException("DeveloperContext is having empty value for id/pwd/host");
}
developerContext = context;
initCallBack = callBack;
initSDKLogin();
}
private void initSDKLogin() {
new DeveloperLogin(JCloudContext.this).doLogin(developerContext);
}
private long doInsertSingleRecord(String tableName, Map record, String jcloudToken) {
return new InsertSingleRecord().insert(tableName, record, jcloudToken);
}
public void onTokenReady(String jcloudToken) {
JCloudToken = jcloudToken;
if (initCallBack != null) {
initCallBack.onSuccess(null);
}
}
private String doInsertManyRecords(String tableName, List> records, String jcloudToken) {
new InsertRecords().insert(tableName, records, jcloudToken);
return null;
}
public void doDeleteById(final Class objectClz, final long recordId) {
String tname = AnnotationParser.LoadAnnotationStructureStrategy(objectClz).getTableName();
if (SystemUtil.isTxtEmpty(tname)) {
throw new IllegalArgumentException("table name is null, invalid objectClz!");
}
doDeleteByCondition(objectClz, new Conditionable() {
public void addWhereConditions(Set queryUnits) {
queryUnits.add(new TableFieldQueryUnit(AnnotationParser.LoadAnnotationStructureStrategy(objectClz).getIdColumName(), QueryCondition.Equals, recordId + ""));
}
public SqlRelationship sqlRelationship() {
return null;
}
});
}
public void doDeleteByIds(Class objectClz, long[] recordId) {
String tname = AnnotationParser.LoadAnnotationStructureStrategy(objectClz).getTableName();
if (SystemUtil.isTxtEmpty(tname)) {
throw new IllegalArgumentException("table name is null, invalid objectClz in doDeleteByIds!");
}
if (recordId == null || recordId.length == 0) {
return;
}
doDeleteByIds(tname, recordId, JCloudToken, null);
}
private String doDeleteByIds(String tableName, long[] recordId, String jcloudToken, Set queryUnit) {
new DeleteRecords().delete(tableName, recordId, jcloudToken, queryUnit);
return null;
}
public void doDeleteByCondition(Class objectClz, Conditionable deleteable) {
if (objectClz == null) {
return;
}
if (deleteable == null) {
return;
}
Set queryUnit = new HashSet();
deleteable.addWhereConditions(queryUnit);
doDeleteByIds(AnnotationParser.LoadAnnotationStructureStrategy(objectClz).getTableName(), null, JCloudToken, queryUnit);
// return null;
}
public String doUpdateTable(Class objectClz, Map newRecordValue, Set queryUnit) {
String tname = AnnotationParser.LoadAnnotationStructureStrategy(objectClz).getTableName();
if (SystemUtil.isTxtEmpty(tname)) {
throw new IllegalArgumentException("table name is null, invalid objectClz in doUpdateTable!");
}
return doUpdateTable(tname, newRecordValue, JCloudToken, queryUnit);
}
private String doUpdateTable(String tableName, Map newRecordValue, String jcloudToken, Set queryUnit) {
new UpdateRecord().update(tableName, newRecordValue, jcloudToken, queryUnit);
return null;
}
public String doUpdateTable(Class objectClz, Updateable updateable) {
if (objectClz == null) {
return null;
}
if (updateable == null) {
return null;
}
Map datas = new HashMap();
updateable.addDatas(datas);
Set queryUnits = new HashSet();
updateable.addWhereConditions(queryUnits);
doUpdateTable(objectClz, datas, queryUnits);
return null;
}
public String doCreateTable(Class tableBean) {
RemoteTable remoteTable = Clazz2TableFields.loadTableFields(tableBean);
// UniversalLogHolder.d(getClass().getSimpleName(), "remoteTable:"+remoteTable);
return doCreateTable(remoteTable.getTableName(), remoteTable.getTableFieldStructures(), JCloudToken);
}
private String doCreateTable(String tableName, List table, String jcloudToken) {
new CreateTable().createTable(tableName, table, jcloudToken);
return null;
}
public Object doGetById(Class clz, long id) {
String id_columName = AnnotationParser.LoadAnnotationStructureStrategy(clz).getIdColumName();
if (SystemUtil.isTxtEmpty(id_columName)) {
throw new IllegalArgumentException("id_columName is null, invalid objectClz in doGetById!");
}
return doGetById(clz, id_columName, id, JCloudToken);
}
private Object doGetById(Class clz, final String id_columName, final long recordId, final String jcloudToken) {
List res = doGetByConditions(clz, null, 0, 1, false, new Getable() {
public void buildOrderBy(Set orderBys) {
}
public void addWhereConditions(Set queryUnits) {
//To change body of generated methods, choose Tools | Templates.
queryUnits.add(new TableFieldQueryUnit(id_columName, QueryCondition.Equals, recordId + ""));
}
public SqlRelationship sqlRelationship() {
return SqlRelationship.And;
}
});
if (res == null || res.isEmpty()) {
return null;
}
return res.get(0);
}
public String doGetRecordSize(Class clz, Getable getable) {
doGetByConditions(clz, null, 0, 1, true, getable);
return null;
}
private List doGetByConditions(Class clz, String[] projection, SqlRelationship relationsShip, final Set queryUnit, final Set orderBys, int offset, int size, boolean getRecordSize, String jcloudToken) {
String tname = AnnotationParser.LoadAnnotationStructureStrategy(clz).getTableName();
UniversalLogHolder.d(getClass().getSimpleName(), "******************"+tname+"******************");
if (SystemUtil.isTxtEmpty(tname)) {
throw new IllegalArgumentException("table name is null, invalid objectClz in doGetById!");
}
return new GetByCondition().get(clz, tname, projection, relationsShip, queryUnit, orderBys, offset, size, getRecordSize, jcloudToken);
}
public List doGetByConditions(Class clz, String[] projection, int offset, int size, boolean getSize, Getable getable) {
if (getable == null) {
return null;
}
Set queryUnit = new HashSet();
Set orderBys = new HashSet();
getable.addWhereConditions(queryUnit);
getable.buildOrderBy(orderBys);
return doGetByConditions(clz, projection, (getable.sqlRelationship() == null ? SqlRelationship.And : getable.sqlRelationship()), queryUnit, orderBys, offset, size, getSize, JCloudToken);
}
public List loadTableStructure(String tableName) {
return new TableStructureLoader().loadTable(tableName, JCloudToken);
}
public List loadTableStructure(Class clz) {
return loadTableStructure(AnnotationParser.getTableName(clz));
}
public void SyncSDK() {
//To change body of generated methods, choose Tools | Templates.
}
public void SDKDestroy() {
new SDKKiller().dismissSession(JCloudToken);
}
public List loadAllTables() {
return new TablesFinder().getTables(JCloudToken); //To change body of generated methods, choose Tools | Templates.
}
@Deprecated
public void executeSql(String sql) {
// new SqlExecutor().execute(sql, JCloudToken);
}
private static final MySqlColumTypeDetector detector = new MySqlColumTypeDetector();
public long doInsertSingleRecord(Object recordData) {
if (recordData == null) {
throw new IllegalArgumentException("the recordData is null in insert");
}
Class clz = recordData.getClass();
if (!clz.isAnnotationPresent(MyTable.class)) {
throw new IllegalArgumentException("invalid class ,no MyTable annotation presented!");
}
Field allAttrs[] = clz.getDeclaredFields();
String tname = AnnotationParser.LoadAnnotationStructureStrategy(clz).getTableName();
Map datas = new HashMap();
for (Field field : allAttrs) {
try {
field.setAccessible(true);
if ((!field.isAnnotationPresent(MyColumn.class))) {
continue;
}
Object x = field.get(recordData);
datas.put(field.getName(), x == null ? "": x+"");
} catch (IllegalArgumentException ex) {
UniversalLogHolder.d(getClass().getSimpleName(), "A:" + ex.getMessage());
} catch (IllegalAccessException ex) {
UniversalLogHolder.d(getClass().getSimpleName(), "B:" + ex.getMessage());
}
if (JDataTypes.JDate.equals(detector.getJavaDataType(field))) {
throw new IllegalArgumentException("the attribute:" + field.getName() + " type java.util.Date is no longer supported,please use java long as the type(Date d = new Date(longValue);long x = new Date().getTime();)!");
}
}
if (datas.isEmpty()) {
return -1;
}
return doInsertSingleRecord(tname, datas, JCloudToken);
}
public void doInsertManyRecords(List records) {
if (records == null || records.isEmpty()) {
return;
}
Class clzTmp = null;
for (Object o : records) {
Class clzType = o.getClass();
if (clzTmp == null) {
clzTmp = clzType;
continue;
}
if (!clzTmp.equals(clzType)) {
throw new IllegalArgumentException("please make sure all items in doInsertManyRecords(List records) are in same class type!");
}
}
String tableName = AnnotationParser.LoadAnnotationStructureStrategy(records.get(0).getClass()).getTableName();
List> datas = new ArrayList>();
for (Object o : records) {
if (o == null) {
continue;
}
if (!o.getClass().isAnnotationPresent(MyTable.class)) {
throw new IllegalArgumentException("invalid class ,no MyTable annotation presented X!");
}
Field allAttrs[] = o.getClass().getDeclaredFields();
Map data = new HashMap();
for (Field field : allAttrs) {
try {
field.setAccessible(true);
if ((!field.isAnnotationPresent(MyColumn.class))) {
continue;
}
data.put(field.getName(), field.get(o) + "");
} catch (IllegalArgumentException ex) {
UniversalLogHolder.d(getClass().getSimpleName(), "A:" + ex.getMessage());
} catch (IllegalAccessException ex) {
UniversalLogHolder.d(getClass().getSimpleName(), "B:" + ex.getMessage());
}
if (JDataTypes.JDate.equals(detector.getJavaDataType(field))) {
throw new IllegalArgumentException("the attribute:" + field.getName() + " type java.util.Date is no longer supported,please use java long as the type(Date d = new Date(longValue);long x = new Date().getTime();)!");
}
}
datas.add(data);
}
doInsertManyRecords(tableName, datas, JCloudToken);
}
public void onError(APIError error) {
if (initCallBack != null) {
initCallBack.onFailed(error); //To change body of generated methods, choose Tools | Templates.
}
}
public String getJCloudToken() {
return JCloudToken;
}
public void doDropTable(Class objectClz) {
if (objectClz == null) {
return;
}
if (!objectClz.isAnnotationPresent(MyTable.class)) {
throw new IllegalArgumentException("invalid class ,no MyTable annotation been presented!");
}
String tname = AnnotationParser.LoadAnnotationStructureStrategy(objectClz).getTableName();
doDropTable(tname);
}
public void doDropTable(String tableName) {
if (SystemUtil.isTxtEmpty(tableName)) {
throw new IllegalArgumentException(" Table name is null in doDropTable()");
}
new TableDroper().droptable(tableName, JCloudToken);
}
}