Skip to content

Commit

Permalink
#2378 refactored to apdaper dme operation
Browse files Browse the repository at this point in the history
  • Loading branch information
gavin2lee committed Jun 15, 2023
1 parent 4d943d9 commit b413b24
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.webank.wecube.platform.core.entity.plugin.PluginConfigInterfaceParameters;
import com.webank.wecube.platform.core.entity.plugin.PluginConfigInterfaces;
import com.webank.wecube.platform.core.service.dme.EntityQueryExpr;
import com.webank.wecube.platform.core.service.dme.EntityQueryExprNodeInfo;

public class DmeOutputParamAttr {
Expand All @@ -14,6 +15,8 @@ public class DmeOutputParamAttr {
private Object retVal;
private boolean processed;
private List<EntityQueryExprNodeInfo> exprNodeInfos;
private EntityQueryExpr entityQueryExpr;

public PluginConfigInterfaces getInterf() {
return interf;
}
Expand Down Expand Up @@ -64,5 +67,12 @@ public boolean isRootEntityAttr(){

return false;
}
public EntityQueryExpr getEntityQueryExpr() {
return entityQueryExpr;
}
public void setEntityQueryExpr(EntityQueryExpr entityQueryExpr) {
this.entityQueryExpr = entityQueryExpr;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class EntityOperationContext {
protected EntityDataRouteFactory entityDataRouteFactory;

protected Map<Object, Object> externalCacheMap;

protected EntityQueryExpr entityQueryExpr;

public EntityQueryLinkNode getHeadEntityQueryLinkNode() {
return headEntityQueryLinkNode;
Expand Down Expand Up @@ -112,4 +114,12 @@ public void setExternalCacheMap(Map<Object, Object> externalCacheMap) {
this.externalCacheMap = externalCacheMap;
}

public EntityQueryExpr getEntityQueryExpr() {
return entityQueryExpr;
}

public void setEntityQueryExpr(EntityQueryExpr entityQueryExpr) {
this.entityQueryExpr = entityQueryExpr;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -83,15 +87,25 @@ public StandardEntityOperationResponseDto query(EntityRouteDescription entityDef

// POST List<Map<String, Object>>
public StandardEntityOperationResponseDto update(EntityRouteDescription entityDef,
List<EntityDataRecord> recordsToUpdate) {
List<EntityDataRecord> recordsToUpdate,Map<String,String> additionalRequestHeaders) {
URI requestUri = buildStandardOperationUri(entityDef, getUpdateUriTemplate());

List<Map<String, Object>> requestBody = convertToMapList(recordsToUpdate);

HttpHeaders headers = new HttpHeaders();
if(additionalRequestHeaders != null) {
for(String headerKey : additionalRequestHeaders.keySet()) {
headers.add(headerKey, additionalRequestHeaders.get(headerKey));
}
}

HttpEntity<List<Map<String, Object>>> requestEntity = new HttpEntity<>(requestBody, headers);
long timeMilliSeconds = System.currentTimeMillis();
log.info("SEND UPDATE post [{}] url={}, request={}", timeMilliSeconds, requestUri.toString(),
toJson(requestBody));
StandardEntityOperationResponseDto result = getRestTemplate().postForObject(requestUri, requestBody,
ResponseEntity<StandardEntityOperationResponseDto> respEntity = getRestTemplate().exchange(requestUri, HttpMethod.POST, requestEntity,
StandardEntityOperationResponseDto.class);
StandardEntityOperationResponseDto result = respEntity.getBody();
log.debug("RECEIVE UPDATE post [{}] url={},result={}", timeMilliSeconds, requestUri.toString(), result);
if(!StandardEntityOperationResponseDto.STATUS_OK.equalsIgnoreCase(result.getStatus())) {
log.error("update failed with error:{} {}", result.getStatus(), result.getMessage());
Expand All @@ -100,14 +114,25 @@ public StandardEntityOperationResponseDto update(EntityRouteDescription entityDe
return result;
}

public StandardEntityOperationResponseDto updateData(EntityRouteDescription entityDef,List<Map<String, Object>> recordsToUpdate) {
public StandardEntityOperationResponseDto updateData(EntityRouteDescription entityDef,List<Map<String, Object>> recordsToUpdate, Map<String,String> additionalRequestHeaders) {
URI requestUri = buildStandardOperationUri(entityDef, getUpdateUriTemplate());

HttpHeaders headers = new HttpHeaders();
if(additionalRequestHeaders != null) {
for(String headerKey : additionalRequestHeaders.keySet()) {
headers.add(headerKey, additionalRequestHeaders.get(headerKey));
}
}

HttpEntity<List<Map<String, Object>>> requestEntity = new HttpEntity<>(recordsToUpdate, headers);

long timeMilliSeconds = System.currentTimeMillis();
log.info("SEND UPDATE post [{}] url={}, request={}", timeMilliSeconds, requestUri.toString(),
toJson(recordsToUpdate));
StandardEntityOperationResponseDto result = getRestTemplate().postForObject(requestUri, recordsToUpdate,
ResponseEntity<StandardEntityOperationResponseDto> respEntity = getRestTemplate().exchange(requestUri, HttpMethod.POST, requestEntity,
StandardEntityOperationResponseDto.class);

StandardEntityOperationResponseDto result = respEntity.getBody();
log.debug("RECEIVE UPDATE post [{}] url={},result={}", timeMilliSeconds, requestUri.toString(), result);

if(!StandardEntityOperationResponseDto.STATUS_OK.equalsIgnoreCase(result.getStatus())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ protected EntityOperationContext buildEntityOperationContext(EntityOperationRoot
List<EntityQueryExprNodeInfo> exprNodeInfos = entityQueryExpr.getExprNodeInfos();

EntityOperationContext ctx = new EntityOperationContext();
ctx.setEntityQueryExpr(entityQueryExpr);
ctx.setEntityQueryExprNodeInfos(exprNodeInfos);
ctx.setOriginalEntityLinkExpression(condition.getEntityLinkExpr());
ctx.setOriginalEntityData(condition.getEntityIdentity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -145,9 +146,15 @@ public void executeUpdate(EntityOperationContext ctx, Object valueToUpdate) {
EntityQueryExprNodeInfo nodeInfo = leafLinkNode.getExprNodeInfo();
EntityRouteDescription entityDef = ctx.getEntityDataRouteFactory()
.deduceEntityDescription(nodeInfo.getPackageName(), nodeInfo.getEntityName());

Map<String,String> additionalRequestHeaders = new HashMap<>();

if(ctx.getEntityQueryExpr()!= null && StringUtils.isNoneBlank(ctx.getEntityQueryExpr().getExprOperation())) {
additionalRequestHeaders.put("operation", ctx.getEntityQueryExpr().getExprOperation());
}

StandardEntityOperationRestClient restClient = ctx.getStandardEntityOperationRestClient();
restClient.update(entityDef, entityDataRecordsToUpdate);
restClient.update(entityDef, entityDataRecordsToUpdate, additionalRequestHeaders);
}

public EntityQueryLinkNode buildEntityQueryLinkNode(List<EntityQueryExprNodeInfo> exprNodeInfos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import com.webank.wecube.platform.core.service.dme.EntityDataAttr;
import com.webank.wecube.platform.core.service.dme.EntityDataRecord;
import com.webank.wecube.platform.core.service.dme.EntityOperationRootCondition;
import com.webank.wecube.platform.core.service.dme.EntityQueryExpr;
import com.webank.wecube.platform.core.service.dme.EntityQueryExprNodeInfo;
import com.webank.wecube.platform.core.service.dme.EntityRouteDescription;
import com.webank.wecube.platform.core.service.dme.EntityTreeNodesOverview;
Expand Down Expand Up @@ -687,7 +688,7 @@ private void tryProcessDataIdProcExecBinding(String bindDataId, WorkflowInstCrea

StandardEntityOperationRestClient restClient = new StandardEntityOperationRestClient(jwtSsoRestTemplate);

StandardEntityOperationResponseDto resultDto = restClient.update(entityDef, recordsToUpdate);
StandardEntityOperationResponseDto resultDto = restClient.update(entityDef, recordsToUpdate, null);
if (StandardEntityOperationResponseDto.STATUS_ERROR.equals(resultDto.getStatus())) {
log.error("errors to update entity:{}", resultDto.getMessage());
return;
Expand Down Expand Up @@ -3779,7 +3780,8 @@ private void tryHandleSingleOutputMapOnceEntityCreation(PluginInterfaceInvocatio
// continue;
}

List<EntityQueryExprNodeInfo> exprNodeInfos = entityQueryExpressionParser.parse(paramExpr).getExprNodeInfos();
EntityQueryExpr entityQueryExpr = entityQueryExpressionParser.parse(paramExpr);
List<EntityQueryExprNodeInfo> exprNodeInfos = entityQueryExpr.getExprNodeInfos();

if (exprNodeInfos == null || exprNodeInfos.isEmpty()) {
String errMsg = String.format("Unknown how to update entity attribute due to invalid expression:%s",
Expand All @@ -3804,6 +3806,7 @@ private void tryHandleSingleOutputMapOnceEntityCreation(PluginInterfaceInvocatio
outputParamAttr.setParamExpr(paramExpr);
outputParamAttr.setParamName(paramName);
outputParamAttr.setRetVal(finalRetVal);
outputParamAttr.setEntityQueryExpr(entityQueryExpr);

allDmeOutputParamAttrs.add(outputParamAttr);
if (outputParamAttr.isRootEntityAttr()) {
Expand Down Expand Up @@ -3855,7 +3858,7 @@ private void tryHandleSingleOutputMapOnceEntityCreation(PluginInterfaceInvocatio
this.jwtSsoRestTemplate);
List<Map<String, Object>> objDataMaps = new ArrayList<>();
objDataMaps.add(objDataMap);
restClient.updateData(entityDef, objDataMaps);
restClient.updateData(entityDef, objDataMaps, null);

} else {
if (verifyIfHasNormalEntityMappingExcludeAssign(rootDemOutputParamAttrs)) {
Expand Down

0 comments on commit b413b24

Please sign in to comment.