Search in sources :

Example 31 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.

the class JpaFieldCreatorProvider method createReferenceField.

@Override
public void createReferenceField(JavaType typeName, JavaType fieldType, JavaSymbolName fieldName, boolean aggregation, JavaSymbolName mappedBy, Cascade[] cascadeType, boolean notNull, String joinColumnName, String referencedColumnName, Fetch fetch, String comment, boolean permitReservedWords, Boolean orphanRemoval, boolean isForce, String formatExpression, String formatMessage) {
    final ClassOrInterfaceTypeDetails childCid = typeLocationService.getTypeDetails(fieldType);
    final ClassOrInterfaceTypeDetails parentCid = typeLocationService.getTypeDetails(typeName);
    Validate.notNull(parentCid, "The type specified, '%s', doesn't exist", typeName);
    Validate.notNull(childCid, "The specified target '--type' does not exist or can not be found. Please create this type first.", fieldType);
    // Check if parent field exist
    checkFieldExists(fieldName, isForce, parentCid, "fieldName");
    if (mappedBy == null) {
        // generate mappedBy name from uncapitalized parentClass name
        mappedBy = new JavaSymbolName(StringUtils.uncapitalize(typeName.getSimpleTypeName()));
    }
    // Check that child 'mappedBy' field isn't equal to child type name uncapitalized
    Validate.isTrue(!StringUtils.uncapitalize(fieldType.getSimpleTypeName()).equals(mappedBy.getSymbolName()), "Child entity field can not have the same name as the referenced entity ('%s') name in " + "lower camel case ('%s'). Please assign it other value using '--mappedBy' option.", fieldType.getSimpleTypeName(), mappedBy.getSymbolName());
    // Check if child field exist
    checkFieldExists(mappedBy, false, childCid, "mappedBy");
    // Check if the requested entity is a JPA @Entity
    final MemberDetails childMemberDetails = memberDetailsScanner.getMemberDetails(this.getClass().getName(), childCid);
    final AnnotationMetadata entityAnnotation = childMemberDetails.getAnnotation(ENTITY);
    final AnnotationMetadata persistentAnnotation = childMemberDetails.getAnnotation(PERSISTENT);
    Validate.isTrue(entityAnnotation != null || persistentAnnotation != null, "The field reference command is only applicable to JPA @Entity or Spring Data @Persistent target types.");
    // Prepare parent field
    if (cascadeType == null) {
        // prepare cascadType
        if (aggregation) {
            cascadeType = Cascade.MERGE_PERSIST;
        } else {
            // Compsition
            cascadeType = Cascade.ALL_ARRAY;
        }
    }
    if (fetch == null) {
        fetch = Fetch.LAZY;
    }
    final ReferenceField parentFieldDetails = new ReferenceField(parentCid.getDeclaredByMetadataId(), fieldType, fieldName, Cardinality.ONE_TO_ONE, cascadeType);
    parentFieldDetails.setFetch(fetch);
    AnnotationMetadataBuilder rooJpaRelationAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_RELATION);
    if (aggregation) {
        rooJpaRelationAnnotation.addEnumAttribute("type", RooEnumDetails.RELATION_TYPE_AGGREGATION);
    } else {
        rooJpaRelationAnnotation.addEnumAttribute("type", RooEnumDetails.RELATION_TYPE_COMPOSITION);
    }
    parentFieldDetails.addAdditionaAnnotation(rooJpaRelationAnnotation);
    if (comment != null) {
        parentFieldDetails.setComment(comment);
    }
    if (orphanRemoval == null && !aggregation) {
        // composition
        orphanRemoval = true;
    }
    parentFieldDetails.setOrphanRemoval(orphanRemoval);
    parentFieldDetails.setMappedBy(mappedBy);
    // ROO-3868: New entity visualization support using a new format annotation
    parentFieldDetails.addAdditionaAnnotation(buildEntityFormatAnnotation(formatExpression, formatMessage, fieldName.getSymbolName()));
    // Prepare child files
    final ReferenceField childFieldDetails = new ReferenceField(childCid.getDeclaredByMetadataId(), typeName, mappedBy, Cardinality.ONE_TO_ONE, null);
    childFieldDetails.setFetch(fetch);
    if (joinColumnName != null) {
        if (referencedColumnName != null) {
            Validate.notNull(joinColumnName, "@JoinColumn name is required if specifying a referencedColumnName");
            childFieldDetails.setJoinColumn(joinColumnName, referencedColumnName);
        } else {
            childFieldDetails.setJoinColumn(joinColumnName);
        }
    }
    childFieldDetails.setNotNull(notNull);
    // ROO-3868: New entity visualization support using a new format annotation
    childFieldDetails.addAdditionaAnnotation(buildEntityFormatAnnotation(formatExpression, formatMessage, fieldName.getSymbolName()));
    // insert child field
    insertField(childFieldDetails, permitReservedWords, false, true);
    // insert parent field
    insertField(parentFieldDetails, permitReservedWords, false, true);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ReferenceField(org.springframework.roo.classpath.operations.jsr303.ReferenceField) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 32 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails 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)

