Search in sources :

Example 41 with FieldMetadataBuilder

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

the class JpaFieldCreatorProvider method insertField.

public void insertField(final FieldDetails fieldDetails, final boolean permitReservedWords, final boolean transientModifier, boolean evictCacheForTargetClass) {
    String module = getFieldModule(fieldDetails);
    final FieldMetadataBuilder fieldBuilder = generateFieldBuilder(fieldDetails, permitReservedWords, transientModifier);
    typeManagementService.addField(fieldBuilder.build(), evictCacheForTargetClass);
    if (module != null) {
        projectOperations.addModuleDependency(module);
    }
}
Also used : FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 42 with FieldMetadataBuilder

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

the class JpaFieldCreatorProvider method generateFieldBuilder.

/**
 * Generates Field builder based on fieldDetails
 *
 * @param fieldDetails
 * @param permitReservedWords
 * @param transientModifier
 * @return
 */
private FieldMetadataBuilder generateFieldBuilder(final FieldDetails fieldDetails, final boolean permitReservedWords, final boolean transientModifier) {
    if (!permitReservedWords) {
        ReservedWords.verifyReservedWordsNotPresent(fieldDetails.getFieldName());
        if (fieldDetails.getColumn() != null) {
            ReservedWords.verifyReservedWordsNotPresent(fieldDetails.getColumn());
        }
    }
    final List<AnnotationMetadataBuilder> annotations = fieldDetails.getInitedAnnotations();
    fieldDetails.decorateAnnotationsList(annotations);
    fieldDetails.setAnnotations(annotations);
    String initializer = null;
    if (fieldDetails instanceof CollectionField) {
        final CollectionField collectionField = (CollectionField) fieldDetails;
        initializer = "new " + collectionField.getInitializer() + "()";
    } else if (fieldDetails instanceof DateField && fieldDetails.getFieldName().getSymbolName().equals("created")) {
        initializer = "new Date()";
    }
    int modifier = Modifier.PRIVATE;
    if (transientModifier) {
        modifier += Modifier.TRANSIENT;
    }
    fieldDetails.setModifiers(modifier);
    // Format the passed-in comment (if given)
    formatFieldComment(fieldDetails);
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails);
    fieldBuilder.setFieldInitializer(initializer);
    return fieldBuilder;
}
Also used : DateField(org.springframework.roo.classpath.operations.jsr303.DateField) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CollectionField(org.springframework.roo.classpath.operations.jsr303.CollectionField) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 43 with FieldMetadataBuilder

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

the class JpaEntityMetadata method getIterableToAddCantBeNullConstant.

/**
 * This method obtains the constant that contains the message
 * "The given Iterable of items to add can't be null!".
 *
 * @return name of the generated constant
 */
private FieldMetadata getIterableToAddCantBeNullConstant() {
    // Check if iterableCantBeNullConstant already exists
    if (iterableToAddCantBeNullConstant != null) {
        return iterableToAddCantBeNullConstant;
    }
    // If not exists, generate a new one and include into builder
    FieldMetadataBuilder constant = new FieldMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.STATIC + Modifier.FINAL, new JavaSymbolName("ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE"), JavaType.STRING, "\"The given Iterable of items to add can't be null!\"");
    iterableToAddCantBeNullConstant = constant.build();
    return iterableToAddCantBeNullConstant;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 44 with FieldMetadataBuilder

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

the class JpaOperationsImpl method getIdentifierField.

/**
 * This method generates the identifier field using the provided values.
 *
 * @param entity
 * @param identifierField
 * @param identifierType
 * @param identifierColumn
 * @param sequenceName
 * @param identifierStrategy
 * @param inheritanceType
 * @return
 */
