Search in sources :

Example 1 with IndexMetadata

use of javax.jdo.metadata.IndexMetadata 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 2 with IndexMetadata

use of javax.jdo.metadata.IndexMetadata in project tests by datanucleus.

the class JDOMetadataAPITest method assertFields.

private void assertFields(final Class<?> clazz, final List<IndexMetadata> indexes, final String... fieldNames) {
    if (fieldNames == null) {
        assertTrue("More indices than expected returned! " + clazz.getSimpleName(), indexes.isEmpty());
    } else {
        assertFalse("More indices than expected returned! " + clazz.getSimpleName(), indexes.size() > fieldNames.length);
        Set<String> indexesNames = new HashSet<String>();
        for (final IndexMetadata indexMetada : indexes) {
            MemberMetadata[] members = indexMetada.getMembers();
            if (members == null) {
                FieldMetadata fieldMetadata = (FieldMetadata) indexMetada.getParent();
                indexesNames.add(fieldMetadata.getName());
            } else {
                for (MemberMetadata memberMetadata : members) {
                    memberMetadata.getIndexMetadata();
                    indexesNames.add(memberMetadata.getName());
                }
            }
        }
        for (final String name : fieldNames) {
            assertTrue("Index not returned: " + name, indexesNames.contains(name));
        }
    }
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) MemberMetadata(javax.jdo.metadata.MemberMetadata) IndexMetadata(javax.jdo.metadata.IndexMetadata) HashSet(java.util.HashSet)

Example 3 with IndexMetadata

use of javax.jdo.metadata.IndexMetadata in project tests by datanucleus.

the class JDOMetadataAPITest method getIndexes.

/**
 * Return a list of the declared indexes at class and field level.
 * @param clazz The class to read the metadata from
 * @param uniqueFlag If true returns only the indexes that are marked as true. If false returns only indexes that
 * are NOT marked as unique.
 */
List<IndexMetadata> getIndexes(final Class<?> clazz, final boolean uniqueFlag) {
    final List<IndexMetadata> indexes = new ArrayList<IndexMetadata>();
    final TypeMetadata metadata = pmf.getMetadata(clazz.getName());
    // Class level
    final IndexMetadata[] indices = metadata.getIndices();
    if (indices != null) {
        for (final IndexMetadata indexMetadata : indices) {
            if ((uniqueFlag && indexMetadata.getUnique()) || (!uniqueFlag && !indexMetadata.getUnique())) {
                indexes.add(indexMetadata);
            }
        }
    }
    // Field level
    final MemberMetadata[] members = metadata.getMembers();
    for (final MemberMetadata memberMetadata : members) {
        final IndexMetadata indexMetadata = memberMetadata.getIndexMetadata();
        if (indexMetadata != null) {
            if ((uniqueFlag && indexMetadata.getUnique()) || (!uniqueFlag && !indexMetadata.getUnique())) {
                indexes.add(indexMetadata);
            }
        }
    }
    return indexes;
}
Also used : TypeMetadata(javax.jdo.metadata.TypeMetadata) ArrayList(java.util.ArrayList) MemberMetadata(javax.jdo.metadata.MemberMetadata) IndexMetadata(javax.jdo.metadata.IndexMetadata)

Aggregations

IndexMetadata (javax.jdo.metadata.IndexMetadata)3 FieldMetadata (javax.jdo.metadata.FieldMetadata)2 MemberMetadata (javax.jdo.metadata.MemberMetadata)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TypeMetadata (javax.jdo.metadata.TypeMetadata)1 LookupDto (org.motechproject.mds.dto.LookupDto)1