Search in sources :

Example 36 with FieldMetadataBuilder

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

the class JpaAuditOperationsImpl method getModifiedDateField.

/**
 * Builds modifiedDate field for storing entity's last modified date
 *
 * @return FieldMetadataBuilder
 */
private FieldMetadataBuilder getModifiedDateField(ClassOrInterfaceTypeDetails entityDetails, String columnName) {
    // Create field annotations
    List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Only add @Column if required by annotation @RooJpaAudit
    if (StringUtils.isNotBlank(columnName)) {
        AnnotationMetadataBuilder columnAnnotation = new AnnotationMetadataBuilder(JpaJavaType.COLUMN);
        columnAnnotation.addStringAttribute("name", columnName);
        annotations.add(columnAnnotation);
    }
    // Add @LastModifiedDate
    AnnotationMetadataBuilder createdDateAnnotation = new AnnotationMetadataBuilder(SpringJavaType.LAST_MODIFIED_DATE);
    annotations.add(createdDateAnnotation);
    // Add @Temporal
    AnnotationMetadataBuilder temporalAnnotation = new AnnotationMetadataBuilder(JpaJavaType.TEMPORAL);
    temporalAnnotation.addEnumAttribute("value", new EnumDetails(JpaJavaType.TEMPORAL_TYPE, new JavaSymbolName("TIMESTAMP")));
    annotations.add(temporalAnnotation);
    // Add @DateTimeFormat
    AnnotationMetadataBuilder dateTimeFormatAnnotation = new AnnotationMetadataBuilder(SpringJavaType.DATE_TIME_FORMAT);
    dateTimeFormatAnnotation.addStringAttribute("style", "M-");
    annotations.add(dateTimeFormatAnnotation);
    // Create field
    FieldDetails fieldDetails = new FieldDetails(PhysicalTypeIdentifier.createIdentifier(entityDetails), JdkJavaType.CALENDAR, new JavaSymbolName("modifiedDate"));
    fieldDetails.setModifiers(Modifier.PRIVATE);
    fieldDetails.setAnnotations(annotations);
    FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails);
    return fieldBuilder;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 37 with FieldMetadataBuilder

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

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

the class JpaDataOnDemandMetadata method getEntityFactoryField.

/**
 * Creates an EntityFactory field related to this entity.
 *
 * @return {@link FieldMetadataBuilder} for building field into ITD.
 */
private FieldMetadataBuilder getEntityFactoryField() {
    // Create field
    FieldMetadataBuilder entityFactoryField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), new JavaSymbolName(FACTORY_VAR), this.entityFactoryMetadata.getGovernorType());
    entityFactoryField.setFieldInitializer(String.format("new %s()", getNameOfJavaType(this.entityFactoryMetadata.getGovernorType())));
    CommentStructure comment = new CommentStructure();
    comment.addComment(new JavadocComment("Factory to create entity instances."), CommentLocation.BEGINNING);
    entityFactoryField.setCommentStructure(comment);
    return entityFactoryField;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 39 with FieldMetadataBuilder

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

the class JpaDataOnDemandMetadata method getEntityManagerField.

/**
 * Creates EntityManager field.
 *
 * @return {@link FieldMetadataBuilder} for building field into ITD.
 */
private FieldMetadataBuilder getEntityManagerField() {
    // Create field
    FieldMetadataBuilder entityManagerField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), new JavaSymbolName(ENTITY_MANAGER_VAR), JpaJavaType.ENTITY_MANAGER);
    CommentStructure comment = new CommentStructure();
    comment.addComment(new JavadocComment("EntityManager to persist the entities."), CommentLocation.BEGINNING);
    entityManagerField.setCommentStructure(comment);
    return entityManagerField;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 40 with FieldMetadataBuilder

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

the class EmbeddableFieldCreatorProvider method insertField.

public void insertField(final FieldDetails fieldDetails, final boolean permitReservedWords, final boolean transientModifier) {
    String module = null;
    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);
    if (fieldDetails.getFieldType() != null) {
        module = fieldDetails.getFieldType().getModule();
    }
    String initializer = null;
    if (fieldDetails instanceof CollectionField) {
        final CollectionField collectionField = (CollectionField) fieldDetails;
        module = collectionField.getGenericParameterTypeName().getModule();
        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);
    typeManagementService.addField(fieldBuilder.build());
    if (module != null) {
        projectOperations.addModuleDependency(module);
    }
}
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)

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