Search in sources :

Example 1 with AqlField

use of org.ehrbase.client.aql.field.AqlField in project openEHR_SDK by ehrbase.

the class RecordGenerator method buildQuery.

private void buildQuery(HashMap<Integer, TypeSpec> typeSpecHashMap) throws IOException {
    TypeSpec.Builder builder = TypeSpec.interfaceBuilder("Query");
    builder.addTypeVariable(TypeVariableName.get("T", Record.class));
    builder.addModifiers(Modifier.PUBLIC);
    builder.addMethod(MethodSpec.methodBuilder("buildAql").addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC).returns(String.class).build());
    builder.addMethod(MethodSpec.methodBuilder("fields").addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC).returns(ArrayTypeName.of(ParameterizedTypeName.get(ClassName.get(AqlField.class), TypeName.OBJECT))).build());
    builder.addMethod(MethodSpec.methodBuilder("buildEntityQuery").addModifiers(Modifier.STATIC, Modifier.PUBLIC).addParameter(ContainmentExpression.class, "containment").addParameter(ArrayTypeName.of(ParameterizedTypeName.get(ClassName.get(SelectAqlField.class), WildcardTypeName.subtypeOf(Object.class))), "selectFields").addCode("return new EntityQuery<>(containment, selectFields);").varargs(true).returns(ParameterizedTypeName.get(EntityQuery.class, Record.class)).build());
    builder.addMethod(MethodSpec.methodBuilder("buildNativeQuery").addModifiers(Modifier.STATIC, Modifier.PUBLIC).addParameter(String.class, "aql").addParameter(ArrayTypeName.of(ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(Object.class))), "expected").addCode(CodeBlock.builder().addStatement("return new NativeQuery<>(aql, $T.stream(expected).map(AqlField::create).toArray($T<?>[]::new))", ClassName.get(Arrays.class), ClassName.get(AqlField.class)).build()).varargs(true).returns(ParameterizedTypeName.get(NativeQuery.class, Record.class)).build());
    for (int n = 1; n <= MAX_RECORD_SIZE; n++) {
        TypeSpec recordNTypeSpec = typeSpecHashMap.get(n);
        builder.addMethod(buildEntityQueryMethod(recordNTypeSpec));
        builder.addMethod(buildNativeQueryMethod(recordNTypeSpec));
    }
    TypeSpec typeSpec = builder.build();
    JavaFile javaFile = JavaFile.builder(QUERY_PACKAGE_NAME, typeSpec).build();
    javaFile.writeTo(DIRECTORY);
}
Also used : NativeQuery(org.ehrbase.client.aql.query.NativeQuery) JavaFile(com.squareup.javapoet.JavaFile) Record(org.ehrbase.client.aql.record.Record) EntityQuery(org.ehrbase.client.aql.query.EntityQuery) Arrays(java.util.Arrays) AqlField(org.ehrbase.client.aql.field.AqlField) SelectAqlField(org.ehrbase.client.aql.field.SelectAqlField) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

JavaFile (com.squareup.javapoet.JavaFile)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Arrays (java.util.Arrays)1 AqlField (org.ehrbase.client.aql.field.AqlField)1 SelectAqlField (org.ehrbase.client.aql.field.SelectAqlField)1 EntityQuery (org.ehrbase.client.aql.query.EntityQuery)1 NativeQuery (org.ehrbase.client.aql.query.NativeQuery)1 Record (org.ehrbase.client.aql.record.Record)1