Search in sources :

Example 21 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class JpaDataOnDemandCreator method getEntityAndRelatedEntitiesList.

/**
 * Searches the related entities of provided entity and returns a
 * {@link List} with all the related entities plus the provided entity.
 *
 * @param entity
 *            the entity JavaType to search for its related entities.
 * @return a List with all the related entities.
 */
private List<JavaType> getEntityAndRelatedEntitiesList(JavaType entity) {
    ClassOrInterfaceTypeDetails entityDetails = getEntityDetails(entity);
    JpaEntityMetadata entityMetadata = metadataService.get(JpaEntityMetadata.createIdentifier(entityDetails));
    List<JavaType> entitiesToCreateFactories = new ArrayList<JavaType>();
    entitiesToCreateFactories.add(entity);
    // Get related child entities
    for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
        // Add to list
        if (!entitiesToCreateFactories.contains(info.childType)) {
            entitiesToCreateFactories.add(info.childType);
        }
    }
    return entitiesToCreateFactories;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 22 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class JpaOperationsImpl method getJpaEntityMetadata.

/**
 * Gets JpaEntityMetadata by a javaType
 * @param domainType
 * @return
 */
private JpaEntityMetadata getJpaEntityMetadata(ClassOrInterfaceTypeDetails domainTypeDetails) {
    final String entityMetadataId = JpaEntityMetadata.createIdentifier(domainTypeDetails);
    JpaEntityMetadata entityMetadata = getMetadataService().get(entityMetadataId);
    return entityMetadata;
}
Also used : JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 23 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class JpaOperationsImpl method getFieldChildPartOfRelation.

@Override
public List<Pair<FieldMetadata, RelationInfo>> getFieldChildPartOfRelation(ClassOrInterfaceTypeDetails entityCdi) {
    JavaType domainType = entityCdi.getType();
    JpaEntityMetadata entityMetadata = getJpaEntityMetadata(entityCdi);
    Validate.notNull(entityMetadata, "%s should be a Jpa Entity", domainType);
    Map<String, FieldMetadata> relations = entityMetadata.getRelationsAsChild();
    List<Pair<FieldMetadata, RelationInfo>> childRelations = new ArrayList<Pair<FieldMetadata, RelationInfo>>();
    JpaEntityMetadata parent;
    JavaType parentType;
    RelationInfo info;
    for (Entry<String, FieldMetadata> fieldEntry : relations.entrySet()) {
        parentType = fieldEntry.getValue().getFieldType().getBaseType();
        parent = getJpaEntityMetadata(parentType);
        Validate.notNull(parent, "Can't get information about Entity %s which is declared as parent in %s.%s field", parentType, domainType, fieldEntry.getKey());
        info = parent.getRelationInfosByMappedBy(domainType, fieldEntry.getKey());
        if (info != null) {
            childRelations.add(Pair.of(fieldEntry.getValue(), info));
        }
    }
    return childRelations;
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) ArrayList(java.util.ArrayList) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) Pair(org.apache.commons.lang3.tuple.Pair)

