Search in sources :

Example 1 with RepositoryJpaCustomMetadata

use of org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaCustomMetadata in project spring-roo by spring-projects.

the class ServiceMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final ServiceAnnotationValues annotationValues = new ServiceAnnotationValues(governorPhysicalTypeMetadata);
    // Getting entity
    JavaType entity = annotationValues.getEntity();
    Validate.notNull(entity, "ERROR: You should specify a valid entity on @RooService annotation");
    final JavaType identifierType = getPersistenceMemberLocator().getIdentifierType(entity);
    Validate.notNull(identifierType, "ERROR: You should specify a valid entity on @RooService annotation");
    final JpaEntityMetadata entityMetadata = getEntityMetadata(entity);
    if (entityMetadata == null) {
        return null;
    }
    registerDependency(entityMetadata.getId(), metadataIdentificationString);
    // Getting associated repository
    ClassOrInterfaceTypeDetails repositoryDetails = getRepositoryJpaLocator().getRepository(entity);
    // Check if we have a valid repository
    Validate.notNull(repositoryDetails, String.format("ERROR: You must generate some @RooJpaRepository for entity '%s' to be able to generate services", entity.getSimpleTypeName()));
    // Get repository metadata
    final String repositoryMetadataKey = RepositoryJpaMetadata.createIdentifier(repositoryDetails);
    registerDependency(repositoryMetadataKey, metadataIdentificationString);
    final RepositoryJpaMetadata repositoryMetadata = getMetadataService().get(repositoryMetadataKey);
    List<MethodMetadata> finders = new ArrayList<MethodMetadata>();
    List<MethodMetadata> countMethods = new ArrayList<MethodMetadata>();
    if (repositoryMetadata == null) {
        // Can't generate metadata yet
        return null;
    }
    // Add dependencies between modules
    for (MethodMetadata finder : repositoryMetadata.getFindersGenerated()) {
        // Add to service finders list
        finders.add(finder);
        registerDependencyModulesOfFinder(governorPhysicalTypeMetadata, finder);
    }
    // Get count methods
    countMethods.addAll(repositoryMetadata.getCountMethods());
    // Get finders and its associated count method from repository metadata
    Map<JavaSymbolName, MethodMetadata> repositoryFindersAndCounts = repositoryMetadata.getFinderMethodsAndCounts();
    // Getting methods declared on related RepositoryJpaCustomMetadata
    final JavaType customRepository = repositoryMetadata.getCustomRepository();
    final ClassOrInterfaceTypeDetails customRepositoryDetails = getTypeLocationService().getTypeDetails(customRepository);
    final String customRepositoryMetadataKey = RepositoryJpaCustomMetadata.createIdentifier(customRepositoryDetails);
    final RepositoryJpaCustomMetadata repositoryCustomMetadata = getMetadataService().get(customRepositoryMetadataKey);
    // Return null if repository dependency is not satisfied
    if (repositoryCustomMetadata == null) {
        return null;
    }
    // Get finders and its associated count method from custom repository metadata
    Map<JavaSymbolName, MethodMetadata> repositoryCustomFindersAndCounts = repositoryCustomMetadata.getFinderMethodsAndCounts();
    // Check if we have a valid custom repository
    Validate.notNull(repositoryCustomMetadata, String.format("ERROR: Can't found a class @RooJpaRepositoryCustom for entity '%s' to be able to generate services", entity.getSimpleTypeName()));
    registerDependency(customRepositoryMetadataKey, metadataIdentificationString);
    final Map<FieldMetadata, MethodMetadata> countByReferencedFieldMethods = new HashMap<FieldMetadata, MethodMetadata>(repositoryMetadata.getCountMethodByReferencedFields());
    // Add custom finders to finders list and add dependencies between modules
    for (Pair<MethodMetadata, PartTree> finderInfo : repositoryCustomMetadata.getCustomFinderMethods()) {
        // Add to service finders list
        finders.add(finderInfo.getKey());
        registerDependencyModulesOfFinder(governorPhysicalTypeMetadata, finderInfo.getKey());
    }
    // Add custom count methods to count method list
    for (Pair<MethodMetadata, PartTree> countInfo : repositoryCustomMetadata.getCustomCountMethods()) {
        countMethods.add(countInfo.getKey());
    }
    // Get related entities metadata
    final Map<JavaType, JpaEntityMetadata> relatedEntities = new HashMap<JavaType, JpaEntityMetadata>();
    // As parent
    JavaType childEntity;
    JpaEntityMetadata childEntityMetadata;
    List<RelationInfo> relatedInfosWithServiceLayer = new ArrayList<RelationInfo>();
    for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
        childEntity = info.fieldMetadata.getFieldType().getBaseType();
        if (relatedEntities.containsKey(childEntity)) {
            continue;
        }
        // No need to add relation methods if related entity has not service
        ClassOrInterfaceTypeDetails childService = getServiceLocator().getFirstService(childEntity);
        if (childService != null) {
            relatedInfosWithServiceLayer.add(info);
            childEntityMetadata = getEntityMetadata(childEntity);
            if (childEntityMetadata == null) {
                // We need child metadata. Return null waiting next metadata iteration
                return null;
            }
            registerDependency(childEntityMetadata.getId(), metadataIdentificationString);
            relatedEntities.put(childEntity, childEntityMetadata);
        }
    }
    // As child
    JavaType parentEntity;
    JpaEntityMetadata parentEntityMetadata;
    for (FieldMetadata fieldAsChild : entityMetadata.getRelationsAsChild().values()) {
        parentEntity = fieldAsChild.getFieldType().getBaseType();
        if (relatedEntities.containsKey(parentEntity)) {
            continue;
        }
        parentEntityMetadata = getEntityMetadata(parentEntity);
        if (parentEntityMetadata == null) {
            // We need parent metadata. Return null waiting next metadata iteration
            return null;
        }
        registerDependency(parentEntityMetadata.getId(), metadataIdentificationString);
        relatedEntities.put(parentEntity, parentEntityMetadata);
    }
    return new ServiceMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, entity, identifierType, entityMetadata, repositoryMetadata, finders, repositoryCustomMetadata.getCurrentFindAllGlobalSearchMethod(), repositoryCustomMetadata.getCurrentFindAllByIdsInGlobalSearchMethod(), repositoryCustomMetadata.getReferencedFieldsFindAllMethods(), countByReferencedFieldMethods, countMethods, relatedEntities, repositoryFindersAndCounts, repositoryCustomFindersAndCounts, relatedInfosWithServiceLayer);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) RepositoryJpaCustomMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaCustomMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) PartTree(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)1 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)1 RepositoryJpaCustomMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaCustomMetadata)1 RepositoryJpaMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata)1 PartTree (org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree)1 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)1 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)1 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)1 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)1 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)1 JavaType (org.springframework.roo.model.JavaType)1 RooJavaType (org.springframework.roo.model.RooJavaType)1