Search in sources :

Example 16 with CodeBuilder

use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.

the class RowMapperBuilder method mapMethod.

private String mapMethod() {
    var builder = new CodeBuilder().append("public Object map({} resultSet) {\n", type(ResultSetWrapper.class));
    String entityClassLiteral = type(entityClass);
    builder.indent(1).append("{} entity = new {}();\n", entityClassLiteral, entityClassLiteral);
    for (Field field : Classes.instanceFields(entityClass)) {
        String fieldName = field.getName();
        Class<?> fieldClass = field.getType();
        String column = field.getDeclaredAnnotation(Column.class).name();
        if (Integer.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getInt(\"{}\");\n", fieldName, column);
        } else if (String.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getString(\"{}\");\n", fieldName, column);
        } else if (Boolean.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getBoolean(\"{}\");\n", fieldName, column);
        } else if (Long.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getLong(\"{}\");\n", fieldName, column);
        } else if (LocalDateTime.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getLocalDateTime(\"{}\");\n", fieldName, column);
        } else if (LocalDate.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getLocalDate(\"{}\");\n", fieldName, column);
        } else if (ZonedDateTime.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getZonedDateTime(\"{}\");\n", fieldName, column);
        } else if (fieldClass.isEnum()) {
            registerEnumClass(fieldClass);
            this.builder.addField("private final {} {}Mappings = new {}({});", type(DBEnumMapper.class), fieldName, type(DBEnumMapper.class), variable(fieldClass));
            builder.indent(1).append("entity.{} = ({}){}Mappings.getEnum(resultSet.getString(\"{}\"));\n", fieldName, type(fieldClass), fieldName, column);
        } else if (Double.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getDouble(\"{}\");\n", fieldName, column);
        } else if (BigDecimal.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getBigDecimal(\"{}\");\n", fieldName, column);
        }
    }
    builder.indent(1).append("return entity;\n");
    builder.append("}");
    return builder.build();
}
Also used : Field(java.lang.reflect.Field) Column(core.framework.db.Column) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 17 with CodeBuilder

use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.

the class ActionDiagram method dot.

