Search in sources :

Example 66 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getRndField.

private FieldMetadataBuilder getRndField() {
    int index = -1;
    while (true) {
        // Compute the required field name
        index++;
        final JavaSymbolName fieldName = new JavaSymbolName("rnd" + StringUtils.repeat("_", index));
        this.rndFieldName = fieldName;
        final FieldMetadata candidate = governorTypeDetails.getField(fieldName);
        if (candidate != null) {
            // Verify if candidate is suitable
            if (!Modifier.isPrivate(candidate.getModifier())) {
                // next possible name)
                continue;
            }
            if (!candidate.getFieldType().equals(RANDOM)) {
                // Candidate isn't a java.util.Random, so it isn't suitable
                continue;
            }
            // we assume the user knows what they're doing and have made one
            return new FieldMetadataBuilder(candidate);
        }
        // Candidate not found, so let's create one
        builder.getImportRegistrationResolver().addImports(RANDOM, SECURE_RANDOM);
        final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId());
        fieldBuilder.setModifier(Modifier.PRIVATE);
        fieldBuilder.setFieldName(fieldName);
        fieldBuilder.setFieldType(RANDOM);
        fieldBuilder.setFieldInitializer("new SecureRandom()");
        CommentStructure comment = new CommentStructure();
        comment.addComment(new JavadocComment("Random generator for the entities index."), CommentLocation.BEGINNING);
        fieldBuilder.setCommentStructure(comment);
        return fieldBuilder;
    }
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 67 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class JpaEntityMetadataProviderImpl method getCompositionRelationField.

/**
 * Gets {@link FieldMetadata} of entity field which declares a composition relationship
 *
 * @param entity
 * @param entityDetails
 * @param relationsAsChild
 * @return
 * @throws ClassNotFoundException
 */
private FieldMetadata getCompositionRelationField(JavaType entity, ClassOrInterfaceTypeDetails entityDetails, Map<String, FieldMetadata> relationsAsChild) throws ClassNotFoundException {
    // Try to identify if it's is a child part of a composition.
    // It uses details and annotation values instead metadata to
    // avoid problems of circular dependencies
    ClassOrInterfaceTypeDetails parentDatils;
    FieldMetadata compositionRelationField = null;
    AnnotationMetadata parentFieldRelationAnnotation;
    JpaRelationType type;
    String parentMappedBy;
    for (FieldMetadata field : relationsAsChild.values()) {
        parentDatils = getTypeLocationService().getTypeDetails(field.getFieldType().getBaseType());
        for (FieldMetadata parentField : parentDatils.getFieldsWithAnnotation(RooJavaType.ROO_JPA_RELATION)) {
            parentFieldRelationAnnotation = parentField.getAnnotation(RooJavaType.ROO_JPA_RELATION);
            if (parentFieldRelationAnnotation != null && entity.equals(parentField.getFieldType().getBaseType())) {
                parentMappedBy = getFieldMappedByAnnotationValue(parentField);
                if (field.getFieldName().getSymbolName().equals(parentMappedBy)) {
                    // Found parent relation field
                    // Check composition
                    EnumDetails value = ((EnumAttributeValue) parentFieldRelationAnnotation.getAttribute(new JavaSymbolName("type"))).getValue();
                    if (JpaRelationType.COMPOSITION.name().equals(value.getField().getSymbolName())) {
                        // Found composition
                        if (compositionRelationField != null) {
                            throw new IllegalArgumentException(String.format("Found to relations which '%s' is child part of composition relation field: '%s' and '%s'", entity.getFullyQualifiedTypeName(), compositionRelationField.getFieldName(), field.getFieldName()));
                        }
                        compositionRelationField = field;
                    }
                }
            }
        }
    }
    return compositionRelationField;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) EnumDetails(org.springframework.roo.model.EnumDetails) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaRelationType(org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 68 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata 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 69 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata 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 70 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadata method addEmbeddedClassFieldMutatorMethodsToBuilder.

private void addEmbeddedClassFieldMutatorMethodsToBuilder(final EmbeddedHolder embeddedHolder, final Set<ClassOrInterfaceTypeDetails> dataOnDemandClasses) {
    final JavaType embeddedFieldType = embeddedHolder.getEmbeddedField().getFieldType();
    final JavaType[] parameterTypes = { embeddedFieldType, JavaType.INT_PRIMITIVE };
    final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
    for (final FieldMetadata field : embeddedHolder.getFields()) {
        final String initializer = getFieldInitializer(field, null, dataOnDemandClasses);
        final JavaSymbolName fieldMutatorMethodName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
        final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
        bodyBuilder.append(getFieldValidationBody(field, initializer, fieldMutatorMethodName, false));
        final JavaSymbolName embeddedClassMethodName = getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName(), field.getFieldName());
        if (governorHasMethod(embeddedClassMethodName, parameterTypes)) {
            // Method found in governor so do not create method in ITD
            continue;
        }
        builder.addMethod(new MethodMetadataBuilder(getId(), Modifier.PUBLIC, embeddedClassMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder));
    }
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Aggregations

FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)141 JavaType (org.springframework.roo.model.JavaType)76 ArrayList (java.util.ArrayList)69 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)40 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)40 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)39 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)38 RooJavaType (org.springframework.roo.model.RooJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)33 JpaJavaType (org.springframework.roo.model.JpaJavaType)32 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)30 SpringJavaType (org.springframework.roo.model.SpringJavaType)30 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)29 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)26 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)21 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)21 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)18 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)17 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15