private FieldMetadata getIdentifierField(final JavaType entity, String identifierField, final JavaType identifierType, final String identifierColumn, final String sequenceName, IdentifierStrategy identifierStrategy, InheritanceType inheritanceType) {
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    final boolean hasIdClass = !(identifierType.isCoreType());
    final JavaType annotationType = hasIdClass ? EMBEDDED_ID : ID;
    annotations.add(new AnnotationMetadataBuilder(annotationType));
    if (StringUtils.isEmpty(identifierField)) {
        identifierField = "id";
    }
    // Compute the column name, as required
    if (!hasIdClass) {
        if (!"".equals(sequenceName)) {
            // ROO-3719: Add SEQUENCE as @GeneratedValue strategy
            // Check if provided identifierStrategy is valid
            boolean isValidIdentifierStrategy = false;
            if (identifierStrategy != null) {
                for (IdentifierStrategy identifierStrategyType : IdentifierStrategy.values()) {
                    if (identifierStrategyType.name().equals(identifierStrategy.name())) {
                        isValidIdentifierStrategy = true;
                        break;
                    }
                }
            }
            if (!isValidIdentifierStrategy) {
                identifierStrategy = IdentifierStrategy.AUTO;
            }
            // InheritanceType.TABLE_PER_CLASS)
            if (IdentifierStrategy.AUTO.name().equals(identifierStrategy.name())) {
                if (inheritanceType != null) {
                    if ("TABLE_PER_CLASS".equals(inheritanceType.name())) {
                        identifierStrategy = IdentifierStrategy.TABLE;
                    }
                }
            }
            final AnnotationMetadataBuilder generatedValueBuilder = new AnnotationMetadataBuilder(GENERATED_VALUE);
            generatedValueBuilder.addEnumAttribute("strategy", new EnumDetails(GENERATION_TYPE, new JavaSymbolName(identifierStrategy.name())));
            if (StringUtils.isNotBlank(sequenceName)) {
                final String sequenceKey = StringUtils.uncapitalize(entity.getSimpleTypeName()) + "Gen";
                generatedValueBuilder.addStringAttribute("generator", sequenceKey);
                final AnnotationMetadataBuilder sequenceGeneratorBuilder = new AnnotationMetadataBuilder(SEQUENCE_GENERATOR);
                sequenceGeneratorBuilder.addStringAttribute("name", sequenceKey);
                sequenceGeneratorBuilder.addStringAttribute("sequenceName", sequenceName);
                annotations.add(sequenceGeneratorBuilder);
            }
            annotations.add(generatedValueBuilder);
        }
        // User has specified alternative columnName
        if (StringUtils.isNotBlank(identifierColumn)) {
            final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
            columnBuilder.addStringAttribute("name", identifierColumn);
            annotations.add(columnBuilder);
        }
    }
    FieldDetails identifierFieldDetails = new FieldDetails(getTypeLocationService().getPhysicalTypeIdentifier(entity), identifierType, new JavaSymbolName(identifierField));
    identifierFieldDetails.setModifiers(Modifier.PRIVATE);
    identifierFieldDetails.addAnnotations(annotations);
    return new FieldMetadataBuilder(identifierFieldDetails).build();
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) EnumDetails(org.springframework.roo.model.EnumDetails) IdentifierStrategy(org.springframework.roo.addon.jpa.addon.entity.IdentifierStrategy) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 45 with FieldMetadataBuilder

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

the class JpaEntityFactoryMetadata method addCollaboratingFieldsToBuilder.

private void addCollaboratingFieldsToBuilder(final Set<ClassOrInterfaceTypeDetails> entityFactoryClasses) {
    final Set<JavaSymbolName> fields = new LinkedHashSet<JavaSymbolName>();
    for (final JavaType entityNeedingCollaborator : this.requiredDataOnDemandCollaborators) {
        final JavaType collaboratorType = getCollaboratingType(entityNeedingCollaborator, entityFactoryClasses);
        if (collaboratorType != null) {
            final String collaboratingFieldName = getCollaboratingFieldName(entityNeedingCollaborator, entityFactoryClasses).getSymbolName();
            final JavaSymbolName fieldSymbolName = new JavaSymbolName(collaboratingFieldName);
            final FieldMetadata candidate = governorTypeDetails.getField(fieldSymbolName);
            if (candidate != null) {
                // We really expect the field to be correct if we're going to
                // rely on it
                Validate.isTrue(candidate.getFieldType().equals(collaboratorType), "Field '%s' on '%s' must be of type '%s'", collaboratingFieldName, destination.getFullyQualifiedTypeName(), collaboratorType.getFullyQualifiedTypeName());
                Validate.isTrue(Modifier.isPrivate(candidate.getModifier()), "Field '%s' on '%s' must be private", collaboratingFieldName, destination.getFullyQualifiedTypeName());
                Validate.notNull(MemberFindingUtils.getAnnotationOfType(candidate.getAnnotations(), AUTOWIRED), "Field '%s' on '%s' must be @Autowired", collaboratingFieldName, destination.getFullyQualifiedTypeName());
                // It's ok, so we can move onto the new field
                continue;
            }
            // Create field and add it to the ITD, if it hasn't already been
            if (!fields.contains(fieldSymbolName)) {
                // Must make the field
                final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
                annotations.add(new AnnotationMetadataBuilder(AUTOWIRED));
                builder.addField(new FieldMetadataBuilder(getId(), 0, annotations, fieldSymbolName, collaboratorType));
                fields.add(fieldSymbolName);
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Aggregations

FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)66 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)48 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)40 ArrayList (java.util.ArrayList)21 JavaType (org.springframework.roo.model.JavaType)19 JdkJavaType (org.springframework.roo.model.JdkJavaType)10 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)8 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)7 ForeignKey (org.springframework.roo.addon.dbre.addon.model.ForeignKey)5 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)5 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)5 LinkedHashMap (java.util.LinkedHashMap)4 Reference (org.springframework.roo.addon.dbre.addon.model.Reference)4 Table (org.springframework.roo.addon.dbre.addon.model.Table)4 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)4 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)4 DateField (org.springframework.roo.classpath.operations.jsr303.DateField)4 EnumDetails (org.springframework.roo.model.EnumDetails)4