Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Reconstruct the Exchangis AppConn #179

Merged
merged 15 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum ExchangisDataSourceExceptionCode {
CLIENT_DATASOURCE_GET_TYPES_ERROR(31015),
CLIENT_DATASOURCE_GET_KEY_DEFINES_ERROR(31016),
CLIENT_METADATA_GET_PARTITION_PROPS(31017),
CLIENT_METADATA_GET_PARTITION(31018),
// 其他错误
PARSE_JSON_ERROR(39000), // Parse Json Error
CONTEXT_GET_DATASOURCE_NULL(39001), // DataSource Context Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.webank.wedatasphere.exchangis.datasource.core.exception.ExchangisDataSourceException;
import com.webank.wedatasphere.exchangis.datasource.core.service.rpc.ServiceRpcClient;

import java.util.List;
import java.util.Map;

public interface MetadataInfoService<C> extends ServiceRpcInf<C> {
public interface MetadataInfoService extends ServiceRpcInf {

/**
* Get properties of partition
Expand All @@ -17,7 +18,7 @@ public interface MetadataInfoService<C> extends ServiceRpcInf<C> {
Map<String, String> getPartitionProps(String userName, Long dataSourceId,
String database, String table, String partition) throws ExchangisDataSourceException;

Map<String, String> getPartitionProps(ServiceRpcClient<C> rpcClient,
Map<String, String> getPartitionProps(ServiceRpcClient<?> rpcClient,
String userName, Long dataSourceId,
String database, String table, String partition) throws ExchangisDataSourceException;

Expand All @@ -31,6 +32,17 @@ Map<String, String> getPartitionProps(ServiceRpcClient<C> rpcClient,
Map<String, String> getTableProps(String userName, Long dataSourceId,
String database, String table) throws ExchangisDataSourceException;

Map<String, String> getTableProps(ServiceRpcClient<C> rpcClient, String userName, Long dataSourceId,
Map<String, String> getTableProps(ServiceRpcClient<?> rpcClient, String userName, Long dataSourceId,
String database, String table) throws ExchangisDataSourceException;

/**
* Get partition keys
* @param userName userName
* @param dataSourceId data source id
* @param database database
* @param table table
* @return
* @throws ExchangisDataSourceException
*/
List<String> getPartitionKeys(String userName, Long dataSourceId, String database, String table) throws ExchangisDataSourceException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* RPC service
* @param <C>
*/
public interface ServiceRpcInf<C> {
public interface ServiceRpcInf {

Class<?> getClientClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public <U> U dispatch(C remoteClient, T operation) throws ExchangisServiceRpcExc
try {
return execute(remoteClient, operation);
}catch(Exception e){

if (e instanceof ExchangisServiceRpcException){
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,51 @@
package com.webank.wedatasphere.exchangis.datasource.core.ui;

public interface ElementUI<T> {
String TEXTAREA = "TEXTAREA";
String INPUT = "INPUT";
String OPTION = "OPTION";
String MAP = "MAP";
import java.util.Map;

public interface ElementUI<T> {
/**
* Type enum
*/
enum Type {
NONE, TEXTAREA, INPUT, OPTION, MAP
}

/**
* Field name
* @return string
*/
String getField();

/**
* Label
* @return label string
*/
String getLabel();

// 类型
/**
* Type name
* @return string
*/
String getType();

Integer getSort();

/**
* Value store
* @return
*/
T getValue();

/**
* Default value
* @return
*/
T getDefaultValue();

/**
* Get value from params
* @param params
* @return
*/
void setValue(Map<String, Object> params);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.webank.wedatasphere.exchangis.datasource.core.ui;

import com.webank.wedatasphere.exchangis.datasource.core.utils.Json;

import java.util.Map;

public class InputElementUI implements ElementUI<String> {
private String key;
private String field;
Expand Down Expand Up @@ -50,7 +54,7 @@ public void setLabel(String label) {

@Override
public String getType() {
return ElementUI.INPUT;
return Type.INPUT.name();
}

@Override
Expand All @@ -74,6 +78,12 @@ public void setValue(String value) {
@Override
public String getDefaultValue() { return defaultValue; }

@Override
public void setValue(Map<String, Object> params) {
// Convert to json string directly
this.value = Json.toJson(params, null);
}

public void setDefaultValue(String defaultValue) { this.defaultValue = defaultValue; }

public String getUnit() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.webank.wedatasphere.exchangis.datasource.core.ui;

import org.apache.commons.lang.StringUtils;

import java.util.Map;
import java.util.Objects;

public class MapElementUI implements ElementUI<Map<String, Object>> {
private String key;
Expand Down Expand Up @@ -52,7 +55,7 @@ public void setLabel(String label) {

@Override
public String getType() {
return ElementUI.MAP;
return Type.MAP.name();
}

@Override
Expand All @@ -69,13 +72,15 @@ public Map<String, Object> getValue() {
return value;
}


public void setValue(Map<String, Object> value) {
this.value = value;
}

@Override
public Map<String, Object> getDefaultValue() { return defaultValue; }


public void setDefaultValue(Map<String, Object> defaultValue) { this.defaultValue = defaultValue; }

public String getUnit() {
Expand Down Expand Up @@ -113,4 +118,8 @@ public void setValidateRange(String validateRange) {
public String getValidateMsg() { return validateMsg; }

public void setValidateMsg(String validateMsg) { this.validateMsg = validateMsg; }

private boolean isBasicType(Class<?> clz){
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.webank.wedatasphere.exchangis.datasource.core.ui;

import com.webank.wedatasphere.exchangis.datasource.core.utils.Json;

import java.util.Collection;
import java.util.Map;

public class OptionElementUI implements ElementUI<String> {
private String key;
Expand All @@ -27,7 +30,7 @@ public String getField() {

@Override
public String getType() {
return ElementUI.OPTION;
return Type.OPTION.name();
}

public void setField(String field) {
Expand Down Expand Up @@ -62,6 +65,11 @@ public void setValue(String value) {
@Override
public String getDefaultValue() { return defaultValue; }

@Override
public void setValue(Map<String, Object> params) {
this.value = Json.toJson(params.values(), null);
}

public void setDefaultValue(String defaultValue) { this.defaultValue = defaultValue; }

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.webank.wedatasphere.exchangis.datasource.core.ui.builder;

import com.webank.wedatasphere.exchangis.datasource.core.ui.ElementUI;
import com.webank.wedatasphere.exchangis.datasource.core.ui.InputElementUI;

import java.lang.annotation.ElementType;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;

/**
* Default element factory
*/
public class DefaultElementUIFactory implements ElementUIFactory{
/**
* Element builders holder
*/
private Map<Identify, Function<Object, ? extends ElementUI<?>>> builders = new HashMap<>();


@Override
@SuppressWarnings("unchecked")
public <T, R> void register(String type, Function<T, ? extends ElementUI<R>> builder, Class<?> inputType) {
builders.putIfAbsent(new Identify(type, inputType), (Function<Object, ? extends ElementUI<?>>) builder);
}

@Override
@SuppressWarnings("unchecked")
public <R> ElementUI<R> createElement(String type, Object input, Class<?> inputType) {
Identify identify = new Identify(type, inputType);
AtomicReference<ElementUI<R>> elementUI = new AtomicReference<>();
Optional.ofNullable(builders.get(identify)).ifPresent(builder -> {
elementUI.set((ElementUI<R>) builder.apply(input));
});
return elementUI.get();
}

/**
* Identify for element builder
*/
private static class Identify{

/**
* Type
*/
private final String type;

/**
* Input class
*/
private final Class<?> inputClass;

public Identify(String type, Class<?> inputClass){
this.type = type;
this.inputClass = inputClass;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Identify identify = (Identify) o;
return Objects.equals(type, identify.type) && Objects.equals(inputClass, identify.inputClass);
}

@Override
public int hashCode() {
return Objects.hash(type, inputClass);
}

@Override
public String toString() {
return "Identify{" +
"type='" + type + '\'' +
", inputClass=" + inputClass.getCanonicalName() +
'}';
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.webank.wedatasphere.exchangis.datasource.core.ui.builder;

import com.webank.wedatasphere.exchangis.datasource.core.ui.ElementUI;

import java.util.function.Function;

/**
* Element factory
*/
public interface ElementUIFactory {

/**
* Register the element builder
* @param type type
* @param builder builder
* @param inputType input type
* @param <T> T
* @param <R> R
*/
<T, R> void register(String type, Function<T, ? extends ElementUI<R>> builder, Class<?> inputType);


/**
* Create element
* @param type type
* @param input input object
* @param <R> element value type
* @return element
*/
<R>ElementUI<R> createElement(String type, Object input, Class<?> inputType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.webank.wedatasphere.exchangis.datasource.core.ui.builder;

import com.webank.wedatasphere.exchangis.datasource.core.ui.ElementUI;
import com.webank.wedatasphere.exchangis.datasource.core.ui.InputElementUI;
import com.webank.wedatasphere.exchangis.datasource.core.ui.MapElementUI;
import com.webank.wedatasphere.exchangis.datasource.core.ui.OptionElementUI;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
* Default element factory in spring
*/
@Component
public class SpringElementUIFactory extends DefaultElementUIFactory{

@PostConstruct
public void init(){
super.register(ElementUI.Type.MAP.name(), (Function<Map<String, Object>, MapElementUI>) params ->
setElementValue(new MapElementUI(), params), Map.class);
super.register(ElementUI.Type.INPUT.name(), (Function<Map<String, Object>, InputElementUI>) params ->
setElementValue(new InputElementUI(), params), Map.class);
super.register(ElementUI.Type.OPTION.name(), (Function<Map<String, Object>, OptionElementUI>) params ->
setElementValue(new OptionElementUI(), params), Map.class);
}

/**
* Set the params into element and return
* @param element element
* @param params params Map
* @param <R> R
* @return
*/
private <R extends ElementUI<?>>R setElementValue(R element, Map<String, Object> params){
element.setValue(params);
return element;
}

public static void main(String[] args){
SpringElementUIFactory elementUIFactory = new SpringElementUIFactory();
elementUIFactory.init();
Map<String, Object> map = new HashMap<>();
map.putIfAbsent("hello", "world");
System.out.println(elementUIFactory.createElement(ElementUI.Type.MAP.name(), map, Map.class).getValue());
}
}
Loading