Search in sources :

Example 1 with FieldMetadata

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

the class EntityMetadataBuilderImpl method setTimeMetadata.

private FieldMetadata setTimeMetadata(ClassMetadata cmd, String name) {
    // for time we register our converter which persists as string
    FieldMetadata fmd = cmd.newFieldMetadata(name);
    fmd.setPersistenceModifier(PersistenceModifier.PERSISTENT);
    fmd.setDefaultFetchGroup(true);
    fmd.newExtensionMetadata(DATANUCLEUS, "type-converter-name", "dn.time-string");
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata)

Example 2 with FieldMetadata

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

the class EntityMetadataBuilderImpl method addIdField.

private void addIdField(ClassMetadata cmd, EntityDto entity, SchemaHolder schemaHolder, Class<?> definition) {
    boolean containsID = null != schemaHolder.getFieldByName(entity, ID_FIELD_NAME);
    boolean isBaseClass = entity.isBaseEntity();
    if (containsID && isBaseClass) {
        FieldMetadata metadata = cmd.newFieldMetadata(ID_FIELD_NAME);
        metadata.setValueStrategy(getIdGeneratorStrategy(metadata, definition));
        metadata.setPrimaryKey(true);
    }
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata)

Example 3 with FieldMetadata

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

the class EntityMetadataBuilderImpl method setAutoGenerationMetadata.

private FieldMetadata setAutoGenerationMetadata(ClassMetadata cmd, String name) {
    FieldMetadata fmd = cmd.newFieldMetadata(name);
    fmd.setPersistenceModifier(PersistenceModifier.PERSISTENT);
    fmd.setDefaultFetchGroup(true);
    fmd.newExtensionMetadata(DATANUCLEUS, VALUE_GENERATOR, "ovg." + name);
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata)

Example 4 with FieldMetadata

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

the class EntityMetadataBuilderImpl method processField.

public void processField(ClassMetadata cmd, ClassData classData, EntityDto entity, EntityType entityType, Class<?> definition, String fieldName, FieldDto field, SchemaHolder schemaHolder) {
    // Metadata for ID field has been added earlier in addIdField() method
    if (!fieldName.equals(ID_FIELD_NAME)) {
        FieldMetadata fmd = null;
        if (isFieldNotInherited(fieldName, entity, schemaHolder)) {
            fmd = setFieldMetadata(cmd, classData, entity, entityType, field, definition);
        }
        // when field is in Lookup, we set field metadata indexed to retrieve instance faster
        if (!field.getLookups().isEmpty() && entityType.equals(EntityType.STANDARD)) {
            if (fmd == null) {
                String inheritedFieldName = ClassName.getSimpleName(entity.getSuperClass()) + "." + fieldName;
                fmd = cmd.newFieldMetadata(inheritedFieldName);
            }
            if (!isBlobOrClob(field)) {
                IndexMetadata imd = getOrCreateIndexMetadata(fmd);
                imd.setName(KeyNames.lookupIndexKeyName(entity.getName(), entity.getId(), fieldName, entityType));
            }
            fmd.setIndexed(false);
            for (LookupDto lookupDto : field.getLookups()) {
                if (lookupDto.isIndexRequired()) {
                    fmd.setIndexed(true);
                    break;
                }
            }
        }
        if (fmd != null) {
            customizeFieldMd(fmd, entity, field, entityType, definition);
        }
    }
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) LookupDto(org.motechproject.mds.dto.LookupDto) IndexMetadata(javax.jdo.metadata.IndexMetadata)

Example 5 with FieldMetadata

use of javax.jdo.metadata.FieldMetadata 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)

Aggregations

FieldMetadata (javax.jdo.metadata.FieldMetadata)16 Test (org.junit.Test)6 FieldDto (org.motechproject.mds.dto.FieldDto)6 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)3 MetadataDto (org.motechproject.mds.dto.MetadataDto)3 CtClass (javassist.CtClass)2 IndexMetadata (javax.jdo.metadata.IndexMetadata)2 JoinMetadata (javax.jdo.metadata.JoinMetadata)2 Matchers.anyString (org.mockito.Matchers.anyString)2 AnotherSample (org.motechproject.mds.annotations.internal.samples.AnotherSample)2 Sample (org.motechproject.mds.builder.Sample)2 LookupDto (org.motechproject.mds.dto.LookupDto)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CtField (javassist.CtField)1 NotFoundException (javassist.NotFoundException)1 ClassMetadata (javax.jdo.metadata.ClassMetadata)1 CollectionMetadata (javax.jdo.metadata.CollectionMetadata)1