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;
}
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;
}
}
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;
}
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;
}
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);
}
}
Aggregations