Search in sources :

Example 1 with ForeignKeyMetadata

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

the class EntityMetadataBuilderImpl method getOrCreateRelFkMetadata.

private ForeignKeyMetadata getOrCreateRelFkMetadata(MemberMetadata mmd, EntityDto entity, FieldDto field) {
    ForeignKeyMetadata fkmd = getOrCreateFkMetadata(mmd);
    fkmd.setName(KeyNames.foreignKeyName(entity.getName(), entity.getId(), field.getBasic().getName(), EntityType.STANDARD));
    return fkmd;
}
Also used : ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata)

Example 2 with ForeignKeyMetadata

use of javax.jdo.metadata.ForeignKeyMetadata in project datanucleus-api-jdo by datanucleus.

the class TypeMetadataImpl method getForeignKeys.

public ForeignKeyMetadata[] getForeignKeys() {
    List<ForeignKeyMetaData> internalFKs = getInternal().getForeignKeyMetaData();
    if (internalFKs == null) {
        return null;
    }
    ForeignKeyMetadata[] fkmds = new ForeignKeyMetadataImpl[internalFKs.size()];
    int i = 0;
    for (ForeignKeyMetaData internalFKMD : internalFKs) {
        ForeignKeyMetadataImpl fkmd = new ForeignKeyMetadataImpl(internalFKMD);
        fkmds[i++] = fkmd;
        fkmd.parent = this;
    }
    return fkmds;
}
Also used : ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData)

Example 3 with ForeignKeyMetadata

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

the class EntityMetadataBuilderTest method shouldAddOneToOneRelationshipMetadata.

@Test
public void shouldAddOneToOneRelationshipMetadata() throws NotFoundException, CannotCompileException {
    final String relClassName = "org.motechproject.test.MyClass";
    final String relFieldName = "myField";
    FieldDto oneToOneField = fieldDto("oneToOneName", OneToOneRelationship.class);
    oneToOneField.addMetadata(new MetadataDto(RELATED_CLASS, relClassName));
    oneToOneField.addMetadata(new MetadataDto(RELATED_FIELD, relFieldName));
    FieldMetadata fmd = mock(FieldMetadata.class);
    when(fmd.getName()).thenReturn("oneToOneName");
    ForeignKeyMetadata fkmd = mock(ForeignKeyMetadata.class);
    when(entity.getName()).thenReturn(ENTITY_NAME);
    when(entity.getId()).thenReturn(3L);
    when(schemaHolder.getFields(entity)).thenReturn(singletonList(oneToOneField));
    when(entity.getTableName()).thenReturn(TABLE_NAME);
    when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
    when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
    when(classMetadata.newFieldMetadata("oneToOneName")).thenReturn(fmd);
    when(fmd.newForeignKeyMetadata()).thenReturn(fkmd);
    /* We simulate configuration for the bi-directional relationship (the related class has got
           a field that links back to the main class) */
    CtClass myClass = mock(CtClass.class);
    CtClass relatedClass = mock(CtClass.class);
    CtField myField = mock(CtField.class);
    CtField relatedField = mock(CtField.class);
    when(myClass.getName()).thenReturn(relClassName);
    when(myClass.getDeclaredFields()).thenReturn(new CtField[] { myField });
    when(myField.getType()).thenReturn(relatedClass);
    when(myField.getName()).thenReturn(relFieldName);
    when(relatedClass.getDeclaredFields()).thenReturn(new CtField[] { relatedField });
    when(relatedClass.getName()).thenReturn(CLASS_NAME);
    entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
    verifyCommonClassMetadata();
    verify(fmd).setDefaultFetchGroup(true);
    verify(fmd).setPersistenceModifier(PersistenceModifier.PERSISTENT);
    verify(fkmd).setName("fk_Sample_oneToOneName_3");
}
Also used : CtClass(javassist.CtClass) FieldMetadata(javax.jdo.metadata.FieldMetadata) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) CtField(javassist.CtField) Matchers.anyString(org.mockito.Matchers.anyString) MetadataDto(org.motechproject.mds.dto.MetadataDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with ForeignKeyMetadata

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

the class EntityMetadataBuilderImpl method addManyToManyMetadata.

private void addManyToManyMetadata(FieldMetadata fmd, RelationshipHolder holder, EntityDto entity, FieldDto field, Class<?> definition) {
    // When using Lists join metadata must be present at two sides of M:N relation
    if (!holder.isOwningSide() || holder.isListManyToMany()) {
        java.lang.reflect.Field fieldDefinition = FieldUtils.getDeclaredField(definition, field.getBasic().getName(), true);
        Join join = fieldDefinition.getAnnotation(Join.class);
        Persistent persistent = fieldDefinition.getAnnotation(Persistent.class);
        Element element = fieldDefinition.getAnnotation(Element.class);
        JoinMetadata jmd = null;
        if (join == null) {
            jmd = fmd.newJoinMetadata();
        }
        setTableNameMetadata(fmd, persistent, entity, field, holder, EntityType.STANDARD);
        setElementMetadata(fmd, element, holder, entity);
        if (join == null || StringUtils.isEmpty(join.column())) {
            setJoinMetadata(jmd, fmd, ClassName.getSimpleName(entity.getClassName()).toUpperCase() + ID_SUFFIX);
            ForeignKeyMetadata fkmd = getOrCreateFkMetadata(jmd);
            fkmd.setName(KeyNames.foreignKeyName(entity.getName(), entity.getId(), field.getBasic().getName(), EntityType.STANDARD));
        }
    }
}
Also used : ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) Element(javax.jdo.annotations.Element) Join(javax.jdo.annotations.Join) Persistent(javax.jdo.annotations.Persistent) JoinMetadata(javax.jdo.metadata.JoinMetadata)

Example 5 with ForeignKeyMetadata

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

the class EntityMetadataBuilderImpl method setComboboxMetadata.

private FieldMetadata setComboboxMetadata(ClassMetadata cmd, EntityDto entity, FieldDto field, Class<?> definition, EntityType entityType) {
    ComboboxHolder holder = new ComboboxHolder(entity, field);
    String fieldName = getNameForMetadata(field);
    FieldMetadata fmd = cmd.newFieldMetadata(fieldName);
    if (holder.isCollection()) {
        addDefaultFetchGroupMetadata(fmd, definition);
        fmd.setTable(ClassTableName.getTableName(cmd.getTable(), fieldName));
        JoinMetadata jm = fmd.newJoinMetadata();
        ForeignKeyMetadata fkmd = getOrCreateFkMetadata(jm);
        fkmd.setName(KeyNames.cbForeignKeyName(entity.getName(), entity.getId(), fieldName, entityType));
        jm.setDeleteAction(ForeignKeyAction.CASCADE);
        jm.newColumnMetadata().setName(fieldName + "_OID");
    }
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) JoinMetadata(javax.jdo.metadata.JoinMetadata)

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