Search in sources :

Example 1 with Parameter

use of ru.curs.celesta.score.Parameter in project celesta by CourseOrchestra.

the class CursorGenerator method buildConstructors.

private static List<MethodSpec> buildConstructors(GrainElement ge, TypeName columnsClassType) {
    List<MethodSpec> results = new ArrayList<>();
    ParameterSpec contextParam = ParameterSpec.builder(CallContext.class, "context").build();
    ParameterSpec fieldsParam = ParameterSpec.builder(ParameterizedTypeName.get(Set.class, String.class), "fields").build();
    ParameterSpec columnsParam = ParameterSpec.builder(ArrayTypeName.of(ParameterizedTypeName.get(ClassName.get(ColumnMeta.class), WildcardTypeName.subtypeOf(Object.class))), "columns").build();
    ParameterSpec parametersParam = ParameterSpec.builder(ParameterizedTypeName.get(Map.class, String.class, Object.class), "parameters").build();
    Supplier<MethodSpec.Builder> msp = () -> MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(contextParam);
    // Common constructor
    MethodSpec.Builder builder = msp.get();
    if (ge instanceof ParameterizedView) {
        builder.addParameter(parametersParam);
        builder.addStatement("super(context, parameters)");
    } else {
        builder.addStatement("super(context)");
    }
    results.add(builder.build());
    if (ge instanceof SequenceElement) {
        return results;
    }
    // Constructor with columns limitation
    builder = msp.get();
    if (ge instanceof ParameterizedView) {
        builder.addParameter(parametersParam);
        builder.addParameter(columnsParam).varargs();
        builder.addStatement("super(context, parameters, columns)");
    } else {
        builder.addParameter(columnsParam).varargs();
        builder.addStatement("super(context, columns)");
    }
    results.add(builder.build());
    // Deprecated constructor with fields limitation
    builder = msp.get();
    if (ge instanceof ParameterizedView) {
        builder.addParameter(fieldsParam);
        builder.addParameter(parametersParam);
        builder.addStatement("super(context, fields, parameters)");
    } else {
        builder.addParameter(fieldsParam);
        builder.addStatement("super(context, fields)");
    }
    results.add(builder.addAnnotation(Deprecated.class).build());
    // ParameterizedView constructors
    if (ge instanceof ParameterizedView) {
        ParameterizedView pv = (ParameterizedView) ge;
        builder = msp.get();
        for (Parameter parameter : pv.getParameters().values()) {
            builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
        }
        String spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "))";
        builder.addStatement(spec, pv.getParameters().keySet().toArray());
        results.add(builder.build());
        builder = msp.get();
        for (Parameter parameter : pv.getParameters().values()) {
            builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
        }
        builder.addParameter(columnsParam).varargs();
        spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "), columns)";
        builder.addStatement(spec, pv.getParameters().keySet().toArray());
        results.add(builder.build());
        results.add(getParameterizedViewTypedConstructorHelper(pv));
    }
    return results;
}
Also used : BasicCursor(ru.curs.celesta.dbutils.BasicCursor) Arrays(java.util.Arrays) Modifier(javax.lang.model.element.Modifier) CelestaException(ru.curs.celesta.CelestaException) ZonedDateTime(java.time.ZonedDateTime) ClassName(com.squareup.javapoet.ClassName) SequenceElement(ru.curs.celesta.score.SequenceElement) Grain(ru.curs.celesta.score.Grain) Parameter(ru.curs.celesta.score.Parameter) ColumnMeta(ru.curs.celesta.score.ColumnMeta) Generated(javax.annotation.Generated) StringColumn(ru.curs.celesta.score.StringColumn) ResultSet(java.sql.ResultSet) Map(java.util.Map) ICelesta(ru.curs.celesta.ICelesta) ZoneOffset(java.time.ZoneOffset) DataGrainElement(ru.curs.celesta.score.DataGrainElement) CelestaGenerated(ru.curs.celesta.dbutils.CelestaGenerated) ParameterSpec(com.squareup.javapoet.ParameterSpec) CallContext(ru.curs.celesta.CallContext) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) Set(java.util.Set) Column(ru.curs.celesta.score.Column) Collectors(java.util.stream.Collectors) JavaFile(com.squareup.javapoet.JavaFile) Objects(java.util.Objects) List(java.util.List) ViewCursor(ru.curs.celesta.dbutils.ViewCursor) TriggerType(ru.curs.celesta.event.TriggerType) BasicTable(ru.curs.celesta.score.BasicTable) GrainElement(ru.curs.celesta.score.GrainElement) BasicDataAccessor(ru.curs.celesta.dbutils.BasicDataAccessor) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) IntegerColumn(ru.curs.celesta.score.IntegerColumn) MaterializedViewCursor(ru.curs.celesta.dbutils.MaterializedViewCursor) ZonedDateTimeColumn(ru.curs.celesta.score.ZonedDateTimeColumn) FieldSpec(com.squareup.javapoet.FieldSpec) TableElement(ru.curs.celesta.score.TableElement) LocalDateTime(java.time.LocalDateTime) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) Cursor(ru.curs.celesta.dbutils.Cursor) HashMap(java.util.HashMap) View(ru.curs.celesta.score.View) ReadOnlyTable(ru.curs.celesta.score.ReadOnlyTable) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Sequence(ru.curs.celesta.dbutils.Sequence) BinaryColumn(ru.curs.celesta.score.BinaryColumn) ParameterizedView(ru.curs.celesta.score.ParameterizedView) CodeBlock(com.squareup.javapoet.CodeBlock) LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) CursorIterator(ru.curs.celesta.dbutils.CursorIterator) TypeSpec(com.squareup.javapoet.TypeSpec) ParameterizedViewCursor(ru.curs.celesta.dbutils.ParameterizedViewCursor) MaterializedView(ru.curs.celesta.score.MaterializedView) Table(ru.curs.celesta.score.Table) File(java.io.File) Consumer(java.util.function.Consumer) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) VersionedElement(ru.curs.celesta.score.VersionedElement) DateTimeFormatter(java.time.format.DateTimeFormatter) NamedElement(ru.curs.celesta.score.NamedElement) FileResource(ru.curs.celesta.score.io.FileResource) ReadOnlyTableCursor(ru.curs.celesta.dbutils.ReadOnlyTableCursor) Collections(java.util.Collections) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) SequenceElement(ru.curs.celesta.score.SequenceElement) ArrayList(java.util.ArrayList) CallContext(ru.curs.celesta.CallContext) Parameter(ru.curs.celesta.score.Parameter) ParameterizedView(ru.curs.celesta.score.ParameterizedView)

