Search in sources :

Example 46 with AnnotationMetadata

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

the class MethodMatcher method getUserDefinedMethod.

private JavaSymbolName getUserDefinedMethod(final List<MemberHoldingTypeDetails> memberHoldingTypeDetailsList, final Map<String, String> pluralMap) {
    if (catalystAnnotationType == null || userDefinedNameAttribute == null) {
        return null;
    }
    final String suffix = suffixPlural || suffixSingular ? getSuffix(memberHoldingTypeDetailsList, suffixSingular, pluralMap) : "";
    final ClassOrInterfaceTypeDetails cid = getMostConcreteClassOrInterfaceTypeDetails(memberHoldingTypeDetailsList);
    for (final AnnotationMetadata annotationMetadata : cid.getAnnotations()) {
        if (annotationMetadata.getAnnotationType().getFullyQualifiedTypeName().equals(catalystAnnotationType.getFullyQualifiedTypeName())) {
            final AnnotationAttributeValue<?> annotationAttributeValue = annotationMetadata.getAttribute(userDefinedNameAttribute);
            if (annotationAttributeValue != null && StringUtils.isNotBlank(annotationAttributeValue.getValue().toString())) {
                return new JavaSymbolName(annotationAttributeValue.getValue().toString() + suffix);
            }
            break;
        }
    }
    return defaultName == null ? null : new JavaSymbolName(defaultName + suffix);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 47 with AnnotationMetadata

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

the class JpaDataOnDemandCreator method createEntityFactory.

@Override
public JavaType createEntityFactory(JavaType currentEntity) {
    Validate.notNull(currentEntity, "Entity to produce a data on demand provider for is required");
    // Verify the requested entity actually exists as a class and is not
    // abstract
    final ClassOrInterfaceTypeDetails cid = getEntityDetails(currentEntity);
    Validate.isTrue(cid.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS, "Type %s is not a class", currentEntity.getFullyQualifiedTypeName());
    Validate.isTrue(!Modifier.isAbstract(cid.getModifier()), "Type %s is abstract", currentEntity.getFullyQualifiedTypeName());
    // Check if the requested entity is a JPA @Entity
    final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(JpaDataOnDemandCreator.class.getName(), cid);
    final AnnotationMetadata entityAnnotation = memberDetails.getAnnotation(ENTITY);
    Validate.isTrue(entityAnnotation != null, "Type %s must be a JPA entity type", currentEntity.getFullyQualifiedTypeName());
    // Get related entities
    List<JavaType> entities = getEntityAndRelatedEntitiesList(currentEntity);
    // Get test Path for module
    final LogicalPath path = LogicalPath.getInstance(Path.SRC_TEST_JAVA, currentEntity.getModule());
    JavaType currentEntityFactory = null;
    for (JavaType entity : entities) {
        // Create the JavaType for the configuration class
        JavaType factoryClass = new JavaType(String.format("%s.dod.%sFactory", entity.getPackage().getFullyQualifiedPackageName(), entity.getSimpleTypeName()), entity.getModule());
        final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(factoryClass, path);
        if (metadataService.get(declaredByMetadataId) != null) {
            // The file already exists
            continue;
        }
        // Create the CID builder
        ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, factoryClass, PhysicalTypeCategory.CLASS);
        // Add @RooEntityFactory annotation
        AnnotationMetadataBuilder entityFactoryAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_ENTITY_FACTORY);
        entityFactoryAnnotation.addClassAttribute("entity", entity);
        cidBuilder.addAnnotation(entityFactoryAnnotation);
        // Write changes to disk
        typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
        // First entity is current entity
        if (currentEntityFactory == null) {
            currentEntityFactory = cidBuilder.getName();
        }
    }
    return currentEntityFactory;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) LogicalPath(org.springframework.roo.project.LogicalPath) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 48 with AnnotationMetadata

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

the class JpaFieldCreatorProvider method createCollectionField.

/**
 * Implementation for {@link #createSetField(JavaType, JavaType, JavaSymbolName, Cardinality, Cascade[], boolean, Integer, Integer, JavaSymbolName, Fetch, String, String, String, String, String, String, boolean, Boolean, Boolean, boolean)}
 * and
 * {@link #createListField(ClassOrInterfaceTypeDetails, Cardinality, JavaType, JavaType, JavaSymbolName, Cascade, boolean, boolean, Integer, Integer, JavaSymbolName, Fetch, String, String, String, String, String, String, boolean, boolean)}
 *
 * @param typeName
 * @param fieldType
 * @param fieldName
 * @param cardinality
 * @param cascadeType
 * @param notNull
 * @param sizeMin
 * @param sizeMax
 * @param mappedBy
 * @param fetch
 * @param comment
 * @param joinColumnName
 * @param referencedColumnName
 * @param joinTable
 * @param joinColumns
 * @param referencedColumns
 * @param inverseJoinColumns
 * @param inverseReferencedColumns
 * @param permitReservedWords
 * @param aggregation
 * @param orphanRemoval
 * @param isForce
 * @param isList true generates List, false generates Set
 * @param formatExpression
 * @param formatMessage
 */
