Search in sources :

Example 1 with MemberMetadata

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

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

the class MetadataServiceImpl method getComboboxTableName.

@Override
public String getComboboxTableName(String entityClassName, String cbFieldName) {
    TypeMetadata typeMetadata = persistenceManagerFactory.getMetadata(entityClassName);
    if (typeMetadata == null) {
        throw new IllegalArgumentException("No type metadata found for " + entityClassName);
    }
    MemberMetadata cbMetadata = findMemberMetadata(typeMetadata, cbFieldName);
    if (cbMetadata == null) {
        throw new IllegalArgumentException("No member metadata for field " + cbFieldName + " in " + entityClassName);
    }
    final String tableName = cbMetadata.getTable();
    if (tableName == null) {
        throw new IllegalArgumentException("No table name specified for member " + cbFieldName + " in " + entityClassName);
    }
    return tableName;
}
Also used : TypeMetadata(javax.jdo.metadata.TypeMetadata) MemberMetadata(javax.jdo.metadata.MemberMetadata)

Example 3 with MemberMetadata

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

Example 4 with MemberMetadata

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

Example 5 with MemberMetadata

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

MemberMetadata (javax.jdo.metadata.MemberMetadata)5 IndexMetadata (javax.jdo.metadata.IndexMetadata)2 TypeMetadata (javax.jdo.metadata.TypeMetadata)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ClassMetadata (javax.jdo.metadata.ClassMetadata)1 CollectionMetadata (javax.jdo.metadata.CollectionMetadata)1 FieldMetadata (javax.jdo.metadata.FieldMetadata)1 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)1 PackageMetadata (javax.jdo.metadata.PackageMetadata)1 EntityType (org.motechproject.mds.domain.EntityType)1 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 FieldDto (org.motechproject.mds.dto.FieldDto)1