Search in sources :

Example 16 with JpaEntityMetadata

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

the class RepositoryJpaIntegrationTestMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final RepositoryJpaIntegrationTestAnnotationValues annotationValues = new RepositoryJpaIntegrationTestAnnotationValues(governorPhysicalTypeMetadata);
    // Find SpringJpaDataDetachableConfiguration class
    Set<JavaType> jpaConfigurationClasses = getTypeLocationService().findTypesWithAnnotation(RooJavaType.ROO_JPA_REPOSITORY_CONFIGURATION);
    Validate.isTrue(!jpaConfigurationClasses.isEmpty(), "Couldn't find the 'SpringDataJpaDetachableRepositoryConfiguration' on the project for '%s'", this.getClass().getName());
    JavaType jpaDetachableRepositoryClass = jpaConfigurationClasses.iterator().next();
    // Get repository metadata
    JavaType repositoryInterface = annotationValues.getTargetClass();
    ClassOrInterfaceTypeDetails repositoryDetails = getTypeLocationService().getTypeDetails(repositoryInterface);
    Validate.notNull(repositoryDetails.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA), "Couldn't find @RooJpaRepository in '%s'", repositoryInterface.getSimpleTypeName());
    String repositoryMetadataId = RepositoryJpaMetadata.createIdentifier(repositoryDetails);
    RepositoryJpaMetadata repositoryMetadata = getMetadataService().get(repositoryMetadataId);
    if (repositoryMetadata == null) {
        return null;
    }
    // Get entity identifier info
    JavaType entity = repositoryMetadata.getEntity();
    JavaType identifierType = getPersistenceMemberLocator().getIdentifierType(entity);
    JavaSymbolName identifierAccessorMethodName = getPersistenceMemberLocator().getIdentifierAccessor(entity).getMethodName();
    // Get entity metadata
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    String jpaEntityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails);
    JpaEntityMetadata jpaEntityMetadata = getMetadataService().get(jpaEntityMetadataId);
    if (jpaEntityMetadata == null) {
        return null;
    }
    // Get entity read only
    boolean isReadOnly = jpaEntityMetadata.isReadOnly();
    // Get entity plural
    String entityPlural = getPluralService().getPlural(entity);
    // Get repository default return type
    JavaType defaultReturnType = repositoryMetadata.getDefaultReturnType();
    return new RepositoryJpaIntegrationTestMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, jpaDetachableRepositoryClass, identifierType, identifierAccessorMethodName, entityPlural, entity, defaultReturnType, isReadOnly);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) JavaType(org.springframework.roo.model.JavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 17 with JpaEntityMetadata

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

the class ServiceMetadata method getRemoveFromRelationMethod.

private MethodMetadata getRemoveFromRelationMethod(RelationInfo relationInfo) {
    final MethodMetadata entityRemoveMethod = relationInfo.removeMethod;
    // Define method name
    JavaSymbolName methodName = entityRemoveMethod.getMethodName();
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    // add parent entity parameter
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(entity));
    parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(entity.getSimpleTypeName())));
    // Get related entity metadata
    JpaEntityMetadata childEntityMetadata = relatedEntitiesMetadata.get(relationInfo.childType);
    Validate.notNull(childEntityMetadata, "Can't get entity metadata for %s entity generating %s", relationInfo.childType, aspectName);
    if (relationInfo.cardinality != Cardinality.ONE_TO_ONE) {
        // add child entity parameter
        parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JavaType.iterableOf(childEntityMetadata.getCurrentIndentifierField().getFieldType())));
        parameterNames.add(entityRemoveMethod.getParameterNames().get(0));
    }
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, entity, parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder.build();
// instance
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 18 with JpaEntityMetadata

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

the class ServiceMetadata method getAddToRelationMethod.

/**
 * Generates "AddToRelation" Method for this service
 * @param relationInfo
 * @return
 */
