Search in sources :

Example 1 with Table

use of io.requery.Table in project requery by requery.

the class JoinEntityGenerator method generate.

@Override
public void generate() throws IOException {
    AssociativeEntityDescriptor descriptor = attribute.associativeEntity().orElseThrow(IllegalStateException::new);
    String name = descriptor.name();
    if (Names.isEmpty(name)) {
        // create junction table name with TableA_TableB
        name = from.tableName() + "_" + to.tableName();
    }
    ClassName entityName = nameResolver.joinEntityName(descriptor, from, to);
    String className = "Abstract" + entityName.simpleName();
    TypeSpec.Builder junctionType = TypeSpec.classBuilder(className).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).addSuperinterface(Serializable.class).addAnnotation(AnnotationSpec.builder(Entity.class).addMember("model", "$S", from.modelName()).addMember("stateless", "$L", from.isStateless()).build()).addAnnotation(AnnotationSpec.builder(Table.class).addMember("name", "$S", name).build());
    CodeGeneration.addGeneratedAnnotation(processingEnvironment, junctionType);
    Set<AssociativeReference> references = descriptor.columns();
    EntityDescriptor[] entities = new EntityDescriptor[] { from, to };
    Map<AssociativeReference, EntityDescriptor> map = new LinkedHashMap<>();
    if (references.isEmpty()) {
        // generate with defaults
        for (int i = 0; i < entities.length; i++) {
            EntityDescriptor type = entities[i];
            String column = type.tableName() + "Id";
            if (from == to) {
                // if self referencing add a number to the column name
                column += (i + 1);
            }
            AssociativeReference reference = new AssociativeReference(column, type.element(), null, ReferentialAction.CASCADE, ReferentialAction.CASCADE);
            references.add(reference);
            map.put(reference, type);
        }
    } else {
        for (AssociativeReference reference : references) {
            for (EntityDescriptor entity : entities) {
                if (reference.referencedType() != null && reference.referencedType().equals(entity.element())) {
                    map.put(reference, entity);
                }
            }
        }
    }
    int index = 0;
    for (AssociativeReference reference : references) {
        ClassName action = ClassName.get(ReferentialAction.class);
        AnnotationSpec.Builder key = AnnotationSpec.builder(ForeignKey.class).addMember("delete", "$T.$L", action, reference.deleteAction().toString()).addMember("update", "$T.$L", action, reference.updateAction().toString());
        TypeElement referenceElement = reference.referencedType();
        EntityDescriptor entity = map.get(reference);
        if (referenceElement == null) {
            if (entity != null) {
                referenceElement = entity.element();
            } else {
                referenceElement = entities[index++].element();
            }
        }
        if (referenceElement != null) {
            key.addMember("references", "$L.class", nameResolver.generatedTypeNameOf(referenceElement).orElseThrow(IllegalStateException::new));
        }
        if (reference.referencedColumn() != null) {
            key.addMember("referencedColumn", "$S", reference.referencedColumn());
        }
        AnnotationSpec.Builder id = AnnotationSpec.builder(Key.class);
        TypeName typeName = TypeName.get(Integer.class);
        if (entity != null) {
            Optional<? extends AttributeDescriptor> keyAttribute = entity.attributes().stream().filter(AttributeDescriptor::isKey).findAny();
            if (keyAttribute.isPresent()) {
                TypeMirror keyType = keyAttribute.get().typeMirror();
                if (keyType.getKind().isPrimitive()) {
                    Types types = processingEnvironment.getTypeUtils();
                    keyType = types.boxedClass((PrimitiveType) keyType).asType();
                }
                typeName = TypeName.get(keyType);
            }
        }
        FieldSpec.Builder field = FieldSpec.builder(typeName, reference.name(), Modifier.PROTECTED).addAnnotation(key.build()).addAnnotation(id.build());
        junctionType.addField(field.build());
    }
    String packageName = entityName.packageName();
    CodeGeneration.writeType(processingEnvironment, packageName, junctionType.build());
}
Also used : Entity(io.requery.Entity) Types(javax.lang.model.util.Types) TypeName(com.squareup.javapoet.TypeName) Table(io.requery.Table) TypeElement(javax.lang.model.element.TypeElement) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) FieldSpec(com.squareup.javapoet.FieldSpec) LinkedHashMap(java.util.LinkedHashMap) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

AnnotationSpec (com.squareup.javapoet.AnnotationSpec)1 ClassName (com.squareup.javapoet.ClassName)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 TypeName (com.squareup.javapoet.TypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Entity (io.requery.Entity)1 Table (io.requery.Table)1 LinkedHashMap (java.util.LinkedHashMap)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Types (javax.lang.model.util.Types)1