public void createCollectionField(JavaType typeName, JavaType fieldType, JavaSymbolName fieldName, Cardinality cardinality, Cascade[] cascadeType, boolean notNull, Integer sizeMin, Integer sizeMax, JavaSymbolName mappedBy, Fetch fetch, String comment, String joinColumnName, String referencedColumnName, String joinTable, String joinColumns, String referencedColumns, String inverseJoinColumns, String inverseReferencedColumns, boolean permitReservedWords, Boolean aggregation, Boolean orphanRemoval, boolean isForce, boolean isList, String formatExpression, String formatMessage) {
    // Check if property 'spring.roo.jpa.require.schema-object-name' is defined
    // on
    // project settings
    String requiredSchemaObjectName = projectSettings.getProperty(SPRING_ROO_JPA_REQUIRE_SCHEMA_OBJECT_NAME);
    // 'joinTable' or 'joinColumnName' is required if property is true
    if (requiredSchemaObjectName != null && requiredSchemaObjectName.equals(TRUE) && joinTable == null && joinColumnName == null) {
        throw new IllegalArgumentException("You must specify one of: 'joinTable' or 'joinColumnName'");
    }
    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");
    // 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);
    boolean isEnumeration = false;
    if (childCid.getPhysicalTypeCategory() == PhysicalTypeCategory.ENUMERATION) {
        isEnumeration = true;
    } else if (entityAnnotation != null || persistentAnnotation != null) {
    // Target is JPA entity
    } else {
        throw new IllegalStateException("The field set command is only applicable to enum, JPA @Entity or Spring Data @Persistence elements");
    }
    if (isEnumeration) {
        // Enumeration
        createCollectionEnumeration(parentCid, fieldType, fieldName, permitReservedWords, sizeMin, sizeMax, comment, notNull, false);
    } else {
        if (mappedBy == null) {
            // generate mappedBy name from uncapitalized parentClass name
            if (cardinality == Cardinality.MANY_TO_MANY) {
                // Get plural
                mappedBy = new JavaSymbolName(StringUtils.uncapitalize(pluralService.getPlural(parentCid)));
            } else {
                mappedBy = new JavaSymbolName(StringUtils.uncapitalize(parentCid.getType().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());
        if (fetch == null) {
            fetch = Fetch.LAZY;
        }
        switch(cardinality) {
            case ONE_TO_MANY:
                createParentFieldOfToManyRelation(parentCid, childCid, fieldName, fieldType, Cardinality.ONE_TO_MANY, permitReservedWords, sizeMin, sizeMax, comment, notNull, mappedBy, fetch, aggregation, orphanRemoval, cascadeType, isList);
                createChildFieldOfOneToManyRelation(childCid, typeName, permitReservedWords, mappedBy, fetch, joinColumnName, referencedColumnName, joinTable, joinColumns, referencedColumns, inverseJoinColumns, inverseReferencedColumns, formatExpression, formatMessage);
                break;
            case MANY_TO_MANY:
                createParentFieldOfToManyRelation(parentCid, childCid, fieldName, fieldType, Cardinality.MANY_TO_MANY, permitReservedWords, sizeMin, sizeMax, comment, notNull, mappedBy, fetch, aggregation, orphanRemoval, cascadeType, isList);
                createChildFieldOfManyToManyRelation(childCid, typeName, permitReservedWords, mappedBy, fetch, joinTable, joinColumns, referencedColumns, inverseJoinColumns, inverseReferencedColumns, isList);
                break;
            default:
                throw new IllegalArgumentException("Cardinality must be ONE_TO_MANY or MANY_TO_MANY for the field set command");
        }
        // Add factory class for child entity if required
        createChildEntityFactory(fieldType, typeName);
    }
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 49 with AnnotationMetadata

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata 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 50 with AnnotationMetadata

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

the class JpaEntityMetadata method getEntityFormatAnnotation.

/**
 * Generates the Springlets `@EntityFormat` annotation to be applied to the entity
 *
 * @return AnnotationMetadata
 */
private AnnotationMetadata getEntityFormatAnnotation() {
    AnnotationMetadata entityFormatAnnotation = getTypeAnnotation(SpringletsJavaType.SPRINGLETS_ENTITY_FORMAT);
    if (entityFormatAnnotation == null) {
        return null;
    }
    String expressionAttribute = this.annotationValues.getEntityFormatExpression();
    String messageAttribute = this.annotationValues.getEntityFormatMessage();
    final AnnotationMetadataBuilder entityFormatBuilder = new AnnotationMetadataBuilder(entityFormatAnnotation);
    // Check for each attribute individually
    if (StringUtils.isNotBlank(expressionAttribute)) {
        entityFormatBuilder.addStringAttribute("value", expressionAttribute);
    }
    if (StringUtils.isNotBlank(messageAttribute)) {
        entityFormatBuilder.addStringAttribute("message", messageAttribute);
    }
    entityFormatAnnotation = entityFormatBuilder.build();
    return entityFormatAnnotation;
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)109 JavaType (org.springframework.roo.model.JavaType)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)52 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)47 ArrayList (java.util.ArrayList)40 RooJavaType (org.springframework.roo.model.RooJavaType)34 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)30 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)24 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)21 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)20 List (java.util.List)19 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)18 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)17 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)16 SpringJavaType (org.springframework.roo.model.SpringJavaType)15 JdkJavaType (org.springframework.roo.model.JdkJavaType)12 JpaJavaType (org.springframework.roo.model.JpaJavaType)12 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)11 LinkedHashMap (java.util.LinkedHashMap)10