private MethodMetadata getAddToRelationMethod(RelationInfo relationInfo) {
    final MethodMetadata entityAddMethod = relationInfo.addMethod;
    // Define method name
    JavaSymbolName methodName = entityAddMethod.getMethodName();
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    // add parent entity parameter
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(entity));
    parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(entity.getSimpleTypeName())));
    // add child entity parameter
    parameterNames.add(entityAddMethod.getParameterNames().get(0));
    if (relationInfo.cardinality == Cardinality.ONE_TO_ONE) {
        parameterTypes.add(AnnotatedJavaType.convertFromJavaType(relationInfo.childType));
    } else if (relationInfo.type == JpaRelationType.COMPOSITION) {
        // Use objects
        parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JavaType.iterableOf(relationInfo.childType)));
    } else {
        // AGGREGATION: Use Child Pk: Get related entity metadata
        JpaEntityMetadata childEntityMetadata = relatedEntitiesMetadata.get(relationInfo.childType);
        Validate.notNull(childEntityMetadata, "Can't get entity metadata for %s entity generating %s", relationInfo.childType, aspectName);
        parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JavaType.iterableOf(childEntityMetadata.getCurrentIndentifierField().getFieldType())));
    }
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, entity, parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder.build();
// instance
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 19 with JpaEntityMetadata

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

the class ServiceMetadata method getSetRelationMethod.

/**
 * Generates "setRelation" Method for this service
 * @param relationInfo
 * @return
 */
private MethodMetadata getSetRelationMethod(RelationInfo relationInfo) {
    // Define method name
    final JavaSymbolName methodName = new JavaSymbolName("set" + relationInfo.fieldMetadata.getFieldName().getSymbolNameCapitalisedFirstLetter());
    // Define method parameter types
    final List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    // add parent entity parameter
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(entity));
    parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(entity.getSimpleTypeName())));
    // add child entity parameter
    parameterNames.add(relationInfo.fieldMetadata.getFieldName());
    if (relationInfo.cardinality == Cardinality.ONE_TO_ONE) {
        parameterTypes.add(AnnotatedJavaType.convertFromJavaType(relationInfo.childType));
    } else if (relationInfo.type == JpaRelationType.COMPOSITION) {
        // Use objects
        parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JavaType.iterableOf(relationInfo.childType)));
    } else {
        // AGGREGATION: Use Child Pk: Get related entity metadata
        JpaEntityMetadata childEntityMetadata = relatedEntitiesMetadata.get(relationInfo.childType);
        Validate.notNull(childEntityMetadata, "Can't get entity metadata for %s entity generating %s", relationInfo.childType, aspectName);
        parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JavaType.iterableOf(childEntityMetadata.getCurrentIndentifierField().getFieldType())));
    }
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, entity, parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder.build();
// instance
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 20 with JpaEntityMetadata

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

the class RepositoryJpaCustomMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final RepositoryJpaCustomAnnotationValues annotationValues = new RepositoryJpaCustomAnnotationValues(governorPhysicalTypeMetadata);
    // Getting repository custom
    JavaType entity = annotationValues.getEntity();
    Validate.notNull(entity, "ERROR: Repository custom interface should be contain an entity on @RooJpaRepositoryCustom annotation");
    final ClassOrInterfaceTypeDetails repositoryClass = getRepositoryJpaLocator().getRepository(entity);
    final String repositoryMedatadataId = RepositoryJpaMetadata.createIdentifier(repositoryClass);
    registerDependency(repositoryMedatadataId, metadataIdentificationString);
    RepositoryJpaMetadata repositoryMetadata = getMetadataService().get(repositoryMedatadataId);
    // This metadata is not available yet.
    if (repositoryMetadata == null) {
        return null;
    }
    // Add dependency between modules
    ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    String module = cid.getName().getModule();
    getTypeLocationService().addModuleDependency(module, entity);
    getTypeLocationService().addModuleDependency(module, repositoryMetadata.getDefaultReturnType());
    // Get field which entity is field part
    List<Pair<FieldMetadata, RelationInfo>> relationsAsChild = getJpaOperations().getFieldChildPartOfRelation(entity);
    for (Pair<FieldMetadata, RelationInfo> fieldInfo : relationsAsChild) {
        // Add dependency between modules
        getTypeLocationService().addModuleDependency(module, fieldInfo.getLeft().getFieldType());
    }
    // Register dependency between JavaBeanMetadata and this one
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    final String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
    registerDependency(javaBeanMetadataKey, metadataIdentificationString);
    String entityMetadataKey = JpaEntityMetadata.createIdentifier(entityDetails);
    JpaEntityMetadata entityMetadata = getMetadataService().get(entityMetadataKey);
    // Entity metadata is not available
    if (entityMetadata == null) {
        return null;
    }
    return new RepositoryJpaCustomMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, entityMetadata.getCurrentIndentifierField().getFieldType(), entity, repositoryMetadata, relationsAsChild);
}
Also used : 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) 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