Example 24 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class JpaUnitTestMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final JpaUnitTestAnnotationValues annotationValues = new JpaUnitTestAnnotationValues(governorPhysicalTypeMetadata);
    JavaType targetType = annotationValues.getTargetClass();
    Validate.notNull(targetType, targetType.getSimpleTypeName().concat(" doesn't exist in the project."));
    // Obtain target type child related entities
    final ClassOrInterfaceTypeDetails cid = getTypeLocationService().getTypeDetails(targetType);
    JpaEntityMetadata entityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(cid));
    List<JavaType> relatedEntities = new ArrayList<JavaType>();
    // Add the current entity first
    relatedEntities.add(targetType);
    Map<String, RelationInfo> relationInfos = entityMetadata.getRelationInfos();
    for (RelationInfo relation : relationInfos.values()) {
        // Get child entity type
        JavaType childType = relation.childType;
        if (!relatedEntities.contains(childType)) {
            relatedEntities.add(childType);
        }
    }
    // Get all entity factories for this entity and child related entities
    Set<ClassOrInterfaceTypeDetails> entityFactories = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY);
    Map<JavaType, JavaType> entityAndItsFactoryMap = new TreeMap<JavaType, JavaType>();
    for (ClassOrInterfaceTypeDetails entityFactory : entityFactories) {
        JavaType entity = (JavaType) entityFactory.getAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY).getAttribute("entity").getValue();
        if (entity != null && relatedEntities.contains(entity)) {
            entityAndItsFactoryMap.put(entity, entityFactory.getType());
        }
    }
    return new JpaUnitTestMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, relationInfos.values(), entityAndItsFactoryMap);
}
Also used : ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) JavaType(org.springframework.roo.model.JavaType) RooJavaType(org.springframework.roo.model.RooJavaType) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 25 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class ServiceImplMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final ServiceImplAnnotationValues annotationValues = new ServiceImplAnnotationValues(governorPhysicalTypeMetadata);
    // Getting service interface
    JavaType serviceInterface = annotationValues.getService();
    ClassOrInterfaceTypeDetails serviceInterfaceDetails = getTypeLocationService().getTypeDetails(serviceInterface);
    final ServiceMetadata serviceMetadata = getServiceMetadata(metadataIdentificationString, serviceInterfaceDetails);
    AnnotationMetadata serviceAnnotation = serviceInterfaceDetails.getAnnotation(RooJavaType.ROO_SERVICE);
    Validate.notNull(serviceAnnotation, "ERROR: Provided service should be annotated with @RooService");
    JavaType entity = (JavaType) serviceAnnotation.getAttribute("entity").getValue();
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    final String entityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails.getType(), PhysicalTypeIdentifier.getPath(entityDetails.getDeclaredByMetadataId()));
    final JpaEntityMetadata entityMetadata = (JpaEntityMetadata) getMetadataService().get(entityMetadataId);
    // Add dependencies between modules
    if (serviceMetadata != null) {
        for (MethodMetadata method : serviceMetadata.getAllMethods()) {
            List<JavaType> types = new ArrayList<JavaType>();
            types.add(method.getReturnType());
            types.addAll(method.getReturnType().getParameters());
            for (AnnotatedJavaType parameter : method.getParameterTypes()) {
                types.add(AnnotatedJavaType.convertFromAnnotatedJavaType(parameter));
                types.addAll(AnnotatedJavaType.convertFromAnnotatedJavaType(parameter).getParameters());
            }
            for (JavaType parameter : types) {
                getTypeLocationService().addModuleDependency(governorPhysicalTypeMetadata.getType().getModule(), parameter);
            }
        }
    }
    // Getting associated repository
    ClassOrInterfaceTypeDetails repositoryDetails = getRepositoryJpaLocator().getRepository(entity);
    final String repositoryMetadataId = RepositoryJpaMetadata.createIdentifier(repositoryDetails.getType(), PhysicalTypeIdentifier.getPath(repositoryDetails.getDeclaredByMetadataId()));
    final RepositoryJpaMetadata repositoryMetadata = (RepositoryJpaMetadata) getMetadataService().get(repositoryMetadataId);
    // Locate related services API types required by relations
    Map<JavaType, ServiceMetadata> requiredServicesByEntity = new HashMap<JavaType, ServiceMetadata>();
    ClassOrInterfaceTypeDetails relatedService;
    for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
        if (info.cardinality != Cardinality.ONE_TO_ONE && !requiredServicesByEntity.containsKey(info.childType)) {
            relatedService = getServiceLocator().getFirstService(info.childType);
            if (relatedService != null) {
                requiredServicesByEntity.put(info.childType, getServiceMetadata(metadataIdentificationString, relatedService));
            }
        }
    }
    // Get child relations info
    List<Pair<FieldMetadata, RelationInfo>> childRelationsInfo = getJpaOperations().getFieldChildPartOfRelation(entityDetails);
    return new ServiceImplMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, serviceInterface, repositoryDetails.getType(), repositoryMetadata, entity, entityMetadata, serviceMetadata, requiredServicesByEntity, childRelationsInfo);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)35 ArrayList (java.util.ArrayList)26 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)23 JavaType (org.springframework.roo.model.JavaType)22 RooJavaType (org.springframework.roo.model.RooJavaType)20 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)18 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)13 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)13 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)11 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)10 HashMap (java.util.HashMap)9 Pair (org.apache.commons.lang3.tuple.Pair)9 JpaJavaType (org.springframework.roo.model.JpaJavaType)9 LinkedHashMap (java.util.LinkedHashMap)8 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)8 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)7 SpringJavaType (org.springframework.roo.model.SpringJavaType)6 List (java.util.List)5 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)5 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)5