Example 2 with Parameter

use of ru.curs.celesta.score.Parameter in project celesta by CourseOrchestra.

the class CursorGenerator method getParameterizedViewTypedConstructorHelper.

private static MethodSpec getParameterizedViewTypedConstructorHelper(ParameterizedView pv) {
    MethodSpec.Builder builder = MethodSpec.methodBuilder("paramsMap").addModifiers(Modifier.PRIVATE, Modifier.STATIC).returns(ParameterizedTypeName.get(Map.class, String.class, Object.class));
    builder.addStatement("$T<$T,$T> params = new $T<>()", Map.class, String.class, Object.class, HashMap.class);
    for (Parameter parameter : pv.getParameters().values()) {
        String paramName = parameter.getName();
        builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), paramName).build());
        builder.addStatement("params.put($S, $N)", paramName, paramName);
    }
    builder.addStatement("return params");
    return builder.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) Parameter(ru.curs.celesta.score.Parameter) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

MethodSpec (com.squareup.javapoet.MethodSpec)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Parameter (ru.curs.celesta.score.Parameter)2 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)1 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)1 ClassName (com.squareup.javapoet.ClassName)1 CodeBlock (com.squareup.javapoet.CodeBlock)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 JavaFile (com.squareup.javapoet.JavaFile)1 ParameterSpec (com.squareup.javapoet.ParameterSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)1 File (java.io.File)1 IOException (java.io.IOException)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1