Search in sources :

Example 1 with JavaBeanMetadata

use of org.springframework.roo.addon.javabean.addon.JavaBeanMetadata in project spring-roo by spring-projects.

the class EntityDeserializerMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final EntityDeserializerAnnotationValues values = new EntityDeserializerAnnotationValues(governorPhysicalTypeMetadata);
    final JavaType deserializerType = governorPhysicalTypeMetadata.getType();
    final JavaType entity = values.getEntity();
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    Validate.notNull(entityDetails, "Can't get details of '%s' defined on '%s.@%s.entity'", entity.getFullyQualifiedTypeName(), deserializerType, RooJavaType.ROO_DESERIALIZER.getSimpleTypeName());
    Validate.notNull(entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Class '%s' defined on '%s.@%s.entity' has no @%s annotation. Only JPA entities can set as mixin", entity.getFullyQualifiedTypeName(), deserializerType, RooJavaType.ROO_DESERIALIZER.getSimpleTypeName());
    // Register JpaEntityMetadata dependency
    final String entityId = JpaEntityMetadata.createIdentifier(entityDetails);
    final JpaEntityMetadata entityMetadata = getMetadataService().get(entityId);
    if (entityMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    registerDependency(entityId, metadataIdentificationString);
    // Register JavaBeanMetadata dependency
    final String javaBeanId = JavaBeanMetadata.createIdentifier(entityDetails);
    final JavaBeanMetadata javaBeanMetadata = getMetadataService().get(javaBeanId);
    if (javaBeanMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    registerDependency(javaBeanId, metadataIdentificationString);
    // Register ServiceMetadata dependency
    ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getService(entity);
    String serviceMetadataId = ServiceMetadata.createIdentifier(serviceDetails);
    ServiceMetadata serviceMetadata = getMetadataService().get(serviceMetadataId);
    if (serviceMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    return new EntityDeserializerMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, values, entityMetadata, serviceMetadata);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaBeanMetadata(org.springframework.roo.addon.javabean.addon.JavaBeanMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata)

Example 2 with JavaBeanMetadata

use of org.springframework.roo.addon.javabean.addon.JavaBeanMetadata in project spring-roo by spring-projects.

the class JaxbEntityMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Getting the annotated entity type
    JavaType annotatedEntity = governorPhysicalTypeMetadata.getType();
    // Getting the entity details
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(annotatedEntity);
    // Getting the plural
    String entityPlural = getPluralService().getPlural(entityDetails);
    // Getting JavaBean Metadata
    String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
    JavaBeanMetadata javaBeanMetadata = getMetadataService().get(javaBeanMetadataKey);
    // Getting JpaEntity Metadata
    String jpaEntityMetadataKey = JpaEntityMetadata.createIdentifier(entityDetails);
    JpaEntityMetadata jpaEntityMetadata = getMetadataService().get(jpaEntityMetadataKey);
    if (javaBeanMetadata == null) {
        // Not ready for this metadata
        return null;
    }
    // Getting the @OneToMany and @ManyToOne getters
    Map<String, String> entityNames = new HashMap<String, String>();
    List<MethodMetadata> oneToManyGetters = new ArrayList<MethodMetadata>();
    List<MethodMetadata> manyToOneGetters = new ArrayList<MethodMetadata>();
    for (FieldMetadata field : entityDetails.getDeclaredFields()) {
        // Getting getter for the oneToMany field
        MethodMetadata getter = javaBeanMetadata.getAccesorMethod(field);
        if (getter != null && (field.getAnnotation(JpaJavaType.ONE_TO_MANY) != null || field.getAnnotation(JpaJavaType.MANY_TO_MANY) != null)) {
            String getterTypeName = getter.getReturnType().getBaseType().getSimpleTypeName().toLowerCase();
            oneToManyGetters.add(getter);
            entityNames.put(getterTypeName, getPluralService().getPlural(getterTypeName));
        } else if (getter != null && field.getAnnotation(JpaJavaType.MANY_TO_ONE) != null) {
            String getterTypeName = getter.getReturnType().getSimpleTypeName().toLowerCase();
            manyToOneGetters.add(getter);
            entityNames.put(getterTypeName, getPluralService().getPlural(getterTypeName));
        }
    }
    // Getting the identifier accessor only if the annotated class doesn't have
    // parent
    MethodMetadata identifierAccessor = null;
    if (jpaEntityMetadata != null && jpaEntityMetadata.getParent() == null) {
        identifierAccessor = jpaEntityMetadata.getCurrentIdentifierAccessor();
    }
    return new JaxbEntityMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, getProjectOperations().getTopLevelPackage(""), annotatedEntity, entityPlural, identifierAccessor, oneToManyGetters, manyToOneGetters, entityNames);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) JavaBeanMetadata(org.springframework.roo.addon.javabean.addon.JavaBeanMetadata) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 3 with JavaBeanMetadata

use of org.springframework.roo.addon.javabean.addon.JavaBeanMetadata in project spring-roo by spring-projects.

the class JpaEntityMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalType, final String itdFilename) {
    if (projectOperations == null) {
        projectOperations = getProjectOperations();
    }
    Validate.notNull(projectOperations, "ProjectOperations is required");
    // Find out the entity-level JPA details from the trigger annotation
    final JpaEntityAnnotationValues jpaEntityAnnotationValues = getJpaEntityAnnotationValues(governorPhysicalType);
    /*
     * Walk the inheritance hierarchy for any existing JpaEntityMetadata. We
     * don't need to monitor any such parent, as any changes to its Java
     * type will trickle down to the governing java type.
     */
    final JpaEntityMetadata parent = getParentMetadata(governorPhysicalType.getMemberHoldingTypeDetails());
    // Get the governor's members
    final MemberDetails governorMemberDetails = getMemberDetails(governorPhysicalType);
    final String moduleName = PhysicalTypeIdentifierNamingUtils.getPath(metadataIdentificationString).getModule();
    if (projectOperations.isProjectAvailable(moduleName)) {
        // If the project itself changes, we want a chance to refresh this
        // item
        getMetadataDependencyRegistry().registerDependency(ProjectMetadata.getProjectIdentifier(moduleName), metadataIdentificationString);
    }
    // Getting entity details
    JavaType entity = JpaEntityMetadata.getJavaType(metadataIdentificationString);
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    // Getting JavaBeanMetadata
    String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
    JavaBeanMetadata entityJavaBeanMetadata = getMetadataService().get(javaBeanMetadataKey);
    // This metadata is not available yet
    if (entityJavaBeanMetadata == null) {
        return null;
    }
    // Locate relation fields to process
    List<FieldMetadata> fieldsParent = new ArrayList<FieldMetadata>();
    Map<String, FieldMetadata> relationsAsChild = new HashMap<String, FieldMetadata>();
    for (FieldMetadata field : entityDetails.getDeclaredFields()) {
        if (field.getAnnotation(RooJavaType.ROO_JPA_RELATION) != null) {
            fieldsParent.add(field);
        } else if (field.getAnnotation(JpaJavaType.ONE_TO_ONE) != null || field.getAnnotation(JpaJavaType.MANY_TO_ONE) != null || field.getAnnotation(JpaJavaType.MANY_TO_MANY) != null) {
            relationsAsChild.put(field.getFieldName().getSymbolName(), field);
        }
    }
    // Check if it's a child part of a composition
    FieldMetadata compositionRelationField;
    try {
        compositionRelationField = getCompositionRelationField(entity, entityDetails, relationsAsChild);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Problems found when trying to identify composition relationship", e);
    }
    // Getting identifier field and version field and its accessors
    FieldMetadata identifierField = null;
    MethodMetadata identifierAccessor = null;
    FieldMetadata versionField = null;
    MethodMetadata versionAccessor = null;
    if (parent == null) {
        // Obtain identifier field from entity details
        List<FieldMetadata> identifierFields = entityDetails.getFieldsWithAnnotation(ID);
        List<FieldMetadata> embeddedIdentifierFields = entityDetails.getFieldsWithAnnotation(EMBEDDED_ID);
        Validate.isTrue(!(identifierFields.isEmpty() && embeddedIdentifierFields.isEmpty()), String.format("ERROR: The annotated entity '%s' doesn't contain any identifier field.", entityDetails.getType().getFullyQualifiedTypeName()));
        if (!identifierFields.isEmpty()) {
            identifierField = identifierFields.get(0);
        } else if (!embeddedIdentifierFields.isEmpty()) {
            identifierField = embeddedIdentifierFields.get(0);
        }
        identifierAccessor = entityJavaBeanMetadata.getAccesorMethod(identifierField);
        // Obtain version field from entity details
        List<FieldMetadata> versionFields = entityDetails.getFieldsWithAnnotation(VERSION);
        // Check and add version field
        if (!versionFields.isEmpty()) {
            versionField = versionFields.get(0);
            versionAccessor = entityJavaBeanMetadata.getAccesorMethod(versionField);
        }
    } else {
        identifierField = parent.getCurrentIndentifierField();
        versionField = parent.getCurrentVersionField();
    }
    return new JpaEntityMetadata(metadataIdentificationString, aspectName, governorPhysicalType, parent, governorMemberDetails, identifierField, identifierAccessor, versionField, versionAccessor, jpaEntityAnnotationValues, entityDetails, fieldsParent, relationsAsChild, compositionRelationField);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaBeanMetadata(org.springframework.roo.addon.javabean.addon.JavaBeanMetadata) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Aggregations

JavaBeanMetadata (org.springframework.roo.addon.javabean.addon.JavaBeanMetadata)3 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)3 JavaType (org.springframework.roo.model.JavaType)3 RooJavaType (org.springframework.roo.model.RooJavaType)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)2 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)2 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)2 JpaJavaType (org.springframework.roo.model.JpaJavaType)2 LinkedHashMap (java.util.LinkedHashMap)1 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)1 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)1