public String dot() {
    var dot = new CodeBuilder().append("digraph {\n");
    dot.append("rankdir=LR;\n");
    dot.append("node [style=\"rounded, filled\", fontname=arial, fontsize=12, fontcolor=black];\n");
    dot.append("edge [arrowsize=0.5];\n");
    for (String app : apps()) {
        if (app.startsWith("_direct_")) {
            dot.append("\"{}\" [label=direct, shape=point];\n", app);
        } else {
            dot.append("\"{}\" [label=\"{}\", shape=circle];\n", app, app);
        }
    }
    messagePublishes().forEach((topic, clients) -> {
        dot.append("\"{}\" [label=\"{}\", shape=box];\n", topic, topic);
        for (String client : clients) {
            dot.append("\"{}\" -> \"{}\" [style=dashed];\n", client, topic);
        }
    });
    for (Map.Entry<Key, List<ActionDocument>> entry : actions.entrySet()) {
        Key key = entry.getKey();
        String app = key.app;
        String action = key.action;
        String color = color();
        String tooltip = tooltip(action, app, entry.getValue());
        dot.append("\"{}\" [label=\"{}\", shape=box, color=\"{}\", fillcolor=\"{}\", tooltip=\"{}\"];\n", key.id(), key.action, color, color, tooltip);
        if (action.startsWith("app:") || action.startsWith("task:") || action.contains(":task:") || action.startsWith("job:")) {
            dot.append("\"{}\" -> \"{}\" [arrowhead=none];\n", app, key.id());
        } else if (action.startsWith("api:")) {
            Set<String> clients = entry.getValue().stream().map(this::clients).flatMap(Set::stream).collect(Collectors.toSet());
            for (String client : clients) {
                dot.append("\"{}\" -> \"{}\";\n", client, key.id());
            }
            dot.append("\"{}\" -> \"{}\";\n", key.id(), app);
        } else if (action.startsWith("topic:")) {
            String topic = action.substring(6);
            dot.append("\"{}\" -> \"{}\" [style=dashed];\n", topic, key.id());
            dot.append("\"{}\" -> \"{}\" [style=dashed];\n", key.id(), app);
        }
    }
    dot.append("}\n");
    return dot.build();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 18 with CodeBuilder

use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.

the class ActionDiagram method tooltip.

String tooltip(String action, String app, List<ActionDocument> actions) {
    var builder = new CodeBuilder();
    builder.append("<table>\n").append("<caption>{}</caption>\n", action).append("<tr><td>app</td><td>{}</td></tr>\n", app);
    buildActionInfo(builder, actions);
    builder.append("<tr><td colspan=2 class=section>action id</td></tr>\n");
    for (ActionDocument doc : actions) {
        if ("WARN".equals(doc.result) || "ERROR".equals(doc.result)) {
            String color = "WARN".equals(doc.result) ? "OrangeRed" : "Red";
            builder.append("<tr style='color:{}'><td>{}</td><td>{}</td></tr>\n", color, doc.id, doc.errorCode);
        } else {
            builder.append("<tr><td colspan=2>{}</td></tr>\n", doc.id);
        }
    }
    builder.append("</table>");
    return builder.build();
}
Also used : CodeBuilder(core.framework.internal.asm.CodeBuilder) ActionDocument(core.log.domain.ActionDocument)

Example 19 with CodeBuilder

use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.

the class EntityEncoderBuilder method build.

EntityEncoder<T> build() {
    String methodName = encodeEntityMethod(entityClass);
    CodeBuilder builder = new CodeBuilder();
    builder.append("public void encode(org.bson.BsonWriter writer, Object entity) {\n").indent(1).append("{} wrapper = new {}(writer);\n", type(BsonWriterWrapper.class), type(BsonWriterWrapper.class)).indent(1).append("{}(writer, wrapper, ({}) entity);\n", methodName, entityClass.getCanonicalName()).append("}");
    this.builder.addMethod(builder.build());
    return this.builder.build();
}
Also used : CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 20 with CodeBuilder

use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.

the class EntityEncoderBuilder method encodeEntityMethod.

private String encodeEntityMethod(Class<?> entityClass) {
    String methodName = encodeMethods.get(entityClass);
    if (methodName != null)
        return methodName;
    methodName = "encode" + entityClass.getSimpleName() + (index++);
    CodeBuilder builder = new CodeBuilder();
    builder.append("private void {}(org.bson.BsonWriter writer, {} wrapper, {} entity) {\n", methodName, type(BsonWriterWrapper.class), type(entityClass));
    builder.indent(1).append("writer.writeStartDocument();\n");
    for (Field field : Classes.instanceFields(entityClass)) {
        String fieldVariable = "entity." + field.getName();
        builder.indent(1).append("writer.writeName({});\n", variable(mongoField(field)));
        encodeField(builder, fieldVariable, field.getGenericType(), 1);
    }
    builder.indent(1).append("writer.writeEndDocument();\n").append('}');
    this.builder.addMethod(builder.build());
    encodeMethods.put(entityClass, methodName);
    return methodName;
}
Also used : Field(java.lang.reflect.Field) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Aggregations

CodeBuilder (core.framework.internal.asm.CodeBuilder)22 Field (java.lang.reflect.Field)9 Type (java.lang.reflect.Type)4 Column (core.framework.db.Column)3 ArrayList (java.util.ArrayList)3 PathParam (core.framework.api.web.service.PathParam)2 QueryParam (core.framework.api.web.service.QueryParam)2 PrimaryKey (core.framework.db.PrimaryKey)2 LocalDate (java.time.LocalDate)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)1 BulkResponseItem (co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem)1 NotNull (core.framework.api.validate.NotNull)1 ResponseStatus (core.framework.api.web.service.ResponseStatus)1 Table (core.framework.db.Table)1 SearchException (core.framework.search.SearchException)1 Request (core.framework.web.Request)1 Response (core.framework.web.Response)1 ActionDocument (core.log.domain.ActionDocument)1