Skip to content

Commit

Permalink
Merge pull request #584 from WeBankPartners/539_autofill_enhancement
Browse files Browse the repository at this point in the history
#539 autofill enhancement
  • Loading branch information
jordanzhangsz authored Dec 26, 2019
2 parents d757e03 + a60cc4d commit a64e296
Show file tree
Hide file tree
Showing 34 changed files with 650 additions and 256 deletions.
26 changes: 0 additions & 26 deletions cmdb-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,6 @@
</dependency>
</dependencies>

<profiles>
<profile>
<id>build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>plugin</id>
<activation>
<property>
<name>buildType</name>
<value>plugin</value>
</property>
</activation>
</profile>
</profiles>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@ public static class ADM_LOG {

public static final List<String> MYSQL_SCHEMA_KEYWORDS = Arrays.asList("ACCESSIBLE","ADD","ALL","ALTER","ANALYZE","AND","AS","ASC","ASENSITIVE","BEFORE","BETWEEN","BIGINT","BINARY","BLOB","BOTH","BY","CALL","CASCADE","CASE","CHANGE","CHAR","CHARACTER","CHECK","COLLATE","COLUMN","CONDITION","CONSTRAINT","CONTINUE","CONVERT","CREATE","CROSS","CUBE","CUME_DIST","CURRENT_DATE","CURRENT_TIME","CURRENT_TIMESTAMP","CURRENT_USER","CURSOR","DATABASE","DATABASES","DAY_HOUR","DAY_MICROSECOND","DAY_MINUTE","DAY_SECOND","DEC","DECIMAL","DECLARE","DEFAULT","DELAYED","DELETE","DENSE_RANK","DESC","DESCRIBE","DETERMINISTIC","DISTINCT","DISTINCTROW","DIV","DOUBLE","DROP","DUAL","EACH","ELSE","ELSEIF","EMPTY","ENCLOSED","ESCAPED","EXCEPT","EXISTS","EXIT","EXPLAIN","FALSE","FETCH","FIRST_VALUE","FLOAT","FLOAT4","FLOAT8","FOR","FORCE","FOREIGN","FROM","FULLTEXT","FUNCTION","GENERATED","GET","GRANT","GROUP","GROUPING","GROUPS","HAVING","HIGH_PRIORITY","HOUR_MICROSECOND","HOUR_MINUTE","HOUR_SECOND","IF","IGNORE","IN","INDEX","INFILE","INNER","INOUT","INSENSITIVE","INSERT","INT","INT1","INT2","INT3","INT4","INT8","INTEGER","INTERVAL","INTO","IO_AFTER_GTIDS","IO_BEFORE_GTIDS","IS","ITERATE","JOIN","JSON_TABLE","KEY","KEYS","KILL","LAG","LAST_VALUE","LATERAL","LEAD","LEADING","LEAVE","LEFT","LIKE","LIMIT","LINEAR","LINES","LOAD","LOCALTIME","LOCALTIMESTAMP","LOCK","LONG","LONGBLOB","LONGTEXT","LOOP","LOW_PRIORITY","MASTER_BIND","MASTER_SSL_VERIFY_SERVER_CERT","MATCH","MAXVALUE","MEDIUMBLOB","MEDIUMINT","MEDIUMTEXT","MEMBER","MIDDLEINT","MINUTE_MICROSECOND","MINUTE_SECOND","MOD","MODIFIES","NATURAL","NOT","NO_WRITE_TO_BINLOG","NTH_VALUE","NTILE","NULL","NUMERIC","OF","ON","OPTIMIZE","OPTIMIZER_COSTS","OPTION","OPTIONALLY","OR","ORDER","OUT","OUTER","OUTFILE","OVER","PARTITION","PERCENT_RANK","PRECISION","PRIMARY","PROCEDURE","PURGE","RANGE","RANK","READ","READS","READ_WRITE","REAL","RECURSIVE","REFERENCES","REGEXP","RELEASE","RENAME","REPEAT","REPLACE","REQUIRE","RESIGNAL","RESTRICT","RETURN","REVOKE","RIGHT","RLIKE","ROW","ROWS","ROW_NUMBER","SCHEMA","SCHEMAS","SECOND_MICROSECOND","SELECT","SENSITIVE","SEPARATOR","SET","SHOW","SIGNAL","SMALLINT","SPATIAL","SPECIFIC","SQL","SQLEXCEPTION","SQLSTATE","SQLWARNING","SQL_BIG_RESULT","SQL_CALC_FOUND_ROWS","SQL_SMALL_RESULT","SSL","STARTING","STORED","STRAIGHT_JOIN","SYSTEM","TABLE","TERMINATED","THEN","TINYBLOB","TINYINT","TINYTEXT","TO","TRAILING","TRIGGER","TRUE","UNDO","UNION","UNIQUE","UNLOCK","UNSIGNED","UPDATE","USAGE","USE","USING","UTC_DATE","UTC_TIME","UTC_TIMESTAMP","VALUES","VARBINARY","VARCHAR","VARCHARACTER","VARYING","VIRTUAL","WHEN","WHERE","WHILE","WINDOW","WITH","WRITE","XOR","YEAR_MONTH","ZEROFILL");
public static final List<String> DEFAULT_FIELDS = Arrays.asList("created_date","created_by","updated_date","updated_by","guid","r_guid","p_guid","key_name","state","code","description","fixed_date","orchestration","biz_key");

public static final String SYMBOL_COMMA = ",";
public static final String SYMBOL_EQUALSIGN = "=";
public static final String SYMBOL_AND = "&";

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,18 @@ public Object getAvailableCiTypeAttributeInputTypes() {
public Object getEffectiveStatus() {
return constantService.getEffectiveStatus();
}

@GetMapping("/static-data/filter-operator")
@ResponseBody
public Object getFilterOperator() {
return constantService.getFilterOperator();
}

@GetMapping("/static-data/special-connector")
@ResponseBody
public Object getSpecialConnector() {
return constantService.getSpecialConnector();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ public class IntegrationQueryDto {
// relation with parent node, it is not needed in root node.
private Relationship parentRs;
private List<IntegrationQueryDto> children = new LinkedList<>();
private List<Filter> filters = new ArrayList<Filter>();
public List<Filter> getFilters() {
return filters;
}

public IntegrationQueryDto() {
public void setFilters(List<Filter> filters) {
this.filters = filters;
}

public IntegrationQueryDto() {
}

public IntegrationQueryDto(String name) {
Expand Down Expand Up @@ -180,8 +188,8 @@ public List<String> getAggKeyNames() {
public void setAggKeyNames(List<String> aggKeyNames) {
this.aggKeyNames = aggKeyNames;
}

@Override
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("ciTypeId", ciTypeId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public AdmCiTypeAttr objectToCiTypeAttr(Object[] arr, AdmCiType admCiType) {
admCiTypeAttr.setIsAuto((Integer) arr[24]);
admCiTypeAttr.setAutoFillRule((String) arr[25]);
admCiTypeAttr.setRegularExpressionRule((String) arr[26]);
admCiTypeAttr.setIsRefreshable((Integer) arr[27]);
admCiTypeAttr.setIsRefreshable(((Integer) arr[27]));
return admCiTypeAttr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1223,15 +1223,15 @@ public void delete(int ciTypeId, List<String> ids) {
public void doDelete(EntityManager entityManager, int ciTypeId, String guid, boolean enableStateTransition) {
DynamicEntityMeta entityMeta = getDynamicEntityMetaMap().get(ciTypeId);
Object entityBean = validateCi(ciTypeId, guid, entityMeta, entityManager, ACTION_REMOVAL);
DynamicEntityHolder entityHolder = new DynamicEntityHolder(entityMeta, entityBean);
if (enableStateTransition) {
ciDataInterceptorService.preDelete(ciTypeId, guid, true, entityMeta);
DynamicEntityHolder entityHolder = new DynamicEntityHolder(entityMeta, entityBean);
this.stateTransEngine.process(entityManager, ciTypeId, guid, StateOperation.Delete.getCode(), null, entityHolder);
} else {
ciDataInterceptorService.preDelete(ciTypeId, guid, false, entityMeta);
entityManager.remove(entityBean);
}
ciDataInterceptorService.postDelete(ciTypeId, guid, entityMeta);
ciDataInterceptorService.postDelete(entityHolder, entityManager, ciTypeId, guid, entityMeta);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.webank.cmdb.service.impl;

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

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.webank.cmdb.constant.CiStatus;
import static com.webank.cmdb.constant.CmdbConstants.SYMBOL_AND;
import static com.webank.cmdb.constant.CmdbConstants.SYMBOL_EQUALSIGN;
import com.webank.cmdb.constant.EffectiveStatus;
import com.webank.cmdb.constant.FilterOperator;
import com.webank.cmdb.constant.InputType;
import com.webank.cmdb.constant.ReferenceType;
import com.webank.cmdb.util.SpecialSymbolUtils;

@Service
public class ConstantService {
Expand All @@ -22,7 +29,7 @@ public List<String> getCiStatus() {
}
return statuses;
}

public List<String> getReferenceTypes() {
List<String> referenceTypes = Lists.newLinkedList();
for (ReferenceType referenceType : ReferenceType.values()) {
Expand All @@ -33,7 +40,7 @@ public List<String> getReferenceTypes() {
}
return referenceTypes;
}

public List<String> getInputTypes() {
List<String> inputTypes = Lists.newLinkedList();
for (InputType inputType : InputType.values()) {
Expand All @@ -44,7 +51,7 @@ public List<String> getInputTypes() {
}
return inputTypes;
}

public List<String> getEffectiveStatus() {
List<String> effectiveStatus = Lists.newLinkedList();
for (EffectiveStatus status : EffectiveStatus.values()) {
Expand All @@ -55,6 +62,34 @@ public List<String> getEffectiveStatus() {
}
return effectiveStatus;
}



public List<Map<Object, Object>> getFilterOperator() {
List<Map<Object, Object>> filterOperatorList = Lists.newLinkedList();
for (FilterOperator status : FilterOperator.values()) {
if (FilterOperator.None.equals(status))
continue;
HashMap<Object, Object> filterOperatorMap = Maps.newHashMap();
filterOperatorMap.put("code", status.getCode());
filterOperatorMap.put("value", status.name());
filterOperatorList.add(filterOperatorMap);
}
return filterOperatorList;
}

public Object getSpecialConnector() {
List<Map<Object, Object>> specialConnector = Lists.newLinkedList();

Map<Object, Object> and = Maps.newHashMap();
and.put("code", SpecialSymbolUtils.getAroundSpecialSymbol(SYMBOL_AND));
and.put("value", SYMBOL_AND);
specialConnector.add(and);

Map<Object, Object> equal = Maps.newHashMap();
equal.put("code", SpecialSymbolUtils.getAroundSpecialSymbol(SYMBOL_EQUALSIGN));
equal.put("value", SYMBOL_EQUALSIGN);
specialConnector.add(equal);

return specialConnector;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String getName() {

@Override
public boolean isTableExisted(String tableName) {
Query tableQuery = entityManager.createNativeQuery(String.format("show tables like '%s'", tableName));
Query tableQuery = entityManager.createNativeQuery(String.format("select 1 from information_schema.tables where table_name = '%s' limit 1", tableName));
List<?> results = tableQuery.getResultList();
return results.size() > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -53,10 +54,12 @@
import com.webank.cmdb.util.DateUtils;
import com.webank.cmdb.util.JpaQueryUtils;
import com.webank.cmdb.util.JsonUtil;
import static com.webank.cmdb.util.SpecialSymbolUtils.getAfterSpecialSymbol;

@Service
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CiDataInterceptorService {
private static final String TARGET_DEFAULT_VALUE = "";
private static final String TARGET_NAME = "targetName";

@Autowired
Expand Down Expand Up @@ -271,7 +274,7 @@ private void validateRequiredFieldForCreation(DynamicEntityHolder entityHolder,
public void postCreate(DynamicEntityHolder entityHolder, Map<String, Object> updateCi, Map<Integer, DynamicEntityMeta> multRefMetaMap, EntityManager entityManager) {
entityManager.flush();
handleAutoFill(entityHolder, entityManager);

handleReferenceAutoFill(entityHolder, entityManager, updateCi);
updateSeqNoForMultiReferenceFields(entityHolder, updateCi, entityManager);
}

Expand Down Expand Up @@ -366,7 +369,7 @@ private List<String> getValueFromResponse(QueryResponse response, List<AutoFillI
List<Map<String, Object>> contents = response.getContents();
List<String> targetValues = new ArrayList<>();
contents.forEach(content -> {
Object targetValue = content.get(TARGET_NAME) != null ? content.get(TARGET_NAME) : content.get("root$guid");
Object targetValue = content.get(TARGET_NAME) != null ? content.get(TARGET_NAME) : TARGET_DEFAULT_VALUE;
if (targetValue != null) {
if (targetValue instanceof CatCodeDto) {
targetValues.add(getValueFromEnumCode(routines, targetValue));
Expand Down Expand Up @@ -424,7 +427,7 @@ private String queryValueByRule(String rootGuid, AdmCiTypeAttr attrWithGuid, Obj
sb.append(value);
}
if (i < targetValues.size() - 1) {
sb.append(",");
sb.append(getAfterSpecialSymbol(CmdbConstants.SYMBOL_COMMA));
}
}
} catch (IOException e) {
Expand All @@ -446,10 +449,11 @@ private boolean checkExpression(String targetValue) {

private QueryResponse queryIntegrateWithRoutines(String guid, AdmCiTypeAttr attrWithGuid, List<AutoFillIntegrationQueryDto> routines) {
AdhocIntegrationQueryDto adhocDto = buildRootDto(routines.get(0), guid, attrWithGuid);
travelFillQueryDto(routines, adhocDto.getCriteria(), 1);
travelFillQueryDto(routines, adhocDto.getCriteria(), adhocDto.getQueryRequest(),1);

return ciService.adhocIntegrateQuery(adhocDto);
}

private List<String> getRootGuids(String guid, AdmCiTypeAttr attrWithGuid, Object autoFillRuleValue) {
List<String> guids = new ArrayList<>();
List<AutoFillItem> autoRuleItems = parserRule(autoFillRuleValue);
Expand All @@ -476,7 +480,7 @@ private List<String> getRootGuids(String guid, AdmCiTypeAttr attrWithGuid, Objec
return guids;
}

private IntegrationQueryDto travelFillQueryDto(List<AutoFillIntegrationQueryDto> routines, IntegrationQueryDto parentDto, int position) {
private IntegrationQueryDto travelFillQueryDto(List<AutoFillIntegrationQueryDto> routines, IntegrationQueryDto parentDto,QueryRequest queryRequest, int position) {
if (position >= routines.size()) {
return null;
}
Expand All @@ -490,49 +494,49 @@ private IntegrationQueryDto travelFillQueryDto(List<AutoFillIntegrationQueryDto>
dto.setName("index" + position);
dto.setCiTypeId(item.getCiTypeId());
dto.setParentRs(parentRs);
dto.setAttrs(Arrays.asList(getGuidAttrIdByCiTypeId(item.getCiTypeId())));
dto.setAttrKeyNames(Arrays.asList(item.getCiTypeId() + "$guid"));

IntegrationQueryDto childDto = travelFillQueryDto(routines, dto, ++position);
List<String> fileds = new ArrayList();
List<Integer> attrs = new ArrayList();
if (position < routines.size()-1) {
attrs.add(getAttrIdByPropertyNameAndCiTypeId(item.getCiTypeId(),"guid"));
fileds.add(item.getCiTypeId() + "$guid");
}
if(item.getFilters().size()>0) {
List<Filter> filters = new ArrayList<Filter>(queryRequest.getFilters());
item.getFilters().stream().forEach(filter -> {
attrs.add(getAttrIdByPropertyNameAndCiTypeId(item.getCiTypeId(),filter.getName()));
filter.setName(item.getCiTypeId() + "$" + filter.getName());
filters.add(filter);
fileds.add(filter.getName());
});
queryRequest.setFilters(filters);
}
dto.setAttrs(attrs);
dto.setAttrKeyNames(fileds);
IntegrationQueryDto childDto = travelFillQueryDto(routines, dto, queryRequest, ++position);

if (childDto == null) {
if (parentDto.getAttrs().contains(parentRs.getAttrId())) {
replaceKeyNameWithTargetName(parentDto);
} else {
addTargetName(parentDto, parentRs);
}
addTargetName(parentDto, dto);
} else {
parentDto.setChildren(Arrays.asList(childDto));
}

return parentDto;
}

private void addTargetName(IntegrationQueryDto parentDto, Relationship parentRs) {
private void addTargetName(IntegrationQueryDto parentDto, IntegrationQueryDto dto) {
List<Integer> attrs = new ArrayList<>();
attrs.add(parentRs.getAttrId());
attrs.addAll(dto.getAttrs());
attrs.addAll(parentDto.getAttrs());
attrs.add(dto.getParentRs().getAttrId());

List<String> attrKeyNames = new ArrayList<>();
attrKeyNames.add(TARGET_NAME);
attrKeyNames.addAll(dto.getAttrKeyNames());
attrKeyNames.addAll(parentDto.getAttrKeyNames());
attrKeyNames.add(TARGET_NAME);

parentDto.setAttrs(attrs);
parentDto.setAttrKeyNames(attrKeyNames);
}

private void replaceKeyNameWithTargetName(IntegrationQueryDto parentDto) {
List<String> attrKeyNames = new ArrayList<>();
parentDto.getAttrKeyNames().forEach(x -> {
if (x.equals(parentDto.getCiTypeId() + "$guid")) {
attrKeyNames.add(TARGET_NAME);
} else {
attrKeyNames.add(x);
}
});
parentDto.setAttrKeyNames(attrKeyNames);
}

private AdhocIntegrationQueryDto buildRootDto(IntegrationQueryDto routineDto, String guid, AdmCiTypeAttr attrWithGuid) {
AdhocIntegrationQueryDto adhocDto = new AdhocIntegrationQueryDto();

Expand All @@ -548,7 +552,7 @@ private AdhocIntegrationQueryDto buildRootDto(IntegrationQueryDto routineDto, St
IntegrationQueryDto rootDto = new IntegrationQueryDto();
rootDto.setName("root");
rootDto.setCiTypeId(routineDto.getCiTypeId());
rootDto.setAttrs(Arrays.asList(getGuidAttrIdByCiTypeId(routineDto.getCiTypeId())));
rootDto.setAttrs(Arrays.asList(getAttrIdByPropertyNameAndCiTypeId(routineDto.getCiTypeId(),"guid")));
rootDto.setAttrKeyNames(Arrays.asList("root$guid"));

adhocDto.setCriteria(rootDto);
Expand All @@ -557,10 +561,10 @@ private AdhocIntegrationQueryDto buildRootDto(IntegrationQueryDto routineDto, St
return adhocDto;
}

private Integer getGuidAttrIdByCiTypeId(int ciTypeId) {
private Integer getAttrIdByPropertyNameAndCiTypeId(int ciTypeId, String propertyName) {
List<AdmCiTypeAttr> attrs = ciTypeAttrRepository.findAllByCiTypeId(ciTypeId);
for (AdmCiTypeAttr attr : attrs) {
if ("guid".equalsIgnoreCase(attr.getPropertyName())) {
if (propertyName.equalsIgnoreCase(attr.getPropertyName())) {
return attr.getIdAdmCiTypeAttr();
}
}
Expand Down Expand Up @@ -665,8 +669,9 @@ public void preDelete(int ciTypeId, String guid, boolean checkFinalState, Dynami
}
}

public void postDelete(int ciTypeId, String guid, DynamicEntityMeta entityMeta) {

public void postDelete(DynamicEntityHolder entityHolder, EntityManager entityManager, int ciTypeId, String guid, DynamicEntityMeta entityMeta) {
//entityManager.flush();
//handleReferenceAutoFill(entityHolder, entityManager, entityHolder.getEntityBeanMap());
}

}
Loading

0 comments on commit a64e296

Please sign in to comment.