Example 33 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String entityFactoryMetadata, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Get related entity
    final JavaType entity = (JavaType) governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(ROO_JPA_ENTITY_FACTORY).getAttribute("entity").getValue();
    if (entity == null) {
        return null;
    }
    final MemberDetails memberDetails = getMemberDetails(entity);
    if (memberDetails == null) {
        return null;
    }
    final MemberHoldingTypeDetails persistenceMemberHoldingTypeDetails = MemberFindingUtils.getMostConcreteMemberHoldingTypeDetailsWithTag(memberDetails, PERSISTENT_TYPE);
    if (persistenceMemberHoldingTypeDetails == null) {
        return null;
    }
    // We need to be informed if our dependent metadata changes
    getMetadataDependencyRegistry().registerDependency(persistenceMemberHoldingTypeDetails.getDeclaredByMetadataId(), entityFactoryMetadata);
    final JavaType identifierType = getPersistenceMemberLocator().getIdentifierType(entity);
    if (identifierType == null) {
        return null;
    }
    // Identify all the fields we care about on the entity
    final Map<FieldMetadata, JpaEntityFactoryMetadata> locatedFields = getLocatedFields(memberDetails, entityFactoryMetadata);
    // Get the list of embedded metadata holders - may be an empty list if
    // no embedded identifier exists
    final List<EmbeddedHolder> embeddedHolders = getEmbeddedHolders(memberDetails, entityFactoryMetadata);
    // Get the embedded identifier metadata holder - may be null if no
    // embedded identifier exists
    final EmbeddedIdHolder embeddedIdHolder = getEmbeddedIdHolder(memberDetails, entityFactoryMetadata);
    Set<ClassOrInterfaceTypeDetails> entityFactoryClasses = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY);
    return new JpaEntityFactoryMetadata(entityFactoryMetadata, aspectName, governorPhysicalTypeMetadata, entity, locatedFields, embeddedHolders, entityFactoryClasses, embeddedIdHolder);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) EmbeddedIdHolder(org.springframework.roo.addon.jpa.addon.dod.EmbeddedIdHolder) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) EmbeddedHolder(org.springframework.roo.addon.jpa.addon.dod.EmbeddedHolder)

Example 34 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadataProviderImpl method getEmbeddedHolders.

private List<EmbeddedHolder> getEmbeddedHolders(final MemberDetails memberDetails, final String metadataIdentificationString) {
    final List<EmbeddedHolder> embeddedHolders = new ArrayList<EmbeddedHolder>();
    final List<FieldMetadata> embeddedFields = MemberFindingUtils.getFieldsWithTag(memberDetails, EMBEDDED_FIELD);
    if (embeddedFields.isEmpty()) {
        return embeddedHolders;
    }
    for (final FieldMetadata embeddedField : embeddedFields) {
        final MemberDetails embeddedMemberDetails = getMemberDetails(embeddedField.getFieldType());
        if (embeddedMemberDetails == null) {
            continue;
        }
        final List<FieldMetadata> fields = new ArrayList<FieldMetadata>();
        for (final FieldMetadata field : embeddedMemberDetails.getFields()) {
            if (!(Modifier.isStatic(field.getModifier()) || Modifier.isFinal(field.getModifier()) || Modifier.isTransient(field.getModifier()))) {
                getMetadataDependencyRegistry().registerDependency(field.getDeclaredByMetadataId(), metadataIdentificationString);
                fields.add(field);
            }
        }
        embeddedHolders.add(new EmbeddedHolder(embeddedField, fields));
    }
    return embeddedHolders;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) EmbeddedHolder(org.springframework.roo.addon.jpa.addon.dod.EmbeddedHolder)

Example 35 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.

the class ControllerCommands method getDetailFieldsRelatedToEntity.

/**
 * Get a field list that can be selected to do a detail controller.
 *
 * @param entity
 *            Entity on which create the detail controller
 * @param parentFieldName
 *            The parent's field name used to construct the field name
 *            related with the original entity
 * @return the related field list
 */
private List<String> getDetailFieldsRelatedToEntity(ClassOrInterfaceTypeDetails entity, String parentFieldName) {
    List<String> results = new ArrayList<String>();
    MemberDetails entityDetails = getMemberDetailsScanner().getMemberDetails(entity.getType().getSimpleTypeName(), entity);
    List<FieldMetadata> fields = entityDetails.getFields();
    for (FieldMetadata field : fields) {
        AnnotationMetadata oneToManyAnnotation = field.getAnnotation(JpaJavaType.ONE_TO_MANY);
        AnnotationMetadata manyToManyAnnotation = field.getAnnotation(JpaJavaType.MANY_TO_MANY);
        if ((oneToManyAnnotation != null || manyToManyAnnotation != null) && (field.getFieldType().getFullyQualifiedTypeName().equals(JavaType.LIST.getFullyQualifiedTypeName()) || field.getFieldType().getFullyQualifiedTypeName().equals(JavaType.SET.getFullyQualifiedTypeName()))) {
            results.add(parentFieldName.concat(field.getFieldName().getSymbolName()));
        }
    }
    return results;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)56 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)36 JavaType (org.springframework.roo.model.JavaType)35 RooJavaType (org.springframework.roo.model.RooJavaType)31 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)29 ArrayList (java.util.ArrayList)25 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)20 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)14 SpringJavaType (org.springframework.roo.model.SpringJavaType)12 List (java.util.List)10 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)9 DetailEntityItem (org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)8 EntityItem (org.springframework.roo.addon.web.mvc.views.components.EntityItem)7 FieldItem (org.springframework.roo.addon.web.mvc.views.components.FieldItem)7 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)7 JdkJavaType (org.springframework.roo.model.JdkJavaType)7 RepositoryJpaMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata)6 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)6