Search in sources :

Example 1 with CollectionMetadata

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

the class EntityMetadataBuilderImpl method setUpCollectionMetadata.

private void setUpCollectionMetadata(FieldMetadata fmd, String relatedClass, RelationshipHolder holder, EntityType entityType) {
    CollectionMetadata colMd = getOrCreateCollectionMetadata(fmd);
    colMd.setElementType(relatedClass);
    colMd.setEmbeddedElement(false);
    colMd.setSerializedElement(false);
    // for 1:N we're setting foreign key with cascade deletion after metadata enhancement
    if (holder.isManyToMany()) {
        colMd.setDependentElement(holder.isCascadeDelete() || entityType == EntityType.TRASH);
    }
    if (holder.isSetManyToMany() && !holder.isOwningSide() && entityType.equals(EntityType.STANDARD)) {
        fmd.setMappedBy(holder.getRelatedField());
    }
}
Also used : CollectionMetadata(javax.jdo.metadata.CollectionMetadata)

Example 2 with CollectionMetadata

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

the class EntityMetadataBuilderImpl method processHistoryTrashRelationship.

private void processHistoryTrashRelationship(ClassMetadata cmd, FieldMetadata fmd, RelationshipHolder holder) {
    if (holder.isOneToOne() || holder.isManyToOne()) {
        fmd.setColumn(holder.getFieldName() + ID_SUFFIX);
    } else {
        fmd.setTable(cmd.getTable() + '_' + holder.getFieldName());
        CollectionMetadata collMd = fmd.newCollectionMetadata();
        collMd.setElementType(Long.class.getName());
        JoinMetadata joinMd = fmd.newJoinMetadata();
        ColumnMetadata joinColumnMd = joinMd.newColumnMetadata();
        joinColumnMd.setName(cmd.getName() + ID_SUFFIX);
        ElementMetadata elementMd = fmd.newElementMetadata();
        elementMd.setColumn(holder.getFieldName() + ID_SUFFIX);
    }
}
Also used : CollectionMetadata(javax.jdo.metadata.CollectionMetadata) ColumnMetadata(javax.jdo.metadata.ColumnMetadata) ElementMetadata(javax.jdo.metadata.ElementMetadata) JoinMetadata(javax.jdo.metadata.JoinMetadata)

Example 3 with CollectionMetadata

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

the class EntityMetadataBuilderTest method shouldAddOneToManyRelationshipMetadata.

@Test
public void shouldAddOneToManyRelationshipMetadata() {
    FieldDto oneToManyField = fieldDto("oneToManyName", OneToManyRelationship.class);
    oneToManyField.addMetadata(new MetadataDto(RELATED_CLASS, "org.motechproject.test.MyClass"));
    FieldMetadata fmd = mock(FieldMetadata.class);
    CollectionMetadata collMd = mock(CollectionMetadata.class);
    when(entity.getName()).thenReturn(ENTITY_NAME);
    when(entity.getId()).thenReturn(2L);
    when(schemaHolder.getFields(entity)).thenReturn(singletonList(oneToManyField));
    when(entity.getTableName()).thenReturn(TABLE_NAME);
    when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
    when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
    when(classMetadata.newFieldMetadata("oneToManyName")).thenReturn(fmd);
    when(fmd.getCollectionMetadata()).thenReturn(collMd);
    when(fmd.getName()).thenReturn("oneToManyName");
    entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
    verifyCommonClassMetadata();
    verify(fmd).setDefaultFetchGroup(true);
    verify(collMd).setEmbeddedElement(false);
    verify(collMd).setSerializedElement(false);
    verify(collMd).setElementType("org.motechproject.test.MyClass");
}
Also used : CollectionMetadata(javax.jdo.metadata.CollectionMetadata) FieldMetadata(javax.jdo.metadata.FieldMetadata) 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 CollectionMetadata

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

the class EntityMetadataBuilderImpl method fixEnhancerIssuesInMetadata.

@Override
public void fixEnhancerIssuesInMetadata(JDOMetadata jdoMetadata, SchemaHolder schemaHolder) {
    for (PackageMetadata pmd : jdoMetadata.getPackages()) {
        for (ClassMetadata cmd : pmd.getClasses()) {
            String className = String.format("%s.%s", pmd.getName(), cmd.getName());
            EntityType entityType = EntityType.forClassName(className);
            if (entityType == EntityType.STANDARD) {
                EntityDto entity = schemaHolder.getEntityByClassName(className);
                if (null != entity) {
                    for (MemberMetadata mmd : cmd.getMembers()) {
                        CollectionMetadata collMd = mmd.getCollectionMetadata();
                        FieldDto field = schemaHolder.getFieldByName(entity, mmd.getName());
                        if (null != collMd) {
                            fixCollectionMetadata(collMd, field);
                        }
                        if (null != field && field.getType().isRelationship()) {
                            fixRelationMetadata(pmd, entity, field);
                        }
                        // Defining column name for join and element results in setting it both as XML attribute and child element
                        fixDuplicateColumnDefinitions(mmd);
                    }
                }
            }
        }
    }
}
Also used : ClassMetadata(javax.jdo.metadata.ClassMetadata) EntityType(org.motechproject.mds.domain.EntityType) EntityDto(org.motechproject.mds.dto.EntityDto) CollectionMetadata(javax.jdo.metadata.CollectionMetadata) PackageMetadata(javax.jdo.metadata.PackageMetadata) MemberMetadata(javax.jdo.metadata.MemberMetadata) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

CollectionMetadata (javax.jdo.metadata.CollectionMetadata)4 FieldDto (org.motechproject.mds.dto.FieldDto)2 ClassMetadata (javax.jdo.metadata.ClassMetadata)1 ColumnMetadata (javax.jdo.metadata.ColumnMetadata)1 ElementMetadata (javax.jdo.metadata.ElementMetadata)1 FieldMetadata (javax.jdo.metadata.FieldMetadata)1 JoinMetadata (javax.jdo.metadata.JoinMetadata)1 MemberMetadata (javax.jdo.metadata.MemberMetadata)1 PackageMetadata (javax.jdo.metadata.PackageMetadata)1 Test (org.junit.Test)1 EntityType (org.motechproject.mds.domain.EntityType)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)1 MetadataDto (org.motechproject.mds.dto.MetadataDto)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1