Search in sources :

Example 6 with ForeignKeyMetadata

use of javax.jdo.metadata.ForeignKeyMetadata in project motech by motech.

the class EntityMetadataBuilderImpl method setElementMetadata.

private void setElementMetadata(FieldMetadata fmd, Element element, RelationshipHolder holder, EntityDto entity) {
    if (element == null || StringUtils.isEmpty(element.column())) {
        ElementMetadata emd = fmd.newElementMetadata();
        emd.setColumn((ClassName.getSimpleName(holder.getRelatedClass()) + ID_SUFFIX).toUpperCase());
        ForeignKeyMetadata fkmd = emd.newForeignKeyMetadata();
        fkmd.setName(KeyNames.foreignKeyName(ClassName.getSimpleName(holder.getRelatedClass()), entity.getId(), holder.getRelatedField(), EntityType.STANDARD));
    }
}
Also used : ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) ElementMetadata(javax.jdo.metadata.ElementMetadata)

Example 7 with ForeignKeyMetadata

use of javax.jdo.metadata.ForeignKeyMetadata in project motech by motech.

the class EntityMetadataBuilderImpl method processRelationship.

private void processRelationship(FieldMetadata fmd, RelationshipHolder holder, EntityDto entity, FieldDto field, Class<?> definition) {
    String relatedClass = holder.getRelatedClass();
    fmd.newExtensionMetadata(DATANUCLEUS, "cascade-persist", holder.isCascadePersist() ? TRUE : FALSE);
    fmd.newExtensionMetadata(DATANUCLEUS, "cascade-update", holder.isCascadeUpdate() ? TRUE : FALSE);
    if (holder.isOneToMany() || holder.isManyToMany()) {
        setUpCollectionMetadata(fmd, relatedClass, holder, EntityType.STANDARD);
    } else if (holder.isOneToOne()) {
        processOneToOneRelationship(fmd, holder);
    }
    if (shouldSetNullDelete(holder, field)) {
        ForeignKeyMetadata fkmd = getOrCreateRelFkMetadata(fmd, entity, field);
        fkmd.setDeleteAction(ForeignKeyAction.NULL);
    }
    if (holder.isManyToMany()) {
        addManyToManyMetadata(fmd, holder, entity, field, definition);
    }
    if (shouldSetCascadeDelete(holder, EntityType.STANDARD)) {
        ForeignKeyMetadata fkmd = getOrCreateFkMetadata(fmd);
        fkmd.setDeleteAction(ForeignKeyAction.CASCADE);
    }
}
Also used : ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata)

Example 8 with ForeignKeyMetadata

use of javax.jdo.metadata.ForeignKeyMetadata in project motech by motech.

the class EntityMetadataBuilderImpl method setMapMetadata.

private FieldMetadata setMapMetadata(ClassMetadata cmd, EntityDto entity, FieldDto field, Class<?> definition, EntityType entityType) {
    FieldMetadata fmd = cmd.newFieldMetadata(getNameForMetadata(field));
    MetadataDto keyMetadata = field.getMetadata(MAP_KEY_TYPE);
    MetadataDto valueMetadata = field.getMetadata(MAP_VALUE_TYPE);
    boolean serialized = shouldSerializeMap(keyMetadata, valueMetadata);
    // Depending on the types of key and value of the map we either serialize the map or create a separate table for it
    fmd.setSerialized(serialized);
    addDefaultFetchGroupMetadata(fmd, definition);
    MapMetadata mmd = fmd.newMapMetadata();
    if (serialized) {
        mmd.setSerializedKey(true);
        mmd.setSerializedValue(true);
    } else {
        mmd.setKeyType(keyMetadata.getValue());
        mmd.setValueType(valueMetadata.getValue());
        fmd.setTable(ClassTableName.getTableName(cmd.getTable(), getNameForMetadata(field)));
        JoinMetadata jmd = fmd.newJoinMetadata();
        ForeignKeyMetadata fkmd = getOrCreateFkMetadata(jmd);
        fkmd.setDeleteAction(ForeignKeyAction.CASCADE);
        fkmd.setName(KeyNames.mapForeignKeyName(entity.getName(), entity.getId(), field.getBasic().getName(), entityType));
    }
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) JoinMetadata(javax.jdo.metadata.JoinMetadata) MetadataDto(org.motechproject.mds.dto.MetadataDto) MapMetadata(javax.jdo.metadata.MapMetadata)

Example 9 with ForeignKeyMetadata

use of javax.jdo.metadata.ForeignKeyMetadata in project motech by motech.

the class EntityMetadataBuilderImpl method fixRelationMetadata.

private void fixRelationMetadata(PackageMetadata pmd, EntityDto entity, FieldDto field) {
    RelationshipHolder holder = new RelationshipHolder(field);
    // this must be set here cause we can't get related class metadata before metadata enhancement
    if (shouldSetCascadeDelete(holder, EntityType.STANDARD)) {
        String relatedClass = ClassName.getSimpleName(holder.getRelatedClass());
        MemberMetadata rfmd = getFieldMetadata(getClassMetadata(pmd, relatedClass), holder.getRelatedField());
        ForeignKeyMetadata rfkmd = getOrCreateRelFkMetadata(rfmd, entity, field);
        rfkmd.setDeleteAction(ForeignKeyAction.CASCADE);
    }
}
Also used : RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) MemberMetadata(javax.jdo.metadata.MemberMetadata)

Aggregations

ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)9 FieldMetadata (javax.jdo.metadata.FieldMetadata)3 JoinMetadata (javax.jdo.metadata.JoinMetadata)3 MetadataDto (org.motechproject.mds.dto.MetadataDto)2 CtClass (javassist.CtClass)1 CtField (javassist.CtField)1 Element (javax.jdo.annotations.Element)1 Join (javax.jdo.annotations.Join)1 Persistent (javax.jdo.annotations.Persistent)1 ElementMetadata (javax.jdo.metadata.ElementMetadata)1 MapMetadata (javax.jdo.metadata.MapMetadata)1 MemberMetadata (javax.jdo.metadata.MemberMetadata)1 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)1 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1