live.document.mavenplugin.method.MethodMindMap4CallTree Maven / Gradle / Ivy
package live.document.mavenplugin.method;
import com.github.vertical_blank.sqlformatter.SqlFormatter;
import com.github.vertical_blank.sqlformatter.core.FormatConfig;
import com.github.vertical_blank.sqlformatter.languages.Dialect;
import live.document.generator.model.CallNode;
import live.document.plsqlscanner.PlSqlObject;
import live.document.scanner.DbObjectTypeEnum;
import live.document.scanner.DbOperationTypeEnum;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class MethodMindMap4CallTree extends EntityMethodMindMap4CallTree {
public static final int MAX_TABLE_COLOR_COUNT = 16;
protected Function hasEntityWriteOperation;
protected Function getTableIndex;
protected Function getTableSampleDiagram;
@Override
protected String getSuffix(CallNode node) {
if (node.isLink() && node.getLinkedNode() != null) {
return getSuffix(node.getLinkedNode()) + " <>";
}
if (hasEntityWriteOperation == null) {
return super.getSuffix(node);
}
boolean writeOperation = hasEntityWriteOperation.apply(node);
String format = "%s%s";
if (node.getText() != null && node.getText().trim().length() > 0) {
format = "\\n%s%s";
}
return String.format(format, writeOperation ? "" : "", super.getSuffix(node));
}
@Override
protected String getNote(CallNode node) {
String result = "";
if (isLeafTableWriteNode(node)) {
String tableName = node.getDbOperation().getName();
result = getParentNodeSql(node, tableName);
}
if (result.length() > 0) {
return "" + result + " ";
}
return result;
}
private String getParentNodeSql(CallNode node, String tableName) {
CallNode parent = node.getParent();
Set sqls = new HashSet<>();
if (parent.getMethodObject() != null) {
sqls = parent.getMethodObject().getTableWriteStatements().get(tableName);
} else if (parent.getDbOperation() != null && parent.getDbOperation().getObjectType() == DbObjectTypeEnum.PROCEDURE_FUNCTION) {
PlSqlObject plSqlObject = plSqlExplained.findByName(parent.getDbOperation().getName());
if (plSqlObject != null) {
sqls = plSqlObject.getTableWriteStatements().get(tableName);
}
}
if (sqls != null && ! sqls.isEmpty()) {
return formatSql(sqls);
}
return "";
}
private String formatSql(Set sqls) {
FormatConfig build = FormatConfig.builder()
.indent(" ") // Defaults to two spaces
.uppercase(true) // Defaults to false (not safe to use when SQL dialect has case-sensitive identifiers)
.maxColumnLength(140) // Defaults to 50
.build();
return sqls.stream().map(s -> SqlFormatter.of(Dialect.PlSql).format(s, build))
.sorted()
.collect(Collectors.joining("\\n"))
.replace("\n", "\\n");
}
private boolean isLeafTableWriteNode(CallNode node) {
return node.getChildren().size() == 0 && isTableWriteNode(node);
}
@Override
protected String getStyle(CallNode node) {
if (node.getMethodObject() == null) {
if (hasDynamicCall(node)) {
return " <>";
}
if (isTableWriteNode(node)
) {
if (getTableIndex != null) {
Integer index = getTableIndex.apply(node.getDbOperation().getName());
if (index >= 0 && index <= MAX_TABLE_COLOR_COUNT) {
return " <>";
}
}
}
return " <>";
}
return super.getStyle(node);
}
private boolean isTableWriteNode(CallNode node) {
return node.getDbOperation() != null &&
node.getDbOperation().getObjectType() == DbObjectTypeEnum.TABLE &&
(node.getDbOperation().getOperationType() == DbOperationTypeEnum.WRITE || node.getDbOperation().getOperationType() == DbOperationTypeEnum.READ_WRITE);
}
public void setGetTableSampleDiagram(Function getTableSampleDiagram) {
this.getTableSampleDiagram = getTableSampleDiagram;
}
@Override
protected String getDocumentSuffix() {
if (getTableSampleDiagram != null) {
return getTableSampleDiagram.apply("");
}
return super.getDocumentSuffix();
}
public void setGetTableIndex(Function getTableIndex) {
this.getTableIndex = getTableIndex;
}
@Override
protected String getNodeSelfExceptions(CallNode node, int level) {
return "";
}
@Override
protected String getNodeIsolatedExceptions(CallNode node, int level) {
return "";
}
public void setHasEntityWriteOperation(Function hasEntityWriteOperation) {
this.hasEntityWriteOperation = hasEntityWriteOperation;
}
}